Compare commits
3 Commits
master
...
master-big
| Author | SHA1 | Date | |
|---|---|---|---|
| 212f84b6de | |||
| f7430c8fc8 | |||
| c81a2d2161 |
@@ -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<? 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) {
|
||||
return this.appliances.stream()
|
||||
.filter(clazz::isInstance)
|
||||
|
||||
@@ -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 extends Appliance> T queryAppliance(Class<T> clazz) {
|
||||
return Main.instance().getAppliance(clazz);
|
||||
}
|
||||
|
||||
9
event/build.gradle
Normal file
9
event/build.gradle
Normal file
@@ -0,0 +1,9 @@
|
||||
dependencies {
|
||||
implementation project(':common')
|
||||
implementation project(':core')
|
||||
|
||||
compileOnly 'io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT'
|
||||
compileOnly 'org.geysermc.floodgate:api:2.2.4-SNAPSHOT'
|
||||
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
|
||||
implementation 'com.sparkjava:spark-core:2.9.4'
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Appliance.Flags(autoload = false)
|
||||
public class Deathrun extends Appliance implements Event {
|
||||
@Override
|
||||
public void initialize(@NotNull JavaPlugin plugin) {
|
||||
World world = Bukkit.getWorlds().getFirst();
|
||||
world.getWorldBorder().setCenter(world.getSpawnLocation());
|
||||
world.getWorldBorder().setSize(20);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
Title title = Title.title(Component.text("Start"), Component.text("Erreiche die höchste x-Koordinate!"));
|
||||
Bukkit.getOnlinePlayers().forEach(player -> player.showTitle(title));
|
||||
|
||||
World world = Bukkit.getWorlds().getFirst();
|
||||
world.getWorldBorder().setSize(world.getWorldBorder().getMaxSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getScore(Player p) {
|
||||
return (int) Math.ceil(p.getLocation().subtract(Bukkit.getWorlds().getFirst().getSpawnLocation()).x());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
Title title = Title.title(Component.text("Ende!"), Component.empty());
|
||||
Bukkit.getOnlinePlayers().forEach(player -> player.showTitle(title));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String displayName() {
|
||||
return "Deathrun";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package eu.mhsl.craftattack.spawn.event.appliances.eventController;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scoreboard.Scoreboard;
|
||||
|
||||
public interface Event {
|
||||
Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
|
||||
|
||||
String displayName();
|
||||
void start();
|
||||
void stop();
|
||||
int getScore(Player p);
|
||||
|
||||
public default void updateSidebar() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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
|
||||
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||
return List.of(
|
||||
new EventCommand()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventCommand extends ApplianceCommand<EventController> {
|
||||
public EventCommand() {
|
||||
super("event");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if(args.length == 0) throw new Error("No argument selected!");
|
||||
|
||||
switch(args[0]) {
|
||||
case "select": {
|
||||
sender.sendMessage("select");
|
||||
break;
|
||||
}
|
||||
case "start": {
|
||||
if(args.length == 1) throw new Error("Not enough arguments for start.");
|
||||
this.getAppliance().enableEvent(findApplianceFromString(args[1]));
|
||||
break;
|
||||
}
|
||||
case "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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if(args.length == 1) return List.of("select", "start", "stop");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,5 @@ include 'core'
|
||||
include 'craftattack'
|
||||
include 'common'
|
||||
include 'varo'
|
||||
include 'event'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user