From f7430c8fc8aa847f18fe28127c9589849236652c Mon Sep 17 00:00:00 2001 From: lars Date: Fri, 28 Nov 2025 21:08:27 +0100 Subject: [PATCH] added possibility to disable appliances --- .../eu/mhsl/craftattack/spawn/core/Main.java | 38 ++++++++++++++----- .../spawn/core/appliance/Appliance.java | 11 ++++++ .../event/appliances/deathrun/Deathrun.java | 1 + .../appliances/eventController/Event.java | 1 + .../eventController/EventController.java | 24 ++++++++++++ .../commands/EventCommand.java | 25 ++++++------ 6 files changed, 80 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/eu/mhsl/craftattack/spawn/core/Main.java b/core/src/main/java/eu/mhsl/craftattack/spawn/core/Main.java index e666a90..d7feeb3 100644 --- a/core/src/main/java/eu/mhsl/craftattack/spawn/core/Main.java +++ b/core/src/main/java/eu/mhsl/craftattack/spawn/core/Main.java @@ -67,10 +67,13 @@ public final class Main extends JavaPlugin { Main.logger().info(String.format("Loaded %d appliances!", this.appliances.size())); Main.logger().info("Initializing appliances..."); - this.appliances.forEach(appliance -> { - appliance.onEnable(); - appliance.initialize(this); - }); + this.appliances.stream() + .filter(appliance -> { + Appliance.Flags flags = appliance.getClass().getAnnotation(Appliance.Flags.class); + if(flags == null) return true; + return flags.autoload(); + }) + .forEach(appliance -> appliance.enableSequence(this)); Main.logger().info(String.format("Initialized %d appliances!", this.appliances.size())); if(Configuration.pluginConfig.getBoolean("httpServerEnabled", true)) { @@ -85,17 +88,34 @@ public final class Main extends JavaPlugin { @Override public void onDisable() { Main.logger().info("Disabling appliances..."); - this.appliances.forEach(appliance -> { - Main.logger().info("Disabling " + appliance.getClass().getSimpleName()); - appliance.onDisable(); - appliance.destruct(this); - }); + this.appliances.forEach(appliance -> appliance.disableSequence(this)); HandlerList.unregisterAll(this); Bukkit.getScheduler().cancelTasks(this); Main.logger().info("Disabled " + this.appliances.size() + " appliances!"); } + public Appliance restartAppliance(Class applianceClass) { + Appliance oldAppliance = this.appliances.stream() + .filter(appliance -> appliance.getClass().equals(applianceClass)) + .findFirst() + .orElseThrow(); + oldAppliance.disableSequence(this); + this.appliances.remove(oldAppliance); + Appliance newAppliance = this.createApplianceInstance(applianceClass); + this.appliances.add(newAppliance); + newAppliance.enableSequence(this); + return newAppliance; + } + + private Appliance createApplianceInstance(Class applianceClass) { + try { + return applianceClass.getDeclaredConstructor().newInstance(); + } catch(Exception e) { + throw new RuntimeException(String.format("Failed to create instance of '%s'", applianceClass.getName()), e); + } + } + public T getAppliance(Class clazz) { return this.appliances.stream() .filter(clazz::isInstance) diff --git a/core/src/main/java/eu/mhsl/craftattack/spawn/core/appliance/Appliance.java b/core/src/main/java/eu/mhsl/craftattack/spawn/core/appliance/Appliance.java index 9650759..f50da8a 100644 --- a/core/src/main/java/eu/mhsl/craftattack/spawn/core/appliance/Appliance.java +++ b/core/src/main/java/eu/mhsl/craftattack/spawn/core/appliance/Appliance.java @@ -28,6 +28,7 @@ public abstract class Appliance { @Retention(RetentionPolicy.RUNTIME) public @interface Flags { boolean enabled() default true; + boolean autoload() default true; } private String localConfigPath; @@ -104,6 +105,16 @@ public abstract class Appliance { this.listeners.forEach(HandlerList::unregisterAll); } + public void enableSequence(JavaPlugin plugin) { + this.onEnable(); + this.initialize(plugin); + } + + public void disableSequence(JavaPlugin plugin) { + this.onDisable(); + this.destruct(plugin); + } + protected T queryAppliance(Class clazz) { return Main.instance().getAppliance(clazz); } diff --git a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java index ac2bce0..37bad35 100644 --- a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java +++ b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/deathrun/Deathrun.java @@ -3,6 +3,7 @@ package eu.mhsl.craftattack.spawn.event.appliances.deathrun; import eu.mhsl.craftattack.spawn.core.appliance.Appliance; import eu.mhsl.craftattack.spawn.event.appliances.eventController.Event; +@Appliance.Flags(autoload = false) public class Deathrun extends Appliance implements Event { @Override public String displayName() { diff --git a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/Event.java b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/Event.java index c458d23..1dcc3ed 100644 --- a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/Event.java +++ b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/Event.java @@ -2,4 +2,5 @@ package eu.mhsl.craftattack.spawn.event.appliances.eventController; public interface Event { String displayName(); + } diff --git a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/EventController.java b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/EventController.java index 83d7559..1c4986b 100644 --- a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/EventController.java +++ b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/EventController.java @@ -1,5 +1,6 @@ package eu.mhsl.craftattack.spawn.event.appliances.eventController; +import eu.mhsl.craftattack.spawn.core.Main; import eu.mhsl.craftattack.spawn.core.appliance.Appliance; import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand; import eu.mhsl.craftattack.spawn.event.appliances.eventController.commands.EventCommand; @@ -8,6 +9,29 @@ import org.jetbrains.annotations.NotNull; import java.util.List; public class EventController extends Appliance { + private List eventAppliances = null; + + @Override + public void onEnable() { + this.eventAppliances = Main.instance().getAppliances().stream() + .filter(appliance -> appliance instanceof Event) + .toList(); + } + + public List getEventAppliances() { + if(eventAppliances == null) throw new IllegalStateException("Event appliances were not initialized"); + return eventAppliances; + } + + public void enableEvent(Appliance event) { + getEventAppliances().forEach(appliance -> appliance.disableSequence(Main.instance())); + Main.instance().restartAppliance(event.getClass()); + } + + public void disableEvent(Appliance event) { + event.disableSequence(Main.instance()); + } + @Override protected @NotNull List> commands() { return List.of( diff --git a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/commands/EventCommand.java b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/commands/EventCommand.java index 487b7bb..7052233 100644 --- a/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/commands/EventCommand.java +++ b/event/src/main/java/eu/mhsl/craftattack/spawn/event/appliances/eventController/commands/EventCommand.java @@ -1,5 +1,6 @@ package eu.mhsl.craftattack.spawn.event.appliances.eventController.commands; +import eu.mhsl.craftattack.spawn.core.appliance.Appliance; import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand; import eu.mhsl.craftattack.spawn.event.appliances.eventController.EventController; import org.bukkit.command.Command; @@ -24,28 +25,30 @@ public class EventCommand extends ApplianceCommand { break; } case "start": { - sender.sendMessage("start"); - break; - } - case "pause": { - sender.sendMessage("pause"); - break; - } - case "resume": { - sender.sendMessage("resume"); + if(args.length == 1) throw new Error("Not enough arguments for start."); + this.getAppliance().enableEvent(findApplianceFromString(args[1])); break; } case "stop": { - sender.sendMessage("stop"); + if(args.length == 1) throw new Error("Not enough arguments for stop."); + this.getAppliance().disableEvent(findApplianceFromString(args[1])); break; } default: throw new Error("No such option: '%s' !".formatted(args[0])); } } + private Appliance findApplianceFromString(String name) { + System.out.println(this.getAppliance().getEventAppliances()); + return this.getAppliance().getEventAppliances().stream() + .filter(appliance -> appliance.getClass().getSimpleName().equalsIgnoreCase(name)) + .findFirst() + .orElseThrow(); + } + @Override public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if(args.length == 1) return List.of("select", "start", "pause", "resume", "stop"); + if(args.length == 1) return List.of("select", "start", "stop"); return null; } }