From 69786772f7b426eaebb7221d24aab9fc37171391 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Fri, 2 Mar 2012 20:40:10 +0100 Subject: [PATCH] v1.2.0-alpha: - initial worldguard 5.5.2-snapshot --- lang/en_US.yml | 8 +- plugin.yml | 2 +- .../minecraft/limitedcreative/Core.java | 3 +- .../minecraft/limitedcreative/LCPlayer.java | 2 +- .../limitedcreative/regions/Flags.java | 12 +-- .../limitedcreative/regions/GameModeFlag.java | 80 ++++--------------- .../regions/RegionListener.java | 44 +++++----- .../worldguard/ApplicableRegions.java | 14 ++++ .../minecraft/worldguard/CCommand.java | 23 ++++-- .../minecraft/worldguard/CRegionManager.java | 13 ++- 10 files changed, 92 insertions(+), 109 deletions(-) diff --git a/lang/en_US.yml b/lang/en_US.yml index 6c95e07..9411b12 100644 --- a/lang/en_US.yml +++ b/lang/en_US.yml @@ -54,10 +54,10 @@ blocked: button: To interact with buttons is not allowed in creative mode lever: To interact with levers is not allowed in creative mode survival_flying: You should stay on ground, when leaving a creative-area - outside_creative: You can not place blocks outside of the creative-area - outside_creative_break: You can not destroy blocks outside of the creative-area - inside_survival: You can not place blocks inside of the survival-area - inside_survival_break: You can not destroy blocks inside of the survival-area + outside_place: You can not place blocks outside of the gamemode-area + outside_break: You can not destroy blocks outside of the gamemode-area + inside_place: You can not place blocks inside of the gamemode-area + inside_break: You can not destroy blocks inside of the gamemode-area use: You are not allowed to use this type of item place: You are not allowed to place this type of block break: You are not allowed to break this type of block diff --git a/plugin.yml b/plugin.yml index d20d5ba..e599b05 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: LimitedCreative main: de.jaschastarke.minecraft.limitedcreative.Core -version: 1.0 +version: 1.2.0-alpha softdepend: [WorldGuard, WorldEdit, MultiInv] dev-url: http://dev.bukkit.org/server-mods/limited-creative/ commands: diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Core.java b/src/de/jaschastarke/minecraft/limitedcreative/Core.java index b3851e8..9dcc317 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/Core.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/Core.java @@ -44,8 +44,9 @@ public class Core extends JavaPlugin { @Override public void onDisable() { plugin.getServer().getScheduler().cancelTasks(this); - try { + if (worldguard != null) worldguard.unload(); + try { Locale.unload(); } catch (NoClassDefFoundError e) {} // prevent unload issue diff --git a/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java b/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java index 7bc5310..52fdd89 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java @@ -100,7 +100,7 @@ public class LCPlayer { Players.getOptions().setRegionGameMode(getName(), gm); } - private GameMode getRegionGameMode() { + public GameMode getRegionGameMode() { if (!options.containsKey("region")) { options.put("region", Players.getOptions().getRegionGameMode(getName())); } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/regions/Flags.java b/src/de/jaschastarke/minecraft/limitedcreative/regions/Flags.java index e921e01..1c20af6 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/regions/Flags.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/regions/Flags.java @@ -4,21 +4,17 @@ import java.util.Arrays; import java.util.List; import com.sk89q.worldguard.protection.flags.Flag; -import com.sk89q.worldguard.protection.flags.RegionGroupFlag; +import com.sk89q.worldguard.protection.flags.RegionGroup; import com.sk89q.worldguard.protection.flags.StateFlag; public final class Flags { public static final StateFlag SPAWNDROPS = new StateFlag("spawndrops", true); - public static final StateFlag CREATIVE = new StateFlag("creative", false); - public static final RegionGroupFlag CREATIVE_GROUP = new RegionGroupFlag("creative-group", RegionGroupFlag.RegionGroup.MEMBERS); - static { - CREATIVE.setGroupFlag(CREATIVE_GROUP); - } + public static final GameModeFlag GAMEMODE = new GameModeFlag("gamemode", RegionGroup.MEMBERS); + public static List> getList() { return Arrays.asList(new Flag[]{ SPAWNDROPS, - CREATIVE, - CREATIVE_GROUP + GAMEMODE, }); } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/regions/GameModeFlag.java b/src/de/jaschastarke/minecraft/limitedcreative/regions/GameModeFlag.java index d53ced9..30a305f 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/regions/GameModeFlag.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/regions/GameModeFlag.java @@ -6,89 +6,41 @@ import org.bukkit.command.CommandSender; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.InvalidFlagFormat; -import com.sk89q.worldguard.protection.flags.RegionGroupFlag; +import com.sk89q.worldguard.protection.flags.RegionGroup; /** * Well, that was an interesting idea, but it doesn't work. */ -public class GameModeFlag extends Flag { - private State def; - private RegionGroupFlag groupFlag; - - public enum State { - CREATIVE, - SURVIVAL, - NONE; - - public GameMode getGameMode() { - return getBukkitGameMode(this); - } - public boolean equals (GameMode gm) { - return gm == this.getGameMode(); - } - public static GameMode getBukkitGameMode(State gm) { - switch (gm) { - case CREATIVE: - return GameMode.CREATIVE; - case SURVIVAL: - return GameMode.SURVIVAL; - default: - return null; - } - } - } - - public GameModeFlag(String name, State def) { - super(name); - this.def = def; - } - - public State getDefault() { - return def; - } - - public RegionGroupFlag getGroupFlag() { - return groupFlag; - } - - public void setGroupFlag(RegionGroupFlag groupFlag) { - this.groupFlag = groupFlag; +public class GameModeFlag extends Flag { + public GameModeFlag(String name, RegionGroup defaultGroup) { + super(name, defaultGroup); } @Override - public State parseInput(WorldGuardPlugin plugin, CommandSender sender, String input) throws InvalidFlagFormat { + public GameMode parseInput(WorldGuardPlugin plugin, CommandSender sender, String input) throws InvalidFlagFormat { input = input.trim(); if (input.equalsIgnoreCase("creative")) { - return State.CREATIVE; + return GameMode.CREATIVE; } else if (input.equalsIgnoreCase("survival")) { - return State.SURVIVAL; + return GameMode.SURVIVAL; } else if (input.equalsIgnoreCase("none")) { return null; } else { - throw new InvalidFlagFormat("Expected none/allow/deny but got '" + input + "'"); + throw new InvalidFlagFormat("Expected survival/creative/none but got '" + input + "'"); } } @Override - public State unmarshal(Object o) { - String input = o.toString(); - if (input.equalsIgnoreCase("creative")) { - return State.CREATIVE; - } else if (input.equalsIgnoreCase("survival")) { - return State.SURVIVAL; - } else { - return null; + public GameMode unmarshal(Object o) { + GameMode gm = null; + if (o != null) { + gm = GameMode.valueOf((String) o); } + return gm; } - + @Override - public Object marshal(State o) { - if (o == State.CREATIVE) { - return "allow"; - } else if (o == State.SURVIVAL) { - return "deny"; - } else { - return null; - } + public Object marshal(GameMode o) { + return o == null ? null : o.name(); } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java b/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java index ae0dd72..a4e82f6 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java @@ -38,31 +38,31 @@ public class RegionListener implements Listener { private ApplicableRegions regionSet(Block block) { return rm.getRegionSet(block); } + @EventHandler public void onBlockBreak(BlockBreakEvent event) { if (event.isCancelled()) return; - + LCPlayer player = Players.get(event.getPlayer()); boolean diffrent_region = rm.isDiffrentRegion(event.getPlayer(), event.getBlock().getLocation()); - boolean creative_world = plugin.com.isCreative(event.getBlock().getWorld()); if (player.isRegionGameMode() && diffrent_region) { // do not break outside of "gamemod-change-region" when in the region - if (!rm.getRegionSet(event.getBlock()).allows(Flags.CREATIVE, event.getPlayer())) { - event.getPlayer().sendMessage(L("blocked.outside_creative_break")); + if (rm.getRegionSet(event.getBlock()).getFlag(Flags.GAMEMODE, event.getPlayer()) != player.getRegionGameMode()) { + event.getPlayer().sendMessage(L("blocked.outside_break")); event.setCancelled(true); } } else if (diffrent_region) { // do not break inside of "survial-region in creative world" when outside - if (rm.getRegionSet(event.getBlock()).allows(Flags.CREATIVE)) { - event.getPlayer().sendMessage(L("blocked.inside_survival_break")); + if (rm.getRegionSet(event.getBlock()).getFlag(Flags.GAMEMODE) != null) { + event.getPlayer().sendMessage(L("blocked.inside_break")); event.setCancelled(true); } } - if (!creative_world) { // in survival world - // prevent any drops for survival players in creative regions in survival worlds - if (event.getPlayer().getGameMode() != GameMode.CREATIVE && rm.getRegionSet(event.getBlock()).allows(Flags.CREATIVE)) { + if (!event.isCancelled()) { + // prevent any drops for survival players in creative regions + if (event.getPlayer().getGameMode() != GameMode.CREATIVE && rm.getRegionSet(event.getBlock()).getFlag(Flags.GAMEMODE) == GameMode.CREATIVE) { plugin.spawnblock.block(event.getBlock(), player); } } @@ -78,14 +78,14 @@ public class RegionListener implements Listener { if (player.isRegionGameMode() && diffrent_region) { // do not build outside of "gamemod-change-region" when in the region - if (!rm.getRegionSet(event.getBlock()).allows(Flags.CREATIVE, event.getPlayer())) { - event.getPlayer().sendMessage(L("blocked.outside_creative")); + if (rm.getRegionSet(event.getBlock()).getFlag(Flags.GAMEMODE, event.getPlayer()) != player.getRegionGameMode()) { + event.getPlayer().sendMessage(L("blocked.outside_place")); event.setCancelled(true); } } else if (diffrent_region) { // do not build inside of "survial-region in creative world" when outside - if (rm.getRegionSet(event.getBlock()).allows(Flags.CREATIVE)) { - event.getPlayer().sendMessage(L("blocked.inside_survival")); + if (rm.getRegionSet(event.getBlock()).getFlag(Flags.GAMEMODE) != null) { + event.getPlayer().sendMessage(L("blocked.inside_place")); event.setCancelled(true); } } @@ -93,17 +93,17 @@ public class RegionListener implements Listener { @EventHandler public void onPlayerChangedArea(PlayerNewLocationAreaEvent event) { - Players.get(event.getPlayer()).setRegionCreativeAllowed(event.getRegionSet().allows(Flags.CREATIVE, event.getPlayer()), event); + Players.get(event.getPlayer()).setRegionCreativeAllowed(event.getRegionSet().getFlag(Flags.GAMEMODE, event.getPlayer()) != null, event); } @EventHandler public void onPlayerSetArea(PlayerSetAreaEvent event) { - Players.get(event.getPlayer()).setRegionCreativeAllowed(event.getRegionSet().allows(Flags.CREATIVE, event.getPlayer()), event); + Players.get(event.getPlayer()).setRegionCreativeAllowed(event.getRegionSet().getFlag(Flags.GAMEMODE, event.getPlayer()) != null, event); } @EventHandler public void onPlayerUpdateArea(PlayerUpdateAreaEvent event) { - Players.get(event.getPlayer()).setRegionCreativeAllowed(event.getRegionSet().allows(Flags.CREATIVE, event.getPlayer()), event); + Players.get(event.getPlayer()).setRegionCreativeAllowed(event.getRegionSet().getFlag(Flags.GAMEMODE, event.getPlayer()) != null, event); } @EventHandler @@ -114,11 +114,11 @@ public class RegionListener implements Listener { Block source = event.getBlock().getRelative(event.getDirection()); Core.debug("PistonExtend "+source.getType()+" "+event.getDirection()); if (source.getType() != Material.AIR) { - if (regionSet(source).allows(Flags.CREATIVE)) { + if (regionSet(source).getFlag(Flags.GAMEMODE) == GameMode.CREATIVE) { for (int i = 1; i <= 12; i++) { Block dest = source.getRelative(event.getDirection(), i); Core.debug("dest "+i+": "+dest.getType()); - if (!regionSet(dest).allows(Flags.CREATIVE)) { + if (regionSet(dest).getFlag(Flags.GAMEMODE) != GameMode.CREATIVE) { plugin.logger.warning(L("blocked.piston", source.getRelative(event.getDirection(), i - 1).getType().toString(), Util.toString(source.getLocation()))); event.setCancelled(true); break; @@ -139,19 +139,19 @@ public class RegionListener implements Listener { Core.debug("PistonRetract "+source.getType()+" "+event.getDirection() + " " + event.isSticky()); if (event.isSticky() && source.getType() != Material.AIR) { Core.debug("dest "+dest.getType()); - if (regionSet(source).allows(Flags.CREATIVE)) { - if (!regionSet(dest).allows(Flags.CREATIVE)) { + if (regionSet(source).getFlag(Flags.GAMEMODE) == GameMode.CREATIVE) { + if (regionSet(dest).getFlag(Flags.GAMEMODE) != GameMode.CREATIVE) { plugin.logger.warning(L("blocked.piston", source.getType().toString(), Util.toString(source.getLocation()))); event.setCancelled(true); } - } else if (regionSet(dest).allows(Flags.CREATIVE)) { + } else if (regionSet(dest).getFlag(Flags.GAMEMODE) == GameMode.CREATIVE) { // source isn't creative plugin.logger.warning(L("blocked.piston_in", source.getType().toString(), Util.toString(source.getLocation()))); event.setCancelled(true); } } } - + @EventHandler public void onItemSpawn(ItemSpawnEvent event) { if (event.isCancelled()) diff --git a/src/de/jaschastarke/minecraft/worldguard/ApplicableRegions.java b/src/de/jaschastarke/minecraft/worldguard/ApplicableRegions.java index 5785926..80e563c 100644 --- a/src/de/jaschastarke/minecraft/worldguard/ApplicableRegions.java +++ b/src/de/jaschastarke/minecraft/worldguard/ApplicableRegions.java @@ -49,6 +49,20 @@ public class ApplicableRegions { contractRegionFlags(); return r; } + + public , V> V getFlag(T flag) { + extendRegionFlags(); + V r = regions.getFlag(flag); + contractRegionFlags(); + return r; + } + + public , V> V getFlag(T flag, Player player) { + extendRegionFlags(); + V r = regions.getFlag(flag, WorldGuardIntegration.wg.wrapPlayer(player)); + contractRegionFlags(); + return r; + } @SuppressWarnings("unchecked") private , V> void extendRegionFlags() { diff --git a/src/de/jaschastarke/minecraft/worldguard/CCommand.java b/src/de/jaschastarke/minecraft/worldguard/CCommand.java index 2fa2cec..e14ac81 100644 --- a/src/de/jaschastarke/minecraft/worldguard/CCommand.java +++ b/src/de/jaschastarke/minecraft/worldguard/CCommand.java @@ -19,6 +19,7 @@ package de.jaschastarke.minecraft.worldguard; import static de.jaschastarke.minecraft.utils.Locale.L; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; import org.bukkit.command.Command; @@ -136,8 +137,11 @@ public class CCommand implements CommandExecutor { hasPermission(sender, Perms.INFO); }*/ - String[] args = new String[]{"info", region.getWorld().getName(), region.getProtectedRegion().getId()}; - wg.onCommand(sender, wg.getCommand("region"), "region", args); + /* + * WorldEdits intercepting Servers privates commandMap via Reflections realy sucks! + * Just because they are to lazy to add all the lines commands to plugin.yml + */ + Bukkit.getServer().dispatchCommand(sender, "region info "+region.getWorld().getName()+ " "+region.getProtectedRegion().getId()); StringBuilder list = new StringBuilder(); for (FlagValue data : region.getFlags()) { @@ -157,15 +161,24 @@ public class CCommand implements CommandExecutor { return; } String flagName = args[2]; + String value = null; + Flag flag = FlagList.getFlag(flagName); + + if (args.length > 3 && args[3].equalsIgnoreCase("-g")) { + flag = flag.getRegionGroupFlag(); + if (args.length > 4) + value = Util.join(args, 4); + } else { + if (args.length > 3) + value = Util.join(args, 3); + } + if (flag == null) { sender.sendMessage(ChatColor.DARK_RED + L("command.worldguard.unknown_flag") + ": " + flagName); sender.sendMessage(ChatColor.DARK_RED + L("command.worldguard.available_flags") + ": " + FlagList.getStringListAvailableFlags(sender)); return; } - String value = null; - if (args.length > 3) - value = Util.join(args, 3); try { if (value != null) { diff --git a/src/de/jaschastarke/minecraft/worldguard/CRegionManager.java b/src/de/jaschastarke/minecraft/worldguard/CRegionManager.java index 568167d..38ffb9f 100644 --- a/src/de/jaschastarke/minecraft/worldguard/CRegionManager.java +++ b/src/de/jaschastarke/minecraft/worldguard/CRegionManager.java @@ -103,7 +103,12 @@ public class CRegionManager { ConfigurationSection fs = rs.contains("flags") ? rs.getConfigurationSection("flags") : rs.createSection("flags"); - fs.set(flag.getName(), flag.marshal((V) value)); + + Object o = null; + if (value != null) + o = flag.marshal((V) value); + + fs.set(flag.getName(), o); try { c.save(file); @@ -122,8 +127,10 @@ public class CRegionManager { ConfigurationSection fs = rs.getConfigurationSection("flags"); for (Map.Entry data : fs.getValues(false).entrySet()) { Flag flag = FlagList.getFlag(data.getKey()); - Object value = flag.unmarshal(data.getValue()); - list.add(new FlagValue(flag, value)); + if (flag != null) { // the flag doesn't exists anymore. just ignore it without error + Object value = flag.unmarshal(data.getValue()); + list.add(new FlagValue(flag, value)); + } } } }