From 8cba7af0e0ad7fa99082ada585019c8ce1b827fd Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Fri, 9 Nov 2012 21:40:33 +0100 Subject: [PATCH] bad rework begin --- config.yml | 3 +- plugin.yml | 7 +- pom.xml | 10 +- src/de/jaschastarke/minecraft/lib/Core.java | 31 +++ src/de/jaschastarke/minecraft/lib/Module.java | 10 + .../minecraft/limitedcreative/Core.java | 13 +- .../prot/InventorySeparation.java | 17 ++ .../limitedcreative/prot/LimitListener.java | 225 ++++++++++++++++++ .../limitedcreative/sepinv/MainListener.java | 87 +++++++ .../utils/NoBlockItemSpawn.java | 96 ++++++++ .../minecraft/utils/Configuration.java | 5 + 11 files changed, 494 insertions(+), 10 deletions(-) create mode 100644 src/de/jaschastarke/minecraft/lib/Core.java create mode 100644 src/de/jaschastarke/minecraft/lib/Module.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/prot/InventorySeparation.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/prot/LimitListener.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/sepinv/MainListener.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/utils/NoBlockItemSpawn.java create mode 100644 src/de/jaschastarke/minecraft/utils/Configuration.java diff --git a/config.yml b/config.yml index 2f501d5..1556531 100644 --- a/config.yml +++ b/config.yml @@ -101,7 +101,8 @@ limit: # UseBlackList # Prevents using or placing of the given blocks in creative mode (and only in creative). - # You can use the technical name (see http://jd.bukkit.org/doxygen/d7/dd9/namespaceorg_1_1bukkit.html#ab7fa290bb19b9a830362aa88028ec80a) + # You can use the technical name (see http://jd.bukkit.org/doxygen/d6/d0e/enumorg_1_1bukkit_1_1Material.html + # or https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/Material.java) # or the id of the block/item (better use the id, if you're not sure). # To prevent Lava you need to add "LAVA_BUCKET", because lava-blocks aren't "placed", therefore Lava-Buckets are # "used". diff --git a/plugin.yml b/plugin.yml index 909bb25..e56c1bd 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,7 +1,7 @@ name: LimitedCreative main: de.jaschastarke.minecraft.limitedcreative.Core -version: 1.3.0c -softdepend: [WorldGuard, WorldEdit, MultiInv] +version: 1.4.0a +softdepend: [WorldGuard, WorldEdit, MultiInv, Vault] dev-url: http://dev.bukkit.org/server-mods/limited-creative/ commands: limitedcreative: @@ -72,4 +72,7 @@ permissions: default: false limitedcreative.nolimit.break: description: Allows bypassing the "block break"-limitation + default: false + limitedcreative.permissions.keep: + description: Allows bypassing the permission based limitations settet temporary via Vault (TESTING) default: false \ No newline at end of file diff --git a/pom.xml b/pom.xml index 846ea2b..a1c06d0 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ de.jaschastarke LimitedCreative LimitedCreative - 1.3.0c + 1.4.0a https://github.com/possi/LimitedCreative scm:git:git://github.com/possi/LimitedCreative.git @@ -31,6 +31,10 @@ onarandombox http://repo.onarandombox.com/content/groups/public + + vault-repo + http://ci.herocraftonline.com/plugin/repository/everything + ${basedir}/src @@ -68,12 +72,12 @@ org.bukkit bukkit - 1.3.1-R1.0 + 1.4.2-R0.2 com.sk89q worldguard - 5.5.4-SNAPSHOT + 5.6.3 uk.org.whoami diff --git a/src/de/jaschastarke/minecraft/lib/Core.java b/src/de/jaschastarke/minecraft/lib/Core.java new file mode 100644 index 0000000..111a97c --- /dev/null +++ b/src/de/jaschastarke/minecraft/lib/Core.java @@ -0,0 +1,31 @@ +package de.jaschastarke.minecraft.lib; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; +import org.bukkit.plugin.java.JavaPlugin; + +public class Core extends JavaPlugin { + public final Logger logger = Logger.getLogger("Minecraft"); + + private List modules = new ArrayList(); + + @SuppressWarnings("unchecked") + public T getModule(Class modclass) { + for (Module module : modules) { + if (modclass.isInstance(module)) { + return (T) module; + } + } + return null; + } + public Module getModule(String modid) { + for (Module module : modules) { + if (module.getIdentifier().equals(modid)) { + return module; + } + } + return null; + } + +} diff --git a/src/de/jaschastarke/minecraft/lib/Module.java b/src/de/jaschastarke/minecraft/lib/Module.java new file mode 100644 index 0000000..e8434f4 --- /dev/null +++ b/src/de/jaschastarke/minecraft/lib/Module.java @@ -0,0 +1,10 @@ +package de.jaschastarke.minecraft.lib; + +abstract public class Module { + abstract public String getIdentifier(); + abstract public void init(); + + public void unload() { + + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Core.java b/src/de/jaschastarke/minecraft/limitedcreative/Core.java index 9dcc317..7668584 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/Core.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/Core.java @@ -25,20 +25,25 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; import de.jaschastarke.minecraft.integration.Communicator; -import de.jaschastarke.minecraft.limitedcreative.listeners.LimitListener; -import de.jaschastarke.minecraft.limitedcreative.listeners.MainListener; +import de.jaschastarke.minecraft.limitedcreative.prot.LimitListener; import de.jaschastarke.minecraft.limitedcreative.regions.WorldGuardIntegration; +import de.jaschastarke.minecraft.limitedcreative.sepinv.MainListener; +import de.jaschastarke.minecraft.limitedcreative.utils.NoBlockItemSpawn; import de.jaschastarke.minecraft.utils.Locale; import de.jaschastarke.minecraft.utils.Permissions; public class Core extends JavaPlugin { + public static Core plugin; public final Logger logger = Logger.getLogger("Minecraft"); - public Configuration config; + + public Permissions perm; public WorldGuardIntegration worldguard; public Communicator com; - public static Core plugin; + + /* Utils */ + public Configuration config; public NoBlockItemSpawn spawnblock; @Override diff --git a/src/de/jaschastarke/minecraft/limitedcreative/prot/InventorySeparation.java b/src/de/jaschastarke/minecraft/limitedcreative/prot/InventorySeparation.java new file mode 100644 index 0000000..2aef0f5 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/prot/InventorySeparation.java @@ -0,0 +1,17 @@ +package de.jaschastarke.minecraft.limitedcreative.prot; + +import de.jaschastarke.minecraft.lib.Module; + +public class InventorySeparation extends Module { + @Override + public String getIdentifier() { + return "inventory_separation"; + } + + @Override + public void init() { + // TODO Auto-generated method stub + + } + +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/prot/LimitListener.java b/src/de/jaschastarke/minecraft/limitedcreative/prot/LimitListener.java new file mode 100644 index 0000000..5ba6d99 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/prot/LimitListener.java @@ -0,0 +1,225 @@ +/* + * 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.prot; + +import static de.jaschastarke.minecraft.utils.Locale.L; + +import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.Sign; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.entity.StorageMinecart; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +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.entity.EntityTargetEvent; +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.inventory.InventoryHolder; +import org.bukkit.material.Button; +import org.bukkit.material.Lever; + +import de.jaschastarke.minecraft.limitedcreative.BlackList; +import de.jaschastarke.minecraft.limitedcreative.Core; +import de.jaschastarke.minecraft.limitedcreative.LCPlayer; +import de.jaschastarke.minecraft.limitedcreative.Perms; +import de.jaschastarke.minecraft.limitedcreative.Players; +import de.jaschastarke.minecraft.limitedcreative.sepinv.MainListener; + +public class LimitListener implements Listener { + private Core plugin; + public LimitListener(Core plugin) { + this.plugin = plugin; + } + + + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) { + Players.get(event.getPlayer()).onDropItem(event); + } + + @EventHandler + public void onPlayerPickupItem(PlayerPickupItemEvent event) { + Players.get(event.getPlayer()).onPickupItem(event); + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onPlayerInteract(PlayerInteractEvent event) { + if (MainListener.isCancelled(event) || event.getPlayer().getGameMode() != GameMode.CREATIVE) + return; + + LCPlayer player = Players.get(event.getPlayer()); + if (!plugin.config.getPermissionsEnabled() || !player.hasPermission(Perms.NoLimit.USE)) { + if (event.getItem() != null && BlackList.isBlackListed(plugin.config.getBlockedUse(), event.getItem())) { + 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 InventoryHolder || block.getType() == Material.ENDER_CHEST) { // Workaround, Bukkit not recognize a Enderchest + player.onChestAccess(event); + } else if (block.getState() instanceof Sign) { + player.onSignAccess(event); + } else if (block.getState() instanceof Lever || block.getState() instanceof Button) { + player.onButtonAccess(event); + } else if (block.getType() == Material.WORKBENCH) { + player.onBenchAccess(event); + } + } + + @EventHandler(priority=EventPriority.LOWEST) + public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { + if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.CREATIVE) + return; + + LCPlayer player = Players.get(event.getPlayer()); + if (!plugin.config.getPermissionsEnabled() || !player.hasPermission(Perms.NoLimit.USE)) { + if (event.getPlayer().getItemInHand() != null && BlackList.isBlackListed(plugin.config.getBlockedUse(), event.getPlayer().getItemInHand())) { + event.setCancelled(true); + event.getPlayer().sendMessage(L("blocked.use")); + return; + } + } + + Entity entity = event.getRightClicked(); + + if (entity instanceof StorageMinecart) { + player.onChestAccess(event); + } + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent event) { + if (event instanceof EntityDamageByEntityEvent) + onEntityDamageByEntity((EntityDamageByEntityEvent) event); + } + + /* + * Registering to that event works, but causes a SEVERE: + * Plugin attempted to register delegated event class class org.bukkit.event.entity.EntityDamageByEntityEvent. + * It should be using class org.bukkit.event.entity.EntityDamageEvent! + */ + protected void onEntityDamageByEntity(EntityDamageByEntityEvent event) { + if (event.isCancelled()) + return; + Entity source = event.getDamager(); + if (source instanceof Projectile) + source = ((Projectile) source).getShooter(); + + if (event.getEntity() instanceof Player) { + Players.get((Player) event.getEntity()).onDamage(source, event); + } + if (!event.isCancelled() && source instanceof Player) { + Players.get((Player) source).onDealDamage(event); + } + } + @EventHandler + public void onEntityTarget(EntityTargetEvent event) { + if (event.isCancelled()) + return; + if (event.getTarget() instanceof Player) { + Players.get((Player) event.getTarget()).onTarget(event); + } + } + + @EventHandler(priority=EventPriority.LOW) + public void onEntityDeath(EntityDeathEvent event) { + if (event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + Players.get(player).onDie(event); + } + } + /*@EventHandler + public void onPlayerRespawn(PlayerRespawnEvent event) { + Players.get(event.getPlayer()).onRespawn(event); + }*/ + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + if (event.isCancelled()) + return; + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) { + LCPlayer player = Players.get(event.getPlayer()); + if (!plugin.config.getPermissionsEnabled() || !player.hasPermission(Perms.NoLimit.BREAK)) { + if (BlackList.isBlackListed(plugin.config.getBlockedBreaks(), event.getBlock())) { + event.setCancelled(true); + event.getPlayer().sendMessage(L("blocked.break")); + } + } + + if (plugin.config.getPermissionsEnabled() && player.hasPermission(Perms.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) { + LCPlayer player = Players.get(event.getPlayer()); + if (!plugin.config.getPermissionsEnabled() || !player.hasPermission(Perms.NoLimit.USE)) { + if (BlackList.isBlackListed(plugin.config.getBlockedUse(), event.getBlock())) { + event.setCancelled(true); + event.getPlayer().sendMessage(L("blocked.place")); + } + } + } + } +} \ No newline at end of file diff --git a/src/de/jaschastarke/minecraft/limitedcreative/sepinv/MainListener.java b/src/de/jaschastarke/minecraft/limitedcreative/sepinv/MainListener.java new file mode 100644 index 0000000..a2fc21c --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/sepinv/MainListener.java @@ -0,0 +1,87 @@ +/* + * 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.sepinv; + +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 org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerRespawnEvent; + +import de.jaschastarke.minecraft.limitedcreative.Core; +import de.jaschastarke.minecraft.limitedcreative.Players; + +public class MainListener implements Listener { + private Core plugin; + public MainListener(Core 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 (Core.isDebug()) { + Core.debug("onPlayerGameModeChange: "+event.getPlayer().getName()); + Core.debug("Current GameMode: "+event.getPlayer().getGameMode()); + Core.debug("New GameMode: "+event.getNewGameMode()); + Core.debug("isLoggedin: "+plugin.com.isLoggedIn(event.getPlayer())); + Core.debug("isCancelled: "+event.isCancelled()); + } + if (!plugin.com.isLoggedIn(event.getPlayer())) + return; + + if (!Players.get(event.getPlayer()).onSetGameMode(event.getNewGameMode())) + event.setCancelled(true); + } + @EventHandler + public void onPlayerRespawn(PlayerRespawnEvent event) { + Players.get(event.getPlayer()).onRevive(); + } + + /** + * Also needed if WorldGuard-Feature is enabled, so can not moved to optional Listener "Limit". + */ + @EventHandler + public void onItemSpawn(ItemSpawnEvent event) { + if (event.isCancelled()) + return; + if (event.getEntity() instanceof Item) { + if (plugin.spawnblock.isBlocked(event.getLocation().getBlock().getLocation(), ((Item) event.getEntity()).getItemStack().getType())) { + event.setCancelled(true); + } + } + } + + public void onLogout(PlayerQuitEvent event) { + // what? i can't cancel a logout event? but how to chain the user to the server than? xD + Players.remove(event.getPlayer().getName()); + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/utils/NoBlockItemSpawn.java b/src/de/jaschastarke/minecraft/limitedcreative/utils/NoBlockItemSpawn.java new file mode 100644 index 0000000..76abcb6 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/utils/NoBlockItemSpawn.java @@ -0,0 +1,96 @@ +/* + * 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.utils; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; + +import de.jaschastarke.minecraft.limitedcreative.LCPlayer; + + +/** + * The "Block" means a Minecraft-Block, not "blocking". So this Class is used to prevent ItemSpawn which are Drops from + * specified Blocks. + */ +public class NoBlockItemSpawn { + public final static long TIME_OFFSET = 250; + + private List list = new ArrayList(); + + public boolean isBlocked(Location l, Material type) { + cleanup(); + for (BlockItemDrop block : list) { + if (block.getLocation().equals(l) && block.getType().equals(type)) + return true; + } + return false; + } + private void cleanup() { + Iterator i = list.iterator(); + while (i.hasNext()) { + BlockItemDrop block = i.next(); + if (block.getTimestamp() < System.currentTimeMillis() - TIME_OFFSET) + i.remove(); + } + } + + private class BlockItemDrop { + public BlockItemDrop(Location l, Material type) { + this.l = l; + this.type = type; + this.timestamp = System.currentTimeMillis(); + } + private Location l; + private Material type; + private long timestamp; + public Location getLocation() { + return l; + } + public Material getType() { + return type; + } + public long getTimestamp() { + return timestamp; + } + } + + public void block(Block block, LCPlayer player) { + if (player.getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { + block(block.getLocation(), block.getType()); + } else { + // doesn't include silktouch + for (ItemStack i : block.getDrops(player.getPlayer().getItemInHand())) { + block(block.getLocation(), i.getType()); + } + } + } + + public void block(Block block) { + block(block, null); + } + public void block(Location l, Material type) { + list.add(new BlockItemDrop(l, type)); + } +} diff --git a/src/de/jaschastarke/minecraft/utils/Configuration.java b/src/de/jaschastarke/minecraft/utils/Configuration.java new file mode 100644 index 0000000..4b39f19 --- /dev/null +++ b/src/de/jaschastarke/minecraft/utils/Configuration.java @@ -0,0 +1,5 @@ +package de.jaschastarke.minecraft.utils; + +public class Configuration { + +}