From 65a50d30c082e8bc0d20a58e027a72543ecbfed9 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Sun, 13 Oct 2013 23:04:57 +0200 Subject: [PATCH] Release 2.1 - Prevent Drops of Killed Mobs - Prevent Minecart-Drops (when TrainCarts is installed) --- pom.xml | 4 +- .../limitedcreative/ModCreativeLimits.java | 15 +++--- .../blockstate/BlockStateConfig.java | 10 ++-- .../limits/EntityListener.java | 8 +++- .../limitedcreative/limits/EntityNoDrop.java | 37 +++++++++++++++ .../limits/PlayerListener.java | 9 +++- .../limits/VehicleListener.java | 47 +++++++++++++++++++ 7 files changed, 112 insertions(+), 18 deletions(-) create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityNoDrop.java create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/VehicleListener.java diff --git a/pom.xml b/pom.xml index a09a2dc..4a6c4f0 100644 --- a/pom.xml +++ b/pom.xml @@ -3,12 +3,12 @@ de.jaschastarke LimitedCreative LimitedCreative - 2.1-SNAPSHOT + 2.1 https://github.com/possi/LimitedCreative UTF-8 - 1.3-SNAPSHOT + 1.3 diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java index 61b4c3a..48669e3 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java @@ -1,21 +1,19 @@ package de.jaschastarke.minecraft.limitedcreative; -import java.util.WeakHashMap; - -import org.bukkit.entity.Entity; - import de.jaschastarke.bukkit.lib.CoreModule; import de.jaschastarke.bukkit.lib.modules.AdditionalBlockBreaks; import de.jaschastarke.minecraft.limitedcreative.limits.BlockListener; import de.jaschastarke.minecraft.limitedcreative.limits.EntityListener; +import de.jaschastarke.minecraft.limitedcreative.limits.EntityNoDrop; import de.jaschastarke.minecraft.limitedcreative.limits.LimitConfig; import de.jaschastarke.minecraft.limitedcreative.limits.PlayerListener; +import de.jaschastarke.minecraft.limitedcreative.limits.VehicleListener; import de.jaschastarke.modularize.IModule; import de.jaschastarke.modularize.ModuleEntry; public class ModCreativeLimits extends CoreModule { protected LimitConfig config; - private WeakHashMap no_xp_mobs = new WeakHashMap(); + private EntityNoDrop noDropsMobs = null; public ModCreativeLimits(LimitedCreative plugin) { super(plugin); @@ -34,6 +32,7 @@ public class ModCreativeLimits extends CoreModule { listeners.addListener(new PlayerListener(this)); listeners.addListener(new EntityListener(this)); listeners.addListener(new BlockListener(this)); + listeners.addListener(new VehicleListener(this)); config = plugin.getPluginConfig().registerSection(new LimitConfig(this, entry)); blockDrops = modules.linkSharedModule(FeatureBlockItemSpawn.class, plugin.getModules()); @@ -54,8 +53,10 @@ public class ModCreativeLimits extends CoreModule { return config; } - public WeakHashMap getNoXPMobs() { - return no_xp_mobs; + public EntityNoDrop getNoDropMobs() { + if (noDropsMobs == null) + noDropsMobs = new EntityNoDrop(); + return noDropsMobs; } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java index 3fa76ce..6500b13 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java @@ -70,10 +70,10 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub /** * BlockStateEnabled * - * This experimental Feature stores the GameMode a Block was created in, and prevents drops if a Block was created + * This Feature stores the GameMode a Block was created in, and prevents drops if a Block was created * in creative mode. * - * Due to the Experimental state this Feature isn't enabled by default. It uses the Database-credentials from + * Due to the huge load of this Feature, it isn't enabled by default. It uses the Database-credentials from * bukkit.yml (http://wiki.bukkit.org/Bukkit.yml#database) in the server-directory. * * default: false @@ -86,9 +86,9 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub /** * BlockStateThreading * - * This experimental variant of the experimental Feature uses Threading to minimize lag. This fully relies on - * Bukkit metadata implementation. You only should need this, if there are often plays more then 10 players at once - * on your server. Be aware that this requires more memory. + * Uses Threading to minimize lag. This fully relies on Bukkit metadata implementation. You only should need this, + * if there are often plays more then 10 players at once on your server. Be aware that this requires more memory, + * to increase the performance * * default: false */ diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityListener.java index a4fe370..f043b66 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityListener.java @@ -54,11 +54,15 @@ public class EntityListener implements Listener { @EventHandler public void onEntityDeath(EntityDeathEvent event) { if (event.getEntity() instanceof LivingEntity && event.getDroppedExp() > 0) { - if (mod.getNoXPMobs().containsKey(event.getEntity())) { + if (mod.getNoDropMobs().isXPPrevented(event.getEntity())) { event.setDroppedExp(0); - mod.getNoXPMobs().remove(event.getEntity()); + event.getDrops().clear(); } } + if (event.getDrops().size() > 0 && mod.getNoDropMobs().isDropPrevented(event.getEntity())) { + event.getDrops().clear(); + } + mod.getNoDropMobs().remove(event.getEntity()); } private boolean checkPermission(Player player, IAbstractPermission perm) { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityNoDrop.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityNoDrop.java new file mode 100644 index 0000000..6b25e87 --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/EntityNoDrop.java @@ -0,0 +1,37 @@ +package de.jaschastarke.minecraft.limitedcreative.limits; + +import java.util.Map; +import java.util.WeakHashMap; + +import org.bukkit.entity.Entity; + +public class EntityNoDrop { + private Map nodrop = new WeakHashMap(); + + private Prevent get(Entity entity) { + if (!nodrop.containsKey(entity)) + nodrop.put(entity, new Prevent()); + return nodrop.get(entity); + } + + public void preventXP(Entity entity) { + get(entity).xp = true; + } + public void preventDrop(Entity entity) { + get(entity).drops = true; + } + public boolean isXPPrevented(Entity entity) { + return nodrop.containsKey(entity) && nodrop.get(entity).xp; + } + public boolean isDropPrevented(Entity entity) { + return nodrop.containsKey(entity) && nodrop.get(entity).drops; + } + public void remove(Entity entity) { + nodrop.remove(entity); + } + + private class Prevent { + boolean xp = false; + boolean drops = false; + } +} diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java index 878fecb..574e331 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java @@ -218,7 +218,7 @@ public class PlayerListener implements Listener { } } - @EventHandler + @EventHandler(ignoreCancelled = true) public void onEntityDamageByEntity(EntityDamageEvent rawevent) { if (rawevent instanceof EntityDamageByEntityEvent && !rawevent.isCancelled()) { EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) rawevent; @@ -240,7 +240,12 @@ public class PlayerListener implements Listener { } } else if (event.getEntity() instanceof LivingEntity) { if (!checkPermission(player, NoLimitPermissions.STATS_XP)) { - mod.getNoXPMobs().put(event.getEntity(), null); + mod.getNoDropMobs().preventXP(event.getEntity()); + } + } + if (!event.isCancelled()) { + if (!checkPermission(player, NoLimitPermissions.DROP)) { + mod.getNoDropMobs().preventDrop(event.getEntity()); } } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/VehicleListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/VehicleListener.java new file mode 100644 index 0000000..f215734 --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/VehicleListener.java @@ -0,0 +1,47 @@ +package de.jaschastarke.minecraft.limitedcreative.limits; + +import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.vehicle.VehicleDestroyEvent; + +import de.jaschastarke.minecraft.limitedcreative.ModCreativeLimits; + +public class VehicleListener implements Listener { + private ModCreativeLimits mod; + public VehicleListener(ModCreativeLimits mod) { + this.mod = mod; + } + + @EventHandler(ignoreCancelled = true) + public void onVehicleDestroy(VehicleDestroyEvent event) { + if (event.getAttacker() instanceof Player) { + Player player = (Player) event.getAttacker(); + if (player.getGameMode() == GameMode.CREATIVE && !mod.getPlugin().getPermManager().hasPermission(player, NoLimitPermissions.DROP)) { + switch (event.getVehicle().getType()) { + case MINECART: + mod.getBlockSpawn().block(event.getVehicle().getLocation().getBlock().getLocation(), Material.MINECART); + break; + case MINECART_CHEST: + mod.getBlockSpawn().block(event.getVehicle().getLocation().getBlock().getLocation(), Material.STORAGE_MINECART); + break; + case MINECART_FURNACE: + mod.getBlockSpawn().block(event.getVehicle().getLocation().getBlock().getLocation(), Material.POWERED_MINECART); + break; + case MINECART_HOPPER: + mod.getBlockSpawn().block(event.getVehicle().getLocation().getBlock().getLocation(), Material.HOPPER_MINECART); + break; + case MINECART_TNT: + mod.getBlockSpawn().block(event.getVehicle().getLocation().getBlock().getLocation(), Material.EXPLOSIVE_MINECART); + break; + default: + break; + + } + } + } + } + +}