Primitive map based island highlighting functioning.

This commit is contained in:
Harrison Deng 2020-05-16 14:33:40 -05:00
parent ec006ffbb2
commit e2f476cd82
8 changed files with 282 additions and 124 deletions

View File

@ -88,7 +88,7 @@ public class HighlightCommand implements CommandRunnable {
Point2 islandOrigin = islandInfo.getIslandOrigin();
int xDirection = GeneralUtilities.addMagnitude(Math.max(Math.min(1, islandOrigin.x - playerX), -1), 1) + playerX;
int yDirection = GeneralUtilities.addMagnitude(Math.max(Math.min(1, islandOrigin.y - playerZ), -1), 1) + playerZ;
spawnParticle(false, false, world, xDirection, playerY, yDirection, ORIGIN_DUST_OPTIONS);
spawnParticle(false, false, world, xDirection, playerY + 1, yDirection, ORIGIN_DUST_OPTIONS);
spawnParticle(true, true, world, islandOrigin.x, playerY, islandOrigin.y, ORIGIN_DUST_OPTIONS);
}
}

View File

@ -1,110 +0,0 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
public class IslandMapItem extends MapRenderer implements VariedItem {
VariationItemIdentifier identifier;
NamespacedKey namespacedKey;
ShapelessRecipe recipe;
@Override
public void initialize(IslandSurvivalCraft islandSurvivalCraft) {
namespacedKey = new NamespacedKey(islandSurvivalCraft, "island_map");
ItemStack resultItem = new ItemStack(getBaseItem(), 1);
ItemMeta metadata = resultItem.getItemMeta();
metadata.setDisplayName(ChatColor.RESET + getName());
metadata.setLocalizedName(getName());
metadata.getPersistentDataContainer().set(namespacedKey, identifier, identifier.getID());
resultItem.setItemMeta(metadata);
recipe = new ShapelessRecipe(namespacedKey, resultItem);
recipe.addIngredient(Material.REDSTONE);
recipe.addIngredient(new ItemStack(Material.MAP));
}
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
}
@Override
public Recipe getRecipe() {
return recipe;
}
@Override
public String getName() {
return ChatColor.GREEN + "Island Map";
}
@Override
public Material getBaseItem() {
return Material.MAP;
}
@Override
public NamespacedKey getNamespacedKey() {
return namespacedKey;
}
@Override
public void onItemCrafted(ItemStack item, HumanEntity crafter) {
ItemMeta metadata = item.getItemMeta();
ArrayList<String> lore = new ArrayList<>();
lore.add(ChatColor.RESET.toString() + ChatColor.GRAY + "Land ho!");
metadata.setLore(lore);
item.setItemMeta(metadata);
}
@Override
public void assignIdentifier(VariationItemIdentifier identifier) {
this.identifier = identifier;
}
@Override
public VariationItemIdentifier getIdentifier() {
return identifier;
}
@Override
public boolean needInitialization() {
return namespacedKey == null;
}
@Override
public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block,
BlockFace blockFace) {
ItemStack freshMap = new ItemStack(Material.FILLED_MAP, 1);
MapMeta mapMeta = (MapMeta) freshMap.getItemMeta();
mapMeta.setDisplayName(ChatColor.RESET + getName());
MapView mapView = Bukkit.createMap(clicker.getWorld());
mapView.addRenderer(this);
mapView.setTrackingPosition(true);
mapMeta.setMapView(mapView);
freshMap.setItemMeta(mapMeta);
clicker.getInventory().setItem(hand, freshMap);
return false;
}
}

View File

@ -0,0 +1,28 @@
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

@ -1,5 +1,7 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import ca.recrown.islandsurvivalcraft.interaction.items.variations.*;
public enum RegisteredVariedItem {
ISLAND_MAP(new IslandMapItem())
;

View File

@ -34,7 +34,7 @@ public interface VariedItem {
*
* @return The material item this item is a variation of.
*/
public Material getBaseItem();
public Material[] getBaseItems();
/**
* @return The namespaced key Bukkit will use to identify this variation of the item.
@ -43,11 +43,12 @@ public interface VariedItem {
/**
* Assigns a persistent metadata identifier.
* This identifier should be saved to the recipe's resulting itemstack.
* This identifier should be saved to any itemstack that is considered this item.
* @param identifier The identifier.
*/
public void assignIdentifier(VariationItemIdentifier identifier);
/**
* Should give the identifier used identify this variation of the base item from the original.
* This usually should just return the assigned identifier.
@ -64,10 +65,12 @@ public interface VariedItem {
/**
* Called when this variety of the item is crafted.
* @param item the resulting itemstack.
* @param crafter the entity performing the crafting.
* @param item The resulting itemstack.
* @param crafter The entity performing the crafting.
* @param recipe The recipe used to craft this.
* @return true if the remaining chain of events for this crafting should not occur. Otherwise, false.
*/
public void onItemCrafted(ItemStack item, HumanEntity crafter);
public boolean onItemCrafted(ItemStack item, HumanEntity crafter);
/**
* Called when this item is right clicked with.
@ -79,4 +82,12 @@ public interface VariedItem {
* @return
*/
public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block, BlockFace blockFace);
/**
* Called when a player joins and called when player picks up this variation of the item.
* Can be used to initialize any data the item doesn't save on server shutdown.
* You may need to check if the item already has been instantiated.
* @param item The itemstack that represents the item in question.
*/
public void initializeInstanceOfItem(ItemStack item);
}

View File

@ -1,13 +1,25 @@
package ca.recrown.islandsurvivalcraft.interaction.items;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.Material;
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.inventory.InventoryEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -23,9 +35,16 @@ public class VariedItemManager implements Listener {
public void loadAllItems() {
RegisteredVariedItem[] items = RegisteredVariedItem.values();
for (int i = 0; i < items.length; i++) {
if (items[i].getVariedItem().needInitialization()) items[i].getVariedItem().initialize(islandSurvivalCraft);
if (items[i].getVariedItem().needInitialization()) {
items[i].getVariedItem().initialize(islandSurvivalCraft);
};
islandSurvivalCraft.getServer().addRecipe(items[i].getVariedItem().getRecipe());
}
Collection<? extends Player> players = islandSurvivalCraft.getServer().getOnlinePlayers();
for (Player player : players) {
initializePlayerItems(player.getInventory());
}
}
public void unloadAllItems() {
@ -35,17 +54,27 @@ public class VariedItemManager implements Listener {
}
}
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.HIGHEST)
public void onItemCrafted(CraftItemEvent e) {
ItemStack item = e.getCurrentItem();
VariedItem variedItem = getVariedItemFromItemStack(item);
if (variedItem != null) {
variedItem.onItemCrafted(item, e.getWhoClicked());
if (variedItem.onItemCrafted(item, e.getWhoClicked())) {
e.setCancelled(true);
}
}
}
@EventHandler(priority = EventPriority.HIGHEST)
@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);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerInteraction(PlayerInteractEvent e) {
if (e.useItemInHand() == Result.DENY || (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK)) return;
ItemStack item = e.getItem();
@ -58,7 +87,29 @@ public class VariedItemManager implements Listener {
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent e) {
initializePlayerItems(e.getPlayer().getInventory());
}
private void initializePlayerItems(Inventory inv) {
RegisteredVariedItem[] registeredVariedItems = RegisteredVariedItem.values();
for (RegisteredVariedItem registeredVariedItem : registeredVariedItems) {
VariedItem variedItem = registeredVariedItem.getVariedItem();
Material[] bases = variedItem.getBaseItems();
for (Material base : bases) {
HashMap<Integer, ? extends ItemStack> potentials = inv.all(base);
for (Entry<Integer, ? extends ItemStack> entry : potentials.entrySet()) {
String identifier = entry.getValue().getItemMeta().getPersistentDataContainer().get(variedItem.getNamespacedKey(), variedItem.getIdentifier());
if (identifier != null) {
variedItem.initializeInstanceOfItem(entry.getValue());
}
}
}
}
}
private VariedItem getVariedItemFromItemStack(ItemStack item) {
if (item == null) return null;

View File

@ -0,0 +1,172 @@
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.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapPalette;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.map.MapView.Scale;
import ca.recrown.islandsurvivalcraft.IslandSurvivalCraft;
import ca.recrown.islandsurvivalcraft.interaction.items.VariationItemIdentifier;
import ca.recrown.islandsurvivalcraft.interaction.items.VariedItem;
import ca.recrown.islandsurvivalcraft.utilities.datatypes.Point2;
import ca.recrown.islandsurvivalcraft.world.WorldInfo;
import ca.recrown.islandsurvivalcraft.world.Information.IslandInformation;
import ca.recrown.islandsurvivalcraft.world.Information.IslandInformationManager;
public class IslandMapItem extends MapRenderer implements VariedItem {
private Material[] baseItems;
private VariationItemIdentifier identifier;
private IslandSurvivalCraft islandSurvivalCraft;
private NamespacedKey namespacedKey;
private ShapelessRecipe recipe;
private ArrayList<String> lore = new ArrayList<>();
@Override
public void initialize(IslandSurvivalCraft islandSurvivalCraft) {
lore.add(ChatColor.RESET.toString() + ChatColor.GRAY + "Land ho!");
this.islandSurvivalCraft = islandSurvivalCraft;
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.setDisplayName(ChatColor.RESET + getName());
metadata.setLocalizedName(getName());
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")
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
WorldInfo worldInfo = islandSurvivalCraft.getWorldInfoManager().retrieve(map.getWorld().getName());
IslandInformationManager islandInformationManager = worldInfo.getIslandInfoManager();
int blocksPerPixel = (int) (1 * Math.pow(2, map.getScale().getValue()));
for (int x = 0; x < 128; x ++) {
for (int y = 0; y < 128; y ++) {
int actualX = blocksPerPixel * (x - Byte.MAX_VALUE/2) + map.getCenterX();
int actualZ = blocksPerPixel * (y - Byte.MAX_VALUE/2) + map.getCenterZ();
byte baseColorVal = canvas.getBasePixel(x, y);
Color baseColor = MapPalette.getColor(baseColorVal);
Point2 pCoords = new Point2(actualX, actualZ);
IslandInformation info = islandInformationManager.getIslandInformation(pCoords);
if (baseColorVal != 0) {
if (info != null && pCoords.distance(new Point2(player.getLocation().getBlockX(), player.getLocation().getBlockZ())) < 32) {
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));
if (info.isEdgeOfIsland(pCoords)) {
canvas.setPixel(x, y, MapPalette.matchColor(255, 120, 70));
} else {
canvas.setPixel(x, y, MapPalette.matchColor(mixed));
}
}
}
}
}
}
@Override
public Recipe getRecipe() {
return recipe;
}
@Override
public String getName() {
return ChatColor.GREEN + "Island Map";
}
@Override
public Material[] getBaseItems() {
return baseItems;
}
@Override
public NamespacedKey getNamespacedKey() {
return namespacedKey;
}
@Override
public boolean onItemCrafted(ItemStack item, HumanEntity crafter) {
if (item.getType() != Material.FILLED_MAP) return false;
Bukkit.getScheduler().runTask(islandSurvivalCraft, () -> {
initializeInstanceOfItem(item);
});
return false;
}
@Override
public void assignIdentifier(VariationItemIdentifier identifier) {
this.identifier = identifier;
}
@Override
public VariationItemIdentifier getIdentifier() {
return identifier;
}
@Override
public boolean needInitialization() {
return namespacedKey == null;
}
@Override
public boolean onItemRightClicked(ItemStack item, HumanEntity clicker, EquipmentSlot hand, Block block,
BlockFace blockFace) {
if (item.getType() != Material.MAP) return false;
ItemStack freshMap = new ItemStack(Material.FILLED_MAP, 1);
MapMeta mapMeta = (MapMeta) freshMap.getItemMeta();
mapMeta.setDisplayName(ChatColor.RESET + getName());
mapMeta.setLore(lore);
mapMeta.getPersistentDataContainer().set(getNamespacedKey(), getIdentifier(), getIdentifier().getID());
MapView mapView = Bukkit.createMap(clicker.getWorld());
mapView.setTrackingPosition(true);
mapView.setCenterX(Math.round(clicker.getLocation().getBlockX() / 128f) * 128);
mapView.setCenterZ(Math.round(clicker.getLocation().getBlockZ() / 128f) * 128);
mapView.setScale(Scale.CLOSEST);
mapMeta.setMapView(mapView);
freshMap.setItemMeta(mapMeta);
initializeInstanceOfItem(freshMap);
if (clicker.getGameMode() != GameMode.CREATIVE) {
clicker.getInventory().setItem(hand, freshMap);
} else {
clicker.getInventory().addItem(freshMap);
}
return true;
}
@Override
public void initializeInstanceOfItem(ItemStack item) {
if (item.getType() != Material.FILLED_MAP) return;
MapMeta mapMeta = (MapMeta) item.getItemMeta();
MapView mapView = mapMeta.getMapView();
mapView.removeRenderer(this);
mapView.addRenderer(this);
mapMeta.setMapView(mapView);
item.setItemMeta(mapMeta);
}
}

View File

@ -96,8 +96,12 @@ public class IslandInformationManager {
public IslandInformation getIslandInformation(Point2 coords) {
HashSet<IslandInformation> chunkIslandSet = getChunkIslandInformationSet(GeneralUtilities.worldToChunkCoordinates(coords), true);
for (IslandInformation islandInformation : chunkIslandSet) {
if (islandInformation.isWithinIsland(coords)) return islandInformation;
try {
for (IslandInformation islandInformation : chunkIslandSet) {
if (islandInformation.isWithinIsland(coords)) return islandInformation;
}
} catch (NullPointerException e) {
}
return null;
}