Create panic system to protect official content creators

This commit is contained in:
AlexTheCoder 2017-08-26 02:06:38 -04:00
parent 559c641adc
commit 0da2691a87
2 changed files with 167 additions and 11 deletions

View File

@ -2,9 +2,11 @@ package mineplex.game.clans.clans.freeze;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -12,12 +14,18 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent; import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import com.google.common.collect.Sets;
import mineplex.core.MiniPlugin; import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager; import mineplex.core.account.CoreClientManager;
import mineplex.core.account.permissions.Permission; import mineplex.core.account.permissions.Permission;
@ -31,7 +39,9 @@ import mineplex.core.recharge.Recharge;
import mineplex.core.updater.UpdateType; import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.clans.event.ClansCommandExecutedEvent; import mineplex.game.clans.clans.event.ClansCommandExecutedEvent;
import mineplex.game.clans.clans.event.IronDoorOpenEvent;
import mineplex.game.clans.clans.freeze.commands.FreezeCommand; import mineplex.game.clans.clans.freeze.commands.FreezeCommand;
import mineplex.game.clans.clans.freeze.commands.PanicCommand;
import mineplex.game.clans.clans.freeze.commands.UnfreezeCommand; import mineplex.game.clans.clans.freeze.commands.UnfreezeCommand;
import mineplex.minecraft.game.classcombat.Skill.event.SkillTriggerEvent; import mineplex.minecraft.game.classcombat.Skill.event.SkillTriggerEvent;
import mineplex.minecraft.game.core.damage.CustomDamageEvent; import mineplex.minecraft.game.core.damage.CustomDamageEvent;
@ -46,8 +56,11 @@ public class ClansFreezeManager extends MiniPlugin
} }
private static final long FREEZE_MESSAGE_INTERVAL = 10000; private static final long FREEZE_MESSAGE_INTERVAL = 10000;
private static final Set<Material> CONTAINERS = Sets.newHashSet(Material.CHEST, Material.TRAPPED_CHEST, Material.HOPPER, Material.FURNACE, Material.BURNING_FURNACE, Material.DISPENSER, Material.DROPPER, Material.WORKBENCH, Material.BREWING_STAND);
private final CoreClientManager _clientManager; private final CoreClientManager _clientManager;
private final Map<UUID, Float> _frozen = new HashMap<>(); private final Map<UUID, Float> _frozen = new HashMap<>();
private final Map<UUID, Float> _panic = new HashMap<>();
public ClansFreezeManager(JavaPlugin plugin, CoreClientManager clientManager) public ClansFreezeManager(JavaPlugin plugin, CoreClientManager clientManager)
{ {
@ -77,6 +90,7 @@ public class ClansFreezeManager extends MiniPlugin
{ {
addCommand(new FreezeCommand(this)); addCommand(new FreezeCommand(this));
addCommand(new UnfreezeCommand(this)); addCommand(new UnfreezeCommand(this));
addCommand(new PanicCommand(this));
} }
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
@ -95,12 +109,19 @@ public class ClansFreezeManager extends MiniPlugin
} }
} }
} }
walkSpeed = _panic.remove(event.getPlayer().getUniqueId());
if (walkSpeed != null)
{
event.getPlayer().setWalkSpeed(walkSpeed);
event.getPlayer().removePotionEffect(PotionEffectType.JUMP);
}
} }
@EventHandler @EventHandler
public void onMove(PlayerMoveEvent event) public void onMove(PlayerMoveEvent event)
{ {
if (isFrozen(event.getPlayer()) && UtilMath.offset2d(event.getFrom().getBlock().getLocation(), event.getTo().getBlock().getLocation()) >= 1) if ((isFrozen(event.getPlayer()) || isPanicking(event.getPlayer())) && UtilMath.offset2d(event.getFrom().getBlock().getLocation(), event.getTo().getBlock().getLocation()) >= 1)
{ {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().teleport(event.getFrom().getBlock().getLocation().add(0, 0.5, 0)); event.getPlayer().teleport(event.getFrom().getBlock().getLocation().add(0, 0.5, 0));
@ -148,7 +169,7 @@ public class ClansFreezeManager extends MiniPlugin
if (event.getTarget() instanceof Player) if (event.getTarget() instanceof Player)
{ {
Player player = (Player) event.getTarget(); Player player = (Player) event.getTarget();
if (isFrozen(player)) if (isFrozen(player) || isPanicking(player))
{ {
event.setCancelled(true); event.setCancelled(true);
} }
@ -161,12 +182,12 @@ public class ClansFreezeManager extends MiniPlugin
Player damager = event.GetDamagerPlayer(event.GetCause() == DamageCause.PROJECTILE); Player damager = event.GetDamagerPlayer(event.GetCause() == DamageCause.PROJECTILE);
Player damagee = event.GetDamageePlayer(); Player damagee = event.GetDamageePlayer();
if (damager != null && isFrozen(damager)) if (damager != null && (isFrozen(damager) || isPanicking(damager)))
{ {
event.SetCancelled("Frozen Attacker"); event.SetCancelled("Frozen Attacker");
UtilPlayer.message(damager, F.main(getName(), "You cannot attack others while frozen!")); UtilPlayer.message(damager, F.main(getName(), "You cannot attack others while frozen!"));
} }
if (damagee != null && isFrozen(damagee)) if (damagee != null && (isFrozen(damagee) || isPanicking(damagee)))
{ {
event.SetCancelled("Frozen Damagee"); event.SetCancelled("Frozen Damagee");
if (damager != null) if (damager != null)
@ -179,7 +200,7 @@ public class ClansFreezeManager extends MiniPlugin
@EventHandler @EventHandler
public void onUseSkill(SkillTriggerEvent event) public void onUseSkill(SkillTriggerEvent event)
{ {
if (isFrozen(event.GetPlayer())) if (isFrozen(event.GetPlayer()) || isPanicking(event.GetPlayer()))
{ {
event.SetCancelled(true); event.SetCancelled(true);
UtilPlayer.message(event.GetPlayer(), F.main(getName(), "You cannot use " + F.skill(event.GetSkillName()) + " while frozen!")); UtilPlayer.message(event.GetPlayer(), F.main(getName(), "You cannot use " + F.skill(event.GetSkillName()) + " while frozen!"));
@ -189,7 +210,7 @@ public class ClansFreezeManager extends MiniPlugin
@EventHandler @EventHandler
public void onBreak(BlockBreakEvent event) public void onBreak(BlockBreakEvent event)
{ {
if (isFrozen(event.getPlayer())) if (isFrozen(event.getPlayer()) || isPanicking(event.getPlayer()))
{ {
event.setCancelled(true); event.setCancelled(true);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot break blocks while frozen!")); UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot break blocks while frozen!"));
@ -199,7 +220,7 @@ public class ClansFreezeManager extends MiniPlugin
@EventHandler @EventHandler
public void onPlace(BlockPlaceEvent event) public void onPlace(BlockPlaceEvent event)
{ {
if (isFrozen(event.getPlayer())) if (isFrozen(event.getPlayer()) || isPanicking(event.getPlayer()))
{ {
event.setCancelled(true); event.setCancelled(true);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot place blocks while frozen!")); UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot place blocks while frozen!"));
@ -209,13 +230,62 @@ public class ClansFreezeManager extends MiniPlugin
@EventHandler @EventHandler
public void onTpHome(ClansCommandExecutedEvent event) public void onTpHome(ClansCommandExecutedEvent event)
{ {
if (isFrozen(event.getPlayer())) if (isFrozen(event.getPlayer()) || isPanicking(event.getPlayer()))
{ {
event.setCancelled(true); event.setCancelled(true);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot use that command while frozen!")); UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot use that command while frozen!"));
} }
} }
@EventHandler
public void onDropItem(PlayerDropItemEvent event)
{
if (isFrozen(event.getPlayer()) || isPanicking(event.getPlayer()))
{
event.setCancelled(true);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot drop items while frozen!"));
}
}
@EventHandler
public void onPickupItem(PlayerPickupItemEvent event)
{
if (isFrozen(event.getPlayer()) || isPanicking(event.getPlayer()))
{
event.setCancelled(true);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot pick up items while frozen!"));
}
}
@EventHandler
public void onOpenContainer(PlayerInteractEvent event)
{
if ((isFrozen(event.getPlayer()) || isPanicking(event.getPlayer())) && event.getClickedBlock() != null && CONTAINERS.contains(event.getClickedBlock().getType()))
{
event.setCancelled(true);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot open containers while frozen!"));
}
}
@EventHandler
public void onOpenDoor(IronDoorOpenEvent event)
{
if (isFrozen(event.getPlayer()) || isPanicking(event.getPlayer()))
{
event.setCancelled(true);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You cannot use doors while frozen!"));
}
}
@EventHandler
public void onToggleFlight(PlayerToggleFlightEvent event)
{
if ((isFrozen(event.getPlayer()) || isPanicking(event.getPlayer())) && event.isFlying())
{
event.setCancelled(true);
}
}
/** /**
* Checks if a player is frozen * Checks if a player is frozen
* @param player The player to check * @param player The player to check
@ -226,6 +296,57 @@ public class ClansFreezeManager extends MiniPlugin
return _frozen.containsKey(player.getUniqueId()); return _frozen.containsKey(player.getUniqueId());
} }
/**
* Checks if a player is panicking
* @param player The player to check
* @return Whether the player is panicking
*/
public boolean isPanicking(Player player)
{
return _panic.containsKey(player.getUniqueId());
}
/**
* Enters a player into panic mode
* @param player The player who is panicking
*/
public void panic(Player player)
{
_panic.put(player.getUniqueId(), _frozen.getOrDefault(player.getUniqueId(), player.getWalkSpeed()));
player.setFlying(false);
player.setWalkSpeed(0);
player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999, -10));
for (Player alert : UtilServer.GetPlayers())
{
if (_clientManager.Get(alert).GetRank().has(null, Rank.ADMIN, new Rank[] {Rank.CMOD, Rank.CMA}, false))
{
UtilPlayer.message(alert, F.main(getName(), F.elem(player.getName()) + " has entered panic mode!"));
}
if (alert.getName().equals(player.getName()))
{
UtilPlayer.message(alert, F.main(getName(), "You have entered panic mode!"));
}
}
}
/**
* Removes a player from panic mode
* @param player The player to unpanic
*/
public void unpanic(Player player)
{
Float walkSpeed = _panic.remove(player.getUniqueId());
if (walkSpeed != null)
{
if (!_frozen.containsKey(player.getUniqueId()))
{
player.setWalkSpeed(walkSpeed);
player.removePotionEffect(PotionEffectType.JUMP);
}
UtilPlayer.message(player, F.main(getName(), "You have exited panic mode!"));
}
}
/** /**
* Freezes a player * Freezes a player
* @param player The player to freeze * @param player The player to freeze
@ -233,7 +354,8 @@ public class ClansFreezeManager extends MiniPlugin
*/ */
public void freeze(Player player, Player staff) public void freeze(Player player, Player staff)
{ {
_frozen.put(player.getUniqueId(), player.getWalkSpeed()); _frozen.put(player.getUniqueId(), _panic.getOrDefault(player.getUniqueId(), player.getWalkSpeed()));
player.setFlying(false);
player.setWalkSpeed(0); player.setWalkSpeed(0);
player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999, -10)); player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 999999, -10));
for (Player alert : UtilServer.GetPlayers()) for (Player alert : UtilServer.GetPlayers())
@ -255,8 +377,11 @@ public class ClansFreezeManager extends MiniPlugin
Float walkSpeed = _frozen.remove(player.getUniqueId()); Float walkSpeed = _frozen.remove(player.getUniqueId());
if (walkSpeed != null) if (walkSpeed != null)
{ {
player.setWalkSpeed(walkSpeed); if (!_panic.containsKey(player.getUniqueId()))
player.removePotionEffect(PotionEffectType.JUMP); {
player.setWalkSpeed(walkSpeed);
player.removePotionEffect(PotionEffectType.JUMP);
}
for (Player alert : UtilServer.GetPlayers()) for (Player alert : UtilServer.GetPlayers())
{ {
if (_clientManager.Get(alert).hasPermission(Perm.NOTIFY)) if (_clientManager.Get(alert).hasPermission(Perm.NOTIFY))

View File

@ -0,0 +1,31 @@
package mineplex.game.clans.clans.freeze.commands;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.game.clans.clans.freeze.ClansFreezeManager;
/**
* Command to enter panic mode
*/
public class PanicCommand extends CommandBase<ClansFreezeManager>
{
public PanicCommand(ClansFreezeManager plugin)
{
super(plugin, Rank.ADMIN, new Rank[] {Rank.YOUTUBE, Rank.TWITCH}, "panic");
}
@Override
public void Execute(Player caller, String[] args)
{
if (Plugin.isPanicking(caller))
{
Plugin.unpanic(caller);
}
else
{
Plugin.panic(caller);
}
}
}