More Type Security

This commit is contained in:
Olof Larsson 2015-05-16 14:27:06 +02:00
parent a374b02013
commit b7ff72fde2
10 changed files with 75 additions and 42 deletions

View File

@ -119,12 +119,9 @@ public class EngineGank extends EngineAbstract
Entity edamager = MUtil.getLiableDamager(event);
// ... is a player ...
if ( ! (edamager instanceof Player)) return;
if (MUtil.isntPlayer(edamager)) return;
Player player = (Player)edamager;
// ... that is not an NPC ...
if (player.hasMetadata("NPC")) return;
// ... that is someone else ...
if (player.equals(entity)) return;

View File

@ -84,7 +84,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
// Prepare vars
EventMassiveCorePlayerToRecipientChat recipientEvent;
final Player sender = event.getPlayer();
if (MUtil.isNpc(sender)) return;
if (MUtil.isntPlayer(sender)) return;
String message = event.getMessage();
String format = event.getFormat();
@ -139,7 +139,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
{
// So the player is watching ...
Player watcher = event.getPlayer();
if (MUtil.isNpc(watcher)) return;
if (MUtil.isntPlayer(watcher)) return;
// Get the lowercased token
String tokenlc = event.getLastToken().toLowerCase();
@ -186,7 +186,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
// This method sets the sender reference to what you decide.
public static void setSenderReferences(CommandSender sender, CommandSender reference)
{
if (MUtil.isNpc(sender)) return;
if (MUtil.isntSender(sender)) return;
String id = IdUtil.getId(sender);
if (id != null)
@ -265,7 +265,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
public void after(PlayerTeleportEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
Bukkit.getScheduler().scheduleSyncDelayedTask(MassiveCore.get(), new EventMassiveCoreAfterPlayerTeleport(event), 0);
}
@ -274,7 +274,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
public void after(PlayerRespawnEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
Bukkit.getScheduler().scheduleSyncDelayedTask(MassiveCore.get(), new EventMassiveCoreAfterPlayerRespawn(event, player.getLocation()), 0);
}
@ -289,7 +289,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
public void causedByKick(PlayerKickEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
final UUID uuid = player.getUniqueId();
kickedPlayerReasons.put(uuid, event.getReason());
@ -299,7 +299,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
public void causedByKick(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
final UUID uuid = player.getUniqueId();
// We do the schedule in order for the set to be correct through out the whole MONITOR priority state.
@ -321,7 +321,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
public void leaveEventKickCall(PlayerKickEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
new EventMassiveCorePlayerLeave(player, true, "kick", event.getReason()).run();
}
@ -330,7 +330,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
public void leaveEventQuitCall(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
new EventMassiveCorePlayerLeave(player, false, "quit", null).run();
}
@ -339,7 +339,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
public void leaveEventQuitClear(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
final UUID uuid = player.getUniqueId();
// We do the schedule in order for the set to be correct through out the whole MONITOR priority state.
@ -445,7 +445,7 @@ public class MassiveCoreEngineMain extends EngineAbstract
{
// Get player id ...
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
final String playerId = player.getUniqueId().toString();
// ... get remote entries ...
@ -467,12 +467,14 @@ public class MassiveCoreEngineMain extends EngineAbstract
@EventHandler(priority = EventPriority.MONITOR)
public void syncOnPlayerLeave(EventMassiveCorePlayerLeave event)
{
// TODO: This is going to take quite a bit of power :(
this.syncAllForPlayer(event.getPlayer());
Player player = event.getPlayer();
this.syncAllForPlayer(player);
}
public void syncAllForPlayer(Player player)
{
if (MUtil.isntPlayer(player)) return;
String playerId = player.getUniqueId().toString();
for (SenderColl<?> coll : Coll.getSenderInstances())
{

View File

@ -48,7 +48,7 @@ public class MassiveCoreEngineVariable extends EngineAbstract
public void variable(PlayerCommandPreprocessEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
event.setMessage(variable(player, event.getMessage()));
}
@ -57,7 +57,7 @@ public class MassiveCoreEngineVariable extends EngineAbstract
public void variable(AsyncPlayerChatEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
event.setMessage(variable(player, event.getMessage()));
}

View File

@ -1,9 +1,11 @@
package com.massivecraft.massivecore.chestgui;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
public class ChestActionAbstract implements ChestAction
{
// -------------------------------------------- //
@ -13,9 +15,8 @@ public class ChestActionAbstract implements ChestAction
@Override
public boolean onClick(InventoryClickEvent event)
{
HumanEntity human = event.getWhoClicked();
if ( ! (human instanceof Player)) return false;
Player player = (Player)human;
Player player = IdUtil.getAsPlayer(event.getWhoClicked());
if (MUtil.isntPlayer(player)) return false;
return onClick(event, player);
}

View File

@ -9,6 +9,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.util.MUtil;
public class ChestGui
{
@ -63,7 +64,7 @@ public class ChestGui
public void playSound(HumanEntity human)
{
if ( ! (human instanceof Player)) return;
if (MUtil.isntPlayer(human)) return;
Player player = (Player)human;
this.playSound(player);
}

View File

@ -94,7 +94,7 @@ public class EngineScheduledTeleport extends EngineAbstract
public void cancelTeleport(Player player)
{
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
// If there there is a ScheduledTeleport ...
ScheduledTeleport scheduledTeleport = teleporteeIdToScheduledTeleport.get(IdUtil.getId(player));
@ -125,7 +125,7 @@ public class EngineScheduledTeleport extends EngineAbstract
Entity entity = event.getEntity();
// ... and that entity is a player ...
if (!(entity instanceof Player)) return;
if (MUtil.isntPlayer(entity)) return;
Player player = (Player)entity;
// ... cancel teleport!

View File

@ -387,7 +387,7 @@ public class IdUtil implements Listener, Runnable
public void playerLoginLowest(PlayerLoginEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
UUID uuid = player.getUniqueId();
String id = uuid.toString();
@ -404,7 +404,7 @@ public class IdUtil implements Listener, Runnable
public void playerJoinLowest(PlayerJoinEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
UUID uuid = player.getUniqueId();
String id = uuid.toString();
@ -421,6 +421,8 @@ public class IdUtil implements Listener, Runnable
public void playerLeaveMonitor(EventMassiveCorePlayerLeave event)
{
Player player = event.getPlayer();
if (MUtil.isntPlayer(player)) return;
UUID uuid = player.getUniqueId();
String id = uuid.toString();
String name = player.getName();

View File

@ -27,7 +27,7 @@ public class InventoryUtil
public static void update(HumanEntity human)
{
if (!(human instanceof Player)) return;
if (MUtil.isntPlayer(human)) return;
Player player = (Player)human;
player.updateInventory();
}

View File

@ -209,7 +209,7 @@ public class MUtil
}
// -------------------------------------------- //
// NPC
// IS(NT) NPC, SENDER, PLAYER
// -------------------------------------------- //
public static boolean isNpc(Object object)
@ -218,6 +218,32 @@ public class MUtil
Metadatable metadatable = (Metadatable)object;
return metadatable.hasMetadata("NPC");
}
public static boolean isntNpc(Object object)
{
return !isNpc(object);
}
public static boolean isSender(Object object)
{
if (!(object instanceof CommandSender)) return false;
if (isNpc(object)) return false;
return true;
}
public static boolean isntSender(Object object)
{
return !isSender(object);
}
public static boolean isPlayer(Object object)
{
if (!(object instanceof Player)) return false;
if (isNpc(object)) return false;
return true;
}
public static boolean isntPlayer(Object object)
{
return !isPlayer(object);
}
// -------------------------------------------- //
// STACK TRACE: GET
@ -1105,7 +1131,7 @@ public class MUtil
public static String kickReason(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return null;
if (MUtil.isntPlayer(player)) return null;
UUID uuid = player.getUniqueId();
return MassiveCoreEngineMain.kickedPlayerReasons.get(uuid);

View File

@ -93,7 +93,7 @@ public class PlayerUtil extends EngineAbstract
public void isJoined(PlayerJoinEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
final UUID id = player.getUniqueId();
Bukkit.getScheduler().scheduleSyncDelayedTask(MassiveCore.get(), new Runnable()
@ -110,7 +110,7 @@ public class PlayerUtil extends EngineAbstract
public void isJoined(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
final UUID id = player.getUniqueId();
joinedPlayerIds.remove(id);
@ -190,7 +190,7 @@ public class PlayerUtil extends EngineAbstract
public static void setLastDamageMillis(Player player, long millis)
{
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
if (player == null) return;
idToLastDamageMillis.put(player.getUniqueId(), millis);
}
@ -225,8 +225,7 @@ public class PlayerUtil extends EngineAbstract
public static long getLastDamageMillis(Player player)
{
if (player == null) return 0;
if (MUtil.isNpc(player)) return 0;
if (MUtil.isntPlayer(player)) return 0;
Long ret = idToLastDamageMillis.get(player.getUniqueId());
if (ret == null) return 0;
return ret;
@ -234,8 +233,7 @@ public class PlayerUtil extends EngineAbstract
public static long getNoDamageMillis(Player player)
{
if (player == null) return 0;
if (MUtil.isNpc(player)) return 0;
if (MUtil.isntPlayer(player)) return 0;
if (player.isDead()) return 0;
if (!player.isOnline()) return 0;
@ -323,7 +321,7 @@ public class PlayerUtil extends EngineAbstract
// Get the id ...
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return false;
if (MUtil.isntPlayer(player)) return false;
final UUID id = player.getUniqueId();
// ... get current ...
@ -370,6 +368,8 @@ public class PlayerUtil extends EngineAbstract
public static void update(Player player)
{
if (MUtil.isntPlayer(player)) return;
EventMassiveCorePlayerUpdate event = new EventMassiveCorePlayerUpdate(player);
event.run();
@ -381,6 +381,8 @@ public class PlayerUtil extends EngineAbstract
public static void reset(Player player)
{
if (MUtil.isntPlayer(player)) return;
setMaxHealth(player, getMaxHealthDefault(player));
setFlyAllowed(player, isFlyAllowedDefault(player));
setFlyActive(player, isFlyActiveDefault(player));
@ -395,7 +397,7 @@ public class PlayerUtil extends EngineAbstract
{
// If we have a player ...
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
// ... and the player is alive ...
if (player.isDead()) return;
@ -409,6 +411,7 @@ public class PlayerUtil extends EngineAbstract
{
// If we have a player ...
Player player = event.getPlayer();
if (MUtil.isntPlayer(player)) return;
// ... and the player is alive ...
if (player.isDead()) return;
@ -422,6 +425,7 @@ public class PlayerUtil extends EngineAbstract
{
// If we have a player ...
Player player = event.getPlayer();
if (MUtil.isntPlayer(player)) return;
// ... and the player is alive ...
if (player.isDead()) return;
@ -435,7 +439,7 @@ public class PlayerUtil extends EngineAbstract
{
// If we have a player ...
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
// ... and the player is alive ...
if (player.isDead()) return;
@ -449,7 +453,7 @@ public class PlayerUtil extends EngineAbstract
{
// If we have a player ...
Player player = event.getPlayer();
if (MUtil.isNpc(player)) return;
if (MUtil.isntPlayer(player)) return;
// ... and the player is alive ...
if (player.isDead()) return;