diff --git a/src/main/java/com/volmit/iris/Iris.java b/src/main/java/com/volmit/iris/Iris.java index 7be139290..2c64fc8ce 100644 --- a/src/main/java/com/volmit/iris/Iris.java +++ b/src/main/java/com/volmit/iris/Iris.java @@ -38,6 +38,8 @@ import com.volmit.iris.engine.object.dimensional.IrisDimension; import com.volmit.iris.engine.platform.BukkitChunkGenerator; import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.collection.KSet; +import com.volmit.iris.util.decree.DecreeCommand; +import com.volmit.iris.util.decree.DecreeSystem; import com.volmit.iris.util.format.C; import com.volmit.iris.util.format.Form; import com.volmit.iris.util.function.NastyRunnable; @@ -77,7 +79,7 @@ import java.net.URL; import java.util.Date; @SuppressWarnings("CanBeFinal") -public class Iris extends VolmitPlugin implements Listener { +public class Iris extends VolmitPlugin implements Listener, DecreeSystem { public static KList executors = new KList<>(); public static Iris instance; public static BukkitAudiences audiences; @@ -717,8 +719,19 @@ public class Iris extends VolmitPlugin implements Listener { static { try { InstanceState.updateInstanceId(); - } catch (Throwable e) { + } catch (Throwable ignored) { } } + + /** + * Should return the root command class
+ * DecreeSystem extends {@link DecreeCommand} so don't bother implementing both on the root class + * + * @return The root command class + */ + @Override + public Class getRoot() { + return this.getClass(); + } } diff --git a/src/main/java/com/volmit/iris/core/command/pregen/CommandIrisPregenStart.java b/src/main/java/com/volmit/iris/core/command/pregen/CommandIrisPregenStart.java index bed3c7c70..4a5fdd9ec 100644 --- a/src/main/java/com/volmit/iris/core/command/pregen/CommandIrisPregenStart.java +++ b/src/main/java/com/volmit/iris/core/command/pregen/CommandIrisPregenStart.java @@ -221,8 +221,8 @@ public class CommandIrisPregenStart extends MortarCommand { IrisToolbelt.pregenerate(PregenTask .builder() .center(new Position2(x, z)) - .width(width >> 9 + 1) - .height(height >> 9 + 1) + .width((width >> 9 + 1) * 2) + .height((height >> 9 + 1) * 2) .build(), world); } catch (Throwable e) { Iris.reportError(e); diff --git a/src/main/java/com/volmit/iris/util/decree/DecreeCategory.java b/src/main/java/com/volmit/iris/util/decree/DecreeCategory.java new file mode 100644 index 000000000..3971aa86e --- /dev/null +++ b/src/main/java/com/volmit/iris/util/decree/DecreeCategory.java @@ -0,0 +1,94 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.volmit.iris.util.decree; + +import com.volmit.iris.util.collection.KList; +import com.volmit.iris.util.decree.annotations.Decree; +import com.volmit.iris.util.decree.annotations.Param; +import com.volmit.iris.util.decree.exceptions.DecreeInstanceException; + +import java.lang.reflect.*; +import java.util.Arrays; + +public class DecreeCategory { + private final Class clazz; + private final Decree decree; + + public DecreeCategory(Class clazz) throws DecreeInstanceException { + this.clazz = clazz; + this.decree = clazz.getDeclaredAnnotation(Decree.class); + if (decree == null){ + throw new DecreeInstanceException("Cannot instantiate DecreeCategory on class not annotated by @Decree"); + } + } + + /** + * Get the subcommands of this decree category + * @return The list of subcommands if ALL are only classes implementing DecreeCommand, else null + */ + public KList getCommands() { + KList c = new KList<>(); + + for(Field i : clazz.getFields()) + { + try { + i.setAccessible(true); + if (DecreeCommand.class.isAssignableFrom(i.getType())) { + c.add((DecreeCommand) i.getType().getConstructor().newInstance()); + } + } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + } + + return c; + } + + public String getName() { + return decree.name().equals(Decree.METHOD_NAME) ? clazz.getName() : decree.name(); + } + + public DecreeOrigin getOrigin() { + return decree.origin(); + } + + public String getDescription() { + return decree.description().isEmpty() ? Decree.DEFAULT_DESCRIPTION : decree.description(); + } + + public KList getAliases() { + KList d = new KList<>(); + + if (Arrays.equals(decree.aliases(), new String[]{Decree.NO_ALIASES})){ + return d; + } + + for(String i : decree.aliases()) + { + if(i.isEmpty()) + { + continue; + } + + d.add(i); + } + + return d; + } +} diff --git a/src/main/java/com/volmit/iris/util/decree/DecreeCommand.java b/src/main/java/com/volmit/iris/util/decree/DecreeCommand.java index 255190726..08c82bce1 100644 --- a/src/main/java/com/volmit/iris/util/decree/DecreeCommand.java +++ b/src/main/java/com/volmit/iris/util/decree/DecreeCommand.java @@ -18,5 +18,6 @@ package com.volmit.iris.util.decree; -public class DecreeCommand { + +public interface DecreeCommand { } diff --git a/src/main/java/com/volmit/iris/util/decree/DecreeContext.java b/src/main/java/com/volmit/iris/util/decree/DecreeContext.java index 8f9a427e7..82a9ebdc9 100644 --- a/src/main/java/com/volmit/iris/util/decree/DecreeContext.java +++ b/src/main/java/com/volmit/iris/util/decree/DecreeContext.java @@ -21,13 +21,9 @@ package com.volmit.iris.util.decree; import com.volmit.iris.util.collection.KMap; import com.volmit.iris.util.plugin.VolmitSender; import com.volmit.iris.util.scheduling.ChronoLatch; -import lombok.AllArgsConstructor; -import lombok.Data; -@Data -@AllArgsConstructor public class DecreeContext { - private static ChronoLatch cl = new ChronoLatch(60000); + private static final ChronoLatch cl = new ChronoLatch(60000); private static final KMap context = new KMap<>(); public static VolmitSender get() { diff --git a/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java b/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java index 2eb987ab5..55f6d09a2 100644 --- a/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java +++ b/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java @@ -20,16 +20,31 @@ package com.volmit.iris.util.decree; import com.volmit.iris.Iris; import com.volmit.iris.util.collection.KList; +import com.volmit.iris.util.decree.annotations.Decree; +import com.volmit.iris.util.math.M; +import org.checkerframework.checker.units.qual.K; -public class DecreeSystem { - private static final KList> handlers = Iris.initialize("com.volmit.iris.util.decree.handlers", null).convert((i) -> (DecreeParameterHandler) i); +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public interface DecreeSystem { + KList> handlers = Iris.initialize("com.volmit.iris.util.decree.handlers", null).convert((i) -> (DecreeParameterHandler) i); + + /** + * Should return the root command
+ * Root must extend {@link DecreeCommand} + * + * @return The root command class (this#getClass) + */ + Class getRoot(); /** * Get the handler for the specified type * @param type The type to handle * @return The corresponding {@link DecreeParameterHandler}, or null */ - public static DecreeParameterHandler getHandler(Class type) + static DecreeParameterHandler getHandler(Class type) { for(DecreeParameterHandler i : handlers) { @@ -40,4 +55,18 @@ public class DecreeSystem { } return null; } + + /** + * Gets the method command to from the raw command parameters + * @param command The raw command parameters + * @return The @{@link Decree} method + */ + default Method getRootCommandFrom(String[] command){ + return getCommandFrom(new KList<>(command), getRoot()); + } + + + default Method getCommandFrom(KList command, Class decree){ + return null; + } } diff --git a/src/main/java/com/volmit/iris/util/decree/EXAMPLE.java b/src/main/java/com/volmit/iris/util/decree/Example.java similarity index 60% rename from src/main/java/com/volmit/iris/util/decree/EXAMPLE.java rename to src/main/java/com/volmit/iris/util/decree/Example.java index 13df0f1f1..a888050cf 100644 --- a/src/main/java/com/volmit/iris/util/decree/EXAMPLE.java +++ b/src/main/java/com/volmit/iris/util/decree/Example.java @@ -22,8 +22,20 @@ import com.volmit.iris.util.decree.annotations.Decree; import com.volmit.iris.util.decree.annotations.Param; import org.bukkit.entity.Player; -public class EXAMPLE extends DecreeCommand { - @Decree +@Decree(description = "Description goes here!", aliases = {"ex", "e"}) +// The description here is shown when hovering over elements in the chat +// The parameter `name` is undefined, which means it defaults to the name of the class, lowercase, so "example" +// The aliases defined give alternate options for calling this category +// You can also define "origin" which gives who can send the command. +// By default, if omitted, this is DecreeOrigin.BOTH, but it can be .PLAYER & .CONSOLE +public class Example implements DecreeCommand { + + // This subcommand, given that it implements DecreeCommand, is automatically indexed and recognised by Decree. + // The way this command is called isn't defined from here. + // Since subCommand can have another name than in /iris example subCommand. + SubExample subCommand; + + @Decree(description = "Kick a player", aliases = "k", origin = DecreeOrigin.CONSOLE) public void kick( @Param(name = "player", description = "The Player to kick from the server", aliases = "p") Player player, diff --git a/src/main/java/com/volmit/iris/util/decree/SubExample.java b/src/main/java/com/volmit/iris/util/decree/SubExample.java new file mode 100644 index 000000000..08f4acebb --- /dev/null +++ b/src/main/java/com/volmit/iris/util/decree/SubExample.java @@ -0,0 +1,12 @@ +package com.volmit.iris.util.decree; + +import com.volmit.iris.util.decree.annotations.Decree; + +@Decree(name = "boop", aliases = {"b", "bp"}, description = "Sub example with another name!") +// the boop command in here can be called with (/super) boop beep, b beep and bp beep +public class SubExample implements DecreeCommand { + @Decree(name = "beep", description = "Boops the sender") // Origin is not defined so both console & player senders can run this command (Default) + public void boop() { // Called with "beep" because name = "beep", "boop" will not work in the command + DecreeContext.get().sendMessage("Boop"); + } +} diff --git a/src/main/java/com/volmit/iris/util/decree/annotations/Decree.java b/src/main/java/com/volmit/iris/util/decree/annotations/Decree.java index 8825e1f82..4d447d66d 100644 --- a/src/main/java/com/volmit/iris/util/decree/annotations/Decree.java +++ b/src/main/java/com/volmit/iris/util/decree/annotations/Decree.java @@ -26,7 +26,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.FIELD}) +@Target({ElementType.METHOD, ElementType.TYPE}) public @interface Decree { String METHOD_NAME = "Default Method Name";