From f322468d40868cb63ccf0e1d794152b4265d96e5 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Sat, 21 Sep 2013 22:12:40 +0200 Subject: [PATCH] Usage of AttachedBlock CustomEvents to prevent drop of torches and co Disabled MultiWorld-GameMode-Hook (as MW-GameMode dosn't work at all) plib 1.3 compatibilty to prevent NPE Region-Command and Flag-Storage fixes Creative-World-Suicide fix --- pom.xml | 6 ++--- .../minecraft/limitedcreative/Hooks.java | 5 ++--- .../limitedcreative/ModBlockStates.java | 5 +++++ .../limitedcreative/ModCreativeLimits.java | 14 +++++++++--- .../blockstate/BlockListener.java | 22 +++++++++++++++++++ .../cmdblocker/CmdBlockList.java | 9 ++++++-- .../cmdblocker/CmdBlockerConfig.java | 2 +- .../limitedcreative/limits/BlackList.java | 11 +++++++--- .../limits/BlackListEntity.java | 11 +++++++--- .../limitedcreative/limits/BlockListener.java | 13 +++++++++++ .../regions/RegionListener.java | 17 +++++++++----- .../regions/RegionsCommand.java | 14 ++++++++++-- .../worldguard/CustomRegionManager.java | 6 ++++- .../regions/worldguard/Region.java | 11 ++++------ .../events/PlayerNewLocationAreaEvent.java | 3 +++ 15 files changed, 116 insertions(+), 33 deletions(-) diff --git a/pom.xml b/pom.xml index 2dcadfe..da23981 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ UTF-8 - 1.2 + 1.3-SNAPSHOT @@ -78,13 +78,13 @@ com.sk89q worldguard - 5.7.3 + 5.8 uk.org.whoami authme - 2.9.3 + 2.9.4 diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java index dc5e9cc..96f3775 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/Hooks.java @@ -6,7 +6,6 @@ import de.jaschastarke.hooking.BooleanHooker; import de.jaschastarke.hooking.GetHooker; import de.jaschastarke.minecraft.limitedcreative.hooks.AuthMeHooks; import de.jaschastarke.minecraft.limitedcreative.hooks.MultiVerseHooks; -import de.jaschastarke.minecraft.limitedcreative.hooks.MultiWorldHooks; import de.jaschastarke.minecraft.limitedcreative.hooks.PlayerCheckHooker; import de.jaschastarke.minecraft.limitedcreative.hooks.WorldTypeHooker; import de.jaschastarke.minecraft.limitedcreative.hooks.xAuthHooks; @@ -35,9 +34,9 @@ public final class Hooks { } if (isPluginEnabled("Multiverse-Core")) { new MultiVerseHooks(plugin); - } else if (isPluginEnabled("MultiWorld")) { + }/* else if (isPluginEnabled("MultiWorld")) { // MultiWord suckz, the Creative-World-Setting doesn't affect anything new MultiWorldHooks(plugin); - } + }*/ InventoryIncompatible.register(new GetHooker.Check() { @Override diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java index 860e27b..80a49dd 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java @@ -2,6 +2,7 @@ package de.jaschastarke.minecraft.limitedcreative; import de.jaschastarke.bukkit.lib.CoreModule; import de.jaschastarke.bukkit.lib.commands.AliasHelpedCommand; +import de.jaschastarke.bukkit.lib.modules.AttachedBlocksDestroy; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockListener; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockStateCommand; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockStateConfig; @@ -41,6 +42,10 @@ public class ModBlockStates extends CoreModule { config = new BlockStateConfig(this, entry); plugin.getPluginConfig().registerSection(config); + if (plugin.getModule(AttachedBlocksDestroy.class) == null) { + plugin.addModule(new AttachedBlocksDestroy(plugin)); + } + listeners.addListener(new BlockListener(this)); listeners.addListener(new HangingListener(this)); listeners.addListener(new PlayerListener(this)); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java index 31c52af..8cad51a 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java @@ -5,6 +5,7 @@ import java.util.WeakHashMap; import org.bukkit.entity.Entity; import de.jaschastarke.bukkit.lib.CoreModule; +import de.jaschastarke.bukkit.lib.modules.AttachedBlocksDestroy; import de.jaschastarke.minecraft.limitedcreative.limits.BlockListener; import de.jaschastarke.minecraft.limitedcreative.limits.EntityListener; import de.jaschastarke.minecraft.limitedcreative.limits.LimitConfig; @@ -36,17 +37,24 @@ public class ModCreativeLimits extends CoreModule { listeners.addListener(new BlockListener(this)); config = plugin.getPluginConfig().registerSection(new LimitConfig(this, entry)); - /*blockDrops = plugin.getModule(FeatureBlockItemSpawn.class); + blockDrops = plugin.getModule(FeatureBlockItemSpawn.class); if (blockDrops == null) blockDrops = plugin.addModule(new FeatureBlockItemSpawn(plugin)).getModule(); - */ - + + if (plugin.getModule(AttachedBlocksDestroy.class) == null) { + plugin.addModule(new AttachedBlocksDestroy(plugin)); + } + if (!config.getEnabled()) { entry.initialState = ModuleState.DISABLED; return; } } + public FeatureBlockItemSpawn getBlockSpawn() { + return blockDrops; + } + @Override public void onEnable() { super.onEnable(); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java index 3dfdd5e..94aceed 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java @@ -19,6 +19,7 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.metadata.FixedMetadataValue; +import de.jaschastarke.bukkit.lib.events.AttachedBlockDestroyedEvent; import de.jaschastarke.minecraft.limitedcreative.ModBlockStates; public class BlockListener implements Listener { @@ -49,6 +50,27 @@ public class BlockListener implements Listener { event.setCancelled(true); } } + + @EventHandler + public void onAttachedBlockBreak(AttachedBlockDestroyedEvent event) { + try { + BlockState s = mod.getModel().getState(event.getBlock()); + if (s != null) { + if (mod.isDebug()) + mod.getLog().debug("Breaking attached block: " + s.toString()); + + if (s.isRestricted()) { + if (mod.isDebug()) + mod.getLog().debug("... was placed by creative. Drop prevented"); + mod.getBlockSpawn().block(event.getBlock()); + } + + mod.getModel().removeState(s); + } + } catch (SQLException e) { + mod.getLog().warn("DB-Error while onBlockBreak: "+e.getMessage()); + } + } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlocksBreakByExplosion(EntityExplodeEvent event) { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java index 9d1cdcd..6ba5fc6 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockList.java @@ -12,7 +12,7 @@ public class CmdBlockList extends ArrayList implements Configura private static final long serialVersionUID = -125544131527849084L; @Override - public boolean add(String cmd) throws InvalidValueException { + public boolean addSetting(String cmd) throws InvalidValueException { if (cmd.startsWith("^")) { return add(new RegexpBlockEntry(cmd)); } else { @@ -21,7 +21,7 @@ public class CmdBlockList extends ArrayList implements Configura } @Override - public boolean remove(String e) { + public boolean removeSetting(String e) { for (Iterator iterator = this.iterator(); iterator.hasNext();) { ICmdBlockEntry entry = iterator.next(); if (entry.toString().equalsIgnoreCase(e)) { @@ -41,4 +41,9 @@ public class CmdBlockList extends ArrayList implements Configura return list; } + @Override + public void clearSettings() { + clear(); + } + } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockerConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockerConfig.java index 9a950e3..c3c95b0 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockerConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/cmdblocker/CmdBlockerConfig.java @@ -97,7 +97,7 @@ public class CmdBlockerConfig extends Configuration implements IConfigurationSub if (config.contains("commands") && config.isList("commands")) { for (Object e : config.getList("commands")) { try { - blockList.add(e.toString()); + blockList.addSetting(e.toString()); } catch (InvalidValueException e1) { mod.getLog().warn(e1.getMessage()); } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java index a5b894c..20f9f7b 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackList.java @@ -75,7 +75,7 @@ public class BlackList extends ArrayList implements Confi } else { try { if (el != null) - add(el.toString()); + addSetting(el.toString()); } catch (InvalidValueException e) { System.err.println(e.getCause().getMessage()); } @@ -108,7 +108,7 @@ public class BlackList extends ArrayList implements Confi } @Override // ConfigurableList, not List - public boolean add(String e) throws InvalidValueException { + public boolean addSetting(String e) throws InvalidValueException { if (!contains(e)) { return add(new Blacklisted(e)); } @@ -116,7 +116,7 @@ public class BlackList extends ArrayList implements Confi } @Override // ConfigurableList, not List - public boolean remove(String e) { + public boolean removeSetting(String e) { Iterator it = iterator(); while (it.hasNext()) { if (it.next().toString().equalsIgnoreCase(e)) { @@ -139,4 +139,9 @@ public class BlackList extends ArrayList implements Confi public List toGeneric() { return toStringList(); } + + @Override + public void clearSettings() { + clear(); + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackListEntity.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackListEntity.java index e3ed81d..d12a5fe 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackListEntity.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlackListEntity.java @@ -67,7 +67,7 @@ public class BlackListEntity extends ArrayList impl } else { try { if (el != null) - add(el.toString()); + addSetting(el.toString()); } catch (InvalidValueException e) { System.err.println((e.getCause() != null ? e.getCause() : e).getMessage()); } @@ -100,7 +100,7 @@ public class BlackListEntity extends ArrayList impl } @Override // ConfigurableList, not List - public boolean add(String e) throws InvalidValueException { + public boolean addSetting(String e) throws InvalidValueException { if (!contains(e)) { return add(new Blacklisted(e)); } @@ -108,7 +108,7 @@ public class BlackListEntity extends ArrayList impl } @Override // ConfigurableList, not List - public boolean remove(String e) { + public boolean removeSetting(String e) { Iterator it = iterator(); while (it.hasNext()) { if (it.next().toString().equalsIgnoreCase(e)) { @@ -131,4 +131,9 @@ public class BlackListEntity extends ArrayList impl public List toGeneric() { return toStringList(); } + + @Override + public void clearSettings() { + clear(); + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlockListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlockListener.java index 083dd02..7e36869 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlockListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/BlockListener.java @@ -24,6 +24,8 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import de.jaschastarke.bukkit.lib.events.AttachedBlockDestroyedByPlayerEvent; +import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission; import de.jaschastarke.minecraft.lib.permissions.IDynamicPermission; import de.jaschastarke.minecraft.limitedcreative.ModCreativeLimits; @@ -55,8 +57,19 @@ public class BlockListener implements Listener { } } } + @EventHandler + public void onAttachedBlockBreak(AttachedBlockDestroyedByPlayerEvent event) { + if (event.getPlayer() != null && event.getPlayer().getGameMode() == GameMode.CREATIVE) { + if (!checkPermission(event.getPlayer(), NoLimitPermissions.DROP)) { + mod.getBlockSpawn().block(event.getBlock()); + } + } + } private boolean checkPermission(Player player, IDynamicPermission perm) { return mod.getPlugin().getPermManager().hasPermission(player, perm); } + private boolean checkPermission(Player player, IAbstractPermission perm) { + return mod.getPlugin().getPermManager().hasPermission(player, perm); + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java index 4cf81dc..0b91fd4 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java @@ -73,11 +73,14 @@ public class RegionListener extends Listener { if (mod.isDebug()) mod.getLog().debug(player.getName()+": changed region: "+regionGameMode+": " + areaEvent); - PlayerMoveEvent moveEvent = null; + GameMode defaultGameMode; if (areaEvent instanceof PlayerChangedAreaEvent) - moveEvent = ((PlayerChangedAreaEvent) areaEvent).getMoveEvent(); + defaultGameMode = Hooks.DefaultWorldGameMode.get(((PlayerChangedAreaEvent) areaEvent).getMoveEvent().getTo().getWorld()); + else if (areaEvent instanceof PlayerNewLocationAreaEvent) + defaultGameMode = Hooks.DefaultWorldGameMode.get(((PlayerNewLocationAreaEvent) areaEvent).getNewLocation().getWorld()); + else + defaultGameMode = Hooks.DefaultWorldGameMode.get(player.getWorld()); GameMode currentGameMode = player.getGameMode(); - GameMode defaultGameMode = Hooks.DefaultWorldGameMode.get(moveEvent != null ? moveEvent.getTo().getWorld() : player.getWorld()); if (regionGameMode != null && currentGameMode != regionGameMode && !pmeta.isActiveRegionGameMode(regionGameMode)) { if (mod.isDebug()) @@ -93,7 +96,9 @@ public class RegionListener extends Listener { pmeta.storeActiveRegionGameMode(regionGameMode); // have to be set, before setGameMode if (!isOptional) { - player.setGameMode(regionGameMode); + if (Hooks.IsLoggedIn.test(player)) { // don't change gamemode on login + player.setGameMode(regionGameMode); + } } } } else if (regionGameMode == null && player.getGameMode() != defaultGameMode && !pmeta.isInPermanentGameMode(currentGameMode) && pmeta.getActiveRegionGameMode() != null) { @@ -106,7 +111,9 @@ public class RegionListener extends Listener { // result: change him back to default mode if (checkSwitchFlight(areaEvent)) { pmeta.storeActiveRegionGameMode(null); - player.setGameMode(defaultGameMode); + if (Hooks.IsLoggedIn.test(player)) { // don't change gamemode on login + player.setGameMode(defaultGameMode); + } } } else if (regionGameMode == null && pmeta.isActiveRegionGameMode()) { if (mod.isDebug()) diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java index ff98092..2d1be6d 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java @@ -37,6 +37,7 @@ import de.jaschastarke.minecraft.limitedcreative.regions.worldguard.Region; */ @ArchiveDocComments public class RegionsCommand extends BukkitCommand implements IHelpDescribed { + private final static String GLOBAL_REGION = "__global__"; private ModRegions mod; private HelpCommand help; private WorldGuardPlugin wg; @@ -127,11 +128,18 @@ public class RegionsCommand extends BukkitCommand implements IHelpDescribed { String msg = L("command.worldguard.unknown_flag") + params.getArgument(1) + "\n" + L("command.worldguard.available_flags") + FlagList.getStringListAvailableFlags(context.getSender()); throw new CommandException(msg); + } else if (params.getFlags().contains("g")) { + flag = flag.getRegionGroupFlag(); + if (flag == null) { + String msg = L("command.worldguard.unknown_flag") + params.getArgument(1) + "-group\n" + + L("command.worldguard.available_flags") + FlagList.getStringListAvailableFlags(context.getSender()); + throw new CommandException(msg); + } } String value = params.getValue(); try { - if (value != null) { + if (value != null && value.trim().length() > 0) { reg.setFlag(flag, flag.parseInput(getWorldGuard(), context.getSender(), value)); } else { reg.setFlag(flag, null); @@ -172,12 +180,14 @@ public class RegionsCommand extends BukkitCommand implements IHelpDescribed { ApplicableRegionSet set = mgr.getApplicableRegions(context.getPlayer().getLocation()); if (set.size() > 0) { region = set.iterator().next(); + } else { + region = getWorldGuard().getGlobalRegionManager().get(w).getRegionExact(GLOBAL_REGION); } } else { int rpc = params.getArgumentCount() > 1 ? 1 : 0; RegionManager mgr = getWorldGuard().getGlobalRegionManager().get(w); region = mgr.getRegion(params.getArgument(rpc)); - if (region == null && params.getArgument(rpc).equalsIgnoreCase("__global__")) { + if (region == null && params.getArgument(rpc).equalsIgnoreCase(GLOBAL_REGION)) { region = new GlobalProtectedRegion(params.getArgument(rpc)); mgr.addRegion(region); } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/CustomRegionManager.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/CustomRegionManager.java index 0eabda2..34be3a0 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/CustomRegionManager.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/CustomRegionManager.java @@ -108,7 +108,11 @@ public class CustomRegionManager { ConfigurationSection fs = rs.contains("flags") ? rs.getConfigurationSection("flags") : rs.createSection("flags"); - fs.set(flag.getName(), flag.marshal((V) value)); + if (value == null) { + fs.set(flag.getName(), null); + } else { + fs.set(flag.getName(), flag.marshal((V) value)); + } try { c.save(file); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/Region.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/Region.java index 681a357..0d90a1d 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/Region.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/Region.java @@ -49,13 +49,10 @@ public class Region { } public void setFlag(Flag flag, Object value) { - if (value == null) { - removeFlag(flag); - } else { - if (flags != null) - flags.add(new FlagValue(flag, value)); - mgr.storeFlag(this, flag, value); - } + removeFlag(flag); + if (flags != null && value != null) + flags.add(new FlagValue(flag, value)); + mgr.storeFlag(this, flag, value); } public ProtectedRegion getProtectedRegion() { return region; diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/events/PlayerNewLocationAreaEvent.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/events/PlayerNewLocationAreaEvent.java index d2f3c79..3d117b5 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/events/PlayerNewLocationAreaEvent.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/worldguard/events/PlayerNewLocationAreaEvent.java @@ -69,4 +69,7 @@ public class PlayerNewLocationAreaEvent extends PlayerAreaEvent { public static HandlerList getHandlerList() { return handlers; } + public Location getNewLocation() { + return location; + } }