From dbdcd10a402e186eb4f96dfa0575e3edfa759634 Mon Sep 17 00:00:00 2001 From: Alexander B Date: Tue, 5 Jan 2021 20:36:08 +0100 Subject: [PATCH] "Various" fixes --- .../spawnerDrops/listener/OnBlockBreak.java | 92 +++++++++++++------ .../spawnerDrops/listener/OnBlockPlace.java | 33 ++++--- 2 files changed, 82 insertions(+), 43 deletions(-) diff --git a/eu/railduction/ruakij/spawnerDrops/listener/OnBlockBreak.java b/eu/railduction/ruakij/spawnerDrops/listener/OnBlockBreak.java index e780c2f..0a368f2 100644 --- a/eu/railduction/ruakij/spawnerDrops/listener/OnBlockBreak.java +++ b/eu/railduction/ruakij/spawnerDrops/listener/OnBlockBreak.java @@ -12,6 +12,9 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.inventory.ItemStack; + +import java.io.IOException; public class OnBlockBreak implements Listener { @@ -21,46 +24,77 @@ public class OnBlockBreak implements Listener { Player p = e.getPlayer(); if(e.getBlock().getType() == Material.MOB_SPAWNER){ - // Allow creative - if(p.getGameMode() == GameMode.CREATIVE) - return; + Location loc = e.getBlock().getLocation(); + String locSer = Main.serialiseLocation(loc); // Get spawner-source (unknown, player, admin) - Location loc = e.getBlock().getLocation(); BlockSource bSource; - Boolean placedByAdmin = Main.data.getBoolean(Main.serialiseLocation(loc)); + Object placedByAdmin = Main.data.get(locSer); 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); + if(!(boolean)placedByAdmin) + bSource = BlockSource.PLAYER; + else + bSource = BlockSource.ADMIN; + + // Ignore creative + if(p.getGameMode() != GameMode.CREATIVE){ + + // # Break-Prerequisite + ConfigurationSection breakPreConfig = Main.config.getConfigurationSection("break.break-prerequisite."+ bSource); + boolean success = false; + // Silktouch + if(breakPreConfig.getBoolean("silktouch")){ + if(p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) + success = true; + else + e.setCancelled(true); + } + // Send msg + String msg = breakPreConfig.getString("msg."+ (success?"success":"fail")); + if(msg != null && !msg.equals("")) p.sendMessage(msg); + + // Stop if breaking wasnt successful + if(!success) return; } - // Send msg - String msg = breakPreConfig.getString("msg."+ (e.isCancelled()?"fail":"success")); - if(msg != null && !msg.equals("")) p.sendMessage(msg); - if(e.isCancelled()) return; + // Remove block from data + if(bSource != BlockSource.UNKNOWN){ + Main.data.set(locSer, null); + try { + Main.data.save("data.yml"); + } catch (IOException ex) { + Main.plugin.getLogger().severe("Cannot save data to data.yml! Data will lost after restart!"); + ex.printStackTrace(); + } + } + + // Ignore creative + if(p.getGameMode() != GameMode.CREATIVE){ + + // # Drop-Prerequisite + ConfigurationSection dropPreConfig = Main.config.getConfigurationSection("break.drop-prerequisite."+ bSource); + boolean success = false; + // Silktouch + if(dropPreConfig.getBoolean("silktouch")){ + if(p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) + success = true; + else + e.setCancelled(true); + } + + // Set drop + if(success){ + ItemStack item = new ItemStack(Material.MOB_SPAWNER); + loc.getWorld().dropItem(loc, item); + } - // # 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 + String msg = dropPreConfig.getString("msg."+ (success?"success":"fail")); + if(msg != null && !msg.equals("")) p.sendMessage(msg); } - // Send msg - msg = dropPreConfig.getString("msg."+ (e.isDropItems()?"success":"fail")); - if(msg != null && !msg.equals("")) p.sendMessage(msg); } } } diff --git a/eu/railduction/ruakij/spawnerDrops/listener/OnBlockPlace.java b/eu/railduction/ruakij/spawnerDrops/listener/OnBlockPlace.java index 94c3e1f..d3c18c7 100644 --- a/eu/railduction/ruakij/spawnerDrops/listener/OnBlockPlace.java +++ b/eu/railduction/ruakij/spawnerDrops/listener/OnBlockPlace.java @@ -4,6 +4,7 @@ import eu.railduction.ruakij.spawnerDrops.Main; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; @@ -11,6 +12,7 @@ import java.io.IOException; public class OnBlockPlace implements Listener { + @EventHandler public void onBlockPlaceEvent(BlockPlaceEvent e){ Player p = e.getPlayer(); @@ -20,29 +22,32 @@ public class OnBlockPlace implements Listener { // Can break? boolean isAdmin = p.hasPermission("spawnerDrops.admin"); - if(!isAdmin && !Main.config.getBoolean("place.player.canPlace")){ + if(!isAdmin && !Main.config.getBoolean("place.PLAYER.canPlace")){ // Cancel place-event e.setCancelled(true); // Send msg - String msg = Main.config.getString("place.player.msg.fail"); + String msg = Main.config.getString("place.PLAYER.msg.fail"); if(msg != null && !msg.equals("")) p.sendMessage(msg); + + return; } - else{ - // Write place-data - Main.data.set(Main.serialiseLocation(loc), isAdmin); - try { - Main.data.save("data.yml"); - } catch (IOException ex) { - Main.plugin.getLogger().severe("Cannot save data to data.yml! Data will lost after restart!"); - ex.printStackTrace(); - } - // Send msg - String msg = Main.config.getString("place.player.msg.success"); - if(msg != null && !msg.equals("")) p.sendMessage(msg); + // Write place-data + Main.plugin.getLogger().info(Main.serialiseLocation(loc) +" - "+ isAdmin); + Main.data.set(Main.serialiseLocation(loc), isAdmin); + + try { + Main.data.save("data.yml"); + } catch (IOException ex) { + Main.plugin.getLogger().severe("Cannot save data to data.yml! Data will lost after restart!"); + ex.printStackTrace(); } + + // Send msg + String msg = Main.config.getString("place."+ (isAdmin?"ADMIN":"PLAYER") +".msg.success"); + if(msg != null && !msg.equals("")) p.sendMessage(msg); } } }