Skip to content

Commit

Permalink
summoning table inventory improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
afamiliarquiet committed Oct 28, 2024
1 parent 12e8a79 commit 5b006bb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
Expand Down Expand Up @@ -180,4 +181,16 @@ protected boolean isPathfindable(BlockState state, PathComputationType pathCompu
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(SUMMONING_TABLE_STATE);
}

@Override
protected void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean movedByPiston) {
// tell the stuff in the inventory to bounce
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof SummoningTableBlockEntity summoningTableInventory) {
for (int i = 0; i < summoningTableInventory.getSlots(); i++) {
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), summoningTableInventory.getStackInSlot(i));
}
}
super.onRemove(state, level, pos, newState, movedByPiston);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.IItemHandlerModifiable;
import net.neoforged.neoforge.items.ItemStackHandler;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class SummoningTableBlockEntity extends BlockEntity implements IItemHandlerModifiable, MenuProvider, Nameable {
public class SummoningTableBlockEntity extends BlockEntity implements IItemHandler, IItemHandlerModifiable, MenuProvider, Nameable {
// i'm really not feeling great about implementing IItemHandlerModifiable. feels like i should be doing something else.
// but it works for now so whatever. i'm tryin my best to be neoforgey!!
// this is.. fine. this is great. this feels so perfect. this is a place of honor! y'know why? because it works well enough!!

// spread out for easier reference (instead of computed)
// as for the other choices on display, no comment
Expand Down Expand Up @@ -74,7 +78,8 @@ public class SummoningTableBlockEntity extends BlockEntity implements IItemHandl
private UUID targetFromCandles = null;
@Nullable
private byte[] targetFromTrueNameInNybbles = null;
private ItemStack item = ItemStack.EMPTY;
private ItemStack trueName = ItemStack.EMPTY;
private final ItemStackHandler offerings = new ItemStackHandler(4);
private int burningPhase = 0; // ticks down from 8 -> 0 when burning, 0 represents not burning

private final ContainerData dataAccess = new ContainerData() {
Expand Down Expand Up @@ -130,8 +135,8 @@ public BlockState tryActivate(BlockState state, boolean simulate) {
public BlockState tryBurnName(BlockState state, boolean simulate) {
// this fails on client because client can't see the true name. maybe that's a problem to deal with? idk

if (!this.item.isEmpty()) {
byte[] parsedTargetFromItem = FamiliarTricks.trueNameToNybbles(this.item.getHoverName().getString());
if (!this.trueName.isEmpty()) {
byte[] parsedTargetFromItem = FamiliarTricks.trueNameToNybbles(this.trueName.getHoverName().getString());

if (parsedTargetFromItem != null) {
if (!simulate) {
Expand Down Expand Up @@ -260,25 +265,34 @@ private static void burnColumn(Level level, BlockPos bottomPos, byte nybbleDigit
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);

if (this.name != null) {
tag.putString("CustomName", Component.Serializer.toJson(this.name, registries));
}
if (!this.item.isEmpty()) {
tag.put("Item", this.item.save(registries));

if (!this.trueName.isEmpty()) {
tag.put("TrueNameItem", this.trueName.save(registries));
}
tag.put("OfferingItems", this.offerings.serializeNBT(registries));


this.lockKey.addToTag(tag);
}

@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);

if (tag.contains("CustomName", 8)) {
this.name = parseCustomNameSafe(tag.getString("CustomName"), registries);
}
if (tag.contains("Items", Tag.TAG_COMPOUND)) {

if (tag.contains("TrueNameItem", Tag.TAG_COMPOUND)) {
// ok this isn't really quite right here but. i'll deal with that later when i get to offerings
this.item = ItemStack.parse(registries, tag.getCompound("Items")).orElse(ItemStack.EMPTY);
this.trueName = ItemStack.parse(registries, tag.getCompound("TrueNameItem")).orElse(ItemStack.EMPTY);
}
if (tag.contains("OfferingItems", Tag.TAG_COMPOUND)) {
this.offerings.deserializeNBT(registries, tag.getCompound("OfferingItems"));
}

this.lockKey = LockCode.fromTag(tag);
Expand All @@ -290,7 +304,13 @@ protected void applyImplicitComponents(BlockEntity.DataComponentInput componentI
this.name = componentInput.get(DataComponents.CUSTOM_NAME);
this.lockKey = componentInput.getOrDefault(DataComponents.LOCK, LockCode.NO_LOCK);

this.item = componentInput.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).copyOne();
List<ItemStack> allItems = componentInput.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).stream().toList();
if (!allItems.isEmpty()) {
this.trueName = allItems.getFirst();
}
for (int i = 1; i < allItems.size() && i < 1+offerings.getSlots(); i++) {
offerings.setStackInSlot(i - 1, allItems.get(i));
}
}

@Override
Expand All @@ -301,7 +321,12 @@ protected void collectImplicitComponents(DataComponentMap.Builder components) {
components.set(DataComponents.LOCK, this.lockKey);
}

components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(List.of(this.item)));
ArrayList<ItemStack> allItems = new ArrayList<>(1 + this.offerings.getSlots());
allItems.add(this.trueName);
for (int i = 0; i < this.offerings.getSlots(); i++) {
allItems.add(this.offerings.getStackInSlot(i));
}
components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(allItems));
}

@Override
Expand All @@ -320,7 +345,8 @@ public Component getCustomName() {
public void removeComponentsFromTag(CompoundTag tag) {
tag.remove("CustomName");
tag.remove("Lock");
tag.remove("Items");
tag.remove("TrueNameItem");
tag.remove("OfferingItems");
}

@Override
Expand All @@ -340,58 +366,85 @@ public AbstractContainerMenu createMenu(int containerId, Inventory playerInvento

@Override
public int getSlots() {
return 1;
return 5;
}

@Override
public ItemStack getStackInSlot(int slot) {
if (slot == 0) {
return this.item;
return this.trueName;
} else if (slot > 0 && slot < 5) {
return this.offerings.getStackInSlot(slot - 1);
} else {
return ItemStack.EMPTY;
}
}

// can you feel her guiding voice? our lady luna speaks these methods into my ears, i am but a vessel
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
if (slot == 0 && this.item.isEmpty() && stack.is(FamiliarItems.TRUE_NAME_ITEM)) {
ItemStack toReturn = stack;

if (slot == 0 && this.trueName.isEmpty() && stack.is(FamiliarItems.TRUE_NAME_ITEM)) {
if (!simulate) {
this.item = stack.copyWithCount(1);
this.trueName = stack.copyWithCount(1);
}

return stack.copyWithCount(stack.getCount() - 1);
} else {
return stack;
toReturn = stack.copyWithCount(stack.getCount() - 1);
} else if (slot > 0 && slot < 5) {
toReturn = offerings.insertItem(slot - 1, stack, simulate);
}

if (toReturn.getCount() != stack.getCount() && !simulate) {
this.setChanged();
}

return toReturn;
}

@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
ItemStack toReturn = ItemStack.EMPTY;

if (slot == 0) {
ItemStack toReturn = this.item.copy();
toReturn = this.trueName.copy();
if (!simulate) {
this.item = ItemStack.EMPTY;
this.trueName = ItemStack.EMPTY;
}
return toReturn;
} else {
return ItemStack.EMPTY;
} else if (slot > 0 && slot < 5) {
toReturn = offerings.extractItem(slot - 1, amount, simulate);
}

if (!toReturn.isEmpty() && !simulate) {
this.setChanged();
}

return toReturn;
}

@Override
public int getSlotLimit(int slot) {
return slot == 0 ? 1 : 0;
switch(slot) {
case 0 -> {return 1;}
case 1,2,3,4 -> {return this.offerings.getSlotLimit(slot - 1);}
default -> {return 0;}
}
}

@Override
public boolean isItemValid(int slot, ItemStack stack) {
return slot == 0 && stack.is(FamiliarItems.TRUE_NAME_ITEM);
return slot == 0 && stack.is(FamiliarItems.TRUE_NAME_ITEM) ||
slot > 0 && slot < 5 && offerings.isItemValid(slot - 1, stack);
}

@Override
public void setStackInSlot(int slot, ItemStack stack) {
if (slot == 0) {
this.item = stack;
this.trueName = stack;
this.setChanged();
} else if (slot > 0 && slot < 5) {
this.offerings.setStackInSlot(slot - 1, stack);
this.setChanged();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SummoningTableMenu extends AbstractContainerMenu {
private final SlotItemHandler trueNameSlot;

public SummoningTableMenu(int containerId, Inventory playerInv) {
this(containerId, playerInv, new ItemStackHandler(1), new SimpleContainerData(4), ContainerLevelAccess.NULL);
this(containerId, playerInv, new ItemStackHandler(5), new SimpleContainerData(4), ContainerLevelAccess.NULL);
}
public SummoningTableMenu(int containerId, Inventory playerInventory, IItemHandler tableInventory, ContainerData tableData, ContainerLevelAccess levelAccess) {
super(FamiliarGUIStuffs.SUMMONING_TABLE_MENU.get(), containerId);
Expand All @@ -37,7 +37,7 @@ public SummoningTableMenu(int containerId, Inventory playerInventory, IItemHandl
this.tableData = tableData;

// table inv
this.trueNameSlot = new SlotItemHandler(tableInventory, 0, 26, 31) {
this.trueNameSlot = new SlotItemHandler(tableInventory, 0, 44, 31) {
@Override
public boolean mayPlace(ItemStack itemStack) {
return itemStack.is(FamiliarItems.TRUE_NAME_ITEM);
Expand All @@ -53,7 +53,13 @@ public int getMaxStackSize() {
return 1;
}
};

this.addSlot(this.trueNameSlot);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
this.addSlot(new SlotItemHandler(tableInventory, j + i * 2 + 1, 116 + j * 18, 22 + i * 18));
}
}

// player inv
for (int i = 0; i < 3; i++) {
Expand Down Expand Up @@ -88,25 +94,25 @@ public ItemStack quickMoveStack(Player player, int index) {
if (slot.hasItem()) {
ItemStack clickedStack = slot.getItem();
copyStack = clickedStack.copy();
if (index == 0) {
if (index >= 0 && index < 5) {
// move out of table
if (!this.moveItemStackTo(clickedStack, 1, 37, true)) {
if (!this.moveItemStackTo(clickedStack, 5, 41, true)) {
return ItemStack.EMPTY;
}
} else if (this.moveItemStackTo(clickedStack, 0, 1, false)) { //Forge Fix Shift Clicking in beacons with stacks larger then 1.
} else if (this.moveItemStackTo(clickedStack, 0, 5, false)) { //Forge Fix Shift Clicking in beacons with stacks larger then 1.
// move into table
return ItemStack.EMPTY;
} else if (index >= 1 && index < 28) {
} else if (index >= 5 && index < 32) {
// move from inventory to hotbar
if (!this.moveItemStackTo(clickedStack, 28, 37, false)) {
if (!this.moveItemStackTo(clickedStack, 32, 41, false)) {
return ItemStack.EMPTY;
}
} else if (index >= 28 && index < 37) {
} else if (index >= 32 && index < 41) {
// move from hotbar to inventory
if (!this.moveItemStackTo(clickedStack, 1, 28, false)) {
if (!this.moveItemStackTo(clickedStack, 5, 32, false)) {
return ItemStack.EMPTY;
}
} else if (!this.moveItemStackTo(clickedStack, 1, 37, false)) {
} else if (!this.moveItemStackTo(clickedStack, 5, 41, false)) {
// somehow, someway, move from somewhere else to hotbar or inventory
return ItemStack.EMPTY;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5b006bb

Please sign in to comment.