Skip to content

Commit

Permalink
Merge pull request #638 from P3pp3rF1y/1.20.x-dev
Browse files Browse the repository at this point in the history
Release merge
  • Loading branch information
P3pp3rF1y authored Jul 26, 2023
2 parents a4bdab3 + 7607a0b commit 32deb62
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 86 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ dependencies {
runtimeOnly fg.deobf("top.theillusivec4.curios:curios-forge:${curios_version}")
compileOnly fg.deobf("curse.maven:jade-324717:${jade_cf_file_id}")
runtimeOnly fg.deobf("curse.maven:jade-324717:${jade_cf_file_id}")
runtimeOnly fg.deobf("curse.maven:twilightforest-227639:4337394")

// compileOnly fg.deobf("curse.maven:tinkers-74072:3576393")
// runtimeOnly fg.deobf("curse.maven:tinkers-74072:3576393")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.jvmargs=-Xmx2048m
minecraft_version=1.20.1
forge_version=47.1.42
mod_version=2.0.34
mod_version=2.0.36
jei_mc_version=1.20.1-forge
jei_version=15.1.0.19
curios_version=5.2.0-beta.3+1.20.1
Expand Down
98 changes: 23 additions & 75 deletions src/main/java/reliquary/blocks/tile/PedestalBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import reliquary.api.IPedestal;
import reliquary.api.IPedestalActionItem;
import reliquary.api.IPedestalRedstoneItem;
Expand All @@ -27,6 +26,7 @@
import reliquary.init.ModBlocks;
import reliquary.items.util.FilteredItemStackHandler;
import reliquary.pedestal.PedestalRegistry;
import reliquary.util.CombinedItemHandler;
import reliquary.util.InventoryHelper;
import reliquary.util.WorldHelper;
import reliquary.util.XRFakePlayerFactory;
Expand All @@ -37,7 +37,6 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

public class PedestalBlockEntity extends PassivePedestalBlockEntity implements IPedestal {
private boolean tickable = false;
Expand All @@ -48,6 +47,8 @@ public class PedestalBlockEntity extends PassivePedestalBlockEntity implements I
private IPedestalRedstoneItem redstoneItem = null;
@Nullable
private IItemHandler itemHandler = null;
@Nullable
private LazyOptional<IItemHandler> combinedHandler = null;
private ItemStack fluidContainer = ItemStack.EMPTY;
private boolean switchedOn = false;
private final List<Long> onSwitches = new ArrayList<>();
Expand Down Expand Up @@ -128,6 +129,13 @@ public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction
pedestalFluidHandler = new PedestalFluidHandler(this);
}
return ForgeCapabilities.FLUID_HANDLER.orEmpty(cap, LazyOptional.of(() -> pedestalFluidHandler));
} else if (cap == ForgeCapabilities.ITEM_HANDLER && itemHandler != null) {
if (combinedHandler == null) {
combinedHandler = LazyOptional.of(() -> super.getCapability(ForgeCapabilities.ITEM_HANDLER, side)
.map(superHandler -> (IItemHandler) new CombinedItemHandler(superHandler, itemHandler))
.orElse(itemHandler));
}
return combinedHandler.cast();
}

return super.getCapability(cap, side);
Expand Down Expand Up @@ -185,6 +193,10 @@ private void resetSpecialItems() {
actionItem = null;
redstoneItem = null;
itemHandler = null;
if (combinedHandler != null) {
combinedHandler.invalidate();
}
combinedHandler = null;
}

public void serverTick(Level level) {
Expand Down Expand Up @@ -304,6 +316,13 @@ public void setItem(ItemStack stack) {
updateItemsAndBlock();
}

@Override
public void setItem(int slot, ItemStack stack) {
if (slot == 0) {
setItem(stack);
}
}

@Override
public List<BlockPos> getPedestalsInRange(Level level, int range) {
return PedestalRegistry.getPositionsInRange(level.dimension().registry(), worldPosition, range);
Expand Down Expand Up @@ -395,41 +414,13 @@ public void removeAndSpawnItem(Level level) {
super.removeAndSpawnItem(level);
}

@Override
public int getContainerSize() {
return applyToItemHandler(IItemHandler::getSlots).orElse(0) + 1;
}

private <T> Optional<T> applyToItemHandler(Function<IItemHandler, T> function) {
return itemHandler != null ? Optional.of(function.apply(itemHandler)) : Optional.empty();
}

private void runOnItemHandler(Consumer<IItemHandler> consumer) {
if (itemHandler != null) {
consumer.accept(itemHandler);
}
}

@Override
public ItemStack getItem(int slot) {
if (slot == 0) {
return item;
}

return applyToItemHandler(ih -> ih.getStackInSlot(getInternalItemHandlerSlot(slot))).orElse(ItemStack.EMPTY);
}

private int getInternalItemHandlerSlot(int slot) {
return slot - 1;
}

@Override
public ItemStack removeItem(int slot, int count) {
if (slot == 0) {
return decrStack(count);
}

return applyToItemHandler(ih -> ih.extractItem(getInternalItemHandlerSlot(slot), count, false)).orElse(ItemStack.EMPTY);
return ItemStack.EMPTY;
}

private ItemStack decrStack(int count) {
Expand Down Expand Up @@ -478,50 +469,7 @@ public ItemStack removeItemNoUpdate(int slot) {
return stack;
}

return applyToItemHandler(ih -> {
int s = getInternalItemHandlerSlot(slot);
ItemStack stack = ih.getStackInSlot(s);
return ih.extractItem(s, stack.getCount(), false);
}).orElse(ItemStack.EMPTY);
}

@Override
public void setItem(int slot, ItemStack stack) {
if (slot == 0) {
setItem(stack);
return;
}

runOnItemHandler(ih -> setItemHandlerSlotContents(slot, stack, ih));
}

private void setItemHandlerSlotContents(int slot, ItemStack stack, IItemHandler ih) {
int adjustedSlot = getInternalItemHandlerSlot(slot);
ItemStack stackInSlot = ih.getStackInSlot(adjustedSlot);

if (!stackInSlot.isEmpty() && !stack.isEmpty() && !ItemHandlerHelper.canItemStacksStack(stack, stackInSlot)) {
return;
}

if (!stackInSlot.isEmpty() && (stack.isEmpty() || stack.getCount() < stackInSlot.getCount())) {
int amount = stackInSlot.getCount() - (stack.isEmpty() ? 0 : stack.getCount());
ih.extractItem(adjustedSlot, amount, false);
} else if (!stack.isEmpty() && (stackInSlot.isEmpty() || stack.getCount() > stackInSlot.getCount())) {
stack.shrink(stackInSlot.isEmpty() ? 0 : stackInSlot.getCount());
ih.insertItem(adjustedSlot, stack, false);
}
}

@Override
public boolean canPlaceItem(int index, ItemStack stack) {
if (super.canPlaceItem(index, stack)) {
return true;
}

return applyToItemHandler(ih -> {
ItemStack returnedStack = ih.insertItem(getInternalItemHandlerSlot(index), stack, true);
return returnedStack.isEmpty() || returnedStack.getCount() != stack.getCount();
}).orElse(false);
return ItemStack.EMPTY;
}

public void toggleSwitch(Level level) {
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/reliquary/crafting/FragmentRecipeHelper.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package reliquary.crafting;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import net.minecraftforge.common.ForgeSpawnEggItem;
import net.minecraftforge.registries.ForgeRegistries;
import reliquary.init.ModItems;
import reliquary.items.MobCharmFragmentItem;

import java.util.Map;
import java.util.Optional;

public class FragmentRecipeHelper {
Expand Down Expand Up @@ -51,13 +49,7 @@ public static Optional<String> getRegistryName(CraftingContainer inv) {
}

public static ItemStack getSpawnEggStack(String regName) {
Map<EntityType<?>, SpawnEggItem> spawnEggs = ObfuscationReflectionHelper.getPrivateValue(SpawnEggItem.class, null, "f_43201_");

if (spawnEggs == null) {
return new ItemStack(FALL_BACK_SPAWN_EGG);
}

SpawnEggItem spawnEggItem = spawnEggs.get(ForgeRegistries.ENTITY_TYPES.getValue(new ResourceLocation(regName)));
SpawnEggItem spawnEggItem = ForgeSpawnEggItem.fromEntityType(ForgeRegistries.ENTITY_TYPES.getValue(new ResourceLocation(regName)));
return new ItemStack(spawnEggItem == null ? FALL_BACK_SPAWN_EGG : spawnEggItem);
}
}
99 changes: 99 additions & 0 deletions src/main/java/reliquary/util/CombinedItemHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package reliquary.util;

import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.EmptyHandler;
import org.jetbrains.annotations.NotNull;

// combines multiple IItemHandler into one interface
public class CombinedItemHandler implements IItemHandler {

protected final IItemHandler[] itemHandler; // the handlers
protected final int[] baseIndex; // index-offsets of the different handlers
protected final int slotCount; // number of total slots

public CombinedItemHandler(IItemHandler... itemHandler) {
this.itemHandler = itemHandler;
baseIndex = new int[itemHandler.length];
int index = 0;
for (int i = 0; i < itemHandler.length; i++) {
index += itemHandler[i].getSlots();
baseIndex[i] = index;
}
slotCount = index;
}

// returns the handler index for the slot
protected int getIndexForSlot(int slot) {
if (slot < 0) {return -1;}

for (int i = 0; i < baseIndex.length; i++) {
if (slot - baseIndex[i] < 0) {
return i;
}
}
return -1;
}

protected IItemHandler getHandlerFromIndex(int index) {
if (index < 0 || index >= itemHandler.length) {
return EmptyHandler.INSTANCE;
}
return itemHandler[index];
}

protected int getSlotFromIndex(int slot, int index) {
if (index <= 0 || index >= baseIndex.length) {
return slot;
}
return slot - baseIndex[index - 1];
}

@Override
public int getSlots() {
return slotCount;
}

@Override
@NotNull
public ItemStack getStackInSlot(int slot) {
int index = getIndexForSlot(slot);
IItemHandler handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(slot, index);
return handler.getStackInSlot(slot);
}

@Override
@NotNull
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
int index = getIndexForSlot(slot);
IItemHandler handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(slot, index);
return handler.insertItem(slot, stack, simulate);
}

@Override
@NotNull
public ItemStack extractItem(int slot, int amount, boolean simulate) {
int index = getIndexForSlot(slot);
IItemHandler handler = getHandlerFromIndex(index);
slot = getSlotFromIndex(slot, index);
return handler.extractItem(slot, amount, simulate);
}

@Override
public int getSlotLimit(int slot) {
int index = getIndexForSlot(slot);
IItemHandler handler = getHandlerFromIndex(index);
int localSlot = getSlotFromIndex(slot, index);
return handler.getSlotLimit(localSlot);
}

@Override
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
int index = getIndexForSlot(slot);
IItemHandler handler = getHandlerFromIndex(index);
int localSlot = getSlotFromIndex(slot, index);
return handler.isItemValid(localSlot, stack);
}
}

0 comments on commit 32deb62

Please sign in to comment.