Merge pull request #53 from D3v1s0m/2.0.0
task fix, load fix, temp bug fix, hologram offset
This commit is contained in:
commit
cd03388551
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.MultiCommand;
|
||||
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.znpcsplus.api.ZApi;
|
||||
import lol.pyr.znpcsplus.api.ZApiProvider;
|
||||
|
@ -110,10 +111,6 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
|
|||
log(ChatColor.WHITE + " * Initializing Adventure...");
|
||||
adventure = BukkitAudiences.create(this);
|
||||
|
||||
log(ChatColor.WHITE + " * Initializing PacketEvents...");
|
||||
packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry), PacketListenerPriority.MONITOR);
|
||||
packetEvents.init();
|
||||
|
||||
metadataFactory = setupMetadataFactory();
|
||||
PacketFactory packetFactory = setupPacketFactory();
|
||||
|
||||
|
@ -125,20 +122,15 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
|
|||
log(ChatColor.WHITE + " * Defining NPC types...");
|
||||
NpcTypeImpl.defineTypes();
|
||||
|
||||
log(ChatColor.WHITE + " * Starting tasks & registering components...");
|
||||
log(ChatColor.WHITE + " * Registering components...");
|
||||
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
|
||||
new Metrics(this, PLUGIN_ID);
|
||||
scheduler = FoliaUtil.isFolia() ? new FoliaScheduler(this) : new SpigotScheduler(this);
|
||||
BungeeUtil bungeeUtil = new BungeeUtil(this);
|
||||
userManager = new UserManager();
|
||||
Bukkit.getOnlinePlayers().forEach(userManager::get);
|
||||
|
||||
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()) {
|
||||
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.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);
|
||||
enabled = true;
|
||||
log(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)");
|
||||
|
@ -212,6 +216,7 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
|
|||
@Override
|
||||
public void onDisable() {
|
||||
if (!enabled) return;
|
||||
scheduler.cancelAll();
|
||||
npcRegistry.save();
|
||||
ZApiProvider.unregister();
|
||||
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(EntityPropertyImpl.class, new EntityPropertyParser(context -> {}));
|
||||
manager.registerParser(Integer.class, new IntegerParser(context -> {}));
|
||||
manager.registerParser(Double.class, new DoubleParser(context -> {}));
|
||||
manager.registerParser(Boolean.class, new BooleanParser(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("insert", new HoloInsertCommand(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 PacketFactory packetFactory;
|
||||
|
||||
private double offset = 0.0;
|
||||
private ZLocation location;
|
||||
private final List<HologramLine> lines = new ArrayList<>();
|
||||
|
||||
|
@ -79,10 +80,19 @@ public class HologramImpl extends Viewable implements Hologram {
|
|||
|
||||
private void relocateLines(HologramLine newLine) {
|
||||
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) {
|
||||
line.setLocation(location.withY(height), line == newLine ? Collections.emptySet() : getViewers());
|
||||
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) {
|
||||
if (value == null) return;
|
||||
if (value.equals(key.getDefaultValue())) removeProperty(key);
|
||||
else propertyMap.put(key, value);
|
||||
_refreshMeta();
|
||||
|
|
|
@ -117,6 +117,18 @@ public final class Reflections {
|
|||
.setStrict(FoliaUtil.isFolia())
|
||||
.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 =
|
||||
new ReflectionBuilder(Entity.class)
|
||||
.withMethodName("teleportAsync")
|
||||
|
|
|
@ -41,5 +41,21 @@ public class FoliaScheduler extends TaskScheduler {
|
|||
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) {
|
||||
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 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)));
|
||||
}
|
||||
}
|
||||
|
||||
for (String line : config.getStringList("hologram")) npc.getHologram().addLine(MiniMessage.miniMessage().deserialize(line));
|
||||
npc.getHologram().setOffset(config.getDouble("hologram.offset", 0.0));
|
||||
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));
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
if (npc.getHologram().getOffset() != 0.0) config.set("hologram.offset", npc.getHologram().getOffset());
|
||||
List<String> lines = new ArrayList<>();
|
||||
for (HologramLine line : npc.getHologram().getLines()) {
|
||||
lines.add(MiniMessage.miniMessage().serialize(line.getText()));
|
||||
}
|
||||
config.set("hologram", lines);
|
||||
config.set("hologram.lines", lines);
|
||||
config.set("actions", npc.getActions().stream()
|
||||
.map(actionRegistry::serialize)
|
||||
.filter(Objects::nonNull)
|
||||
|
|
Loading…
Reference in a new issue