diff --git a/lang/default.yml b/lang/default.yml index bcf47c9..45779ef 100644 --- a/lang/default.yml +++ b/lang/default.yml @@ -1,3 +1,7 @@ +basic: + loaded: + worldguard: Successfully loaded with WorldGuard-Integration + no_worldguard: Successfully loaded (without WorldGuard) command: player: player switch: @@ -39,4 +43,7 @@ blocked: sign: To interact with signs 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 \ No newline at end of file + outside_creative_break: You can not destroy blocks outside of the creative-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 \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index e68fdbf..aeffb66 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: LimitedCreative main: de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore -version: 0.6-beta +version: 0.8-beta softdepend: [WorldGuard] dev-url: http://dev.bukkit.org/server-mods/limited-creative/ commands: diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java b/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java index e719787..c0012d9 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java @@ -42,6 +42,13 @@ public class Configuration { c = plugin.getConfig(); } + + /** + * Intended to be undocumented ;) + */ + public boolean getDebug() { + return c.getBoolean("debug", false); + } public boolean getStoreEnabled() { return c.getBoolean("store.enabled", true); @@ -150,5 +157,4 @@ public class Configuration { } return list; } - } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java b/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java index 063fc5f..bf4e701 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/LCPlayer.java @@ -17,8 +17,6 @@ */ package de.jaschastarke.minecraft.limitedcreative; -import static de.jaschastarke.minecraft.utils.Locale.L; - import java.io.File; import java.io.IOException; import java.util.HashMap; @@ -44,6 +42,7 @@ import org.bukkit.inventory.ItemStack; import de.jaschastarke.minecraft.limitedcreative.Commands.LackingPermissionException; import de.jaschastarke.minecraft.limitedcreative.serialize.Items; +import static de.jaschastarke.minecraft.utils.Locale.L; public class LCPlayer { private static LimitedCreativeCore plugin = LimitedCreativeCore.plugin; @@ -153,7 +152,7 @@ public class LCPlayer { if (player.getGameMode() == GameMode.CREATIVE) { if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.drop")) return; - event.setCancelled(true); + event.getItemDrop().remove(); } } public void onPickupItem(PlayerPickupItemEvent event) { @@ -239,7 +238,7 @@ public class LCPlayer { Location newloc = event.getTo().clone(); newloc.setX(event.getFrom().getX()); - //newloc.setY(event.getFrom().getY()); + newloc.setY(event.getFrom().getY()); // well, otherwise flying high out of the region is possible newloc.setZ(event.getFrom().getZ()); event.setTo(newloc); } else { diff --git a/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java b/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java index 92404d4..2a2f676 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java @@ -22,6 +22,8 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; import static de.jaschastarke.minecraft.utils.Locale.L; +import de.jaschastarke.minecraft.limitedcreative.listeners.LimitListener; +import de.jaschastarke.minecraft.limitedcreative.listeners.MainListener; import de.jaschastarke.minecraft.limitedcreative.regions.WorldGuardIntegration; import de.jaschastarke.minecraft.utils.Locale; @@ -32,41 +34,53 @@ public class LimitedCreativeCore extends JavaPlugin { public WorldGuardIntegration worldguard; public static LimitedCreativeCore plugin; public NoBlockItemSpawn spawnblock; - - public static boolean serializeFallBack = false; @Override public void onDisable() { + plugin = null; + worldguard = null; + config = null; + spawnblock = null; Locale.unload(); - logger.info("["+this.getDescription().getName()+"] cleanly unloaded."); + //info("cleanly unloaded."); } @Override public void onEnable() { plugin = this; config = new Configuration(this); - try { - Class.forName("org.bukkit.configuration.file.YamlConstructor", false, getClassLoader()); - } catch (ClassNotFoundException e) { - serializeFallBack = true; - } new Locale(this); spawnblock = new NoBlockItemSpawn(); - Listener.register(this); + getServer().getPluginManager().registerEvents(new MainListener(this), this); + if (config.getLimitEnabled()) + getServer().getPluginManager().registerEvents(new LimitListener(this), this); if (config.getRegionEnabled() && WorldGuardIntegration.available()) { worldguard = new WorldGuardIntegration(this); - worldguard.init(); - } else { - logger.info("["+this.getDescription().getName()+"] "+L("warning.no_worldguard_found")); + } else if(config.getRegionEnabled()) { + warn(L("warning.no_worldguard_found")); } Commands.register(this); PluginDescriptionFile df = this.getDescription(); - logger.info("["+df.getName() + " v" + df.getVersion() + "] done loading."); + if (worldguard != null) + logger.info("["+df.getName() + " v" + df.getVersion() + "] "+L("basic.loaded.worldguard")); + else + logger.info("["+df.getName() + " v" + df.getVersion() + "] "+L("basic.loaded.no_worldguard")); + } + + public void info(String s) { + logger.info("["+this.getDescription().getName()+"] " + s); + } + public void warn(String s) { + logger.warning("["+this.getDescription().getName()+"] " + s); + } + public static void debug(String s) { + if (plugin.config.getDebug()) + plugin.info("DEBUG: " + s); } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Listener.java b/src/de/jaschastarke/minecraft/limitedcreative/Listener.java deleted file mode 100644 index b51d83a..0000000 --- a/src/de/jaschastarke/minecraft/limitedcreative/Listener.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * Limited Creative - (Bukkit Plugin) - * Copyright (C) 2012 jascha@ja-s.de - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package de.jaschastarke.minecraft.limitedcreative; - -import org.bukkit.GameMode; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.block.ContainerBlock; -import org.bukkit.block.Sign; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Item; -import org.bukkit.entity.Player; -import org.bukkit.entity.StorageMinecart; -import org.bukkit.event.Event; -import org.bukkit.event.Event.Priority; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockListener; -import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.entity.EntityListener; -import org.bukkit.event.entity.ItemSpawnEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerGameModeChangeEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerListener; -import org.bukkit.event.player.PlayerPickupItemEvent; -import org.bukkit.event.player.PlayerRespawnEvent; -import org.bukkit.plugin.PluginManager; - -public final class Listener { - private static LimitedCreativeCore plugin; - private static PluginManager pm; - - public static class PlayerListen extends PlayerListener { - @Override - public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) { - if (event.getNewGameMode() == GameMode.CREATIVE) { - if (!LCPlayer.get(event.getPlayer()).onSetCreative()) - event.setCancelled(true); - } else if (event.getNewGameMode() == GameMode.SURVIVAL) { - if (!LCPlayer.get(event.getPlayer()).onSetSurvival()) - event.setCancelled(true); - } - } - - - @Override - public void onPlayerRespawn(PlayerRespawnEvent event) { - LCPlayer.get(event.getPlayer()).onRespawn(event); - } - - - @Override - public void onPlayerDropItem(PlayerDropItemEvent event) { - LCPlayer.get(event.getPlayer()).onDropItem(event); - } - - @Override - public void onPlayerPickupItem(PlayerPickupItemEvent event) { - LCPlayer.get(event.getPlayer()).onPickupItem(event); - } - - @Override - public void onPlayerInteract(PlayerInteractEvent event) { - if (isCancelled(event) || event.getPlayer().getGameMode() != GameMode.CREATIVE) - return; - - if (!plugin.config.getPermissionsEnabled() || event.getPlayer().hasPermission("limitedcreative.nolimit.use")) { - if (event.getItem() != null && plugin.config.getBlockedUse().contains(event.getItem().getType())) { - event.setCancelled(true); - event.setUseItemInHand(Event.Result.DENY); - return; - } - } - - if (event.getAction() != Action.RIGHT_CLICK_BLOCK) - return; - - Block block = event.getClickedBlock(); - - if (block.getState() instanceof ContainerBlock) { - LCPlayer.get(event.getPlayer()).onChestAccess(event); - } - if (block.getState() instanceof Sign) { - LCPlayer.get(event.getPlayer()).onSignAccess(event); - } - } - /** - * The isCancelled in PlayerInteractEvent doesn't check useItemInHand, even this decides (when clicking on - * entity with e.g. a bucket) - * @param event - * @return The relevant "isCancelled" - */ - public static boolean isCancelled(PlayerInteractEvent event) { - return event.useInteractedBlock() == Event.Result.DENY && event.useItemInHand() == Event.Result.DENY; - } - - @Override - public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { - if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.CREATIVE) - return; - - if (!plugin.config.getPermissionsEnabled() || event.getPlayer().hasPermission("limitedcreative.nolimit.use")) { - if (event.getPlayer().getItemInHand() != null && plugin.config.getBlockedUse().contains(event.getPlayer().getItemInHand().getType())) { - event.setCancelled(true); - return; - } - } - - Entity entity = event.getRightClicked(); - - if (entity instanceof StorageMinecart) { - LCPlayer.get(event.getPlayer()).onChestAccess(event); - } - } - - private void register() { - pm.registerEvent(Event.Type.PLAYER_GAME_MODE_CHANGE, this, Priority.Normal, plugin); - if (plugin.config.getLimitEnabled()) { - pm.registerEvent(Event.Type.PLAYER_DROP_ITEM, this, Priority.Normal, plugin); - pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, this, Priority.Normal, plugin); - pm.registerEvent(Event.Type.PLAYER_INTERACT, this, Priority.Lowest, plugin); - pm.registerEvent(Event.Type.PLAYER_INTERACT_ENTITY, this, Priority.Lowest, plugin); - pm.registerEvent(Event.Type.PLAYER_RESPAWN, this, Priority.Normal, plugin); - } - } - } - - public static class EntityListen extends EntityListener { - @Override - public void onEntityDamage(EntityDamageEvent meta_event) { - if (meta_event instanceof EntityDamageByEntityEvent) { - EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) meta_event; - if (event.getEntity() instanceof Player) { - LCPlayer.get((Player) event.getEntity()).onDamage(event); - } - } - } - - @Override - public void onEntityDeath(EntityDeathEvent event) { - if (event.getEntity() instanceof Player) { - Player player = (Player) event.getEntity(); - LCPlayer.get(player).onDie(event); - } - } - - @Override - public void onItemSpawn(ItemSpawnEvent event) { - if (event.getEntity() instanceof Item) { - if (plugin.spawnblock.isBlocked(event.getLocation().getBlock().getLocation(), ((Item) event.getEntity()).getItemStack().getType())) { - event.setCancelled(true); - } - } - } - - private void register() { - if (plugin.config.getLimitEnabled() || plugin.worldguard != null) { - pm.registerEvent(Event.Type.ITEM_SPAWN, this, Priority.Normal, plugin); - } - if (plugin.config.getLimitEnabled()) { - pm.registerEvent(Event.Type.ENTITY_DAMAGE, this, Priority.Normal, plugin); - pm.registerEvent(Event.Type.ENTITY_DEATH, this, Priority.Low, plugin); - } - } - } - - public static class BlockListen extends BlockListener { - @Override - public void onBlockBreak(BlockBreakEvent event) { - if (event.isCancelled()) - return; - if (event.getPlayer().getGameMode() == GameMode.CREATIVE) { - if (!plugin.config.getPermissionsEnabled() || event.getPlayer().hasPermission("limitedcreative.nolimit.break")) { - if (plugin.config.getBlockedBreaks().contains(event.getBlock().getType())) { - event.setCancelled(true); - } - } - - if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.drop")) - return; - // Prevent dropping of doors and beds when destroying the wrong part - Block block = event.getBlock(); - Material mat = block.getType(); - switch (event.getBlock().getType()) { - case WOODEN_DOOR: - mat = Material.WOOD_DOOR; - plugin.spawnblock.block(block.getRelative(BlockFace.DOWN).getLocation(), mat); - break; - case IRON_DOOR_BLOCK: - mat = Material.IRON_DOOR; - plugin.spawnblock.block(block.getRelative(BlockFace.DOWN).getLocation(), mat); - break; - case BED_BLOCK: - mat = Material.BED; - plugin.spawnblock.block(block.getRelative(BlockFace.NORTH).getLocation(), mat); - plugin.spawnblock.block(block.getRelative(BlockFace.EAST).getLocation(), mat); - plugin.spawnblock.block(block.getRelative(BlockFace.SOUTH).getLocation(), mat); - plugin.spawnblock.block(block.getRelative(BlockFace.WEST).getLocation(), mat); - break; - default: - plugin.spawnblock.block(event.getBlock().getLocation(), mat); - } - } - } - @Override - public void onBlockPlace(BlockPlaceEvent event) { - if (event.isCancelled()) - return; - if (event.getPlayer().getGameMode() == GameMode.CREATIVE) { - if (!plugin.config.getPermissionsEnabled() || event.getPlayer().hasPermission("limitedcreative.nolimit.use")) { - if (plugin.config.getBlockedUse().contains(event.getBlock().getType())) { - event.setCancelled(true); - } - } - } - } - private void register() { - if (plugin.config.getLimitEnabled()) { - pm.registerEvent(Event.Type.BLOCK_BREAK, this, Priority.Normal, plugin); - pm.registerEvent(Event.Type.BLOCK_PLACE, this, Priority.Normal, plugin); - } - } - } - /* - public static class VehicleListen extends VehicleListener { - @Override - public void onVehicleDestroy(VehicleDestroyEvent event) { - if (event.isCancelled()) - return; - if (event.getAttacker() instanceof Player) { - Player player = (Player) event.getAttacker(); - if (player.getGameMode() == GameMode.CREATIVE) { - if (plugin.config.getPermissionsEnabled() && player.hasPermission("limitedcreative.nolimit.drop")) - return; - plugin.logger.info("Vehicle destroy: "+event.getVehicle() + " - "+event.getVehicle().getEntityId()); - } - } - } - - private void register() { - if (plugin.config.getLimitEnabled()) { - pm.registerEvent(Event.Type.VEHICLE_DESTROY, this, Priority.Normal, plugin); - } - } - }*/ - - public static void register(LimitedCreativeCore pplugin) { - plugin = pplugin; - pm = plugin.getServer().getPluginManager(); - - new PlayerListen().register(); - new EntityListen().register(); - new BlockListen().register(); - //new VehicleListen().register(); - } -} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/NoBlockItemSpawn.java b/src/de/jaschastarke/minecraft/limitedcreative/NoBlockItemSpawn.java index 29ab766..e81c791 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/NoBlockItemSpawn.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/NoBlockItemSpawn.java @@ -25,10 +25,11 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; public class NoBlockItemSpawn { - public final static long TIME_OFFSET = 1000; + public final static long TIME_OFFSET = 250; private List list = new ArrayList(); @@ -70,12 +71,14 @@ public class NoBlockItemSpawn { } public void block(Block block, LCPlayer player) { - Material mat = block.getType(); - if (player == null || !player.getRaw().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { // different drop type prevention - net.minecraft.server.Block type = net.minecraft.server.Block.byId[mat.getId()]; - mat = Material.getMaterial(type.getDropType(block.getData(), null, 0)); + if (player.getRaw().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { + block(block.getLocation(), block.getType()); + } else { + // doesn't include silktouch + for (ItemStack i : block.getDrops(player.getRaw().getItemInHand())) { + block(block.getLocation(), i.getType()); + } } - block(block.getLocation(), mat); } public void block(Block block) { diff --git a/src/de/jaschastarke/minecraft/limitedcreative/listeners/LimitListener.java b/src/de/jaschastarke/minecraft/limitedcreative/listeners/LimitListener.java new file mode 100644 index 0000000..11404d7 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/listeners/LimitListener.java @@ -0,0 +1,186 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.listeners; + +import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.ContainerBlock; +import org.bukkit.block.Sign; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.entity.StorageMinecart; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDeathEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerPickupItemEvent; +import org.bukkit.event.player.PlayerRespawnEvent; + +import de.jaschastarke.minecraft.limitedcreative.LCPlayer; +import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore; +import static de.jaschastarke.minecraft.utils.Locale.L; + +public class LimitListener implements Listener { + private LimitedCreativeCore plugin; + public LimitListener(LimitedCreativeCore plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onPlayerRespawn(PlayerRespawnEvent event) { + LCPlayer.get(event.getPlayer()).onRespawn(event); + } + + + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) { + LCPlayer.get(event.getPlayer()).onDropItem(event); + } + + @EventHandler + public void onPlayerPickupItem(PlayerPickupItemEvent event) { + LCPlayer.get(event.getPlayer()).onPickupItem(event); + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (MainListener.isCancelled(event) || event.getPlayer().getGameMode() != GameMode.CREATIVE) + return; + + if (!plugin.config.getPermissionsEnabled() || !event.getPlayer().hasPermission("limitedcreative.nolimit.use")) { + if (event.getItem() != null && plugin.config.getBlockedUse().contains(event.getItem().getType())) { + event.setCancelled(true); + event.setUseItemInHand(Event.Result.DENY); + event.getPlayer().sendMessage(L("blocked.use")); + return; + } + } + + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) + return; + + Block block = event.getClickedBlock(); + + if (block.getState() instanceof ContainerBlock) { + LCPlayer.get(event.getPlayer()).onChestAccess(event); + } + if (block.getState() instanceof Sign) { + LCPlayer.get(event.getPlayer()).onSignAccess(event); + } + } + + @EventHandler + public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { + if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.CREATIVE) + return; + + if (!plugin.config.getPermissionsEnabled() || !event.getPlayer().hasPermission("limitedcreative.nolimit.use")) { + if (event.getPlayer().getItemInHand() != null && plugin.config.getBlockedUse().contains(event.getPlayer().getItemInHand().getType())) { + event.setCancelled(true); + event.getPlayer().sendMessage(L("blocked.use")); + return; + } + } + + Entity entity = event.getRightClicked(); + + if (entity instanceof StorageMinecart) { + LCPlayer.get(event.getPlayer()).onChestAccess(event); + } + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent meta_event) { + if (meta_event instanceof EntityDamageByEntityEvent) { + EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) meta_event; + if (event.getEntity() instanceof Player) { + LCPlayer.get((Player) event.getEntity()).onDamage(event); + } + } + } + + @EventHandler + public void onEntityDeath(EntityDeathEvent event) { + if (event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + LCPlayer.get(player).onDie(event); + } + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + if (event.isCancelled()) + return; + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) { + if (!plugin.config.getPermissionsEnabled() || !event.getPlayer().hasPermission("limitedcreative.nolimit.break")) { + if (plugin.config.getBlockedBreaks().contains(event.getBlock().getType())) { + event.setCancelled(true); + event.getPlayer().sendMessage(L("blocked.break")); + } + } + + if (plugin.config.getPermissionsEnabled() && event.getPlayer().hasPermission("limitedcreative.nolimit.drop")) + return; + // Prevent dropping of doors and beds when destroying the wrong part + Block block = event.getBlock(); + Material mat = block.getType(); + switch (event.getBlock().getType()) { + case WOODEN_DOOR: + mat = Material.WOOD_DOOR; + plugin.spawnblock.block(block.getRelative(BlockFace.DOWN).getLocation(), mat); + break; + case IRON_DOOR_BLOCK: + mat = Material.IRON_DOOR; + plugin.spawnblock.block(block.getRelative(BlockFace.DOWN).getLocation(), mat); + break; + case BED_BLOCK: + mat = Material.BED; + plugin.spawnblock.block(block.getRelative(BlockFace.NORTH).getLocation(), mat); + plugin.spawnblock.block(block.getRelative(BlockFace.EAST).getLocation(), mat); + plugin.spawnblock.block(block.getRelative(BlockFace.SOUTH).getLocation(), mat); + plugin.spawnblock.block(block.getRelative(BlockFace.WEST).getLocation(), mat); + break; + default: + plugin.spawnblock.block(event.getBlock().getLocation(), mat); + } + } + } + @EventHandler + public void onBlockPlace(BlockPlaceEvent event) { + if (event.isCancelled()) + return; + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) { + if (!plugin.config.getPermissionsEnabled() || !event.getPlayer().hasPermission("limitedcreative.nolimit.use")) { + if (plugin.config.getBlockedUse().contains(event.getBlock().getType())) { + event.setCancelled(true); + event.getPlayer().sendMessage(L("blocked.place")); + } + } + } + } +} \ No newline at end of file diff --git a/src/de/jaschastarke/minecraft/limitedcreative/listeners/MainListener.java b/src/de/jaschastarke/minecraft/limitedcreative/listeners/MainListener.java new file mode 100644 index 0000000..1963869 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/listeners/MainListener.java @@ -0,0 +1,94 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.listeners; + +import org.bukkit.GameMode; +import org.bukkit.entity.Item; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.ItemSpawnEvent; +import org.bukkit.event.player.PlayerGameModeChangeEvent; +import org.bukkit.event.player.PlayerInteractEvent; + +import de.jaschastarke.minecraft.limitedcreative.LCPlayer; +import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore; + +public class MainListener implements Listener { + private LimitedCreativeCore plugin; + public MainListener(LimitedCreativeCore plugin) { + this.plugin = plugin; + } + + /** + * The isCancelled in PlayerInteractEvent doesn't check useItemInHand, even this decides (when clicking on + * entity with e.g. a bucket) + * @param event + * @return The relevant "isCancelled" + */ + public static boolean isCancelled(PlayerInteractEvent event) { + return event.useInteractedBlock() == Event.Result.DENY && event.useItemInHand() == Event.Result.DENY; + } + + @EventHandler + public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) { + if (event.getNewGameMode() == GameMode.CREATIVE) { + if (!LCPlayer.get(event.getPlayer()).onSetCreative()) + event.setCancelled(true); + } else if (event.getNewGameMode() == GameMode.SURVIVAL) { + if (!LCPlayer.get(event.getPlayer()).onSetSurvival()) + event.setCancelled(true); + } + } + + /** + * Also needed if WorldGuard-Feature is enabled, so can not moved to optional Listener "Limit". + */ + @EventHandler + public void onItemSpawn(ItemSpawnEvent event) { + if (event.getEntity() instanceof Item) { + if (plugin.spawnblock.isBlocked(event.getLocation().getBlock().getLocation(), ((Item) event.getEntity()).getItemStack().getType())) { + event.setCancelled(true); + } + } + } + + + /* + public static class VehicleListen extends VehicleListener { + @EventHandler + public void onVehicleDestroy(VehicleDestroyEvent event) { + if (event.isCancelled()) + return; + if (event.getAttacker() instanceof Player) { + Player player = (Player) event.getAttacker(); + if (player.getGameMode() == GameMode.CREATIVE) { + if (plugin.config.getPermissionsEnabled() && player.hasPermission("limitedcreative.nolimit.drop")) + return; + plugin.logger.info("Vehicle destroy: "+event.getVehicle() + " - "+event.getVehicle().getEntityId()); + } + } + } + + private void register() { + if (plugin.config.getLimitEnabled()) { + pm.registerEvent(Event.Type.VEHICLE_DESTROY, this, Priority.Normal, plugin); + } + } + }*/ +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/regions/Flags.java b/src/de/jaschastarke/minecraft/limitedcreative/regions/Flags.java new file mode 100644 index 0000000..bcc784b --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/regions/Flags.java @@ -0,0 +1,12 @@ +package de.jaschastarke.minecraft.limitedcreative.regions; + +import com.sk89q.worldguard.protection.flags.RegionGroupFlag; +import com.sk89q.worldguard.protection.flags.StateFlag; + +public final class Flags { + 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); + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java b/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java new file mode 100644 index 0000000..31897cd --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/regions/RegionListener.java @@ -0,0 +1,97 @@ +package de.jaschastarke.minecraft.limitedcreative.regions; + +import static de.jaschastarke.minecraft.utils.Locale.L; + +import org.bukkit.GameMode; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +import com.sk89q.worldedit.Vector; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.managers.RegionManager; + +import de.jaschastarke.minecraft.limitedcreative.LCPlayer; +import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore; +import de.jaschastarke.minecraft.worldguard.ApplicableRegions; +import de.jaschastarke.minecraft.worldguard.CRegionManager; + +public class RegionListener implements Listener { + private static LimitedCreativeCore plugin = WorldGuardIntegration.plugin; + private static WorldGuardPlugin wg = WorldGuardIntegration.wg; + private CRegionManager rm; + public RegionListener(WorldGuardIntegration wgi) { + rm = wgi.getRegionManager(); + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + if (event.isCancelled()) + return; + LCPlayer player = LCPlayer.get(event.getPlayer()); + RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); + Vector pt = new Vector(event.getBlock().getLocation().getBlockX(), event.getBlock().getLocation().getBlockY(), event.getBlock().getLocation().getBlockZ()); + ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); + + if (player.isRegionCreative() && !set.allows(Flags.CREATIVE, player)) { + event.getPlayer().sendMessage(L("blocked.outside_creative_break")); + event.setCancelled(true); + } else if (set.allows(Flags.CREATIVE) && player.getRaw().getGameMode() != GameMode.CREATIVE) { + plugin.spawnblock.block(event.getBlock(), player); + } + } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent event) { + if (event.isCancelled()) + return; + LCPlayer player = LCPlayer.get(event.getPlayer()); + if (player.isRegionCreative()) { + // do not build outside of creative regions, when in the region + RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); + Vector pt = new Vector(event.getBlock().getLocation().getBlockX(), event.getBlock().getLocation().getBlockY(), event.getBlock().getLocation().getBlockZ()); + ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); + if (!set.allows(Flags.CREATIVE, player)) { + event.getPlayer().sendMessage(L("blocked.outside_creative")); + event.setCancelled(true); + } + } + } + + @EventHandler + public void onPlayerMove(PlayerMoveEvent event) { + if (event.isCancelled()) + return; + if (event.getFrom().getBlockX() != event.getTo().getBlockX() + || event.getFrom().getBlockY() != event.getTo().getBlockY() + || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { + + LCPlayer player = LCPlayer.get(event.getPlayer()); + RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); + Vector pt = new Vector(event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ()); + ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); + + player.setRegionCreativeAllowed(set.allows(Flags.CREATIVE, player), event); + } + } + + @EventHandler + public void onPlayerTeleport(PlayerTeleportEvent event) { + if (event.isCancelled()) + return; + if (event.getFrom().getBlockX() != event.getTo().getBlockX() + || event.getFrom().getBlockY() != event.getTo().getBlockY() + || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { + + LCPlayer player = LCPlayer.get(event.getPlayer()); + RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); + Vector pt = new Vector(event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ()); + ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); + + player.setRegionCreativeAllowed(set.allows(Flags.CREATIVE, player), event); + } + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/regions/WorldGuardIntegration.java b/src/de/jaschastarke/minecraft/limitedcreative/regions/WorldGuardIntegration.java index 696b41c..4c41a52 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/regions/WorldGuardIntegration.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/regions/WorldGuardIntegration.java @@ -19,144 +19,34 @@ package de.jaschastarke.minecraft.limitedcreative.regions; import java.io.File; -import org.bukkit.GameMode; -import org.bukkit.event.Event; -import org.bukkit.event.Event.Priority; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockListener; -import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.event.player.PlayerListener; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerTeleportEvent; - - -import com.sk89q.worldedit.Vector; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.protection.flags.RegionGroupFlag; -import com.sk89q.worldguard.protection.flags.StateFlag; -import com.sk89q.worldguard.protection.managers.RegionManager; -import de.jaschastarke.minecraft.limitedcreative.LCPlayer; import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore; -import de.jaschastarke.minecraft.worldguard.ApplicableRegions; import de.jaschastarke.minecraft.worldguard.CRegionManager; import de.jaschastarke.minecraft.worldguard.FlagList; -import static de.jaschastarke.minecraft.utils.Locale.L; public class WorldGuardIntegration { public static LimitedCreativeCore plugin; public static WorldGuardPlugin wg; private CRegionManager rm; - public WorldGuardIntegration(LimitedCreativeCore pplugin) { - plugin = pplugin; + public WorldGuardIntegration(LimitedCreativeCore plugin) { + WorldGuardIntegration.plugin = plugin; wg = (WorldGuardPlugin) plugin.getServer().getPluginManager().getPlugin("WorldGuard"); + + rm = new CRegionManager(new File(plugin.getDataFolder(), "regions.yml")); + + FlagList.addFlag(Flags.CREATIVE); + FlagList.addFlag(Flags.CREATIVE_GROUP); + + plugin.getServer().getPluginManager().registerEvents(new RegionListener(this), plugin); } public static boolean available() { return LimitedCreativeCore.plugin.getServer().getPluginManager().getPlugin("WorldGuard") != null; } - - 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 void init() { - rm = new CRegionManager(new File(plugin.getDataFolder(), "regions.yml")); - - FlagList.addFlag(CREATIVE); - FlagList.addFlag(CREATIVE_GROUP); - - new WGIPlayerListen().register(); - new WGIBlockListen().register(); - } - public CRegionManager getRegionManager() { return rm; } - - - public class WGIBlockListen extends BlockListener { - @Override - public void onBlockBreak(BlockBreakEvent event) { - if (event.isCancelled()) - return; - LCPlayer player = LCPlayer.get(event.getPlayer()); - RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); - Vector pt = new Vector(event.getBlock().getLocation().getBlockX(), event.getBlock().getLocation().getBlockY(), event.getBlock().getLocation().getBlockZ()); - ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); - - if (player.isRegionCreative() && !set.allows(CREATIVE, player)) { - event.getPlayer().sendMessage(L("blocked.outside_creative_break")); - event.setCancelled(true); - } else if (set.allows(CREATIVE) && player.getRaw().getGameMode() != GameMode.CREATIVE) { - plugin.spawnblock.block(event.getBlock(), player); - } - } - - @Override - public void onBlockPlace(BlockPlaceEvent event) { - if (event.isCancelled()) - return; - LCPlayer player = LCPlayer.get(event.getPlayer()); - if (player.isRegionCreative()) { - // do not build outside of creative regions, when in the region - RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); - Vector pt = new Vector(event.getBlock().getLocation().getBlockX(), event.getBlock().getLocation().getBlockY(), event.getBlock().getLocation().getBlockZ()); - ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); - if (!set.allows(CREATIVE, player)) { - event.getPlayer().sendMessage(L("blocked.outside_creative")); - event.setCancelled(true); - } - } - } - - private void register() { - plugin.getServer().getPluginManager().registerEvent(Event.Type.BLOCK_BREAK, this, Priority.Normal, plugin); - plugin.getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PLACE, this, Priority.Normal, plugin); - } - } - public class WGIPlayerListen extends PlayerListener { - @Override - public void onPlayerMove(PlayerMoveEvent event) { - if (event.isCancelled()) - return; - if (event.getFrom().getBlockX() != event.getTo().getBlockX() - || event.getFrom().getBlockY() != event.getTo().getBlockY() - || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { - - LCPlayer player = LCPlayer.get(event.getPlayer()); - RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); - Vector pt = new Vector(event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ()); - ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); - - player.setRegionCreativeAllowed(set.allows(CREATIVE, player), event); - } - } - - @Override - public void onPlayerTeleport(PlayerTeleportEvent event) { - if (event.isCancelled()) - return; - if (event.getFrom().getBlockX() != event.getTo().getBlockX() - || event.getFrom().getBlockY() != event.getTo().getBlockY() - || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { - - LCPlayer player = LCPlayer.get(event.getPlayer()); - RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld()); - Vector pt = new Vector(event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ()); - ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld())); - - player.setRegionCreativeAllowed(set.allows(CREATIVE, player), event); - } - } - - private void register() { - plugin.getServer().getPluginManager().registerEvent(Event.Type.PLAYER_MOVE, this, Priority.Normal, plugin); - plugin.getServer().getPluginManager().registerEvent(Event.Type.PLAYER_TELEPORT, this, Priority.Normal, plugin); - } - } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java b/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java index cc4d285..facc28b 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java @@ -52,9 +52,9 @@ public class Items implements Storeable { } public static void sectionSetItem(ConfigurationSection section, String path, ItemStack item) { - if (!LimitedCreativeCore.serializeFallBack && !LimitedCreativeCore.plugin.config.getUnsafeStorage()) { + if (!LimitedCreativeCore.plugin.config.getUnsafeStorage()) { section.set(path, item); - } else { // compatibility fallback + } else { // unsafe enchants fallback Map serialize = item.serialize(); if (serialize.containsKey("type") && serialize.get("type") instanceof Material) serialize.put("type", serialize.get("type").toString()); @@ -68,8 +68,6 @@ public class Items implements Storeable { ConfigurationSection s = section.getConfigurationSection(path); Map serialize = s.getValues(false); serialize.remove("enchantments"); - if (s.contains("damage") && LimitedCreativeCore.serializeFallBack) - serialize.put("damage", new Integer(s.getInt("damage")).shortValue()); ItemStack result = ItemStack.deserialize(serialize); Map item = section.getConfigurationSection(path).getValues(false); item.remove("enchantments"); diff --git a/src/de/jaschastarke/minecraft/utils/Locale.java b/src/de/jaschastarke/minecraft/utils/Locale.java index 1780570..7ba678a 100644 --- a/src/de/jaschastarke/minecraft/utils/Locale.java +++ b/src/de/jaschastarke/minecraft/utils/Locale.java @@ -17,6 +17,7 @@ */ package de.jaschastarke.minecraft.utils; +import java.io.File; import java.text.MessageFormat; import org.bukkit.configuration.file.YamlConfiguration; @@ -27,9 +28,17 @@ public class Locale { private static Locale inst = null; public Locale(JavaPlugin plugin) { - lang = YamlConfiguration.loadConfiguration(plugin.getResource("lang/default.yml")); + String fn = getFilename("default"); + File localefile = new File(plugin.getDataFolder(), fn); + if (localefile.exists()) + lang = YamlConfiguration.loadConfiguration(localefile); + else + lang = YamlConfiguration.loadConfiguration(plugin.getResource(fn)); inst = this; } + private String getFilename(String locale) { + return "lang/"+locale+".yml"; + } public String get(String msg) { if (lang.contains(msg)) return lang.getString(msg); diff --git a/src/de/jaschastarke/minecraft/worldguard/CCommand.java b/src/de/jaschastarke/minecraft/worldguard/CCommand.java index f54e7fc..57b0a80 100644 --- a/src/de/jaschastarke/minecraft/worldguard/CCommand.java +++ b/src/de/jaschastarke/minecraft/worldguard/CCommand.java @@ -63,7 +63,7 @@ public class CCommand implements CommandExecutor { return false; } if (!sender.hasPermission("limitedcreative.regions")) { - sender.sendMessage(ChatColor.DARK_RED + "exception.command.lackingpermission"); + sender.sendMessage(ChatColor.DARK_RED + L("exception.command.lackingpermission")); return true; } Player player = sender instanceof Player ? (Player) sender : null; @@ -117,7 +117,7 @@ public class CCommand implements CommandExecutor { private void onInfo(CommandSender sender, Player player, CRegion region) { String[] args = new String[]{"info", region.getWorld().getName(), region.getProtectedRegion().getId()}; - wg.onCommand(sender, wg.getCommand("region"), "/region", args); + wg.onCommand(sender, wg.getCommand("region"), "region", args); StringBuilder list = new StringBuilder(); for (FlagValue data : region.getFlags()) {