Update for the new HologramManager changes.

This commit is contained in:
Fernando Pettinelli 2021-12-20 19:34:57 -03:00
parent 3b25d337a4
commit d601049117
4 changed files with 73 additions and 47 deletions

View File

@ -226,6 +226,7 @@ public class EpicFurnaces extends SongodaPlugin {
} }
this.dataManager.getFurnaces((furnaces) -> { this.dataManager.getFurnaces((furnaces) -> {
furnaces.values().forEach(Furnace::createHologram);
this.furnaceManager.addFurnaces(furnaces.values()); this.furnaceManager.addFurnaces(furnaces.values());
this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts)); this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts));
}); });
@ -266,68 +267,76 @@ public class EpicFurnaces extends SongodaPlugin {
} }
public void clearHologram(Furnace furnace) { public void clearHologram(Furnace furnace) {
HologramManager.removeHologram(furnace.getLocation().add(0, .15, 0)); furnace.removeHologram();
} }
public void updateHolograms(Collection<Furnace> furnaces) { public void updateHolograms(Collection<Furnace> furnaces) {
// are holograms enabled? // are holograms enabled?
if (!Settings.HOLOGRAMS.getBoolean() || !HologramManager.getManager().isEnabled()) return; if (!Settings.HOLOGRAMS.getBoolean() || !HologramManager.getManager().isEnabled()) return;
Map<Location, List<String>> holograms = new HashMap<>(furnaces.size()); Map<String, List<String>> holograms = new HashMap<>(furnaces.size());
for (Furnace furnace : furnaces) { for (Furnace furnace : furnaces) {
// don't try to load furnaces in chunks that aren't loaded // don't try to load furnaces in chunks that aren't loaded
if (!furnace.isInLoadedChunk()) continue; if (!furnace.isInLoadedChunk()) continue;
BlockState state = furnace.getLocation().getBlock().getState(); if (!HologramManager.isHologramLoaded(furnace.getHologramId())) {
continue;
// verify that this is a furnace
if (!(state instanceof org.bukkit.block.Furnace)) continue;
org.bukkit.block.Furnace furnaceBlock = ((org.bukkit.block.Furnace) state);
int performance = (furnaceBlock.getCookTime() - furnace.getPerformanceTotal(furnaceBlock.getType())) <= 0 ? 0 : furnace.getPerformanceTotal(furnaceBlock.getType());
float percent = (float) (furnaceBlock.getCookTime() - performance) / (200 - performance);
int progressBars = (int) (6 * percent) + (percent == 0 ? 0 : 1);
int leftOver = (6 - progressBars);
String progress;
if (furnaceBlock.getInventory().getFuel() != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < progressBars; i++) {
sb.append("&a=");
}
for (int i = 0; i < leftOver; i++) {
sb.append("&c=");
}
progress = Methods.formatText(sb.toString());
} else {
progress = getLocale().getMessage("general.hologram.outoffuel").getMessage();
} }
int inAmt = 0; holograms.put(furnace.getHologramId(), getHologramLines(furnace));
int outAmt = 0;
if (furnaceBlock.getInventory().getSmelting() != null) {
inAmt = furnaceBlock.getInventory().getSmelting().getAmount();
}
if (furnaceBlock.getInventory().getResult() != null) {
outAmt = furnaceBlock.getInventory().getResult().getAmount();
}
String stats = getLocale().getMessage("general.hologram.stats")
.processPlaceholder("in", inAmt)
.processPlaceholder("out", Math.min(outAmt, 64)).getMessage();
holograms.put(furnace.getLocation().add(0, .15, 0), Arrays.asList(progress, stats));
} }
// Update holograms // Update holograms
HologramManager.bulkUpdateHolograms(holograms); HologramManager.bulkUpdateHolograms(holograms);
} }
public List<String> getHologramLines(Furnace furnace) {
BlockState state = furnace.getLocation().getBlock().getState();
// verify that this is a furnace
if (!(state instanceof org.bukkit.block.Furnace)) return Collections.emptyList();
org.bukkit.block.Furnace furnaceBlock = ((org.bukkit.block.Furnace) state);
int performance = (furnaceBlock.getCookTime() - furnace.getPerformanceTotal(furnaceBlock.getType())) <= 0 ? 0 : furnace.getPerformanceTotal(furnaceBlock.getType());
float percent = (float) (furnaceBlock.getCookTime() - performance) / (200 - performance);
int progressBars = (int) (6 * percent) + (percent == 0 ? 0 : 1);
int leftOver = (6 - progressBars);
String progress;
if (furnaceBlock.getInventory().getFuel() != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < progressBars; i++) {
sb.append("&a=");
}
for (int i = 0; i < leftOver; i++) {
sb.append("&c=");
}
progress = Methods.formatText(sb.toString());
} else {
progress = getLocale().getMessage("general.hologram.outoffuel").getMessage();
}
int inAmt = 0;
int outAmt = 0;
if (furnaceBlock.getInventory().getSmelting() != null) {
inAmt = furnaceBlock.getInventory().getSmelting().getAmount();
}
if (furnaceBlock.getInventory().getResult() != null) {
outAmt = furnaceBlock.getInventory().getResult().getAmount();
}
String stats = getLocale().getMessage("general.hologram.stats")
.processPlaceholder("in", inAmt)
.processPlaceholder("out", Math.min(outAmt, 64)).getMessage();
return Arrays.asList(progress, stats);
}
private void loadLevelManager() { private void loadLevelManager() {
if (!levelsFile.getFile().exists()) if (!levelsFile.getFile().exists())
this.saveResource("levels.yml", false); this.saveResource("levels.yml", false);

View File

@ -4,6 +4,7 @@ import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion; import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.gui.GuiManager; import com.songoda.core.gui.GuiManager;
import com.songoda.core.hooks.EconomyManager; import com.songoda.core.hooks.EconomyManager;
import com.songoda.core.hooks.HologramManager;
import com.songoda.core.math.MathUtils; import com.songoda.core.math.MathUtils;
import com.songoda.core.hooks.ProtectionManager; import com.songoda.core.hooks.ProtectionManager;
import com.songoda.epicfurnaces.EpicFurnaces; import com.songoda.epicfurnaces.EpicFurnaces;
@ -42,6 +43,11 @@ public class Furnace {
private final EpicFurnaces plugin = EpicFurnaces.getInstance(); private final EpicFurnaces plugin = EpicFurnaces.getInstance();
// This is the unique identifier for this furnace.
// It is reset on every plugin load.
// Used for holograms.
private final UUID uniqueId = UUID.randomUUID();
// Identifier for database use. // Identifier for database use.
private int id; private int id;
@ -402,4 +408,16 @@ public class Furnace {
if (result != null) if (result != null)
world.dropItemNaturally(location, result); world.dropItemNaturally(location, result);
} }
public String getHologramId() {
return "EpicFurnaces-" + uniqueId;
}
public void createHologram() {
HologramManager.createHologram(getHologramId(), getLocation().add(0, 0.15, 0), plugin.getHologramLines(this));
}
public void removeHologram() {
HologramManager.removeHologram(getHologramId());
}
} }

View File

@ -18,7 +18,6 @@ import org.bukkit.event.block.BlockFormEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -90,10 +89,10 @@ public class BlockListeners implements Listener {
.setPlacedBy(event.getPlayer().getUniqueId()).build() .setPlacedBy(event.getPlayer().getUniqueId()).build()
: new FurnaceBuilder(location).setPlacedBy(event.getPlayer().getUniqueId()).build(); : new FurnaceBuilder(location).setPlacedBy(event.getPlayer().getUniqueId()).build();
plugin.getFurnaceManager().addFurnace(furnace);
plugin.getDataManager().createFurnace(furnace); plugin.getDataManager().createFurnace(furnace);
plugin.getFurnaceManager().addFurnace(furnace);
plugin.updateHolograms(Collections.singleton(furnace)); furnace.createHologram();
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)

View File

@ -1,7 +1,7 @@
name: EpicFurnaces name: EpicFurnaces
description: EpicFurnaces description: EpicFurnaces
version: maven-version-number version: maven-version-number
softdepend: [ FabledSkyBlock, HolographicDisplays, PlotSquared, GriefPrevention, USkyBlock, SkyBlock, WorldGuard, Factions, Lands, RedProtect, UltimateClaims, BentoBox, Vault ] softdepend: [ FabledSkyBlock, HolographicDisplays, PlotSquared, GriefPrevention, USkyBlock, SkyBlock, WorldGuard, Factions, Lands, RedProtect, UltimateClaims, BentoBox, Vault, DecentHolograms ]
main: com.songoda.epicfurnaces.EpicFurnaces main: com.songoda.epicfurnaces.EpicFurnaces
author: songoda author: songoda
api-version: 1.13 api-version: 1.13