Map is now toggle instead of crafting.

This commit is contained in:
Harrison Deng 2020-09-09 13:57:08 -05:00
parent bde35f8de8
commit 94dbda6ac6
13 changed files with 139 additions and 181 deletions

View File

@ -5,6 +5,17 @@
} }
], ],
"settings": { "settings": {
"java.configuration.updateBuildConfiguration": "automatic" "java.configuration.updateBuildConfiguration": "automatic",
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"cSpell.words": [
"Bukkit",
"crafter",
"itemstack"
]
} }
} }

View File

@ -12,7 +12,7 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import ca.recrown.islandsurvivalcraft.interaction.commands.CommandProcessor; import ca.recrown.islandsurvivalcraft.interaction.commands.CommandProcessor;
import ca.recrown.islandsurvivalcraft.interaction.items.VariedItemManager; import ca.recrown.islandsurvivalcraft.interaction.items.ItemVariantManager;
import ca.recrown.islandsurvivalcraft.world.WorldInfoManager; import ca.recrown.islandsurvivalcraft.world.WorldInfoManager;
import ca.recrown.islandsurvivalcraft.world.generation.GeneratorModes; import ca.recrown.islandsurvivalcraft.world.generation.GeneratorModes;
@ -20,11 +20,11 @@ public class IslandSurvivalCraft extends JavaPlugin implements Listener {
private PluginManager pluginManager; private PluginManager pluginManager;
private WorldInfoManager worldInfoManager; private WorldInfoManager worldInfoManager;
private CommandProcessor commandProcessor; private CommandProcessor commandProcessor;
private VariedItemManager variedItemManager; private ItemVariantManager variedItemManager;
public IslandSurvivalCraft() { public IslandSurvivalCraft() {
worldInfoManager = new WorldInfoManager(); worldInfoManager = new WorldInfoManager();
variedItemManager = new VariedItemManager(this); variedItemManager = new ItemVariantManager(this);
commandProcessor = new CommandProcessor(this); commandProcessor = new CommandProcessor(this);
} }

View File

@ -77,10 +77,10 @@ public class IslandCommand implements CommandRunnable {
@Override @Override
public void run() { public void run() {
for (Player player : playersHighlighting) { for (Player player : playersHighlighting) {
if (player == null) return; if (player == null) continue;
if (!player.isOnline()) { if (!player.isOnline()) {
playersHighlighting.remove(player); playersHighlighting.remove(player);
return; continue;
} }
World world = player.getWorld(); World world = player.getWorld();
WorldInfo worldInfo = islandsurvivalcraft.getWorldInfoManager().retrieve(world.getName()); WorldInfo worldInfo = islandsurvivalcraft.getWorldInfoManager().retrieve(world.getName());

View File

@ -1,7 +1,6 @@
package ca.recrown.islandsurvivalcraft.interaction.items; package ca.recrown.islandsurvivalcraft.interaction.items;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
@ -11,24 +10,26 @@ import org.bukkit.inventory.Recipe;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft; import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
public interface VariedItem { public interface ItemVariant {
/** /**
* Should be used to perform anything that need only be called once. * Should be used to perform anything that need only be called once.
* Is called only when the needInitialization method returns true. * Is called only when the needInitialization() method returns true.
* Is called after assigning identity. * Is called after assigning identity.
* @param islandSurvivalCraft The IslandSurvivalCraft plugin object that contains references to various managers that may be used. * @param islandSurvivalCraft The IslandSurvivalCraft plugin object that contains references to various managers that may be used.
*/ */
public void initialize(IslandSurvivalCraft islandSurvivalCraft); public void initialize(IslandSurvivalCraft islandSurvivalCraft);
/** /**
* The recipe of the item variant. If null, these features will hook directly into the original map.
* @return the recipe to be registered with bukkit. * @return the recipe to be registered with bukkit.
*/ */
public Recipe getRecipe(); public Recipe getRecipe();
/** /**
* @return The official, displayed name of the item. * @return a string representing the key name of the item.
*/ */
public String getName(); public String getItemName();
/** /**
* *
@ -36,17 +37,12 @@ public interface VariedItem {
*/ */
public Material[] getBaseItems(); public Material[] getBaseItems();
/**
* @return The namespaced key Bukkit will use to identify this variation of the item.
*/
public NamespacedKey getNamespacedKey();
/** /**
* Assigns a persistent metadata identifier. * Assigns a persistent metadata identifier.
* This identifier should be saved to any itemstack that is considered this item. * This identifier should be saved to any itemstack that is considered this item.
* @param identifier The identifier. * @param identifier The identifier.
*/ */
public void assignIdentifier(VariationItemIdentifier identifier); public void assignIdentifier(ItemVariantIdentifier identifier);
/** /**
@ -54,7 +50,7 @@ public interface VariedItem {
* This usually should just return the assigned identifier. * This usually should just return the assigned identifier.
* @return The identifier metadata. * @return The identifier metadata.
*/ */
public VariationItemIdentifier getIdentifier(); public ItemVariantIdentifier getIdentifier();
/** /**
* If this item needs initializing. * If this item needs initializing.
@ -104,10 +100,4 @@ 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

@ -5,10 +5,10 @@ import java.nio.charset.StandardCharsets;
import org.bukkit.persistence.PersistentDataAdapterContext; import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
public class VariationItemIdentifier implements PersistentDataType<byte[], String> { public class ItemVariantIdentifier implements PersistentDataType<byte[], String> {
private final String ID; private final String ID;
public VariationItemIdentifier(String ID) { public ItemVariantIdentifier(String ID) {
this.ID = ID; this.ID = ID;
} }

View File

@ -6,6 +6,7 @@ import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.EntityType; 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;
@ -22,24 +23,28 @@ import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft; import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
public class VariedItemManager implements Listener { public class ItemVariantManager implements Listener {
private final IslandSurvivalCraft islandSurvivalCraft; private final IslandSurvivalCraft islandSurvivalCraft;
public VariedItemManager(IslandSurvivalCraft islandSurvivalCraft) { public ItemVariantManager(IslandSurvivalCraft islandSurvivalCraft) {
this.islandSurvivalCraft = islandSurvivalCraft; this.islandSurvivalCraft = islandSurvivalCraft;
} }
public void loadAllItems() { public void loadAllItems() {
RegisteredVariedItem[] items = RegisteredVariedItem.values(); RegisteredItemVariants[] items = RegisteredItemVariants.values();
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
if (items[i].getVariedItem().needInitialization()) { if (items[i].getVariedItem().needInitialization()) {
items[i].getVariedItem().initialize(islandSurvivalCraft); items[i].getVariedItem().initialize(islandSurvivalCraft);
}; }
islandSurvivalCraft.getServer().addRecipe(items[i].getVariedItem().getRecipe()); Recipe currRecipe = items[i].getVariedItem().getRecipe();
if (currRecipe != null) {
islandSurvivalCraft.getServer().addRecipe(currRecipe);
}
} }
Collection<? extends Player> players = islandSurvivalCraft.getServer().getOnlinePlayers(); Collection<? extends Player> players = islandSurvivalCraft.getServer().getOnlinePlayers();
@ -49,16 +54,16 @@ public class VariedItemManager implements Listener {
} }
public void unloadAllItems() { public void unloadAllItems() {
RegisteredVariedItem[] items = RegisteredVariedItem.values(); RegisteredItemVariants[] items = RegisteredItemVariants.values();
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
Bukkit.removeRecipe(items[i].getVariedItem().getNamespacedKey()); Bukkit.removeRecipe(new NamespacedKey(islandSurvivalCraft, items[i].getVariedItem().getItemName()));
} }
} }
@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); ItemVariant variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) { if (variedItem != null) {
variedItem.onItemHeld(item, e.getPlayer()); variedItem.onItemHeld(item, e.getPlayer());
} else { } else {
@ -73,7 +78,7 @@ public class VariedItemManager implements Listener {
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onInventoryInteract(InventoryClickEvent e) { public void onInventoryInteract(InventoryClickEvent e) {
ItemStack item = e.getCurrentItem(); ItemStack item = e.getCurrentItem();
VariedItem variedItem = getVariedItemFromItemStack(item); ItemVariant variedItem = getVariedItemFromItemStack(item);
if (e.getSlotType() == SlotType.RESULT) { if (e.getSlotType() == SlotType.RESULT) {
if (variedItem != null) { if (variedItem != null) {
if (variedItem.onItemCrafted(item, e.getWhoClicked())) { if (variedItem.onItemCrafted(item, e.getWhoClicked())) {
@ -94,7 +99,7 @@ public class VariedItemManager implements Listener {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerDropItem(PlayerDropItemEvent e) { public void onPlayerDropItem(PlayerDropItemEvent e) {
ItemStack item = e.getItemDrop().getItemStack(); ItemStack item = e.getItemDrop().getItemStack();
VariedItem variedItem = getVariedItemFromItemStack(item); ItemVariant variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) { if (variedItem != null) {
variedItem.onItemPutAway(item, e.getPlayer()); variedItem.onItemPutAway(item, e.getPlayer());
} }
@ -104,7 +109,7 @@ public class VariedItemManager implements Listener {
public void onEntityPickup(EntityPickupItemEvent e) { public void onEntityPickup(EntityPickupItemEvent e) {
ItemStack item = e.getItem().getItemStack(); ItemStack item = e.getItem().getItemStack();
if (e.getEntityType() == EntityType.PLAYER) { if (e.getEntityType() == EntityType.PLAYER) {
VariedItem variedItem = getVariedItemFromItemStack(item); ItemVariant variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) { if (variedItem != null) {
Player player = (Player) e.getEntity(); Player player = (Player) e.getEntity();
if (player.getInventory().getHeldItemSlot() == player.getInventory().firstEmpty()) { if (player.getInventory().getHeldItemSlot() == player.getInventory().firstEmpty()) {
@ -118,7 +123,7 @@ public class VariedItemManager implements Listener {
public void onPlayerInteraction(PlayerInteractEvent e) { public void onPlayerInteraction(PlayerInteractEvent e) {
if (e.useItemInHand() == Result.DENY || (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK)) return; if (e.useItemInHand() == Result.DENY || (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK)) return;
ItemStack item = e.getItem(); ItemStack item = e.getItem();
VariedItem variedItem = getVariedItemFromItemStack(item); ItemVariant variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) { if (variedItem != null) {
if(variedItem.onItemRightClicked(item, e.getPlayer(), e.getHand(), e.getClickedBlock(), e.getBlockFace())) { if(variedItem.onItemRightClicked(item, e.getPlayer(), e.getHand(), e.getClickedBlock(), e.getBlockFace())) {
e.setCancelled(true); e.setCancelled(true);
@ -131,38 +136,50 @@ public class VariedItemManager implements Listener {
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
initializeInventoryVariedItems(e.getPlayer().getInventory()); initializeInventoryVariedItems(e.getPlayer().getInventory());
ItemStack item = e.getPlayer().getInventory().getItemInMainHand(); ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
VariedItem variedItem = getVariedItemFromItemStack(item); ItemVariant variedItem = getVariedItemFromItemStack(item); //Check to see of we can find a variation of the item.
if (variedItem != null) { if (variedItem != null) {
variedItem.onItemHeld(item, e.getPlayer()); variedItem.onItemHeld(item, e.getPlayer());
} }
} }
private void initializeInventoryVariedItems(Inventory inv) { private void initializeInventoryVariedItems(Inventory inv) {
RegisteredVariedItem[] registeredVariedItems = RegisteredVariedItem.values(); RegisteredItemVariants[] registeredVariedItems = RegisteredItemVariants.values();
for (RegisteredVariedItem registeredVariedItem : registeredVariedItems) { for (RegisteredItemVariants registeredVariedItem : registeredVariedItems) {
VariedItem variedItem = registeredVariedItem.getVariedItem(); ItemVariant variedItem = registeredVariedItem.getVariedItem();
Material[] bases = variedItem.getBaseItems(); Material[] bases = variedItem.getBaseItems();
for (Material base : bases) { for (Material base : bases) {
HashMap<Integer, ? extends ItemStack> potentials = inv.all(base); HashMap<Integer, ? extends ItemStack> potentials = inv.all(base);
for (Entry<Integer, ? extends ItemStack> entry : potentials.entrySet()) { for (Entry<Integer, ? extends ItemStack> entry : potentials.entrySet()) {
String identifier = entry.getValue().getItemMeta().getPersistentDataContainer().get(variedItem.getNamespacedKey(), variedItem.getIdentifier()); if (registeredVariedItem.getVariedItem().getRecipe() != null) {
if (identifier != null) { NamespacedKey key = new NamespacedKey(islandSurvivalCraft, variedItem.getItemName() + ".identifier");
String identifier = entry.getValue().getItemMeta().getPersistentDataContainer().get(key, variedItem.getIdentifier());
if (identifier != null) {
variedItem.initializeInstanceOfItem(entry.getValue());
}
} else {
variedItem.initializeInstanceOfItem(entry.getValue()); variedItem.initializeInstanceOfItem(entry.getValue());
} }
} }
} }
} }
} }
private VariedItem getVariedItemFromItemStack(ItemStack item) { private ItemVariant getVariedItemFromItemStack(ItemStack item) {
if (item == null || item.getType() == Material.AIR) return null; if (item == null || item.getType() == Material.AIR) return null;
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
RegisteredVariedItem[] items = RegisteredVariedItem.values(); RegisteredItemVariants[] items = RegisteredItemVariants.values();
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
VariedItem curr = items[i].getVariedItem(); ItemVariant curr = items[i].getVariedItem();
String identifier = meta.getPersistentDataContainer().get(curr.getNamespacedKey(), curr.getIdentifier()); if (curr.getRecipe() == null) {
for (Material material : curr.getBaseItems()) {
if (item.getType() == material) {
return curr;
}
}
}
NamespacedKey key = new NamespacedKey(islandSurvivalCraft, curr.getItemName());
String identifier = meta.getPersistentDataContainer().get(key, curr.getIdentifier());
if (identifier != null) { if (identifier != null) {
if (curr.getIdentifier().getID().equals(identifier)) { if (curr.getIdentifier().getID().equals(identifier)) {
return curr; return curr;

View File

@ -1,28 +0,0 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataType;
public class PersistentBooleanDatatype implements PersistentDataType<Boolean, Boolean> {
@Override
public Class<Boolean> getPrimitiveType() {
return Boolean.class;
}
@Override
public Class<Boolean> getComplexType() {
return Boolean.class;
}
@Override
public Boolean toPrimitive(Boolean complex, PersistentDataAdapterContext context) {
return complex;
}
@Override
public Boolean fromPrimitive(Boolean primitive, PersistentDataAdapterContext context) {
return primitive;
}
}

View File

@ -2,20 +2,20 @@ package ca.recrown.islandsurvivalcraft.interaction.items;
import ca.recrown.islandsurvivalcraft.interaction.items.variations.*; import ca.recrown.islandsurvivalcraft.interaction.items.variations.*;
public enum RegisteredVariedItem { public enum RegisteredItemVariants {
ISLAND_MAP(new IslandMapItem()) ISLAND_MAP(new IslandMapItem())
; ;
private final VariedItem variedItem; private final ItemVariant variedItem;
private RegisteredVariedItem(VariedItem alternateItem) { private RegisteredItemVariants(ItemVariant alternateItem) {
this.variedItem = alternateItem; this.variedItem = alternateItem;
this.variedItem.assignIdentifier(new VariationItemIdentifier(this.name())); this.variedItem.assignIdentifier(new ItemVariantIdentifier(this.name() + ".identifier"));
} }
/** /**
* @return the variedItem * @return the variedItem
*/ */
public VariedItem getVariedItem() { public ItemVariant getVariedItem() {
return variedItem; return variedItem;
} }
} }

View File

@ -1,11 +1,5 @@
package ca.recrown.islandsurvivalcraft.interaction.items.variations; package ca.recrown.islandsurvivalcraft.interaction.items.variations;
import java.awt.Color;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -15,55 +9,35 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe; import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MapMeta; import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapCanvas; import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapPalette; import org.bukkit.map.MapPalette;
import org.bukkit.map.MapRenderer; import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView; import org.bukkit.map.MapView;
import org.bukkit.map.MapView.Scale; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft; import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
import ca.recrown.islandsurvivalcraft.interaction.items.VariationItemIdentifier; import ca.recrown.islandsurvivalcraft.interaction.items.ItemVariant;
import ca.recrown.islandsurvivalcraft.interaction.items.VariedItem; import ca.recrown.islandsurvivalcraft.interaction.items.ItemVariantIdentifier;
import ca.recrown.islandsurvivalcraft.utilities.GeneralUtilities; import ca.recrown.islandsurvivalcraft.utilities.GeneralUtilities;
import ca.recrown.islandsurvivalcraft.utilities.datatypes.Point2; import ca.recrown.islandsurvivalcraft.utilities.datatypes.Point2;
import ca.recrown.islandsurvivalcraft.world.WorldInfo; import ca.recrown.islandsurvivalcraft.world.WorldInfo;
import ca.recrown.islandsurvivalcraft.world.Information.IslandInformation; import ca.recrown.islandsurvivalcraft.world.Information.IslandInformation;
import ca.recrown.islandsurvivalcraft.world.Information.IslandInformationManager; import ca.recrown.islandsurvivalcraft.world.Information.IslandInformationManager;
public class IslandMapItem extends MapRenderer implements VariedItem { import java.awt.Color;
Color mainColor = new Color(0, 180, 0);
private Material[] baseItems;
private VariationItemIdentifier identifier;
private IslandSurvivalCraft islandSurvivalCraft;
private NamespacedKey namespacedKey;
private ShapelessRecipe recipe;
private ArrayList<String> lore = new ArrayList<>();
@Override public class IslandMapItem extends MapRenderer implements ItemVariant {
public void initialize(IslandSurvivalCraft islandSurvivalCraft) { IslandSurvivalCraft plugin;
lore.add(ChatColor.RESET.toString() + ChatColor.GRAY + "Land ho!"); ItemVariantIdentifier identifier;
this.islandSurvivalCraft = islandSurvivalCraft; Color mainColor = new Color(0, 180, 0);
baseItems = new Material[2];
baseItems[0] = Material.MAP;
baseItems[1] = Material.FILLED_MAP;
namespacedKey = new NamespacedKey(islandSurvivalCraft, "island_map");
ItemStack resultItem = new ItemStack(getBaseItems()[0], 1);
ItemMeta metadata = resultItem.getItemMeta();
metadata.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID());
metadata.setLore(lore);
resultItem.setItemMeta(metadata);
recipe = new ShapelessRecipe(namespacedKey, resultItem);
recipe.addIngredient(Material.REDSTONE);
recipe.addIngredient(new ItemStack(Material.MAP));
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void render(MapView map, MapCanvas canvas, Player player) { public void render(MapView map, MapCanvas canvas, Player player) {
WorldInfo worldInfo = islandSurvivalCraft.getWorldInfoManager().retrieve(map.getWorld().getName()); WorldInfo worldInfo = plugin.getWorldInfoManager().retrieve(map.getWorld().getName());
IslandInformationManager islandInformationManager = worldInfo.getIslandInfoManager(); IslandInformationManager islandInformationManager = worldInfo.getIslandInfoManager();
int blocksPerPixel = (int) (1 * Math.pow(2, map.getScale().getValue())); int blocksPerPixel = (int) (1 * Math.pow(2, map.getScale().getValue()));
for (int x = 0; x < 128; x += 1) { for (int x = 0; x < 128; x += 1) {
@ -76,8 +50,6 @@ public class IslandMapItem extends MapRenderer implements VariedItem {
IslandInformation info = islandInformationManager.getIslandInformation(pCoords, false); IslandInformation info = islandInformationManager.getIslandInformation(pCoords, false);
if (info != null) { if (info != null) {
canvas.setPixel(x, y, MapPalette.matchColor(mainColor)); canvas.setPixel(x, y, MapPalette.matchColor(mainColor));
} else {
canvas.setPixel(x, y, canvas.getBasePixel(x, y));
} }
} else if (worldInfo.getIslandMap().isIsland(actualX, actualZ)) { } else if (worldInfo.getIslandMap().isIsland(actualX, actualZ)) {
canvas.setPixel(x, y, MapPalette.matchColor(mainColor)); canvas.setPixel(x, y, MapPalette.matchColor(mainColor));
@ -88,87 +60,72 @@ public class IslandMapItem extends MapRenderer implements VariedItem {
} }
@Override @Override
public Recipe getRecipe() { public void initialize(IslandSurvivalCraft islandSurvivalCraft) {
return recipe; this.plugin = islandSurvivalCraft;
} }
@Override @Override
public String getName() { public Recipe getRecipe() {
return "Island Map"; return null;
}
@Override
public String getItemName() {
return "Island_Map";
} }
@Override @Override
public Material[] getBaseItems() { public Material[] getBaseItems() {
return baseItems; return new Material[] {Material.FILLED_MAP};
} }
@Override @Override
public NamespacedKey getNamespacedKey() { public void assignIdentifier(ItemVariantIdentifier identifier) {
return namespacedKey;
}
@Override
public boolean onItemCrafted(ItemStack item, HumanEntity crafter) {
if (item.getType() != Material.FILLED_MAP) return false;
Bukkit.getScheduler().scheduleSyncDelayedTask(islandSurvivalCraft, () -> {
initializeInstanceOfItem(item);
}, 15);
return false;
}
@Override
public void assignIdentifier(VariationItemIdentifier identifier) {
this.identifier = identifier; this.identifier = identifier;
} }
@Override @Override
public VariationItemIdentifier getIdentifier() { public ItemVariantIdentifier getIdentifier() {
return identifier; return this.identifier;
} }
@Override @Override
public boolean needInitialization() { public boolean needInitialization() {
return namespacedKey == null; return this.plugin == null;
}
@Override
public boolean onItemCrafted(ItemStack item, HumanEntity crafter) {
return false;
} }
@Override @Override
public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block, public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block,
BlockFace blockFace) { BlockFace blockFace) {
if (item.getType() != Material.MAP) return false; if (((Player)clicker).isSneaking()) {
ItemStack freshMap = new ItemStack(Material.FILLED_MAP, 1); ItemMeta meta = item.getItemMeta();
MapMeta mapMeta = (MapMeta) freshMap.getItemMeta(); PersistentDataContainer container = meta.getPersistentDataContainer();
mapMeta.setLore(lore); NamespacedKey key = new NamespacedKey(plugin, getItemName() + ".state");
mapMeta.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID()); if (!container.has(key, PersistentDataType.BYTE)) {
MapView mapView = Bukkit.createMap(clicker.getWorld()); container.set(key, PersistentDataType.BYTE, (byte) 0);
mapView.setTrackingPosition(true); }
mapView.setCenterX((Math.round(clicker.getLocation().getBlockX() / 128f) * 128) - 1); if (container.get(key, PersistentDataType.BYTE) == 0) {
mapView.setCenterZ((Math.round(clicker.getLocation().getBlockZ() / 128f) * 128) - 1); container.set(key, PersistentDataType.BYTE, (byte) 1);
mapView.setScale(Scale.CLOSEST); MapMeta mapMeta = (MapMeta) item.getItemMeta();
mapMeta.setMapView(mapView); mapMeta.getMapView().addRenderer(this);
freshMap.setItemMeta(mapMeta); } else {
initializeInstanceOfItem(freshMap); container.set(key, PersistentDataType.BYTE, (byte) 0);
if (clicker.getGameMode() != GameMode.CREATIVE) { MapMeta mapMeta = (MapMeta) item.getItemMeta();
clicker.getInventory().setItem(hand, freshMap); mapMeta.getMapView().removeRenderer(this);
} else { }
clicker.getInventory().addItem(freshMap);
}
return true;
}
@Override item.setItemMeta(meta);
public void initializeInstanceOfItem(ItemStack item) { }
if (item.getType() != Material.FILLED_MAP) return; return false;
MapMeta mapMeta = (MapMeta) item.getItemMeta();
MapView mapView = mapMeta.getMapView();
if (mapView.getRenderers().contains(this)) return;
mapView.addRenderer(this);
mapMeta.setMapView(mapView);
item.setItemMeta(mapMeta);
} }
@Override @Override
public void onItemHeld(ItemStack heldItem, HumanEntity holder) { public void onItemHeld(ItemStack heldItem, HumanEntity holder) {
initializeInstanceOfItem(heldItem);
} }
@Override @Override
@ -176,7 +133,17 @@ public class IslandMapItem extends MapRenderer implements VariedItem {
} }
@Override @Override
public String getDescription() { public void initializeInstanceOfItem(ItemStack item) {
return "A map that has island hunting properties. Will highlight islands in relation on map."; MapMeta mapMeta = (MapMeta) item.getItemMeta();
PersistentDataContainer container = mapMeta.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(plugin, getItemName() + ".state");
if (container.has(key, PersistentDataType.BYTE)) {
if (container.get(key, PersistentDataType.BYTE) == 1) {
mapMeta = (MapMeta) item.getItemMeta();
mapMeta.getMapView().addRenderer(this);
item.setItemMeta(mapMeta);
}
}
} }
} }

View File

@ -140,9 +140,9 @@ public class IslandInformationManager implements Runnable {
while (buildingCache.size() > 0) { while (buildingCache.size() > 0) {
Point2 chunkCoords = buildingCache.getMostRecentKey(); Point2 chunkCoords = buildingCache.getMostRecentKey();
CompletableFuture<HashSet<IslandInformation>> future = buildingCache.remove(chunkCoords); CompletableFuture<HashSet<IslandInformation>> future = buildingCache.remove(chunkCoords);
HashSet<IslandInformation> islandInformations = buildChunkIslandInformation(chunkCoords); HashSet<IslandInformation> islandInfos = buildChunkIslandInformation(chunkCoords);
islandSets.put(chunkCoords, islandInformations); islandSets.put(chunkCoords, islandInfos);
future.complete(islandInformations); future.complete(islandInfos);
} }
try { try {
synchronized (loader) { synchronized (loader) {

View File

@ -158,7 +158,7 @@ public class IslandWorldMap {
* Find the island's origin block. Will call the coordinate target validatable to end early if nessecary. * Find the island's origin block. Will call the coordinate target validatable to end early if nessecary.
* @param worldX The x coordinate of the island block in question. * @param worldX The x coordinate of the island block in question.
* @param worldZ The y coordinate of the island block in question. * @param worldZ The y coordinate of the island block in question.
* @param targetValidator The coordinate target validator to use. Optional and is null. * @param targetValidator The coordinate target validator to use. May be null.
* @return The island origin point. Will be null if the passed target target validator stops search prematurely. * @return The island origin point. Will be null if the passed target target validator stops search prematurely.
*/ */
public Point2 findIslandOrigin(int worldX, int worldZ, CoordinateValidatable targetValidator) { public Point2 findIslandOrigin(int worldX, int worldZ, CoordinateValidatable targetValidator) {

View File

@ -15,7 +15,6 @@ import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.api.TestInstance.Lifecycle;
import ca.recrown.islandsurvivalcraft.utilities.datatypes.Point2; import ca.recrown.islandsurvivalcraft.utilities.datatypes.Point2;
import ca.recrown.islandsurvivalcraft.utilities.pathfinding.CoordinateValidatable;
/** /**
* Unit test for simple App. * Unit test for simple App.

View File

@ -24,6 +24,7 @@ settings:
shutdown-message: Server closed shutdown-message: Server closed
minimum-api: none minimum-api: none
spawn-limits: spawn-limits:
water-ambient: 20
monsters: 70 monsters: 70
animals: 10 animals: 10
water-animals: 15 water-animals: 15
@ -31,6 +32,7 @@ spawn-limits:
chunk-gc: chunk-gc:
period-in-ticks: 600 period-in-ticks: 600
ticks-per: ticks-per:
water-ambient-spawns: 1
animal-spawns: 400 animal-spawns: 400
monster-spawns: 1 monster-spawns: 1
water-spawns: 1 water-spawns: 1