Merge pull request #53 from D3v1s0m/2.0.0

task fix, load fix, temp bug fix, hologram offset
This commit is contained in:
Pyr 2023-05-21 09:24:27 +01:00 committed by GitHub
commit cd03388551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 106 additions and 15 deletions

@ -8,6 +8,7 @@ import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder
import lol.pyr.director.adventure.command.CommandManager; import lol.pyr.director.adventure.command.CommandManager;
import lol.pyr.director.adventure.command.MultiCommand; import lol.pyr.director.adventure.command.MultiCommand;
import lol.pyr.director.adventure.parse.primitive.BooleanParser; import lol.pyr.director.adventure.parse.primitive.BooleanParser;
import lol.pyr.director.adventure.parse.primitive.DoubleParser;
import lol.pyr.director.adventure.parse.primitive.IntegerParser; import lol.pyr.director.adventure.parse.primitive.IntegerParser;
import lol.pyr.znpcsplus.api.ZApi; import lol.pyr.znpcsplus.api.ZApi;
import lol.pyr.znpcsplus.api.ZApiProvider; import lol.pyr.znpcsplus.api.ZApiProvider;
@ -110,10 +111,6 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
log(ChatColor.WHITE + " * Initializing Adventure..."); log(ChatColor.WHITE + " * Initializing Adventure...");
adventure = BukkitAudiences.create(this); adventure = BukkitAudiences.create(this);
log(ChatColor.WHITE + " * Initializing PacketEvents...");
packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry), PacketListenerPriority.MONITOR);
packetEvents.init();
metadataFactory = setupMetadataFactory(); metadataFactory = setupMetadataFactory();
PacketFactory packetFactory = setupPacketFactory(); PacketFactory packetFactory = setupPacketFactory();
@ -125,20 +122,15 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
log(ChatColor.WHITE + " * Defining NPC types..."); log(ChatColor.WHITE + " * Defining NPC types...");
NpcTypeImpl.defineTypes(); NpcTypeImpl.defineTypes();
log(ChatColor.WHITE + " * Starting tasks & registering components..."); log(ChatColor.WHITE + " * Registering components...");
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
new Metrics(this, PLUGIN_ID); new Metrics(this, PLUGIN_ID);
scheduler = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this); scheduler = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this);
BungeeUtil bungeeUtil = new BungeeUtil(this); BungeeUtil bungeeUtil = new BungeeUtil(this);
userManager = new UserManager(); userManager = new UserManager();
Bukkit.getOnlinePlayers().forEach(userManager::get); Bukkit.getOnlinePlayers().forEach(userManager::get);
pluginManager.registerEvents(new UserListener(userManager), this); pluginManager.registerEvents(new UserListener(userManager), this);
scheduler.runDelayedTimerAsync(new NpcVisibilityTask(npcRegistry, configManager), 60L, 10L);
skinCache = new SkinCache(configManager);
scheduler.runDelayedTimerAsync(new SkinCacheCleanTask(skinCache), 1200, 1200);
registerCommands();
if (configManager.getConfig().checkForUpdates()) { if (configManager.getConfig().checkForUpdates()) {
UpdateChecker updateChecker = new UpdateChecker(this.getDescription()); UpdateChecker updateChecker = new UpdateChecker(this.getDescription());
@ -151,6 +143,18 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
npcRegistry = new NpcRegistryImpl(configManager, this, packetFactory, actionRegistry); npcRegistry = new NpcRegistryImpl(configManager, this, packetFactory, actionRegistry);
npcRegistry.reload(); npcRegistry.reload();
log(ChatColor.WHITE + " * Initializing PacketEvents...");
packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry), PacketListenerPriority.MONITOR);
packetEvents.init();
log(ChatColor.WHITE + " * Starting tasks...");
scheduler.runDelayedTimerAsync(new NpcVisibilityTask(npcRegistry, configManager), 60L, 10L);
skinCache = new SkinCache(configManager);
scheduler.runDelayedTimerAsync(new SkinCacheCleanTask(skinCache), 1200, 1200);
log(ChatColor.WHITE + " * Registering commands...");
registerCommands();
ZApiProvider.register(this); ZApiProvider.register(this);
enabled = true; enabled = true;
log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)"); log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)");
@ -212,6 +216,7 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
@Override @Override
public void onDisable() { public void onDisable() {
if (!enabled) return; if (!enabled) return;
scheduler.cancelAll();
npcRegistry.save(); npcRegistry.save();
ZApiProvider.unregister(); ZApiProvider.unregister();
Bukkit.getOnlinePlayers().forEach(userManager::remove); Bukkit.getOnlinePlayers().forEach(userManager::remove);
@ -227,6 +232,7 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
manager.registerParser(NpcEntryImpl.class, new NpcEntryParser(npcRegistry, context -> {})); manager.registerParser(NpcEntryImpl.class, new NpcEntryParser(npcRegistry, context -> {}));
manager.registerParser(EntityPropertyImpl.class, new EntityPropertyParser(context -> {})); manager.registerParser(EntityPropertyImpl.class, new EntityPropertyParser(context -> {}));
manager.registerParser(Integer.class, new IntegerParser(context -> {})); manager.registerParser(Integer.class, new IntegerParser(context -> {}));
manager.registerParser(Double.class, new DoubleParser(context -> {}));
manager.registerParser(Boolean.class, new BooleanParser(context -> {})); manager.registerParser(Boolean.class, new BooleanParser(context -> {}));
manager.registerParser(NamedTextColor.class, new NamedTextColorParser(context -> {})); manager.registerParser(NamedTextColor.class, new NamedTextColorParser(context -> {}));
@ -250,7 +256,7 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
.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)))
); );
} }

@ -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 HoloOffsetCommand implements CommandHandler {
private final NpcRegistryImpl npcRegistry;
public HoloOffsetCommand(NpcRegistryImpl npcRegistry) {
this.npcRegistry = npcRegistry;
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " holo offset <id> <offset>");
HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram();
double offset = context.parse(Double.class);
hologram.setOffset(offset);
context.send("NPC hologram offset set!");
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.modifiableIds());
if (context.argSize() == 2) {
HologramImpl hologram = context.suggestionParse(0, NpcEntryImpl.class).getNpc().getHologram();
return context.suggestLiteral(String.valueOf(hologram.getOffset()));
}
return Collections.emptyList();
}
}

@ -16,6 +16,7 @@ public class HologramImpl extends Viewable implements Hologram {
private final ConfigManager configManager; private final ConfigManager configManager;
private final PacketFactory packetFactory; private final PacketFactory packetFactory;
private double offset = 0.0;
private ZLocation location; private ZLocation location;
private final List<HologramLine> lines = new ArrayList<>(); private final List<HologramLine> lines = new ArrayList<>();
@ -79,10 +80,19 @@ public class HologramImpl extends Viewable implements Hologram {
private void relocateLines(HologramLine newLine) { private void relocateLines(HologramLine newLine) {
final double lineSpacing = configManager.getConfig().lineSpacing(); final double lineSpacing = configManager.getConfig().lineSpacing();
double height = location.getY() + (lines.size() - 1) * lineSpacing; double height = location.getY() + (lines.size() - 1) * lineSpacing + getOffset();
for (HologramLine line : lines) { for (HologramLine line : lines) {
line.setLocation(location.withY(height), line == newLine ? Collections.emptySet() : getViewers()); line.setLocation(location.withY(height), line == newLine ? Collections.emptySet() : getViewers());
height -= lineSpacing; height -= lineSpacing;
} }
} }
public void setOffset(double offset) {
this.offset = offset;
relocateLines();
}
public double getOffset() {
return offset;
}
} }

@ -113,6 +113,7 @@ public class NpcImpl extends Viewable implements Npc {
} }
public <T> void setProperty(EntityPropertyImpl<T> key, T value) { public <T> void setProperty(EntityPropertyImpl<T> key, T value) {
if (value == null) return;
if (value.equals(key.getDefaultValue())) removeProperty(key); if (value.equals(key.getDefaultValue())) removeProperty(key);
else propertyMap.put(key, value); else propertyMap.put(key, value);
_refreshMeta(); _refreshMeta();

@ -117,6 +117,18 @@ public final class Reflections {
.setStrict(FoliaUtil.isFolia()) .setStrict(FoliaUtil.isFolia())
.toMethodReflection(); .toMethodReflection();
public static final ReflectionLazyLoader<Method> FOLIA_CANCEL_ASYNC_TASKS =
new ReflectionBuilder(ASYNC_SCHEDULER_CLASS)
.withMethodName("cancelTasks")
.setStrict(FoliaUtil.isFolia())
.toMethodReflection();
public static final ReflectionLazyLoader<Method> FOLIA_CANCEL_GLOBAL_TASKS =
new ReflectionBuilder(GLOBAL_REGION_SCHEDULER_CLASS)
.withMethodName("cancelTasks")
.setStrict(FoliaUtil.isFolia())
.toMethodReflection();
public static final ReflectionLazyLoader<Method> FOLIA_TELEPORT_ASYNC = public static final ReflectionLazyLoader<Method> FOLIA_TELEPORT_ASYNC =
new ReflectionBuilder(Entity.class) new ReflectionBuilder(Entity.class)
.withMethodName("teleportAsync") .withMethodName("teleportAsync")

@ -41,5 +41,21 @@ public class FoliaScheduler extends TaskScheduler {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override
public void cancelAll() {
try {
Object asyncScheduler = Reflections.FOLIA_GET_ASYNC_SCHEDULER.get().invoke(null);
Reflections.FOLIA_CANCEL_ASYNC_TASKS.get().invoke(asyncScheduler, plugin);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
try {
Object globalScheduler = Reflections.FOLIA_GET_GLOBAL_REGION_SCHEDULER.get().invoke(null);
Reflections.FOLIA_CANCEL_GLOBAL_TASKS.get().invoke(globalScheduler, plugin);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
} }

@ -22,4 +22,9 @@ public class SpigotScheduler extends TaskScheduler {
public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) { public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) {
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, ticks); Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, ticks);
} }
@Override
public void cancelAll() {
Bukkit.getScheduler().cancelTasks(plugin);
}
} }

@ -14,4 +14,6 @@ public abstract class TaskScheduler {
public abstract void runLaterAsync(Runnable runnable, long ticks); public abstract void runLaterAsync(Runnable runnable, long ticks);
public abstract void runDelayedTimerAsync(Runnable runnable, long delay, long ticks); public abstract void runDelayedTimerAsync(Runnable runnable, long delay, long ticks);
public abstract void cancelAll();
} }

@ -51,8 +51,8 @@ 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));
for (String line : config.getStringList("hologram")) npc.getHologram().addLine(MiniMessage.miniMessage().deserialize(line)); for (String line : config.getStringList("hologram.lines")) npc.getHologram().addLine(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);
@ -84,11 +84,12 @@ 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());
List<String> lines = new ArrayList<>(); List<String> lines = new ArrayList<>();
for (HologramLine line : npc.getHologram().getLines()) { for (HologramLine line : npc.getHologram().getLines()) {
lines.add(MiniMessage.miniMessage().serialize(line.getText())); lines.add(MiniMessage.miniMessage().serialize(line.getText()));
} }
config.set("hologram", lines); config.set("hologram.lines", lines);
config.set("actions", npc.getActions().stream() config.set("actions", npc.getActions().stream()
.map(actionRegistry::serialize) .map(actionRegistry::serialize)
.filter(Objects::nonNull) .filter(Objects::nonNull)