Skip to content

Commit

Permalink
Check slots before modification
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Jul 22, 2024
1 parent d21610b commit 7c00ea9
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ static void error(String translationKey) {
static <T extends AbstractContainerMenu> void returnSlotsToPlayerInventory(MenuInfoContext<T, ?, ?> context, DumpHandler<T, ?> dumpHandler, SlotAccessor slotAccessor) {
ItemStack stackToReturn = slotAccessor.getItemStack();
if (!stackToReturn.isEmpty()) {
if (!slotAccessor.allowModification(context.getPlayerEntity())) {
error("rei.rei.no.slot.in.inv");
}

for (; !(stackToReturn = slotAccessor.getItemStack()).isEmpty(); slotAccessor.takeStack(1)) {
ItemStack stackToInsert = stackToReturn.copy();
stackToInsert.setCount(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface DumpHandler<T extends AbstractContainerMenu, D extends Display>

static SlotAccessor getOccupiedSlotWithRoomForStack(ItemStack stack, Iterable<SlotAccessor> inventoryStacks) {
for (SlotAccessor inventoryStack : inventoryStacks) {
if (canStackAddMore(inventoryStack.getItemStack(), stack)) {
if (canStackAddMore(inventoryStack.getItemStack(), stack) && inventoryStack.canPlace(stack)) {
return inventoryStack;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package me.shedaniel.rei.api.common.transfer.info.stack;

import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

/**
Expand Down Expand Up @@ -53,4 +54,14 @@ public void setItemStack(ItemStack stack) {
public ItemStack takeStack(int amount) {
return this.container.removeItem(index, amount);
}

@Override
public boolean allowModification(Player player) {
return canPlace(getItemStack());
}

@Override
public boolean canPlace(ItemStack stack) {
return this.container.canPlaceItem(index, stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public interface SlotAccessor {

ItemStack takeStack(int amount);

default boolean allowModification(Player player) {
return true;
}

default boolean canPlace(ItemStack stack) {
return true;
}

static SlotAccessor fromSlot(Slot slot) {
return new VanillaSlotAccessor(slot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package me.shedaniel.rei.api.common.transfer.info.stack;

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;

Expand Down Expand Up @@ -54,4 +55,14 @@ public ItemStack takeStack(int amount) {
public Slot getSlot() {
return slot;
}

@Override
public boolean allowModification(Player player) {
return slot.allowModification(player);
}

@Override
public boolean canPlace(ItemStack stack) {
return slot.mayPlace(stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ protected void fillInputSlot(SlotAccessor slot, ItemStack toBeTakenStack) {
}

takenStack.setCount(1);
if (!slot.canPlace(takenStack)) {
return;
}

if (slot.getItemStack().isEmpty()) {
slot.setItemStack(takenStack);
} else {
Expand Down Expand Up @@ -148,13 +152,22 @@ protected void fillInputSlots(RecipeFinder recipeFinder, NonNullList<Ingredient>

@Nullable
public SlotAccessor takeInventoryStack(ItemStack itemStack) {
boolean rejectedModification = false;
for (SlotAccessor inventoryStack : inventoryStacks) {
ItemStack itemStack1 = inventoryStack.getItemStack();
if (!itemStack1.isEmpty() && areItemsEqual(itemStack, itemStack1) && !itemStack1.isDamaged() && !itemStack1.isEnchanted() && !itemStack1.hasCustomHoverName()) {
return inventoryStack;
if (!inventoryStack.allowModification(player)) {
rejectedModification = true;
} else {
return inventoryStack;
}
}
}

if (rejectedModification) {
throw new IllegalStateException("Unable to take item from inventory due to slot not allowing modification! Item requested: " + itemStack);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ protected void markDirty() {
protected void cleanInputs() {
for (SlotAccessor slot : getInputSlots()) {
ItemStack stackToReturn = slot.getItemStack();
if (!slot.allowModification(player)) {
throw new IllegalStateException("Slot " + slot + " is not modifiable!");
}
if (!stackToReturn.isEmpty()) {
for (; !(stackToReturn = slot.getItemStack()).isEmpty(); slot.takeStack(1)) {
ItemStack stackToInsert = stackToReturn.copy();
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pluginManagement {
repositories {
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.fabricmc.net/" }
maven { url "https://files.minecraftforge.net/maven/" }
gradlePluginPortal()
maven { url "https://files.minecraftforge.net/maven/" }
}
}

Expand Down

0 comments on commit 7c00ea9

Please sign in to comment.