From 040489245566dae641d08c0075196a2ed7dab006 Mon Sep 17 00:00:00 2001 From: Ka0rX Date: Tue, 12 Jul 2022 20:29:39 +0200 Subject: [PATCH] 3D Gui enhancements --- .../mmocore/gui/api/EditableInventory.java | 8 +- .../gui/api/adaptor/ThreeDimAdaptor.java | 165 +++++++++++++++++- .../mmocore/loot/droptable/DropTable.java | 3 +- 3 files changed, 167 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/gui/api/EditableInventory.java b/src/main/java/net/Indyuce/mmocore/gui/api/EditableInventory.java index c35b9280..84183989 100644 --- a/src/main/java/net/Indyuce/mmocore/gui/api/EditableInventory.java +++ b/src/main/java/net/Indyuce/mmocore/gui/api/EditableInventory.java @@ -22,7 +22,7 @@ public abstract class EditableInventory { private String name; private int slots; - protected double radius, angleGap, verticalGap, curvature, verticalOffset; + protected double radius, angleGap, verticalGap, curvature, verticalOffset, interactSensitivity; /* * this set is linked so it keeps the order/priority in which the items are * loaded from the config. @@ -47,7 +47,7 @@ public abstract class EditableInventory { this.verticalGap = config.getDouble("vertical-gap", 1); this.curvature = config.getDouble("curvature", 1); this.verticalOffset = config.getDouble("vertical-offset", 0); - + this.interactSensitivity = config.getDouble("interact-sensitivity", 0.97); this.name = config.getString("name"); Validate.notNull(name, "Name must not be null"); @@ -103,6 +103,10 @@ public abstract class EditableInventory { return verticalGap; } + public double getInteractSensitivity() { + return interactSensitivity; + } + public double getCurvature() { return curvature; } diff --git a/src/main/java/net/Indyuce/mmocore/gui/api/adaptor/ThreeDimAdaptor.java b/src/main/java/net/Indyuce/mmocore/gui/api/adaptor/ThreeDimAdaptor.java index 24868132..641c430d 100644 --- a/src/main/java/net/Indyuce/mmocore/gui/api/adaptor/ThreeDimAdaptor.java +++ b/src/main/java/net/Indyuce/mmocore/gui/api/adaptor/ThreeDimAdaptor.java @@ -12,10 +12,7 @@ import net.Indyuce.mmocore.gui.api.item.InventoryItem; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.NamespacedKey; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; +import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityDamageByEntityEvent; @@ -31,6 +28,7 @@ import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.util.Vector; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.function.Consumer; @@ -42,11 +40,19 @@ public class ThreeDimAdaptor extends Adaptor { private InteractListener interactListener; private final HashMap armorStands = new HashMap<>(); + private final HashSet loreArmorStand = new HashSet<>(); + + private final HashMap hiddenArmorStand =new HashMap<>(); + private boolean firstTime = true; private final Vector direction = generated.getPlayer().getEyeLocation().getDirection().setY(0); private final Location location = generated.getPlayer().getLocation().add(new Vector(0, generated.getEditable().getVerticalOffset(), 0)); + //Zoomed=-1 no armorstand are under zoom + private int zoomed = -1; + + public ThreeDimAdaptor(GeneratedInventory generated) { super(generated); } @@ -94,6 +100,8 @@ public class ThreeDimAdaptor extends Adaptor { //Closes the packet listener,the interact listener and destroys the armor stands. //MMOCore.plugin.protocolManager.removePacketListener(packetListener); interactListener.close(); + removeLore(); + new BukkitRunnable() { double total_percentage = 1; @@ -127,6 +135,10 @@ public class ThreeDimAdaptor extends Adaptor { setInventoryItem(item, 1); } } + if (zoomed != -1) { + displayLore(zoomed); + armorStands.get(zoomed).teleport(getLocation(zoomed, 0.75)); + } } @@ -140,6 +152,7 @@ public class ThreeDimAdaptor extends Adaptor { for (ArmorStand armorStand : armorStands.values()) armorStand.remove(); + removeLore(); } @@ -187,6 +200,113 @@ public class ThreeDimAdaptor extends Adaptor { } + public void displayLore(int n) { + ArmorStand armorStand = armorStands.get(n); + ItemStack itemStack = armorStand.getEquipment().getItem(EquipmentSlot.HEAD); + + Location initalLocation = getLocation(n, 1); + + for (ArmorStand armorStand1 : armorStands.values()) { + armorStand1.setCustomNameVisible(false); + } + + Vector xAxis=initalLocation.clone().toVector().subtract(generated.getPlayer().getLocation().toVector()).setY(0).normalize(); + Vector yAxis = new Vector(0, 1, 0); + Vector zAxis = xAxis.rotateAroundAxis(yAxis, -Math.PI / 2); + + + //Empiric height of a line: 0.25 + //Empiric size of a char: 0.13 + double lineHeight = 0.25; + double charSize = 0.09; + //We search the biggest line in the lore + int max=0; + for(String line:itemStack.getItemMeta().getLore()){ + if(line.length()>max) + max=line.length(); + } + + + + initalLocation.add(zAxis.clone().multiply(max*charSize/2+0.8)).add(yAxis.clone().multiply(2 + 0.125 * itemStack.getItemMeta().getLore().size())); + for (String line : itemStack.getItemMeta().getLore()) { + if (line.length() != 0) { + ArmorStand as = (ArmorStand) initalLocation.getWorld().spawnEntity(initalLocation, EntityType.ARMOR_STAND); + as.setSmall(false); + as.setMarker(true); + as.setVisible(false); + as.setCustomNameVisible(true); + as.setCustomName(line); + loreArmorStand.add(as); + } + + initalLocation.add(yAxis.clone().multiply(-lineHeight)); + + } + + double totalHeight=lineHeight*itemStack.getItemMeta().getLore().size(); + double totalLength=max*charSize; + Location topCorner=getLocation(n, 1).add(yAxis.clone().multiply(2 +totalHeight/2)); + + //We remove the items that can be in the field of vision + for(ArmorStand otherArmorStand:armorStands.values()) { + if(!otherArmorStand.equals(armorStand)) { + //Calculates the direction between the player and otherArmorStand + Vector direction=otherArmorStand.getLocation().add(new Vector(0,0.25*otherArmorStand.getHeight(),0)).subtract(generated.getPlayer().getLocation()).toVector(); + //The vector between the plan and the player. + Vector vector=initalLocation.clone().toVector().subtract(generated.getPlayer().getLocation().toVector()).setY(0); + //The intersection between 'direction' and the plane + Vector projection=generated.getPlayer().getLocation().toVector().add(xAxis.clone().multiply(vector.dot(direction))); + Vector relativeProjection=projection.subtract(topCorner.toVector()); + double z=zAxis.dot(relativeProjection); + double y=-yAxis.dot(relativeProjection); + Bukkit.broadcastMessage(otherArmorStand.getName()+" z:"+z+" y:"+y); + if(z>0&&z0&&y generated.getEditable().getInteractSensitivity()) { + as.teleport(getLocation(n, 0.75)); + zoomed = n; + displayLore(zoomed); + } + + + } + + } + } + + } + } @EventHandler diff --git a/src/main/java/net/Indyuce/mmocore/loot/droptable/DropTable.java b/src/main/java/net/Indyuce/mmocore/loot/droptable/DropTable.java index ffc90a61..4e37479d 100644 --- a/src/main/java/net/Indyuce/mmocore/loot/droptable/DropTable.java +++ b/src/main/java/net/Indyuce/mmocore/loot/droptable/DropTable.java @@ -11,6 +11,7 @@ import net.Indyuce.mmocore.loot.chest.condition.Condition; import net.Indyuce.mmocore.loot.chest.condition.ConditionInstance; import org.apache.commons.lang.Validate; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.ArmorStand; import org.bukkit.inventory.ItemStack; import net.Indyuce.mmocore.loot.LootBuilder; @@ -30,7 +31,7 @@ public class DropTable extends PostLoadObject { public DropTable(String id) { super(null); - + ArmorStand armorStand; this.id = id; }