Merge pull request #528 from CocoTheOwner/decree+2

More docs more framework, glhf!
This commit is contained in:
Dan 2021-08-13 08:25:19 -04:00 committed by GitHub
commit efe49ee4c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 173 additions and 16 deletions

View File

@ -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<GroupedExecutor> 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<br>
* DecreeSystem extends {@link DecreeCommand} so don't bother implementing both on the root class
*
* @return The root command class
*/
@Override
public Class<? extends DecreeCommand> getRoot() {
return this.getClass();
}
}

View File

@ -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);

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<DecreeCommand> getCommands() {
KList<DecreeCommand> 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<String> getAliases() {
KList<String> 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;
}
}

View File

@ -18,5 +18,6 @@
package com.volmit.iris.util.decree;
public class DecreeCommand {
public interface DecreeCommand {
}

View File

@ -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<Thread, VolmitSender> context = new KMap<>();
public static VolmitSender get() {

View File

@ -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<DecreeParameterHandler<?>> 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<DecreeParameterHandler<?>> handlers = Iris.initialize("com.volmit.iris.util.decree.handlers", null).convert((i) -> (DecreeParameterHandler<?>) i);
/**
* Should return the root command<br>
* Root must extend {@link DecreeCommand}
*
* @return The root command class (this#getClass)
*/
Class<? extends DecreeCommand> 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<String> command, Class<? extends DecreeCommand> decree){
return null;
}
}

View File

@ -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,

View File

@ -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");
}
}

View File

@ -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";