Sit/src/main/java/one/oth3r/sit/SitCommand.java

93 lines
4.4 KiB
Java
Raw Normal View History

2023-07-20 20:17:52 -05:00
package one.oth3r.sit;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
2023-08-02 23:04:36 -05:00
import net.minecraft.entity.EntityType;
import net.minecraft.entity.decoration.DisplayEntity;
2023-07-20 20:17:52 -05:00
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TextColor;
import net.minecraft.util.Formatting;
2023-08-02 23:04:36 -05:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2023-07-20 20:17:52 -05:00
import java.util.concurrent.CompletableFuture;
public class SitCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("sit")
2023-08-02 23:04:36 -05:00
.requires((commandSource) -> commandSource.hasPermissionLevel(0))
2023-07-20 20:17:52 -05:00
.executes((context2) -> command(context2.getSource(), context2.getInput()))
.then(CommandManager.argument("args", StringArgumentType.string())
2023-08-02 23:04:36 -05:00
.requires((commandSource) -> commandSource.hasPermissionLevel(2))
2023-07-20 20:17:52 -05:00
.suggests(SitCommand::getSuggestions)
.executes((context2) -> command(context2.getSource(), context2.getInput()))));
}
public static CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
builder.suggest("reload");
builder.suggest("purgeChairEntities");
return builder.buildFuture();
}
private static int command(ServerCommandSource source, String arg) {
ServerPlayerEntity player = source.getPlayer();
//trim all the arguments before the command
String keyword = "sit";
int index = Integer.MAX_VALUE;
if (arg.contains(keyword)) index = arg.indexOf(keyword);
//trims the words before the text
if (index != Integer.MAX_VALUE) arg = arg.substring(index).trim();
String[] args = arg.split(" ");
if (args[0].equalsIgnoreCase("sit"))
args = arg.replaceFirst("sit ", "").split(" ");
2023-11-25 10:35:51 -06:00
// if console
if (player == null) {
if (args[0].equalsIgnoreCase("reload")) {
config.load();
2024-03-10 23:02:30 -05:00
Sit.LOGGER.info(Sit.lang("key.sit.command.reloaded").getString());
2023-11-25 10:35:51 -06:00
}
return 1;
}
2023-08-02 23:04:36 -05:00
if (args[0].equalsIgnoreCase("sit")) {
BlockPos pos = player.getBlockPos();
if (!(player.getY() -((int) player.getY()) > 0.00)) {
pos = pos.add(0,-1,0);
}
World world = player.getWorld();
2023-11-25 10:36:50 -06:00
// if already sitting, ignore
if (Events.entities.containsKey(player)) return 1;
// make entity first to check the blocks
DisplayEntity.TextDisplayEntity entity = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY,player.getServerWorld());
Events.setEntity(pos,world,entity);
if (Events.checkBlocks(pos,world,Events.isAboveBlockheight(entity))) {
2023-08-02 23:04:36 -05:00
player.getServerWorld().spawnEntity(entity);
player.startRiding(entity);
Events.entities.put(player,entity);
return 1;
}
}
2023-07-20 20:17:52 -05:00
if (args[0].equalsIgnoreCase("reload")) {
config.load();
2024-03-10 23:02:30 -05:00
player.sendMessage(Sit.lang("key.sit.command.reloaded").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN))));
2023-07-20 20:17:52 -05:00
}
if (args[0].equalsIgnoreCase("purgeChairEntities")) {
String cmd = "kill @e[type=minecraft:text_display,name=\""+Sit.ENTITY_NAME+"\"]";
try {
ParseResults<ServerCommandSource> parse =
Sit.commandManager.getDispatcher().parse(cmd, player.getCommandSource());
Sit.commandManager.getDispatcher().execute(parse);
2024-03-10 23:02:30 -05:00
player.sendMessage(Sit.lang("key.sit.command.purged"));
2023-07-20 20:17:52 -05:00
} catch (CommandSyntaxException e) {
2024-03-10 23:02:30 -05:00
player.sendMessage(Sit.lang("key.sit.command.purged"));
2023-07-20 20:17:52 -05:00
e.printStackTrace();
}
}
return 1;
}
}