From 6ef8a521c0bbd7d2f8da8f3a44c00991472b3c8f Mon Sep 17 00:00:00 2001 From: Lightling Date: Thu, 19 Sep 2019 20:55:50 -0400 Subject: [PATCH] improved upon chunk loader code - Changed LoaderPlace to use getItemInHand().getLore() - Ensured LoaderDestroy drops a loader - Made a static method similar to the internal Setup() method that returns the loader for LoaderDestroy to use - Switched around crafting recipe - Made the ForceChunkActive/Inactive methods return a boolean to make sure the LoaderDestroy only drops the item if the chunk is a loader and that LoaderPlace drops the item back to the player if the chunk was already a loader --- .../gibsoniacraft/GibsoniaCraft.java | 15 +++++++++-- .../gibsoniacraft/crafting/ChunkLoader.java | 27 ++++++++++++++++--- .../gibsoniacraft/listener/BlockListener.java | 27 ++++++++++++++++--- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java b/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java index fb44293..8a487bd 100644 --- a/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java +++ b/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java @@ -13,6 +13,7 @@ import lightling.gibsoniacraft.crafting.Hammer; import lightling.gibsoniacraft.crafting.LumberAxe; import lightling.gibsoniacraft.listener.BlockListener; import lightling.gibsoniacraft.listener.PlayerInteractListener; +import lightling.gibsoniacraft.crafting.ChunkLoader; @SuppressWarnings("unused") public final class GibsoniaCraft extends JavaPlugin { @@ -20,6 +21,7 @@ public final class GibsoniaCraft extends JavaPlugin { private Excavator excavatorClass; private Hammer hammerClass; private LumberAxe lumberAxeClass; + private ChunkLoader chunkLoaderClass; private PlayerInteractListener pListener; private BlockListener bListener; private ArrayList loadedChunks; @@ -29,6 +31,7 @@ public final class GibsoniaCraft extends JavaPlugin { excavatorClass = new Excavator(this); hammerClass = new Hammer(this); lumberAxeClass = new LumberAxe(this); + chunkLoaderClass = new ChunkLoader(this); pListener = new PlayerInteractListener(this); bListener = new BlockListener(this); @@ -56,26 +59,34 @@ public final class GibsoniaCraft extends JavaPlugin { /** * Adds a chunk to the list of force-loaded chunks * @param chunk The chunk to set as force-loaded + * @return Whether a chunk was successfully added */ - public void ForceChunkActive(final Chunk chunk) + public boolean ForceChunkActive(final Chunk chunk) { if (!this.loadedChunks.contains(chunk)) { chunk.setForceLoaded(true); this.loadedChunks.add(chunk); + return true; } + + return false; } /** * Removes a chunk from the list of force-loaded chunks * @param chunk The chunk to remove as force-loaded + * @return Whether a chunk was successfully removed */ - public void ForceChunkInactive(final Chunk chunk) + public boolean ForceChunkInactive(final Chunk chunk) { if (this.loadedChunks.contains(chunk)) { chunk.setForceLoaded(false); this.loadedChunks.remove(chunk); + return true; } + + return false; } } diff --git a/plugin/src/lightling/gibsoniacraft/crafting/ChunkLoader.java b/plugin/src/lightling/gibsoniacraft/crafting/ChunkLoader.java index 188d1ce..f62e037 100644 --- a/plugin/src/lightling/gibsoniacraft/crafting/ChunkLoader.java +++ b/plugin/src/lightling/gibsoniacraft/crafting/ChunkLoader.java @@ -24,7 +24,7 @@ public class ChunkLoader { /** * Initializes a chunk loader - * @param plugin + * @param plugin GibsoniaCraft plugin */ public ChunkLoader(final JavaPlugin plugin) { @@ -33,7 +33,7 @@ public class ChunkLoader { /** * Defines the chunk loader block - * @param plugin + * @param plugin GibsoniaCraft plugin */ public void Setup(JavaPlugin plugin) { @@ -51,7 +51,7 @@ public class ChunkLoader { // Handle recipe chunkRecipe = new ShapedRecipe(chunkKey, chunkItem); - chunkRecipe.shape(new String[] { "ooo", "eme", "epe" }); + chunkRecipe.shape(new String[] { "epe", "eme", "ooo" }); chunkRecipe.setIngredient('o', Material.OBSIDIAN); chunkRecipe.setIngredient('e', Material.EMERALD); chunkRecipe.setIngredient('m', Material.MAGMA_BLOCK); @@ -59,4 +59,25 @@ public class ChunkLoader { Server server = plugin.getServer(); server.addRecipe(chunkRecipe); } + + /** + * Defines the chunk loader drop + * @return the chunk loader drop + */ + public static ItemStack Setup() + { + // Instantiate item + ItemStack item = new ItemStack(Material.BEACON); + + // Handle meta + ItemMeta meta = item.getItemMeta(); + String loreString = "Keeps chunks loaded"; + ArrayList lore = new ArrayList() {{ add(loreString); }}; + meta.setLore(lore); + meta.setDisplayName("Chunk Loader"); + item.setItemMeta(meta); + + // Return the appropriately edited item + return item; + } } diff --git a/plugin/src/lightling/gibsoniacraft/listener/BlockListener.java b/plugin/src/lightling/gibsoniacraft/listener/BlockListener.java index 9b2f0f7..a3167de 100644 --- a/plugin/src/lightling/gibsoniacraft/listener/BlockListener.java +++ b/plugin/src/lightling/gibsoniacraft/listener/BlockListener.java @@ -10,6 +10,7 @@ import org.bukkit.Location; // Collections import java.util.ArrayList; +import java.util.List; import java.util.Map; // Needed for handling events @@ -20,6 +21,7 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import lightling.gibsoniacraft.GibsoniaCraft; +import lightling.gibsoniacraft.crafting.ChunkLoader; import lightling.gibsoniacraft.lib.BlockRef; import lightling.gibsoniacraft.util.ToolUtil; @@ -314,10 +316,19 @@ public class BlockListener implements Listener @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void LoaderPlace(BlockPlaceEvent bpEvent) { - if (bpEvent.getBlock().hasMetadata("Keeps chunks loaded")) + List lore = bpEvent.getItemInHand().getLore(); + if (lore != null && lore.contains("Keeps chunks loaded")) { Chunk chunk = bpEvent.getBlock().getLocation().getChunk(); - this.gcPlugin.ForceChunkActive(chunk); + + // If the chunk was already a loader, drop the loader again and set the block as air + if (!this.gcPlugin.ForceChunkActive(chunk)) + { + Block b = bpEvent.getBlock(); + b.getWorld().dropItemNaturally(b.getLocation(), ChunkLoader.Setup()); + b.setType(Material.AIR); + bpEvent.setCancelled(true); + } } } @@ -328,10 +339,18 @@ public class BlockListener implements Listener @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void LoaderDestroy(BlockBreakEvent bbEvent) { - if (bbEvent.getBlock().hasMetadata("Keeps chunks loaded")) + if (bbEvent.getBlock().getType() == Material.BEACON) { Chunk chunk = bbEvent.getBlock().getLocation().getChunk(); - this.gcPlugin.ForceChunkInactive(chunk); + + // As long as the beacon was acting as a loader, drop the loader + if (gcPlugin.ForceChunkInactive(chunk)) + { + Block block = bbEvent.getBlock(); + block.setType(Material.AIR); + ItemStack drop = ChunkLoader.Setup(); + block.getWorld().dropItemNaturally(block.getLocation(), drop); + } } } }