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 b171391 commit d5427ca
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/curseforge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
with:
java-version: 17
- name: Upload to CurseForge
run: ./gradlew build publish forge:curseforge fabric:curseforge jei-internals-workaround:curseforge --stacktrace
run: ./gradlew build publish fabric:curseforge --stacktrace
env:
danielshe_curse_api_key: ${{ secrets.CF_API_KEY }}
BRANCH_NAME: ${{ github.ref }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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.getCount() > 0; slotAccessor.takeStack(1)) {
ItemStack stackToInsert = stackToReturn.copy();
stackToInsert.setCount(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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.inventory.Slot;
import net.minecraft.world.item.ItemStack;

Expand All @@ -37,6 +38,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 @@ -50,4 +51,14 @@ public void setItemStack(ItemStack stack) {
public ItemStack takeStack(int amount) {
return slot.remove(amount);
}

@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 @@ -128,6 +128,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 @@ -161,13 +165,22 @@ protected void cleanInputs() {

@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
8 changes: 4 additions & 4 deletions 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 All @@ -13,7 +13,7 @@ include "api"
include "default-plugin"
include "runtime"
include "fabric"
include "forge"
//include "forge"

include "jei-compatibility-layer"
include "jei-internals-workaround"
//include "jei-compatibility-layer"
//include "jei-internals-workaround"

0 comments on commit d5427ca

Please sign in to comment.