From 161b5d491db44808f9aaa089d42ebbd4496651fd Mon Sep 17 00:00:00 2001 From: gnembon Date: Sun, 9 Feb 2020 01:56:30 -0400 Subject: [PATCH] added options for drop command --- .../java/carpet/commands/PlayerCommand.java | 15 +++++++++++ .../helpers/EntityPlayerActionPack.java | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/main/java/carpet/commands/PlayerCommand.java b/src/main/java/carpet/commands/PlayerCommand.java index 47c63e59..4440efb4 100644 --- a/src/main/java/carpet/commands/PlayerCommand.java +++ b/src/main/java/carpet/commands/PlayerCommand.java @@ -53,7 +53,9 @@ public class PlayerCommand .then(makeActionCommand("jump", EntityPlayerActionPack.ActionType.JUMP)) .then(makeActionCommand("attack", EntityPlayerActionPack.ActionType.ATTACK)) .then(makeActionCommand("drop", EntityPlayerActionPack.ActionType.DROP_ITEM)) + .then(makeDropCommand("drop", false)) .then(makeActionCommand("dropStack", EntityPlayerActionPack.ActionType.DROP_STACK)) + .then(makeDropCommand("dropStack", true)) .then(makeActionCommand("swapHands", EntityPlayerActionPack.ActionType.SWAP_HANDS)) .then(literal("kill").executes(PlayerCommand::kill)) .then(literal("shadow"). executes(PlayerCommand::shadow)) @@ -105,6 +107,19 @@ public class PlayerCommand .executes(c -> action(c, type, EntityPlayerActionPack.Action.interval(IntegerArgumentType.getInteger(c, "ticks")))))); } + private static LiteralArgumentBuilder makeDropCommand(String actionName, boolean dropAll) + { + return literal(actionName) + .then(literal("all").executes(c ->manipulate(c, ap -> ap.drop(-2,dropAll)))) + .then(literal("mainhand").executes(c ->manipulate(c, ap -> ap.drop(-1,dropAll)))) + .then(literal("offhand").executes(c ->manipulate(c, ap -> ap.drop(40,dropAll)))) + .then(argument("slot", IntegerArgumentType.integer(0, 40)). + executes(c ->manipulate(c, ap -> ap.drop( + IntegerArgumentType.getInteger(c,"slot"), + dropAll + )))); + } + private static Collection getPlayers(ServerCommandSource source) { Set players = Sets.newLinkedHashSet(Arrays.asList("Steve", "Alex")); diff --git a/src/main/java/carpet/helpers/EntityPlayerActionPack.java b/src/main/java/carpet/helpers/EntityPlayerActionPack.java index 0b0d2d1f..6d6b4670 100644 --- a/src/main/java/carpet/helpers/EntityPlayerActionPack.java +++ b/src/main/java/carpet/helpers/EntityPlayerActionPack.java @@ -4,6 +4,7 @@ import carpet.fakes.ServerPlayerEntityInterface; import net.minecraft.block.BlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.passive.HorseBaseEntity; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.vehicle.BoatEntity; import net.minecraft.entity.vehicle.MinecartEntity; import net.minecraft.item.ItemStack; @@ -222,6 +223,31 @@ public class EntityPlayerActionPack return Tracer.rayTrace(player, 1, reach, false); } + private void dropItemFromSlot(int slot, boolean dropAll) + { + PlayerInventory inv = player.inventory; + if (!inv.getInvStack(slot).isEmpty()) + player.dropItem(inv.takeInvStack(slot, + dropAll ? inv.getInvStack(slot).getCount() : 1 + ), false, true); // scatter, keep owner + } + + public void drop(int selectedSlot, boolean dropAll) + { + PlayerInventory inv = player.inventory; + if (selectedSlot == -2) // all + { + for (int i = inv.getInvSize(); i >= 0; i--) + dropItemFromSlot(i, dropAll); + } + else // one slot + { + if (selectedSlot == -1) + selectedSlot = inv.selectedSlot; + dropItemFromSlot(selectedSlot, dropAll); + } + } + public enum ActionType { USE(true)