Improved the varied item structure.

Map now shows islands within map range without needing player to travel there.
This commit is contained in:
Harrison Deng 2020-05-16 17:58:51 -05:00
parent 1207693a1d
commit 7d985f4489
4 changed files with 109 additions and 27 deletions

View File

@ -79,9 +79,23 @@ public interface VariedItem {
* @param hand The hand that it was in. * @param hand The hand that it was in.
* @param block The block the item was right clicked on. * @param block The block the item was right clicked on.
* @param blockFace The face of the block that was clicked. * @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); 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. * 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. * @param item The itemstack that represents the item in question.
*/ */
public void initializeInstanceOfItem(ItemStack item); public void initializeInstanceOfItem(ItemStack item);
/**
*
* @return A short description in regards to this variation of the item.
*/
public String getDescription();
} }

View File

@ -6,13 +6,17 @@ import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.Event.Result; import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action; 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.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
@ -40,7 +44,7 @@ public class VariedItemManager implements Listener {
Collection<? extends Player> players = islandSurvivalCraft.getServer().getOnlinePlayers(); Collection<? extends Player> players = islandSurvivalCraft.getServer().getOnlinePlayers();
for (Player player : players) { 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) @EventHandler(priority = EventPriority.MONITOR)
public void onHeld(PlayerItemHeldEvent e) { public void onHeld(PlayerItemHeldEvent e) {
ItemStack item = e.getPlayer().getInventory().getItem(e.getNewSlot()); ItemStack item = e.getPlayer().getInventory().getItem(e.getNewSlot());
VariedItem variedItem = getVariedItemFromItemStack(item); VariedItem variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) { 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) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent e) { 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(); RegisteredVariedItem[] registeredVariedItems = RegisteredVariedItem.values();
for (RegisteredVariedItem registeredVariedItem : registeredVariedItems) { for (RegisteredVariedItem registeredVariedItem : registeredVariedItems) {
VariedItem variedItem = registeredVariedItem.getVariedItem(); VariedItem variedItem = registeredVariedItem.getVariedItem();
@ -109,7 +158,7 @@ public class VariedItemManager implements Listener {
} }
private VariedItem getVariedItemFromItemStack(ItemStack item) { private VariedItem getVariedItemFromItemStack(ItemStack item) {
if (item == null) return null; if (item == null || item.getType() == Material.AIR) return null;
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
RegisteredVariedItem[] items = RegisteredVariedItem.values(); RegisteredVariedItem[] items = RegisteredVariedItem.values();
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {

View File

@ -50,8 +50,6 @@ public class IslandMapItem extends MapRenderer implements VariedItem {
namespacedKey = new NamespacedKey(islandSurvivalCraft, "island_map"); namespacedKey = new NamespacedKey(islandSurvivalCraft, "island_map");
ItemStack resultItem = new ItemStack(getBaseItems()[0], 1); ItemStack resultItem = new ItemStack(getBaseItems()[0], 1);
ItemMeta metadata = resultItem.getItemMeta(); ItemMeta metadata = resultItem.getItemMeta();
metadata.setDisplayName(getName());
metadata.setLocalizedName(getName());
metadata.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID()); metadata.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID());
metadata.setLore(lore); metadata.setLore(lore);
resultItem.setItemMeta(metadata); resultItem.setItemMeta(metadata);
@ -74,11 +72,9 @@ public class IslandMapItem extends MapRenderer implements VariedItem {
Color baseColor = MapPalette.getColor(baseColorVal); Color baseColor = MapPalette.getColor(baseColorVal);
Point2 pCoords = new Point2(actualX, actualZ); Point2 pCoords = new Point2(actualX, actualZ);
IslandInformation info = islandInformationManager.getIslandInformation(pCoords, false); IslandInformation info = islandInformationManager.getIslandInformation(pCoords, false);
if (baseColorVal != 0) { if (info != null) {
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));
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));
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; if (item.getType() != Material.MAP) return false;
ItemStack freshMap = new ItemStack(Material.FILLED_MAP, 1); ItemStack freshMap = new ItemStack(Material.FILLED_MAP, 1);
MapMeta mapMeta = (MapMeta) freshMap.getItemMeta(); MapMeta mapMeta = (MapMeta) freshMap.getItemMeta();
mapMeta.setDisplayName(getName());
mapMeta.setLore(lore); mapMeta.setLore(lore);
mapMeta.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID()); mapMeta.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID());
MapView mapView = Bukkit.createMap(clicker.getWorld()); MapView mapView = Bukkit.createMap(clicker.getWorld());
@ -164,4 +159,16 @@ public class IslandMapItem extends MapRenderer implements VariedItem {
item.setItemMeta(mapMeta); 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.";
}
} }

View File

@ -93,7 +93,7 @@ public class IslandInformationManager {
islandSets.remove(chunkCoords); islandSets.remove(chunkCoords);
} }
public HashSet<IslandInformation> getChunkIslandInformationSet(Point2 chunkCoords, boolean checkLoading) { private HashSet<IslandInformation> getChunkIslandInformationSet(Point2 chunkCoords, boolean checkLoading) {
if (!islandSets.containsKey(chunkCoords)) { if (!islandSets.containsKey(chunkCoords)) {
if (checkLoading && building.containsKey(chunkCoords)) { if (checkLoading && building.containsKey(chunkCoords)) {
try { try {
@ -105,6 +105,12 @@ public class IslandInformationManager {
return islandSets.get(chunkCoords); 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) { public IslandInformation getIslandInformation(Point2 coords, boolean checkLoading) {
HashSet<IslandInformation> chunkIslandSet = getChunkIslandInformationSet(GeneralUtilities.worldToChunkCoordinates(coords), checkLoading); HashSet<IslandInformation> chunkIslandSet = getChunkIslandInformationSet(GeneralUtilities.worldToChunkCoordinates(coords), checkLoading);
if (chunkIslandSet != null) { if (chunkIslandSet != null) {