added possibility to disable appliances
This commit is contained in:
@@ -67,10 +67,13 @@ public final class Main extends JavaPlugin {
|
|||||||
Main.logger().info(String.format("Loaded %d appliances!", this.appliances.size()));
|
Main.logger().info(String.format("Loaded %d appliances!", this.appliances.size()));
|
||||||
|
|
||||||
Main.logger().info("Initializing appliances...");
|
Main.logger().info("Initializing appliances...");
|
||||||
this.appliances.forEach(appliance -> {
|
this.appliances.stream()
|
||||||
appliance.onEnable();
|
.filter(appliance -> {
|
||||||
appliance.initialize(this);
|
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()));
|
Main.logger().info(String.format("Initialized %d appliances!", this.appliances.size()));
|
||||||
|
|
||||||
if(Configuration.pluginConfig.getBoolean("httpServerEnabled", true)) {
|
if(Configuration.pluginConfig.getBoolean("httpServerEnabled", true)) {
|
||||||
@@ -85,17 +88,34 @@ public final class Main extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
Main.logger().info("Disabling appliances...");
|
Main.logger().info("Disabling appliances...");
|
||||||
this.appliances.forEach(appliance -> {
|
this.appliances.forEach(appliance -> appliance.disableSequence(this));
|
||||||
Main.logger().info("Disabling " + appliance.getClass().getSimpleName());
|
|
||||||
appliance.onDisable();
|
|
||||||
appliance.destruct(this);
|
|
||||||
});
|
|
||||||
|
|
||||||
HandlerList.unregisterAll(this);
|
HandlerList.unregisterAll(this);
|
||||||
Bukkit.getScheduler().cancelTasks(this);
|
Bukkit.getScheduler().cancelTasks(this);
|
||||||
Main.logger().info("Disabled " + this.appliances.size() + " appliances!");
|
Main.logger().info("Disabled " + this.appliances.size() + " appliances!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Appliance restartAppliance(Class<? extends Appliance> 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<? extends Appliance> 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 extends Appliance> T getAppliance(Class<T> clazz) {
|
public <T extends Appliance> T getAppliance(Class<T> clazz) {
|
||||||
return this.appliances.stream()
|
return this.appliances.stream()
|
||||||
.filter(clazz::isInstance)
|
.filter(clazz::isInstance)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ public abstract class Appliance {
|
|||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Flags {
|
public @interface Flags {
|
||||||
boolean enabled() default true;
|
boolean enabled() default true;
|
||||||
|
boolean autoload() default true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String localConfigPath;
|
private String localConfigPath;
|
||||||
@@ -104,6 +105,16 @@ public abstract class Appliance {
|
|||||||
this.listeners.forEach(HandlerList::unregisterAll);
|
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 extends Appliance> T queryAppliance(Class<T> clazz) {
|
protected <T extends Appliance> T queryAppliance(Class<T> clazz) {
|
||||||
return Main.instance().getAppliance(clazz);
|
return Main.instance().getAppliance(clazz);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.core.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.event.appliances.eventController.Event;
|
import eu.mhsl.craftattack.spawn.event.appliances.eventController.Event;
|
||||||
|
|
||||||
|
@Appliance.Flags(autoload = false)
|
||||||
public class Deathrun extends Appliance implements Event {
|
public class Deathrun extends Appliance implements Event {
|
||||||
@Override
|
@Override
|
||||||
public String displayName() {
|
public String displayName() {
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ package eu.mhsl.craftattack.spawn.event.appliances.eventController;
|
|||||||
|
|
||||||
public interface Event {
|
public interface Event {
|
||||||
String displayName();
|
String displayName();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package eu.mhsl.craftattack.spawn.event.appliances.eventController;
|
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.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
|
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
|
||||||
import eu.mhsl.craftattack.spawn.event.appliances.eventController.commands.EventCommand;
|
import eu.mhsl.craftattack.spawn.event.appliances.eventController.commands.EventCommand;
|
||||||
@@ -8,6 +9,29 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class EventController extends Appliance {
|
public class EventController extends Appliance {
|
||||||
|
private List<Appliance> eventAppliances = null;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
this.eventAppliances = Main.instance().getAppliances().stream()
|
||||||
|
.filter(appliance -> appliance instanceof Event)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Appliance> 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
|
@Override
|
||||||
protected @NotNull List<ApplianceCommand<?>> commands() {
|
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||||
return List.of(
|
return List.of(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package eu.mhsl.craftattack.spawn.event.appliances.eventController.commands;
|
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.core.appliance.ApplianceCommand;
|
||||||
import eu.mhsl.craftattack.spawn.event.appliances.eventController.EventController;
|
import eu.mhsl.craftattack.spawn.event.appliances.eventController.EventController;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
@@ -24,28 +25,30 @@ public class EventCommand extends ApplianceCommand<EventController> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "start": {
|
case "start": {
|
||||||
sender.sendMessage("start");
|
if(args.length == 1) throw new Error("Not enough arguments for start.");
|
||||||
break;
|
this.getAppliance().enableEvent(findApplianceFromString(args[1]));
|
||||||
}
|
|
||||||
case "pause": {
|
|
||||||
sender.sendMessage("pause");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "resume": {
|
|
||||||
sender.sendMessage("resume");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "stop": {
|
case "stop": {
|
||||||
sender.sendMessage("stop");
|
if(args.length == 1) throw new Error("Not enough arguments for stop.");
|
||||||
|
this.getAppliance().disableEvent(findApplianceFromString(args[1]));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: throw new Error("No such option: '%s' !".formatted(args[0]));
|
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
|
@Override
|
||||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
public @Nullable List<String> 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;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user