Skip to content

Commit

Permalink
Fixes that /fzmm fullcontainer, if the first slot is not specified,…
Browse files Browse the repository at this point in the history
… replaces the items from the first slot, when it should add them
  • Loading branch information
Zailer43 committed Jul 8, 2024
1 parent 4de1ccb commit 953e971
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions src/main/java/fzmm/zailer/me/client/FzmmCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import fzmm.zailer.me.utils.skin.GetSkinFromMojang;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.block.entity.ShulkerBoxBlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.network.ClientPlayerEntity;
Expand Down Expand Up @@ -259,7 +260,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
.executes(ctx -> sendHelpMessage("commands.fzmm.fullcontainer.help", BASE_COMMAND + " fullcontainer <slots to fill> <first slot>"))
.then(ClientCommandManager.argument("slots to fill", IntegerArgumentType.integer(1, 27)).executes(ctx -> {

fullContainer(ctx.getArgument("slots to fill", int.class), 0);
fullContainer(ctx.getArgument("slots to fill", int.class), -1);
return 1;

}).then(ClientCommandManager.argument("first slot", IntegerArgumentType.integer(0, 27)).executes(ctx -> {
Expand Down Expand Up @@ -567,7 +568,10 @@ private static void getHead(GetSkinDecorator skinDecorator, String playerName) {
}));
}

private static void fullContainer(int slotsToFill, int firstSlots) {
/**
* @param firstSlot if -1, it will fill empty slots starting at 0
*/
private static void fullContainer(int slotsToFill, int firstSlot) {
MinecraftClient client = MinecraftClient.getInstance();
assert client.player != null;

Expand All @@ -576,15 +580,11 @@ private static void fullContainer(int slotsToFill, int firstSlots) {

containerStack.apply(DataComponentTypes.CONTAINER, ContainerComponent.DEFAULT, component -> {
List<ItemStack> stacksCopy = new ArrayList<>(component.stream().toList());
int finalSlot = firstSlots + slotsToFill;
if (slotsToFill > stacksCopy.size()) {
for (int i = stacksCopy.size(); i < finalSlot; i++) {
stacksCopy.add(ItemStack.EMPTY);
}
}

for (int i = firstSlots; i != finalSlot; i++) {
stacksCopy.set(i, itemStack);
if (firstSlot == -1) {
fullContainerEmptySlots(stacksCopy, itemStack, slotsToFill);
} else {
fullContainer(stacksCopy, itemStack, slotsToFill, firstSlot);
}

return ContainerComponent.fromStacks(stacksCopy);
Expand All @@ -593,6 +593,40 @@ private static void fullContainer(int slotsToFill, int firstSlots) {
FzmmUtils.giveItem(containerStack);
}

private static void fullContainer(List<ItemStack> stackList, ItemStack stack, int slotsToFill, int firstSlot) {
int finalSlot = firstSlot + slotsToFill;
if (slotsToFill > stackList.size()) {
for (int i = stackList.size(); i < finalSlot; i++) {
stackList.add(ItemStack.EMPTY);
}
}

for (int i = firstSlot; i != finalSlot; i++) {
stackList.set(i, stack);
}
}

private static void fullContainerEmptySlots(List<ItemStack> stackList, ItemStack stack, int slotsToFill) {
int finalSlot = Math.min(stackList.size() + slotsToFill, ShulkerBoxBlockEntity.INVENTORY_SIZE);
if (finalSlot > stackList.size()) {
for (int i = stackList.size(); i < finalSlot; i++) {
stackList.add(ItemStack.EMPTY);
}
}

for (int i = 0; i != finalSlot; i++) {
if (stackList.get(i).isEmpty()) {
stackList.set(i, stack);
slotsToFill--;
}

if (slotsToFill == 0) {
break;
}
}
}


private static void lockContainer(String key) {
MinecraftClient client = MinecraftClient.getInstance();
assert client.player != null;
Expand Down

0 comments on commit 953e971

Please sign in to comment.