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 3a055c99..a612fbc3 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.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 @@ private void registerPortalCommand(CommandRegister commandRegister) { 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 AdvancedPortalsModule getModule() { 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 c4f6a248..114ece91 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.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 void playerLeave(PlayerContainer player) { 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 fc0771f1..6078dacb 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 boolean registerSubCommand(String arg, SubCommand subCommand, String... a 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 9dc95e8e..42b3413c 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 d3439697..f4da15b8 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.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 void onCommand(CommandSenderContainer sender, String[] args) { } } } - /*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 List onTabComplete(CommandSenderContainer sender, String[] args) @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 8fe8430d..67bc676e 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.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 00000000..54611bed --- /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 a8175837..231162e5 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 8714caa1..78f2df90 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.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 void onEnable() { 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();