Skip to content

Commit

Permalink
Added clear command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro270707 committed Sep 20, 2023
1 parent cfa7b17 commit 708ad85
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ public void sendMessage(String message) {
public World getWorld() {
return this.getSender().world;
}

@Override
public @Deprecated CommandHandler getCommandHandler() {
return this.mc.commandHandler;
}

@Override
public @Deprecated CommandSender getCommandSender() {
return this.mc.thePlayer.sender;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.core.net.command.Command;
import net.minecraft.core.net.command.Commands;
import net.pedroricardo.commander.content.commands.*;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;

@SuppressWarnings("unchecked")
public class CommanderCommandManager {
public static int FAILURE = 0;
public static int SINGLE_SUCCESS = 1;
private static final CommandDispatcher<CommanderCommandSource> DISPATCHER = new CommandDispatcher<>();

static {
AchievementCommand.register(DISPATCHER);
SummonCommand.register(DISPATCHER);
SetBlockCommand.register(DISPATCHER);
ClearCommand.register(DISPATCHER);
KillCommand.register(DISPATCHER);
SeedCommand.register(DISPATCHER);
SetBlockCommand.register(DISPATCHER);
SummonCommand.register(DISPATCHER);

TestCommand.register(DISPATCHER);
registerLegacyCommands();

// TestCommand.register(DISPATCHER);
}

public static void execute(String s, CommanderCommandSource commandSource) throws CommandSyntaxException {
Expand All @@ -43,6 +54,19 @@ public static <S> CommandSyntaxException getParseException(ParseResults<S> parse
return CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(parseResults.getReader());
}

private static void registerLegacyCommands() {
for (Command command : Commands.commands) {
if (DISPATCHER.findNode(Collections.singletonList(command.getName())) == null) {
DISPATCHER.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal(command.getName())
.then(RequiredArgumentBuilder.argument("command", StringArgumentType.greedyString())
.executes(c -> {
Commands.getCommand(command.getName()).execute(((CommanderCommandSource) c.getSource()).getCommandHandler(), ((CommanderCommandSource) c.getSource()).getCommandSender(), c.getArgument("command", String.class).split(" "));
return SINGLE_SUCCESS;
})));
}
}
}

public static void init() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ default Collection<String> getEntitySuggestions() {
void sendMessage(String message);

World getWorld();

@Deprecated CommandHandler getCommandHandler();

@Deprecated CommandSender getCommandSender();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public void sendMessage(String message) {

@Override
public World getWorld() {
assert this.getSender() != null;
return this.getSender().world;
}

@Override
public @Deprecated CommandHandler getCommandHandler() {
return this.server.serverCommandHandler;
}

@Override
public @Deprecated CommandSender getCommandSender() {
return new ServerPlayerCommandSender(this.server, this.player);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package net.pedroricardo.commander.content.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.core.achievement.Achievement;
import net.minecraft.core.achievement.AchievementList;
import net.minecraft.core.entity.Entity;
import net.minecraft.core.entity.EntityLiving;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.lang.I18n;
import net.pedroricardo.commander.Commander;
import net.pedroricardo.commander.content.CommanderCommandManager;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.arguments.AchievementArgumentType;
import net.pedroricardo.commander.content.arguments.EntityArgumentType;
import net.pedroricardo.commander.content.exceptions.CommanderExceptions;
import net.pedroricardo.commander.content.helpers.EntitySelector;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

// TODO: add `/clear <entity> [item]` once item stack argument type is added

@SuppressWarnings("unchecked")
public class ClearCommand {
public static void register(CommandDispatcher<CommanderCommandSource> dispatcher) {
dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("clear")
.requires(source -> ((CommanderCommandSource)source).hasAdmin())
.executes(c -> {
if (((CommanderCommandSource)c.getSource()).getSender() == null) throw CommanderExceptions.notInWorld().create();

EntityPlayer sender = ((CommanderCommandSource)c.getSource()).getSender();

int itemsCleared = clearItemsFromEntity(sender);

((CommanderCommandSource) c.getSource()).sendMessage(getMessage(itemsCleared, Collections.singletonList(sender)));

return CommanderCommandManager.SINGLE_SUCCESS;
})
.then(RequiredArgumentBuilder.argument("players", EntityArgumentType.players())
.executes(c -> {
List<? extends Entity> players = c.getArgument("players", EntitySelector.class).get(((CommanderCommandSource)c.getSource()));

int itemsCleared = 0;

for (Entity player : players) {
itemsCleared += clearItemsFromEntity((EntityPlayer)player);
}

((CommanderCommandSource) c.getSource()).sendMessage(getMessage(itemsCleared, players));

return CommanderCommandManager.SINGLE_SUCCESS;
})));
}

private static int clearItemsFromEntity(EntityPlayer entityPlayer) {
int itemsCleared = 0;

for (int i = 0; i < entityPlayer.inventory.getSizeInventory(); ++i) {
if (entityPlayer.inventory.getStackInSlot(i) != null) {
itemsCleared++;
entityPlayer.inventory.setInventorySlotContents(i, null);
}
}

return itemsCleared;
}

private static String getMessage(int itemsCleared, List<? extends Entity> players) {
if (itemsCleared == 0) return I18n.getInstance().translateKey("commands.commander.clear.failure");

StringBuilder keyBuilder = new StringBuilder("commands.commander.clear.success_");
if (itemsCleared > 1) {
keyBuilder.append("multiple_items_");
} else if (itemsCleared == 1) {
keyBuilder.append("single_item_");
}

if (players.size() > 1) {
keyBuilder.append("multiple_entities");
} else if (players.size() == 1) {
keyBuilder.append("single_entity");
}

return I18n.getInstance().translateKeyAndFormat(keyBuilder.toString(), itemsCleared, players.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,51 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.core.entity.Entity;
import net.minecraft.core.util.phys.Vec3d;
import net.minecraft.core.world.World;
import net.pedroricardo.commander.content.CommanderCommandManager;
import net.pedroricardo.commander.content.CommanderCommandSource;
import net.pedroricardo.commander.content.arguments.EntitySummonArgumentType;
import net.pedroricardo.commander.content.arguments.Vec3dArgumentType;
import net.pedroricardo.commander.content.exceptions.CommanderExceptions;
import net.pedroricardo.commander.content.helpers.DoubleCoordinates;

import java.util.Objects;

@SuppressWarnings("unchecked")
public class SummonCommand {
public static void register(CommandDispatcher<CommanderCommandSource> dispatcher) {
dispatcher.register((LiteralArgumentBuilder)LiteralArgumentBuilder.literal("summon")
.requires(c -> ((CommanderCommandSource)c).hasAdmin())
.then(RequiredArgumentBuilder.argument("entity", EntitySummonArgumentType.entity())
.executes(c -> {
Class<? extends Entity> entityClass = c.getArgument("entity", Class.class);
Entity entity;
try {
entity = entityClass.getConstructor(World.class).newInstance(((CommanderCommandSource)c.getSource()).getWorld());
} catch (Exception e) {
throw new RuntimeException(e);
}
entity.spawnInit();
entity.moveTo(Objects.requireNonNull(((CommanderCommandSource) c.getSource()).getSender()).x, ((CommanderCommandSource)c.getSource()).getSender().y - 1.6, ((CommanderCommandSource)c.getSource()).getSender().z, 0.0f, 0.0f);
((CommanderCommandSource)c.getSource()).getWorld().entityJoinedWorld(entity);
Vec3d coordinates = ((CommanderCommandSource)c.getSource()).getCoordinates();
if (coordinates == null) throw CommanderExceptions.notInWorld().create();

summonEntityAt(c, coordinates.xCoord, coordinates.yCoord - 1.6, coordinates.zCoord, 0.0f, 0.0f);

return CommanderCommandManager.SINGLE_SUCCESS;
})
.then(RequiredArgumentBuilder.argument("pos", Vec3dArgumentType.vec3d())
.executes(c -> {
Class<? extends Entity> entityClass = c.getArgument("entity", Class.class);
DoubleCoordinates coordinates = c.getArgument("pos", DoubleCoordinates.class);
Entity entity;
try {
entity = entityClass.getConstructor(World.class).newInstance(((CommanderCommandSource)c.getSource()).getWorld());
} catch (Exception e) {
throw new RuntimeException(e);
}
entity.spawnInit();
entity.moveTo(coordinates.getX(((CommanderCommandSource)c.getSource())), coordinates.getY(((CommanderCommandSource)c.getSource()), true), coordinates.getZ(((CommanderCommandSource)c.getSource())), 0.0f, 0.0f);
((CommanderCommandSource)c.getSource()).getWorld().entityJoinedWorld(entity);

summonEntityAt(c, coordinates.getX(((CommanderCommandSource)c.getSource())), coordinates.getY(((CommanderCommandSource)c.getSource()), true), coordinates.getZ(((CommanderCommandSource)c.getSource())), 0.0f, 0.0f);

return CommanderCommandManager.SINGLE_SUCCESS;
}))));
}

private static void summonEntityAt(CommandContext<Object> c, double x, double y, double z, float yaw, float pitch) {
Class<? extends Entity> entityClass = c.getArgument("entity", Class.class);
Entity entity;
try {
entity = entityClass.getConstructor(World.class).newInstance(((CommanderCommandSource)c.getSource()).getWorld());
} catch (Exception e) {
throw new RuntimeException(e);
}
entity.spawnInit();
entity.moveTo(x, y, z, yaw, pitch);
((CommanderCommandSource)c.getSource()).getWorld().entityJoinedWorld(entity);
}
}
5 changes: 5 additions & 0 deletions src/main/resources/lang/commander/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ commands.commander.achievement.grant.success_multiple_entities=Granted achieveme
commands.commander.achievement.grant.success_single_entity=Granted achievement §1[%s]§r to %s
commands.commander.achievement.grant.failure_empty_selector=No entity was found to grant achievement
commands.commander.seed.success=Seed: §5[%s]§r
commands.commander.clear.success_single_item_single_entity=%s item slot cleared
commands.commander.clear.success_multiple_items_single_entity=%s item slots cleared
commands.commander.clear.success_single_item_multiple_entities=%s item slot cleared from %s entities
commands.commander.clear.success_multiple_items_multiple_entities=%s item slots cleared from %s entities
commands.commander.clear.failure=No items were cleared
argument_types.commander.incomplete=Incomplete argument
argument_types.commander.not_in_world=Argument requires sender to be in world, but they are not present

0 comments on commit 708ad85

Please sign in to comment.