Change how stackables holograms work

This commit is contained in:
Esophose 2019-03-01 18:49:17 -07:00
parent da8a77f2d4
commit 8c8d612dfa
4 changed files with 65 additions and 37 deletions

View File

@ -55,6 +55,7 @@ public class Block implements Listener {
this.skyblock = skyblock; this.skyblock = skyblock;
} }
@SuppressWarnings("deprecation")
@EventHandler @EventHandler
public void onBlockBreak(BlockBreakEvent event) { public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -87,20 +88,23 @@ public class Block implements Listener {
&& stackableManager.isStacked(block.getLocation())) { && stackableManager.isStacked(block.getLocation())) {
Stackable stackable = stackableManager.getStack(block.getLocation(), block.getType()); Stackable stackable = stackableManager.getStack(block.getLocation(), block.getType());
if (stackable != null) { if (stackable != null) {
Material material = block.getType();
byte data = block.getData();
int droppedAmount = 0; int droppedAmount = 0;
if (event.getPlayer().isSneaking()) { if (event.getPlayer().isSneaking()) {
Location dropLoc = event.getBlock().getLocation().clone().add(0.5, 0.5, 0.5); Location dropLoc = event.getBlock().getLocation().clone().add(0.5, 0.5, 0.5);
int count = stackable.getSize(); int count = stackable.getSize();
droppedAmount = count; droppedAmount = count;
while (count > 64) { while (count > 64) {
dropLoc.getWorld().dropItemNaturally(dropLoc, new ItemStack(event.getBlock().getType(), 64)); dropLoc.getWorld().dropItemNaturally(dropLoc, new ItemStack(material, 64, data));
count -= 64; count -= 64;
} }
dropLoc.getWorld().dropItemNaturally(dropLoc, new ItemStack(event.getBlock().getType(), count)); dropLoc.getWorld().dropItemNaturally(dropLoc, new ItemStack(material, count, block.getData()));
block.setType(Material.AIR); block.setType(Material.AIR);
stackable.setSize(0); stackable.setSize(0);
} else { } else {
block.getWorld().dropItemNaturally(block.getLocation().clone().add(.5, 1, .5), new ItemStack(block.getType())); block.getWorld().dropItemNaturally(block.getLocation().clone().add(.5, 1, .5), new ItemStack(material, 1, data));
stackable.takeOne(); stackable.takeOne();
droppedAmount = 1; droppedAmount = 1;
} }
@ -113,14 +117,11 @@ public class Block implements Listener {
FileConfiguration configLoad = config.getFileConfiguration(); FileConfiguration configLoad = config.getFileConfiguration();
if (configLoad.getBoolean("Island.Block.Level.Enable")) { if (configLoad.getBoolean("Island.Block.Level.Enable")) {
Materials materials = Materials.getMaterials(block.getType(), block.getData()); Materials materials = Materials.getMaterials(material, data);
if (materials != null) { if (materials != null) {
IslandLevel level = island.getLevel(); IslandLevel level = island.getLevel();
if (level.hasMaterial(materials.name())) { if (level.hasMaterial(materials.name())) {
int materialAmount = level.getMaterialAmount(materials.name()); int materialAmount = level.getMaterialAmount(materials.name());
if (materialAmount - droppedAmount <= 0) { if (materialAmount - droppedAmount <= 0) {
@ -154,7 +155,6 @@ public class Block implements Listener {
if (event.isCancelled() || !configLoad.getBoolean("Island.Block.Level.Enable")) return; if (event.isCancelled() || !configLoad.getBoolean("Island.Block.Level.Enable")) return;
@SuppressWarnings("deprecation")
Materials materials = Materials.getMaterials(block.getType(), block.getData()); Materials materials = Materials.getMaterials(block.getType(), block.getData());
if (materials == null) return; if (materials == null) return;

View File

@ -3,18 +3,15 @@ package me.goodandevil.skyblock.listeners;
import java.io.File; import java.io.File;
import java.util.Set; import java.util.Set;
import me.goodandevil.skyblock.config.FileManager;
import me.goodandevil.skyblock.island.Island;
import me.goodandevil.skyblock.island.IslandLevel;
import me.goodandevil.skyblock.stackable.Stackable;
import me.goodandevil.skyblock.stackable.StackableManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.*; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.StorageMinecart; import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -28,9 +25,14 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import me.goodandevil.skyblock.SkyBlock; import me.goodandevil.skyblock.SkyBlock;
import me.goodandevil.skyblock.config.FileManager;
import me.goodandevil.skyblock.island.Island;
import me.goodandevil.skyblock.island.IslandLevel;
import me.goodandevil.skyblock.island.IslandManager;
import me.goodandevil.skyblock.message.MessageManager; import me.goodandevil.skyblock.message.MessageManager;
import me.goodandevil.skyblock.sound.SoundManager; import me.goodandevil.skyblock.sound.SoundManager;
import me.goodandevil.skyblock.island.IslandManager; import me.goodandevil.skyblock.stackable.Stackable;
import me.goodandevil.skyblock.stackable.StackableManager;
import me.goodandevil.skyblock.utils.item.InventoryUtil; import me.goodandevil.skyblock.utils.item.InventoryUtil;
import me.goodandevil.skyblock.utils.structure.StructureUtil; import me.goodandevil.skyblock.utils.structure.StructureUtil;
import me.goodandevil.skyblock.utils.version.Materials; import me.goodandevil.skyblock.utils.version.Materials;
@ -95,7 +97,6 @@ public class Interact implements Listener {
} }
level.setMaterialAmount(materials.name(), materialAmount + 1); level.setMaterialAmount(materials.name(), materialAmount + 1);
} }
if (block.getType() == Material.ANVIL) { if (block.getType() == Material.ANVIL) {
if (!islandManager.hasPermission(player, block.getLocation(), "Anvil")) { if (!islandManager.hasPermission(player, block.getLocation(), "Anvil")) {

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.lang3.text.WordUtils; import org.apache.commons.lang3.text.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -25,14 +26,15 @@ public class Stackable {
private Location location; private Location location;
private Material material; private Material material;
private Integer size = 2; private Integer size = 2;
private ArmorStand display;
public Stackable(Location location, Material material) { public Stackable(Location location, Material material) {
this.uuid = UUID.randomUUID(); this.uuid = UUID.randomUUID();
this.location = location; this.location = location;
this.material = material; this.material = material;
updateDisplay(); this.updateDisplay();
SkyBlock.getInstance().getSoundManager().playSound(location, Sounds.ANVIL_LAND.bukkitSound(), 1.0F, 1.0F); SkyBlock.getInstance().getSoundManager().playSound(location, Sounds.ANVIL_LAND.bukkitSound(), 1.0F, 1.0F);
save(); this.save();
} }
public Stackable(UUID uuid, Location location, Material material, int size) { public Stackable(UUID uuid, Location location, Material material, int size) {
@ -40,7 +42,7 @@ public class Stackable {
this.location = location; this.location = location;
this.material = material; this.material = material;
this.size = size; this.size = size;
updateDisplay(); this.updateDisplay();
} }
public UUID getUuid() { public UUID getUuid() {
@ -61,7 +63,7 @@ public class Stackable {
public void setMaterial(Material material) { public void setMaterial(Material material) {
this.material = material; this.material = material;
save(); this.save();
} }
public Integer getSize() { public Integer getSize() {
@ -70,29 +72,43 @@ public class Stackable {
public void setSize(Integer size) { public void setSize(Integer size) {
this.size = size; this.size = size;
updateDisplay(); this.updateDisplay();
save(); this.save();
} }
public void addOne() { public void addOne() {
this.size ++; this.size ++;
updateDisplay(); this.updateDisplay();
SkyBlock.getInstance().getSoundManager().playSound(location, Sounds.LEVEL_UP.bukkitSound(), 1.0F, 1.0F); SkyBlock.getInstance().getSoundManager().playSound(location, Sounds.LEVEL_UP.bukkitSound(), 1.0F, 1.0F);
save(); this.save();
} }
public void takeOne() { public void takeOne() {
this.size --; this.size --;
updateDisplay(); this.updateDisplay();
SkyBlock.getInstance().getSoundManager().playSound(location, Sounds.ARROW_HIT.bukkitSound(), 1.0F, 1.0F); SkyBlock.getInstance().getSoundManager().playSound(location, Sounds.ARROW_HIT.bukkitSound(), 1.0F, 1.0F);
save(); this.save();
} }
private void updateDisplay() { private void updateDisplay() {
removeDisplay(); if (this.size > 1) {
Location dropLocation = location.clone().add(.5,1,.5); if (this.display != null) {
this.display.setCustomName(WordUtils.capitalize(material.name().toLowerCase()).replace("_", " ") + "s: " + size);
ArmorStand as = (ArmorStand) location.getWorld().spawnEntity(dropLocation, EntityType.ARMOR_STAND); } else {
if (!this.findExistingDisplay()) {
this.createDisplay();
}
this.updateDisplay();
}
} else {
this.removeDisplay();
}
}
private void createDisplay() {
Location initialLocation = location.clone().add(0.5, -1, 0.5);
Location dropLocation = location.clone().add(0.5, 1, 0.5);
ArmorStand as = (ArmorStand) location.getWorld().spawnEntity(initialLocation, EntityType.ARMOR_STAND);
as.setVisible(false); as.setVisible(false);
as.setGravity(false); as.setGravity(false);
as.setSmall(true); as.setSmall(true);
@ -103,13 +119,25 @@ public class Stackable {
as.setHelmet(new ItemStack(material)); as.setHelmet(new ItemStack(material));
as.setCustomName(WordUtils.capitalize(material.name().toLowerCase()).replace("_", " ") + "s: " + size); as.setCustomName(WordUtils.capitalize(material.name().toLowerCase()).replace("_", " ") + "s: " + size);
as.setCustomNameVisible(true); as.setCustomNameVisible(true);
this.display = as;
Bukkit.getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> {
this.display.teleport(dropLocation);
});
}
private boolean findExistingDisplay() {
for (Entity entity : this.location.getWorld().getNearbyEntities(this.location.clone().add(0.5, 0.55, 0.5), 0.1, 0.5, 0.1)) {
if (entity instanceof ArmorStand) {
this.display = (ArmorStand) entity;
return true;
}
}
return false;
} }
void removeDisplay() { private void removeDisplay() {
for (Entity entity : location.getWorld().getNearbyEntities(getLocation().add(.5,.55,.5), .1,.5,.1)) { if (this.display != null) {
if (entity.getType() != EntityType.ARMOR_STAND) continue; this.display.remove();
entity.remove();
} }
} }

View File

@ -96,7 +96,6 @@ public class StackableManager {
public void removeStack(Stackable stackable) { public void removeStack(Stackable stackable) {
stackable.setSize(0); stackable.setSize(0);
stackable.removeDisplay();
stacks.remove(stackable.getLocation()); stacks.remove(stackable.getLocation());
} }
} }