From 02dfd5967929b56078a14ad991cd98cf2748cfa4 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Mon, 11 Dec 2023 03:20:16 +0000 Subject: [PATCH] feat: add abstracted game scheduler --- .../core/AdvancedPortalsCore.java | 10 ++- .../advancedportals/core/CoreListeners.java | 8 ++ .../core/commands/CommandWithSubCommands.java | 6 +- .../core/commands/SubCommand.java | 4 + ...Command.java => ShowPortalSubCommand.java} | 45 ++++------ .../core/module/AdvancedPortalsModule.java | 1 + .../core/util/GameScheduler.java | 86 +++++++++++++++++++ lang/src/main/resources/lang/en_GB.lang | 3 + .../spigot/AdvancedPortalsPlugin.java | 4 + 9 files changed, 137 insertions(+), 30 deletions(-) rename core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/{DebugPortalsSubCommand.java => ShowPortalSubCommand.java} (73%) create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/util/GameScheduler.java diff --git a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index 3a055c9..a612fbc 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -14,6 +14,7 @@ import com.sekwah.advancedportals.core.module.AdvancedPortalsModule; import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.tags.activation.DestiTag; import com.sekwah.advancedportals.core.tags.activation.NameTag; +import com.sekwah.advancedportals.core.util.GameScheduler; import com.sekwah.advancedportals.core.util.InfoLogger; import com.sekwah.advancedportals.core.util.Lang; @@ -43,6 +44,9 @@ public class AdvancedPortalsCore { @Inject private TagRegistry tagRegistry; + @Inject + private GameScheduler gameScheduler; + public AdvancedPortalsCore(File dataStorageLoc, InfoLogger infoLogger) { this.dataStorage = new DataStorage(dataStorageLoc); this.infoLogger = infoLogger; @@ -101,7 +105,7 @@ public class AdvancedPortalsCore { this.portalCommand.registerSubCommand("create", new CreatePortalSubCommand()); this.portalCommand.registerSubCommand("remove", new RemovePortalSubCommand()); this.portalCommand.registerSubCommand("list", new ListPortalsSubCommand()); - this.portalCommand.registerSubCommand("debug", new DebugPortalsSubCommand()); + this.portalCommand.registerSubCommand("show", new ShowPortalSubCommand()); commandRegister.registerCommand("portal", this.portalCommand); } @@ -143,4 +147,8 @@ public class AdvancedPortalsCore { public TagRegistry getTagRegistry() { return this.tagRegistry; } + + public GameScheduler getGameScheduler() { + return gameScheduler; + } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java b/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java index c4f6a24..114ece9 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java @@ -11,6 +11,7 @@ import com.sekwah.advancedportals.core.permissions.PortalPermissions; import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.services.PortalServices; import com.sekwah.advancedportals.core.services.PortalTempDataServices; +import com.sekwah.advancedportals.core.util.GameScheduler; import java.util.Objects; @@ -25,6 +26,9 @@ public class CoreListeners { @Inject private ConfigRepository configRepository; + @Inject + private GameScheduler gameScheduler; + public void playerJoin(PlayerContainer player) { this.portalTempDataServices.activateCooldown(player); } @@ -37,6 +41,10 @@ public class CoreListeners { this.portalTempDataServices.playerLeave(player); } + public void tick() { + this.gameScheduler.tick(); + } + /** * @param loc where the entity spawns * @return if the entity is allowed to spawn diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java index fc0771f..6078dac 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java @@ -27,7 +27,11 @@ public class CommandWithSubCommands implements CommandTemplate { for(String additionalArg : aliasArgs) { hasRegistered = this.subCommandRegistry.registerSubCommand(additionalArg,subCommand) || hasRegistered; } - return this.subCommandRegistry.registerSubCommand(arg,subCommand) || hasRegistered; + boolean result = this.subCommandRegistry.registerSubCommand(arg,subCommand) || hasRegistered; + if(subCommand instanceof SubCommand.SubCommandOnInit init) { + init.registered(); + } + return result; } public ArrayList getSubCommands(){ diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/SubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/SubCommand.java index 9dc95e8..42b3413 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/SubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/SubCommand.java @@ -33,4 +33,8 @@ public interface SubCommand { * @return the string to show if help then the tag is listed. */ String getDetailedHelpText(); + + interface SubCommandOnInit { + void registered(); + } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/DebugPortalsSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java similarity index 73% rename from core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/DebugPortalsSubCommand.java rename to core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java index d343969..f4da15b 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/DebugPortalsSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java @@ -8,17 +8,23 @@ import com.sekwah.advancedportals.core.serializeddata.BlockLocation; import com.sekwah.advancedportals.core.serializeddata.PlayerTempData; import com.sekwah.advancedportals.core.services.PortalTempDataServices; import com.sekwah.advancedportals.core.util.Debug; -import com.sekwah.advancedportals.core.util.FriendlyDataOutput; +import com.sekwah.advancedportals.core.util.GameScheduler; import com.sekwah.advancedportals.core.util.Lang; import java.awt.*; import java.util.List; -public class DebugPortalsSubCommand implements SubCommand { +/** + * This will be different from the old show command and I believe it is 1.16+ till the latest version as of writing this. + */ +public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOnInit { @Inject PortalTempDataServices tempDataServices; + @Inject + GameScheduler gameScheduler; + @Override public void onCommand(CommandSenderContainer sender, String[] args) { sender.sendMessage("Debug"); @@ -57,30 +63,6 @@ public class DebugPortalsSubCommand implements SubCommand { } } } - /*int minX = Math.min(tempData.getPos1().posX, tempData.getPos2().posX); - int minY = Math.min(tempData.getPos1().posY, tempData.getPos2().posY); - int minZ = Math.min(tempData.getPos1().posZ, tempData.getPos2().posZ); - - int maxX = Math.max(tempData.getPos1().posX, tempData.getPos2().posX); - int maxY = Math.max(tempData.getPos1().posY, tempData.getPos2().posY); - int maxZ = Math.max(tempData.getPos1().posZ, tempData.getPos2().posZ); - - FriendlyDataOutput out = new FriendlyDataOutput(); - - out.writeUtf("minecraft:overworld"); - - // Bounding Box - out.writeInt(minX); - out.writeInt(minY); - out.writeInt(minZ); - out.writeInt(maxX); - out.writeInt(maxY); - out.writeInt(maxZ); - - // Count - out.writeInt(0); - - playerContainer.sendPacket("minecraft:debug/structures", out.toByteArray());*/ } } @@ -96,11 +78,18 @@ public class DebugPortalsSubCommand implements SubCommand { @Override public String getBasicHelpText() { - return Lang.translate("command.portal.list.debug"); + return Lang.translate("command.portal.show.help"); } @Override public String getDetailedHelpText() { - return Lang.translate("command.portal.list.debug"); + return Lang.translate("command.portal.show.detailedhelp"); + } + + @Override + public void registered() { + gameScheduler.intervalTickEvent("show_portal", () -> { + System.out.println("check visibility"); + }, 1, 20); } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java b/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java index 8fe8430..67bc676 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/module/AdvancedPortalsModule.java @@ -12,6 +12,7 @@ import com.sekwah.advancedportals.core.repository.IPortalRepository; import com.sekwah.advancedportals.core.repository.impl.ConfigRepositoryImpl; import com.sekwah.advancedportals.core.repository.impl.DestinationRepositoryImpl; import com.sekwah.advancedportals.core.repository.impl.PortalRepositoryImpl; +import com.sekwah.advancedportals.core.util.GameScheduler; import com.sekwah.advancedportals.core.util.InfoLogger; import javax.annotation.Nonnull; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/util/GameScheduler.java b/core/src/main/java/com/sekwah/advancedportals/core/util/GameScheduler.java new file mode 100644 index 0000000..54611be --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/util/GameScheduler.java @@ -0,0 +1,86 @@ +package com.sekwah.advancedportals.core.util; + +import javax.inject.Singleton; +import java.util.ArrayList; +import java.util.Iterator; + +/** + * For all delayed and repeating tasks. + */ +@Singleton +public final class GameScheduler { + + private final ArrayList newTickEvents = new ArrayList<>(); + private final ArrayList delayedTickEvents = new ArrayList<>(); + + + public void tick() { + this.delayedTickEvents.addAll(this.newTickEvents); + this.newTickEvents.clear(); + Iterator tickEventIterator = this.delayedTickEvents.iterator(); + while (tickEventIterator.hasNext()) { + DelayedGameTickEvent event = tickEventIterator.next(); + event.tick(); + if (event.shouldRun()) { + event.run(); + if(!(event instanceof DelayedGameIntervalEvent)) + tickEventIterator.remove(); + } + } + } + + public void delayedTickEvent(String name, Runnable consumer, int tickDelay) { + this.newTickEvents.add(new DelayedGameTickEvent(name, consumer, tickDelay)); + } + + public void intervalTickEvent(String name, Runnable consumer, int tickDelay, int interval) { + this.newTickEvents.add(new DelayedGameIntervalEvent(name, consumer, tickDelay, interval)); + } + + public void clearAllEvents() { + this.newTickEvents.clear(); + this.delayedTickEvents.clear(); + } + + public static class DelayedGameTickEvent { + + // So we can find it later and remove it if needed + public final String name; + public final Runnable consumer; + public int ticks; + + public DelayedGameTickEvent(String name, Runnable consumer, int ticks) { + this.name = name; + this.consumer = consumer; + this.ticks = ticks; + } + + public void tick() { + this.ticks--; + } + + public boolean shouldRun() { + return this.ticks <= 0; + } + + public void run() { + this.consumer.run(); + } + } + + public static class DelayedGameIntervalEvent extends DelayedGameTickEvent { + + public int interval; + + public DelayedGameIntervalEvent(String name, Runnable consumer, int ticks, int interval) { + super(name, consumer, ticks); + this.interval = interval; + } + + public void run() { + this.ticks = interval; + super.run(); + } + + } +} diff --git a/lang/src/main/resources/lang/en_GB.lang b/lang/src/main/resources/lang/en_GB.lang index a817583..231162e 100644 --- a/lang/src/main/resources/lang/en_GB.lang +++ b/lang/src/main/resources/lang/en_GB.lang @@ -79,6 +79,9 @@ command.portal.remove.invalidselection=The portal selection you had is no longer command.portal.remove.noselection=You don't have a portal selected command.portal.remove.complete= The portal has been successfully removed. +command.portal.show.help=Shows nearby portals +command.portal.show.detailedhelp=Shows nearby portals. Relies on debug markers so may not work on certain versions of minecraft (1.16+ atm only). + command.destination.remove.error= There was a problem removing the destination. command.portal.list.help=Lists portals diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java index 8714caa..78f2df9 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java @@ -4,6 +4,7 @@ import com.google.inject.Injector; import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.connector.commands.CommandRegister; import com.sekwah.advancedportals.core.module.AdvancedPortalsModule; +import com.sekwah.advancedportals.core.util.GameScheduler; import com.sekwah.advancedportals.spigot.connector.command.SpigotCommandRegister; import com.sekwah.advancedportals.spigot.metrics.Metrics; import org.bukkit.plugin.java.JavaPlugin; @@ -38,6 +39,9 @@ public class AdvancedPortalsPlugin extends JavaPlugin { injector.injectMembers(listeners); this.getServer().getPluginManager().registerEvents(listeners, this); + GameScheduler scheduler = injector.getInstance(GameScheduler.class); + this.getServer().getScheduler().scheduleSyncRepeatingTask(this, scheduler::tick, 1, 1); + // Try to do this after setting up everything that would need to be injected to. this.portalsCore.onEnable();