diff --git a/eu/railduction/ruakij/spawnerDrops/BlockSource.java b/eu/railduction/ruakij/spawnerDrops/BlockSource.java new file mode 100644 index 0000000..83adb9e --- /dev/null +++ b/eu/railduction/ruakij/spawnerDrops/BlockSource.java @@ -0,0 +1,7 @@ +package eu.railduction.ruakij.spawnerDrops; + +public enum BlockSource { + UNKNOWN, + PLAYER, + ADMIN +} diff --git a/eu/railduction/ruakij/spawnerDrops/Main.java b/eu/railduction/ruakij/spawnerDrops/Main.java index b33da29..9391385 100644 --- a/eu/railduction/ruakij/spawnerDrops/Main.java +++ b/eu/railduction/ruakij/spawnerDrops/Main.java @@ -1,5 +1,6 @@ package eu.railduction.ruakij.spawnerDrops; +import eu.railduction.ruakij.spawnerDrops.listener.OnBlockBreak; import eu.railduction.ruakij.spawnerDrops.listener.OnBlockPlace; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -19,6 +20,7 @@ public class Main extends JavaPlugin { PluginManager pluginManager = Bukkit.getPluginManager(); pluginManager.registerEvents(new OnBlockPlace(), this); + pluginManager.registerEvents(new OnBlockBreak(), this); loadConfigs(); diff --git a/eu/railduction/ruakij/spawnerDrops/listener/OnBlockBreak.java b/eu/railduction/ruakij/spawnerDrops/listener/OnBlockBreak.java new file mode 100644 index 0000000..e780c2f --- /dev/null +++ b/eu/railduction/ruakij/spawnerDrops/listener/OnBlockBreak.java @@ -0,0 +1,66 @@ +package eu.railduction.ruakij.spawnerDrops.listener; + +import eu.railduction.ruakij.spawnerDrops.BlockSource; +import eu.railduction.ruakij.spawnerDrops.Main; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; + +public class OnBlockBreak implements Listener { + + @EventHandler + public void onBlockBreakEvent(BlockBreakEvent e){ + + Player p = e.getPlayer(); + + if(e.getBlock().getType() == Material.MOB_SPAWNER){ + // Allow creative + if(p.getGameMode() == GameMode.CREATIVE) + return; + + // Get spawner-source (unknown, player, admin) + Location loc = e.getBlock().getLocation(); + BlockSource bSource; + Boolean placedByAdmin = Main.data.getBoolean(Main.serialiseLocation(loc)); + if(placedByAdmin == null) + bSource = BlockSource.UNKNOWN; + else + if(!placedByAdmin) + bSource = BlockSource.PLAYER; + else + bSource = BlockSource.ADMIN; + + // # Break-Prerequisite + ConfigurationSection breakPreConfig = Main.config.getConfigurationSection("break.break-prerequisite."+ bSource); + // Silktouch + if(breakPreConfig.getBoolean("silktouch")){ + if(!p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) + e.setCancelled(true); + } + // Send msg + String msg = breakPreConfig.getString("msg."+ (e.isCancelled()?"fail":"success")); + if(msg != null && !msg.equals("")) p.sendMessage(msg); + + + if(e.isCancelled()) return; + + // # Drop-Prerequisite + ConfigurationSection dropPreConfig = Main.config.getConfigurationSection("break.drop-prerequisite."+ bSource); + // Silktouch + if(breakPreConfig.getBoolean("silktouch")){ + if(!p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) + e.setDropItems(false); + } + // Send msg + msg = dropPreConfig.getString("msg."+ (e.isDropItems()?"success":"fail")); + if(msg != null && !msg.equals("")) p.sendMessage(msg); + } + } +}