From 7d985f4489f0ce0e1408ac1cbbe9dc86f8849120 Mon Sep 17 00:00:00 2001 From: Harrison Date: Sat, 16 May 2020 17:58:51 -0500 Subject: [PATCH] Improved the varied item structure. Map now shows islands within map range without needing player to travel there. --- .../interaction/items/VariedItem.java | 22 ++++- .../interaction/items/VariedItemManager.java | 83 +++++++++++++++---- .../items/variations/IslandMapItem.java | 23 +++-- .../Information/IslandInformationManager.java | 8 +- 4 files changed, 109 insertions(+), 27 deletions(-) diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItem.java b/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItem.java index 2b672a3..9afbd0b 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItem.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItem.java @@ -79,9 +79,23 @@ public interface VariedItem { * @param hand The hand that it was in. * @param block The block the item was right clicked on. * @param blockFace The face of the block that was clicked. - * @return + * @return True if the vanilla chain of events should no longer run. False otherwise. */ public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block, BlockFace blockFace); + + /** + * Called once when the player changes to holding the item. + * @param heldItem The itemstack representing the held item. + * @param holder The player holding the item. + */ + public void onItemHeld(ItemStack heldItem, HumanEntity holder); + + /** + * Called when this item is put away. + * @param item The itemstack that was put away. + * @param holder The player that put away the itemstack. + */ + public void onItemPutAway(ItemStack item, HumanEntity holder); /** * Called when a player joins and called when player picks up this variation of the item. @@ -90,4 +104,10 @@ public interface VariedItem { * @param item The itemstack that represents the item in question. */ public void initializeInstanceOfItem(ItemStack item); + + /** + * + * @return A short description in regards to this variation of the item. + */ + public String getDescription(); } \ No newline at end of file diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItemManager.java b/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItemManager.java index 416a64b..2023ae8 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItemManager.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/VariedItemManager.java @@ -6,13 +6,17 @@ import java.util.Map.Entry; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.Event.Result; import org.bukkit.event.block.Action; -import org.bukkit.event.inventory.CraftItemEvent; +import org.bukkit.event.entity.EntityPickupItemEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType.SlotType; +import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerJoinEvent; @@ -40,7 +44,7 @@ public class VariedItemManager implements Listener { Collection players = islandSurvivalCraft.getServer().getOnlinePlayers(); for (Player player : players) { - initializePlayerItems(player.getInventory()); + initializeInventoryVariedItems(player.getInventory()); } } @@ -51,23 +55,63 @@ public class VariedItemManager implements Listener { } } - @EventHandler(priority = EventPriority.HIGHEST) - public void onItemCrafted(CraftItemEvent e) { - ItemStack item = e.getCurrentItem(); - VariedItem variedItem = getVariedItemFromItemStack(item); - if (variedItem != null) { - if (variedItem.onItemCrafted(item, e.getWhoClicked())) { - e.setCancelled(true); - } - } - } - @EventHandler(priority = EventPriority.MONITOR) public void onHeld(PlayerItemHeldEvent e) { ItemStack item = e.getPlayer().getInventory().getItem(e.getNewSlot()); VariedItem variedItem = getVariedItemFromItemStack(item); if (variedItem != null) { - // variedItem.initializeInstanceOfItem(item); + variedItem.onItemHeld(item, e.getPlayer()); + } else { + item = e.getPlayer().getInventory().getItem(e.getPreviousSlot()); + variedItem = getVariedItemFromItemStack(item); + if (variedItem != null) { + variedItem.onItemPutAway(item, e.getPlayer()); + } + } + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onInventoryInteract(InventoryClickEvent e) { + ItemStack item = e.getCurrentItem(); + VariedItem variedItem = getVariedItemFromItemStack(item); + if (e.getSlotType() == SlotType.RESULT) { + if (variedItem != null) { + if (variedItem.onItemCrafted(item, e.getWhoClicked())) { + e.setCancelled(true); + } + } + } else if (e.getSlotType() == SlotType.QUICKBAR) { + if (e.getWhoClicked().getInventory().getHeldItemSlot() == e.getSlot()) { + if (variedItem != null) { + variedItem.onItemPutAway(item, e.getWhoClicked()); + } else if ((variedItem = getVariedItemFromItemStack(e.getCursor())) != null) { + variedItem.onItemHeld(item, e.getWhoClicked()); + } + } + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerDropItem(PlayerDropItemEvent e) { + ItemStack item = e.getItemDrop().getItemStack(); + VariedItem variedItem = getVariedItemFromItemStack(item); + if (variedItem != null) { + variedItem.onItemPutAway(item, e.getPlayer()); + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onEntityPickup(EntityPickupItemEvent e) { + ItemStack item = e.getItem().getItemStack(); + System.out.println("PICKUP"); + VariedItem variedItem = getVariedItemFromItemStack(item); + if (variedItem != null) { + if (e.getEntityType() == EntityType.PLAYER) { + Player player = (Player) e.getEntity(); + if (player.getInventory().getHeldItemSlot() == player.getInventory().firstEmpty()) { + variedItem.onItemHeld(item, player); + } + } } } @@ -86,10 +130,15 @@ public class VariedItemManager implements Listener { @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent e) { - initializePlayerItems(e.getPlayer().getInventory()); + initializeInventoryVariedItems(e.getPlayer().getInventory()); + ItemStack item = e.getPlayer().getInventory().getItemInMainHand(); + VariedItem variedItem = getVariedItemFromItemStack(item); + if (variedItem != null) { + variedItem.onItemHeld(item, e.getPlayer()); + } } - private void initializePlayerItems(Inventory inv) { + private void initializeInventoryVariedItems(Inventory inv) { RegisteredVariedItem[] registeredVariedItems = RegisteredVariedItem.values(); for (RegisteredVariedItem registeredVariedItem : registeredVariedItems) { VariedItem variedItem = registeredVariedItem.getVariedItem(); @@ -109,7 +158,7 @@ public class VariedItemManager implements Listener { } private VariedItem getVariedItemFromItemStack(ItemStack item) { - if (item == null) return null; + if (item == null || item.getType() == Material.AIR) return null; ItemMeta meta = item.getItemMeta(); RegisteredVariedItem[] items = RegisteredVariedItem.values(); for (int i = 0; i < items.length; i++) { diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/variations/IslandMapItem.java b/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/variations/IslandMapItem.java index 4aa7b9e..00bccd1 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/variations/IslandMapItem.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/interaction/items/variations/IslandMapItem.java @@ -50,8 +50,6 @@ public class IslandMapItem extends MapRenderer implements VariedItem { namespacedKey = new NamespacedKey(islandSurvivalCraft, "island_map"); ItemStack resultItem = new ItemStack(getBaseItems()[0], 1); ItemMeta metadata = resultItem.getItemMeta(); - metadata.setDisplayName(getName()); - metadata.setLocalizedName(getName()); metadata.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID()); metadata.setLore(lore); resultItem.setItemMeta(metadata); @@ -74,11 +72,9 @@ public class IslandMapItem extends MapRenderer implements VariedItem { Color baseColor = MapPalette.getColor(baseColorVal); Point2 pCoords = new Point2(actualX, actualZ); IslandInformation info = islandInformationManager.getIslandInformation(pCoords, false); - if (baseColorVal != 0) { - if (info != null && pCoords.distance(new Point2(player.getLocation().getBlockX(), player.getLocation().getBlockZ())) < 128) { - Color mixed = new Color((int) Math.min(255, baseColor.getRed() * 1.5f), (int) Math.min(255, baseColor.getGreen() * 0.7f), (int) Math.min(255, baseColor.getBlue() * 0.44f)); - canvas.setPixel(x, y, MapPalette.matchColor(mixed)); - } + if (info != null) { + Color mixed = new Color((int) Math.min(255, baseColor.getRed() * 1.5f), (int) Math.min(255, baseColor.getGreen() * 0.7f), (int) Math.min(255, baseColor.getBlue() * 0.44f)); + canvas.setPixel(x, y, MapPalette.matchColor(mixed)); } } } @@ -134,7 +130,6 @@ public class IslandMapItem extends MapRenderer implements VariedItem { if (item.getType() != Material.MAP) return false; ItemStack freshMap = new ItemStack(Material.FILLED_MAP, 1); MapMeta mapMeta = (MapMeta) freshMap.getItemMeta(); - mapMeta.setDisplayName(getName()); mapMeta.setLore(lore); mapMeta.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID()); MapView mapView = Bukkit.createMap(clicker.getWorld()); @@ -164,4 +159,16 @@ public class IslandMapItem extends MapRenderer implements VariedItem { item.setItemMeta(mapMeta); } + @Override + public void onItemHeld(ItemStack heldItem, HumanEntity holder) { + } + + @Override + public void onItemPutAway(ItemStack item, HumanEntity holder) { + } + + @Override + public String getDescription() { + return "A map that has island hunting properties. Will highlight islands in relation on map."; + } } \ No newline at end of file diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/world/Information/IslandInformationManager.java b/src/main/java/ca/recrown/islandsurvivalcraft/world/Information/IslandInformationManager.java index f94d759..10eca7f 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/world/Information/IslandInformationManager.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/world/Information/IslandInformationManager.java @@ -93,7 +93,7 @@ public class IslandInformationManager { islandSets.remove(chunkCoords); } - public HashSet getChunkIslandInformationSet(Point2 chunkCoords, boolean checkLoading) { + private HashSet getChunkIslandInformationSet(Point2 chunkCoords, boolean checkLoading) { if (!islandSets.containsKey(chunkCoords)) { if (checkLoading && building.containsKey(chunkCoords)) { try { @@ -105,6 +105,12 @@ public class IslandInformationManager { return islandSets.get(chunkCoords); } + /** + * Gets the island information for the given coordinates. + * @param coords The coordinates to check for island information. + * @param checkLoading Whether or not to check for the currently loading chunks. + * @return The island information or null if there is no island at those coordinates. + */ public IslandInformation getIslandInformation(Point2 coords, boolean checkLoading) { HashSet chunkIslandSet = getChunkIslandInformationSet(GeneralUtilities.worldToChunkCoordinates(coords), checkLoading); if (chunkIslandSet != null) {