add hologram refreshing
This commit is contained in:
parent
8ef9a5fc93
commit
0d02fc806b
6 changed files with 96 additions and 5 deletions
|
@ -38,6 +38,7 @@ import lol.pyr.znpcsplus.scheduling.SpigotScheduler;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
import lol.pyr.znpcsplus.skin.cache.SkinCache;
|
import lol.pyr.znpcsplus.skin.cache.SkinCache;
|
||||||
import lol.pyr.znpcsplus.skin.cache.SkinCacheCleanTask;
|
import lol.pyr.znpcsplus.skin.cache.SkinCacheCleanTask;
|
||||||
|
import lol.pyr.znpcsplus.tasks.HologramRefreshTask;
|
||||||
import lol.pyr.znpcsplus.tasks.NpcProcessorTask;
|
import lol.pyr.znpcsplus.tasks.NpcProcessorTask;
|
||||||
import lol.pyr.znpcsplus.updater.UpdateChecker;
|
import lol.pyr.znpcsplus.updater.UpdateChecker;
|
||||||
import lol.pyr.znpcsplus.updater.UpdateNotificationListener;
|
import lol.pyr.znpcsplus.updater.UpdateNotificationListener;
|
||||||
|
@ -162,6 +163,7 @@ public class ZNpcsPlus extends JavaPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduler.runDelayedTimerAsync(new NpcProcessorTask(npcRegistry, configManager, propertyRegistry), 60L, 3L);
|
scheduler.runDelayedTimerAsync(new NpcProcessorTask(npcRegistry, configManager, propertyRegistry), 60L, 3L);
|
||||||
|
scheduler.runDelayedTimerAsync(new HologramRefreshTask(npcRegistry), 60L, 20L);
|
||||||
scheduler.runDelayedTimerAsync(new SkinCacheCleanTask(skinCache), 1200, 1200);
|
scheduler.runDelayedTimerAsync(new SkinCacheCleanTask(skinCache), 1200, 1200);
|
||||||
|
|
||||||
log(ChatColor.WHITE + " * Loading data...");
|
log(ChatColor.WHITE + " * Loading data...");
|
||||||
|
@ -294,7 +296,8 @@ public class ZNpcsPlus extends JavaPlugin {
|
||||||
.addSubcommand("info", new HoloInfoCommand(npcRegistry))
|
.addSubcommand("info", new HoloInfoCommand(npcRegistry))
|
||||||
.addSubcommand("insert", new HoloInsertCommand(npcRegistry, textSerializer))
|
.addSubcommand("insert", new HoloInsertCommand(npcRegistry, textSerializer))
|
||||||
.addSubcommand("set", new HoloSetCommand(npcRegistry, textSerializer))
|
.addSubcommand("set", new HoloSetCommand(npcRegistry, textSerializer))
|
||||||
.addSubcommand("offset", new HoloOffsetCommand(npcRegistry)))
|
.addSubcommand("offset", new HoloOffsetCommand(npcRegistry))
|
||||||
|
.addSubcommand("refreshdelay", new HoloRefreshDelayCommand(npcRegistry)))
|
||||||
.addSubcommand("action", new MultiCommand(loadHelpMessage("action"))
|
.addSubcommand("action", new MultiCommand(loadHelpMessage("action"))
|
||||||
.addSubcommand("add", new ActionAddCommand(npcRegistry, actionRegistry))
|
.addSubcommand("add", new ActionAddCommand(npcRegistry, actionRegistry))
|
||||||
.addSubcommand("delete", new ActionDeleteCommand(npcRegistry))
|
.addSubcommand("delete", new ActionDeleteCommand(npcRegistry))
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package lol.pyr.znpcsplus.commands.hologram;
|
||||||
|
|
||||||
|
import lol.pyr.director.adventure.command.CommandContext;
|
||||||
|
import lol.pyr.director.adventure.command.CommandHandler;
|
||||||
|
import lol.pyr.director.common.command.CommandExecutionException;
|
||||||
|
import lol.pyr.znpcsplus.hologram.HologramImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class HoloRefreshDelayCommand implements CommandHandler {
|
||||||
|
private final NpcRegistryImpl npcRegistry;
|
||||||
|
|
||||||
|
public HoloRefreshDelayCommand(NpcRegistryImpl npcRegistry) {
|
||||||
|
this.npcRegistry = npcRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
context.setUsage(context.getLabel() + " holo refreshdelay <id> <delay>");
|
||||||
|
HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram();
|
||||||
|
double delay = context.parse(Double.class);
|
||||||
|
hologram.setRefreshDelay((long) (delay * 1000));
|
||||||
|
context.send("NPC refresh delay set!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
|
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
||||||
|
if (context.argSize() == 2) {
|
||||||
|
HologramImpl hologram = context.suggestionParse(0, NpcEntryImpl.class).getNpc().getHologram();
|
||||||
|
return context.suggestLiteral(String.valueOf(((double) hologram.getRefreshDelay()) / 1000));
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,8 @@ public class HologramImpl extends Viewable implements Hologram {
|
||||||
private final LegacyComponentSerializer textSerializer;
|
private final LegacyComponentSerializer textSerializer;
|
||||||
|
|
||||||
private double offset = 0.0;
|
private double offset = 0.0;
|
||||||
|
private long refreshDelay = -1;
|
||||||
|
private long lastRefresh = System.currentTimeMillis();
|
||||||
private NpcLocation location;
|
private NpcLocation location;
|
||||||
private final List<HologramLine> lines = new ArrayList<>();
|
private final List<HologramLine> lines = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -84,6 +86,23 @@ public class HologramImpl extends Viewable implements Hologram {
|
||||||
for (HologramLine line : lines) line.hide(player);
|
for (HologramLine line : lines) line.hide(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getRefreshDelay() {
|
||||||
|
return refreshDelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRefreshDelay(long refreshDelay) {
|
||||||
|
this.refreshDelay = refreshDelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean shouldRefresh() {
|
||||||
|
return refreshDelay != -1 && (System.currentTimeMillis() - lastRefresh) > refreshDelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void refresh() {
|
||||||
|
lastRefresh = System.currentTimeMillis();
|
||||||
|
for (HologramLine line : lines) for (Player viewer : getViewers()) line.refreshMeta(viewer);
|
||||||
|
}
|
||||||
|
|
||||||
public void setLocation(NpcLocation location) {
|
public void setLocation(NpcLocation location) {
|
||||||
this.location = location;
|
this.location = location;
|
||||||
relocateLines();
|
relocateLines();
|
||||||
|
|
|
@ -28,6 +28,10 @@ public class HologramLine implements PropertyHolder {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void refreshMeta(Player player) {
|
||||||
|
armorStand.refreshMeta(player);
|
||||||
|
}
|
||||||
|
|
||||||
protected void show(Player player) {
|
protected void show(Player player) {
|
||||||
armorStand.spawn(player);
|
armorStand.spawn(player);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.storage.yaml;
|
||||||
import lol.pyr.znpcsplus.config.ConfigManager;
|
import lol.pyr.znpcsplus.config.ConfigManager;
|
||||||
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
|
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
|
||||||
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
|
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
|
||||||
|
import lol.pyr.znpcsplus.hologram.HologramImpl;
|
||||||
import lol.pyr.znpcsplus.hologram.HologramLine;
|
import lol.pyr.znpcsplus.hologram.HologramLine;
|
||||||
import lol.pyr.znpcsplus.interaction.ActionRegistry;
|
import lol.pyr.znpcsplus.interaction.ActionRegistry;
|
||||||
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
|
@ -61,8 +62,10 @@ public class YamlStorage implements NpcStorage {
|
||||||
npc.UNSAFE_setProperty(property, property.deserialize(properties.getString(key)));
|
npc.UNSAFE_setProperty(property, property.deserialize(properties.getString(key)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
npc.getHologram().setOffset(config.getDouble("hologram.offset", 0.0));
|
HologramImpl hologram = npc.getHologram();
|
||||||
for (String line : config.getStringList("hologram.lines")) npc.getHologram().addLineComponent(MiniMessage.miniMessage().deserialize(line));
|
hologram.setOffset(config.getDouble("hologram.offset", 0.0));
|
||||||
|
hologram.setRefreshDelay(config.getLong("hologram.refresh-delay", -1));
|
||||||
|
for (String line : config.getStringList("hologram.lines")) hologram.addLineComponent(MiniMessage.miniMessage().deserialize(line));
|
||||||
for (String s : config.getStringList("actions")) npc.addAction(actionRegistry.deserialize(s));
|
for (String s : config.getStringList("actions")) npc.addAction(actionRegistry.deserialize(s));
|
||||||
|
|
||||||
NpcEntryImpl entry = new NpcEntryImpl(config.getString("id"), npc);
|
NpcEntryImpl entry = new NpcEntryImpl(config.getString("id"), npc);
|
||||||
|
@ -96,9 +99,11 @@ public class YamlStorage implements NpcStorage {
|
||||||
config.set("properties." + property.getName(), property.serialize(npc));
|
config.set("properties." + property.getName(), property.serialize(npc));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (npc.getHologram().getOffset() != 0.0) config.set("hologram.offset", npc.getHologram().getOffset());
|
HologramImpl hologram = npc.getHologram();
|
||||||
|
if (hologram.getOffset() != 0.0) config.set("hologram.offset", hologram.getOffset());
|
||||||
|
if (hologram.getRefreshDelay() != -1) config.set("hologram.refresh-delay", hologram.getRefreshDelay());
|
||||||
List<String> lines = new ArrayList<>(npc.getHologram().getLines().size());
|
List<String> lines = new ArrayList<>(npc.getHologram().getLines().size());
|
||||||
for (HologramLine line : npc.getHologram().getLines()) {
|
for (HologramLine line : hologram.getLines()) {
|
||||||
lines.add(MiniMessage.miniMessage().serialize(line.getText()));
|
lines.add(MiniMessage.miniMessage().serialize(line.getText()));
|
||||||
}
|
}
|
||||||
config.set("hologram.lines", lines);
|
config.set("hologram.lines", lines);
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package lol.pyr.znpcsplus.tasks;
|
||||||
|
|
||||||
|
import lol.pyr.znpcsplus.hologram.HologramImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
public class HologramRefreshTask extends BukkitRunnable {
|
||||||
|
private final NpcRegistryImpl npcRegistry;
|
||||||
|
|
||||||
|
public HologramRefreshTask(NpcRegistryImpl npcRegistry) {
|
||||||
|
this.npcRegistry = npcRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (NpcEntryImpl entry : npcRegistry.getProcessable()) {
|
||||||
|
HologramImpl hologram = entry.getNpc().getHologram();
|
||||||
|
if (hologram.shouldRefresh()) hologram.refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue