Skip to content

Commit

Permalink
feat: add abstracted game scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Dec 11, 2023
1 parent 9a92323 commit 02dfd59
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -143,4 +147,8 @@ public AdvancedPortalsModule getModule() {
public TagRegistry getTagRegistry() {
return this.tagRegistry;
}

public GameScheduler getGameScheduler() {
return gameScheduler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,6 +26,9 @@ public class CoreListeners {
@Inject
private ConfigRepository configRepository;

@Inject
private GameScheduler gameScheduler;

public void playerJoin(PlayerContainer player) {
this.portalTempDataServices.activateCooldown(player);
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getSubCommands(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());*/
}
}

Expand All @@ -96,11 +78,18 @@ public List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
}
3 changes: 3 additions & 0 deletions lang/src/main/resources/lang/en_GB.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 02dfd59

Please sign in to comment.