public static PortalHandler GetInstance() { if ( instance == null ) { instance = new PortalHandler(); Player.Moved += new EventHandler<Events.PlayerMovedEventArgs>( Player_Moved ); Player.JoinedWorld += new EventHandler<Events.PlayerJoinedWorldEventArgs>( Player_JoinedWorld ); Player.PlacingBlock += new EventHandler<Events.PlayerPlacingBlockEventArgs>( Player_PlacingBlock ); } return instance; }
static void Player_PlacingBlock(object sender, Events.PlayerPlacingBlockEventArgs e) { try { if (e.Player.World.Map.Portals != null && e.Player.World.Map.Portals.Count > 0 && e.Context != BlockChangeContext.Portal) { lock (e.Player.World.Map.Portals.SyncRoot) { foreach (Portal portal in e.Player.World.Map.Portals) { if (portal.IsInRange(e.Coords)) { e.Result = CanPlaceResult.Revert; e.Player.Message("You can not place a block inside portal: " + portal.Name); return; } } } } if (e.NewBlock == Block.Red) { if (e.Context == BlockChangeContext.Manual) { if (e.Player.PortalCache.Name != null) { if (e.Player.PortalCache.AffectedBlocks != null) { if (e.Player.PortalCache.AffectedBlocks.Contains(e.Coords)) //stop output being inside the unfinished portal { e.Result = CanPlaceResult.Revert; e.Player.Message("You can not place a block inside a portal"); return; } e.Player.PortalCache.DesiredOutputX = e.Coords.ToPlayerCoords().X; e.Player.PortalCache.DesiredOutputY = e.Coords.ToPlayerCoords().Y; e.Player.PortalCache.DesiredOutputZ = (e.Coords.Z + 2) * 32; e.Player.PortalCache.DesiredOutputR = e.Player.Position.R; e.Player.PortalCache.DesiredOutputL = e.Player.Position.L; e.Player.PortalCache.Name = Portal.GenerateName(e.Player.PortalCache.World, true); string oldWorld = e.Player.PortalCache.World; e.Player.PortalCache.World = e.Player.World.Name; PortalHandler.CreatePortal(e.Player.PortalCache, WorldManager.FindWorldExact(oldWorld), true); e.Player.Message(" Portal finalized: Exit point at {0} on world {1}", e.Coords.ToString(), e.Player.World.ClassyName); e.Player.PortalCache = new Portal(); e.Result = CanPlaceResult.Revert; } } } } } catch (Exception ex) { Logger.Log(LogType.Error, "PortalHandler.Player_PlacedBlock: " + ex); } }
public static PortalHandler GetInstance() { if (instance == null) { instance = new PortalHandler(); Player.Moved += new EventHandler <Events.PlayerMovedEventArgs>(Player_Moved); Player.JoinedWorld += new EventHandler <Events.PlayerJoinedWorldEventArgs>(Player_JoinedWorld); Player.PlacingBlock += new EventHandler <Events.PlayerPlacingBlockEventArgs>(Player_PlacingBlock); } return(instance); }
static void Player_Moved(object sender, Events.PlayerMovedEventArgs e) { try { if (e.Player.PortalsEnabled) { lock (e.Player.PortalLock) { if (e.Player.CanUsePortal) { if ((e.OldPosition.X != e.NewPosition.X) || (e.OldPosition.Y != e.NewPosition.Y) || (e.OldPosition.Z != (e.NewPosition.Z))) { if (e.Player.Can(Permission.UsePortal)) { if (PortalHandler.GetInstance().GetPortal(e.Player) != null && !e.Player.StandingInPortal) { if (e.Player.LastUsedPortal != null && (DateTime.UtcNow - e.Player.LastUsedPortal).TotalSeconds < 4) { // To prevent portal loops if (e.Player.LastWarnedPortal == null || (DateTime.UtcNow - e.Player.LastWarnedPortal).TotalSeconds > 2) { e.Player.LastWarnedPortal = DateTime.UtcNow; e.Player.Message("You cannot use portals for another {0} seconds.", 4 - (DateTime.UtcNow - e.Player.LastUsedPortal).Seconds); } return; } // Make sure this method isn't called twice e.Player.CanUsePortal = false; e.Player.StandingInPortal = true; Portal portal = PortalHandler.GetInstance().GetPortal(e.Player); World world = WorldManager.FindWorldExact(portal.World); if (world == null) { return; } // Teleport player, portal protection switch (world.AccessSecurity.CheckDetailed(e.Player.Info)) { case SecurityCheckResult.Allowed: case SecurityCheckResult.WhiteListed: if (world.IsFull) { e.Player.Message("Cannot join {0}&S: world is full.", world.ClassyName); return; } e.Player.StopSpectating(); if (portal.World == e.Player.World.Name) { if (!portal.HasDesiredOutput) { e.Player.TeleportTo(e.Player.World.Map.Spawn); } else { e.Player.TeleportTo(new Position((short)portal.DesiredOutputX, (short)portal.DesiredOutputY, (short)portal.DesiredOutputZ, portal.DesiredOutputR, portal.DesiredOutputL)); } e.Player.LastWarnedPortal = DateTime.UtcNow; e.Player.StandingInPortal = false; e.Player.CanUsePortal = true; e.Player.LastUsedPortal = DateTime.UtcNow; } else { if (!portal.HasDesiredOutput) { e.Player.JoinWorld(WorldManager.FindWorldExact(portal.World), WorldChangeReason.Portal); } else { e.Player.JoinWorld(WorldManager.FindWorldExact(portal.World), WorldChangeReason.Portal, new Position(( short )portal.DesiredOutputX, ( short )portal.DesiredOutputY, ( short )portal.DesiredOutputZ, portal.DesiredOutputR, portal.DesiredOutputL)); } } e.Player.Message("You used portal: " + portal.Name); break; case SecurityCheckResult.BlackListed: e.Player.Message("Cannot join world {0}&S: you are blacklisted.", world.ClassyName); break; case SecurityCheckResult.RankTooLow: e.Player.Message("Cannot join world {0}&S: must be {1}+", world.ClassyName, world.AccessSecurity.MinRank.ClassyName); break; } } else { e.Player.StandingInPortal = false; } } } } } } } catch (Exception ex) { Logger.Log(LogType.Error, "PortalHandler.Player_Moved: " + ex); } }
static void Player_Moved(object sender, Events.PlayerMovedEventArgs e) { //abuse portal moved event and add in message blocks right here Vector3I oldPos = new Vector3I(e.OldPosition.X / 32, e.OldPosition.Y / 32, e.OldPosition.Z / 32); //get positions as block coords Vector3I newPos = new Vector3I(e.NewPosition.X / 32, e.NewPosition.Y / 32, e.NewPosition.Z / 32); if (oldPos.X != newPos.X || oldPos.Y != newPos.Y || oldPos.Z != newPos.Z) //check if player has moved at least one block { //loop through all message blocks and check if we triggered one foreach (var messageBlock in e.Player.World.MessageBlocks) { Tuple <Vector3I, string> tuple = messageBlock.Value; //player is sitting on the message block, play the message if (e.Player.Position.ToBlockCoords() == tuple.Item1) { e.Player.Message("__" + messageBlock.Key + "__"); e.Player.Message(messageBlock.Value.Item2); e.Player.Message(e.Player.Position.ToBlockCoords().ToString()); e.Player.Message(tuple.Item1.ToString()); } } } try { if (e.Player.PortalsEnabled) { lock (e.Player.PortalLock) { if (e.Player.CanUsePortal) { if ((e.OldPosition.X != e.NewPosition.X) || (e.OldPosition.Y != e.NewPosition.Y) || (e.OldPosition.Z != (e.NewPosition.Z))) { if (e.Player.Can(Permission.UsePortal)) { if (PortalHandler.GetInstance().GetPortal(e.Player) != null && !e.Player.StandingInPortal) { if (e.Player.LastUsedPortal != null && (DateTime.UtcNow - e.Player.LastUsedPortal).TotalSeconds < 5) { // To prevent portal loops if (e.Player.LastWarnedPortal == null || (DateTime.UtcNow - e.Player.LastWarnedPortal).TotalSeconds > 2) { e.Player.LastWarnedPortal = DateTime.Now; e.Player.Message("You can not use portals within 5 seconds of joining a world."); } return; } // Make sure this method isn't called twice e.Player.CanUsePortal = false; e.Player.StandingInPortal = true; Portal portal = PortalHandler.GetInstance().GetPortal(e.Player); World world = WorldManager.FindWorldExact(portal.World); // Teleport player, portal protection switch (world.AccessSecurity.CheckDetailed(e.Player.Info)) { case SecurityCheckResult.Allowed: case SecurityCheckResult.WhiteListed: if (world.IsFull) { e.Player.Message("Cannot join {0}&S: world is full.", world.ClassyName); return; } e.Player.StopSpectating(); e.Player.JoinWorld(WorldManager.FindWorldExact(portal.World), WorldChangeReason.Portal); e.Player.Message("You used portal: " + portal.Name); break; case SecurityCheckResult.BlackListed: e.Player.Message("Cannot join world {0}&S: you are blacklisted.", world.ClassyName); break; case SecurityCheckResult.RankTooLow: e.Player.Message("Cannot join world {0}&S: must be {1}+", world.ClassyName, world.AccessSecurity.MinRank.ClassyName); break; } } else { e.Player.StandingInPortal = false; } } } } } } } catch (Exception ex) { Logger.Log(LogType.Error, "PortalHandler.Player_Moved: " + ex); } }