Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic command system #14

Merged
merged 8 commits into from
Jun 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: basic command system
  • Loading branch information
MatzHilven committed Jun 19, 2024
commit f991209a58b92842bc3ff41a89cd849062595d23
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -29,6 +29,11 @@
<artifactId>logback-classic</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
</dependencies>

<build>
38 changes: 24 additions & 14 deletions src/main/java/com/slampvp/factory/FactoryServer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.slampvp.factory;

import com.slampvp.factory.command.FactoryCommand;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
@@ -10,12 +11,14 @@
import net.minestom.server.instance.InstanceManager;
import net.minestom.server.instance.LightingChunk;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.Set;
import java.util.stream.Stream;


public final class FactoryServer {

@@ -40,20 +43,27 @@ public static void main(String[] args) {
player.setRespawnPoint(new Pos(0, 40, 0));
});

Block.GRASS_BLOCK.withHandler(new BlockHandler() {
streamPackage("com.slampvp.factory.command", FactoryCommand.class).forEach(c ->
MinecraftServer.getCommandManager().register(c)
);

@Override
public void onDestroy(@NotNull BlockHandler.Destroy destroy) {
LOGGER.debug("break at {" + destroy.getBlockPosition() + "}");
}
minecraftServer.start("0.0.0.0", 25565);
}

@Override
public @NotNull NamespaceID getNamespaceId() {
return NamespaceID.from("minecraft:grass_block");
}
});
public static <T> Stream<T> streamPackage(String packageName, Class<T> clazz) {
Reflections reflections = new Reflections(packageName);
Set<Class<? extends T>> subTypes = reflections.getSubTypesOf(clazz);

minecraftServer.start("0.0.0.0", 25565);
return subTypes.stream()
.map(subClass -> {
try {
return clazz.cast(subClass.getDeclaredConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
return null;
}
})
.filter(java.util.Objects::nonNull);
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/slampvp/factory/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.slampvp.factory.command;

import com.slampvp.factory.player.Rank;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Command {
String description();

String usage();

Rank minimumRank();

boolean playerOnly();
}
34 changes: 34 additions & 0 deletions src/main/java/com/slampvp/factory/command/FactoryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.slampvp.factory.command;


import com.slampvp.factory.common.Locale;
import net.kyori.adventure.text.TextReplacementConfig;
import net.minestom.server.command.ConsoleSender;
import org.jetbrains.annotations.NotNull;

public abstract class FactoryCommand extends net.minestom.server.command.builder.Command {
public FactoryCommand(@NotNull String name) {
super(name);

Command annotation = getClass().getAnnotation(Command.class);

setDefaultExecutor((sender, context) -> {
sender.sendMessage(Locale.Command.INVALID_COMMAND
.replaceText(TextReplacementConfig.builder().match("<usage>").replacement(annotation.usage()).build())
);
});

setCondition((commandSender, string) -> {
if (commandSender instanceof ConsoleSender) {
return !annotation.playerOnly();
}

// TODO: implement permission checking when we implement ranks
return true;
});

init();
}

public abstract void init();
}
24 changes: 24 additions & 0 deletions src/main/java/com/slampvp/factory/command/generic/TestCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.slampvp.factory.command.generic;

import com.slampvp.factory.command.Command;
import com.slampvp.factory.command.FactoryCommand;
import com.slampvp.factory.player.Rank;
import net.minestom.server.command.builder.arguments.ArgumentType;
import net.minestom.server.command.builder.arguments.number.ArgumentInteger;

@Command(description = "Test", usage = "/test <number>", minimumRank = Rank.DEFAULT, playerOnly = false)
public class TestCommand extends FactoryCommand {
public TestCommand() {
super("test");
}

@Override
public void init() {
ArgumentInteger numberArgument = ArgumentType.Integer("number");

addSyntax((sender, context) -> {
final int number = context.get(numberArgument);
sender.sendMessage("You typed the number " + number);
}, numberArgument);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/slampvp/factory/common/Locale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.slampvp.factory.common;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;

public final class Locale {
public static final class Command {
public static final TextComponent INVALID_COMMAND = Component.text("Usage: <usage>").color(NamedTextColor.RED);
public static final TextComponent INVALID_PERMISSIONS = Component.text("You don't have the permissions to execute this command!").color(NamedTextColor.RED);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/slampvp/factory/player/Rank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.slampvp.factory.player;

public enum Rank {
DEFAULT(1);

private final int weight;

Rank(int weight) {
this.weight = weight;
}

public int getWeight() {
return weight;
}

public boolean isEqualOrHigherThan(Rank rank) {
return this.getWeight() <= rank.getWeight();
}
}