feat: add abstracted game scheduler

This commit is contained in:
Sekwah 2023-12-11 03:20:16 +00:00
parent 9a92323660
commit 02dfd59679
9 changed files with 137 additions and 30 deletions

View File

@ -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;
}
}

View File

@ -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

View File

@ -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<String> getSubCommands(){

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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<DelayedGameTickEvent> newTickEvents = new ArrayList<>();
private final ArrayList<DelayedGameTickEvent> delayedTickEvents = new ArrayList<>();
public void tick() {
this.delayedTickEvents.addAll(this.newTickEvents);
this.newTickEvents.clear();
Iterator<DelayedGameTickEvent> 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();
}
}
}

View File

@ -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

View File

@ -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();