Skip to content

Commit ccd036e

Browse files
committed
improved inventory handling in many places. Resolves #852
1 parent 6cf8ca1 commit ccd036e

File tree

10 files changed

+34
-51
lines changed

10 files changed

+34
-51
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ org.gradle.caching=true
99
minecraft_version=1.21.1
1010
parchment_minecraft_version=1.21.1
1111
parchment_mappings_version=2024.11.17
12-
neoforge_version=21.1.219
12+
neoforge_version=21.1.222
1313
neoforge_version_range=[21.1.186,)
1414

1515
# Mod Properties

src/main/java/de/dafuqs/spectrum/api/block/ImplementedInventory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ default void setItem(int slot, @NotNull ItemStack stack) {
123123
@Override
124124
default void clearContent() {
125125
getItems().clear();
126-
inventoryChanged();
126+
setChanged();
127127
}
128128

129129
/**

src/main/java/de/dafuqs/spectrum/blocks/InWorldInteractionBlockEntity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.world.*;
1111
import net.minecraft.world.entity.player.*;
1212
import net.minecraft.world.item.*;
13+
import net.minecraft.world.level.block.*;
1314
import net.minecraft.world.level.block.entity.*;
1415
import net.minecraft.world.level.block.state.*;
1516
import net.minecraft.world.level.storage.loot.*;
@@ -29,8 +30,9 @@ public InWorldInteractionBlockEntity(BlockEntityType<?> type, BlockPos pos, Bloc
2930

3031
// interaction methods
3132
public void updateInClientWorld() {
32-
if (level instanceof ServerLevel serverWorld)
33-
serverWorld.getChunkSource().blockChanged(worldPosition);
33+
if (level != null) {
34+
level.sendBlockUpdated(worldPosition, level.getBlockState(worldPosition), level.getBlockState(worldPosition), Block.UPDATE_INVISIBLE);
35+
}
3436
}
3537

3638
// Called when the chunk is first loaded to initialize this be
@@ -65,7 +67,6 @@ public void unpackLootTable(@Nullable Player player) {
6567
lootTableSeed = level.getRandom().nextLong();
6668
}
6769
RandomizableContainer.super.unpackLootTable(player);
68-
this.setChanged();
6970
}
7071

7172
@Nullable
@@ -88,8 +89,8 @@ public void inventoryChanged() {
8889
@Override
8990
public void setChanged() {
9091
super.setChanged();
91-
if(this.level != null && !this.level.isClientSide()) {
92-
updateInClientWorld();
92+
if (level instanceof ServerLevel serverWorld) {
93+
serverWorld.getChunkSource().blockChanged(worldPosition);
9394
}
9495
}
9596

src/main/java/de/dafuqs/spectrum/blocks/chests/BlackHoleChestBlockEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public boolean hasExperienceStorageItem() {
361361
@Override
362362
public int @NotNull [] getSlotsForFace(Direction direction) {
363363
if(direction == Direction.UP) {
364-
return new int[EXPERIENCE_STORAGE_PROVIDER_ITEM_SLOT];
364+
return new int[]{EXPERIENCE_STORAGE_PROVIDER_ITEM_SLOT};
365365
}
366366
return IntStream.rangeClosed(0, EXPERIENCE_STORAGE_PROVIDER_ITEM_SLOT - 1).toArray();
367367
}

src/main/java/de/dafuqs/spectrum/blocks/chests/HeartboundChestBlockEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public boolean wasRecentlyTriedToOpenByNonOwner() {
161161
}
162162

163163
@Override
164-
public int[] getSlotsForFace(Direction side) {
164+
public int @NotNull [] getSlotsForFace(@NotNull Direction side) {
165165
return new int[0];
166166
}
167167

src/main/java/de/dafuqs/spectrum/blocks/crystallarieum/CrystallarieumBlock.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level
8282
if (player.isShiftKeyDown() || stack.isEmpty()) {
8383
// sneaking or empty hand: remove items
8484
if (retrieveStack(world, pos, player, hand, stack, crystallarieumBlockEntity, 1) || retrieveStack(world, pos, player, hand, stack, crystallarieumBlockEntity, 0)) {
85-
crystallarieumBlockEntity.inventoryChanged();
8685
crystallarieumBlockEntity.setOwner(player);
8786
}
8887
return ItemInteractionResult.CONSUME;
@@ -96,12 +95,10 @@ public ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level
9695
// hand is full and inventory already contains item: exchange them
9796
else if (stack.getItem() instanceof InkStorageItem<?> inkStorageItem) {
9897
if (inkStorageItem.getDrainability().canDrain(false) && exchangeStack(world, pos, player, hand, stack, crystallarieumBlockEntity, CrystallarieumBlockEntity.INK_STORAGE_STACK_SLOT_ID)) {
99-
crystallarieumBlockEntity.inventoryChanged();
10098
crystallarieumBlockEntity.setOwner(player);
10199
}
102100
} else {
103101
if (exchangeStack(world, pos, player, hand, stack, crystallarieumBlockEntity, CrystallarieumBlockEntity.CATALYST_SLOT_ID)) {
104-
crystallarieumBlockEntity.inventoryChanged();
105102
crystallarieumBlockEntity.setOwner(player);
106103
}
107104
}

src/main/java/de/dafuqs/spectrum/blocks/enchanter/EnchanterBlock.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ public static boolean verifyStructure(Level world, BlockPos blockPos, @Nullable
6262
if (serverPlayerEntity != null) {
6363
SpectrumAdvancementCriteria.COMPLETED_MULTIBLOCK.trigger(serverPlayerEntity, multiblock);
6464
}
65-
} else {
66-
if (world.isClientSide) {
67-
ModonomiconHelper.renderMultiblock(SpectrumMultiblocks.get(SpectrumMultiblocks.ENCHANTER), SpectrumMultiblocks.ENCHANTER_TEXT, blockPos.below(4), Rotation.NONE);
68-
}
65+
} else if (world.isClientSide) {
66+
ModonomiconHelper.renderMultiblock(SpectrumMultiblocks.get(SpectrumMultiblocks.ENCHANTER), SpectrumMultiblocks.ENCHANTER_TEXT, blockPos.below(4), Rotation.NONE);
6967
}
7068

7169
return valid;
@@ -108,7 +106,6 @@ public ItemInteractionResult useItemOn(ItemStack handStack, BlockState state, Le
108106
if (retrieveStack(world, pos, player, hand, handStack, enchanterBlockEntity, i)) {
109107
enchanterBlockEntity.setItemFacingDirection(player.getDirection());
110108
enchanterBlockEntity.setOwner(player);
111-
enchanterBlockEntity.inventoryChanged();
112109
break;
113110
}
114111
}
@@ -120,7 +117,6 @@ public ItemInteractionResult useItemOn(ItemStack handStack, BlockState state, Le
120117
if (exchangeStack(world, pos, player, hand, handStack, enchanterBlockEntity, inputInventorySlotIndex)) {
121118
enchanterBlockEntity.setItemFacingDirection(player.getDirection());
122119
enchanterBlockEntity.setOwner(player);
123-
enchanterBlockEntity.inventoryChanged();
124120
}
125121
}
126122
}

src/main/java/de/dafuqs/spectrum/blocks/enchanter/EnchanterBlockEntity.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import net.minecraft.core.*;
2222
import net.minecraft.core.component.*;
2323
import net.minecraft.nbt.*;
24+
import net.minecraft.network.protocol.*;
25+
import net.minecraft.network.protocol.game.*;
2426
import net.minecraft.server.level.*;
2527
import net.minecraft.sounds.*;
2628
import net.minecraft.stats.*;
@@ -699,7 +701,7 @@ protected void stopCraftingMusic() {
699701

700702
@Override
701703
public void saveAdditional(CompoundTag nbt, HolderLookup.Provider registryLookup) {
702-
super.saveAdditional(nbt, registryLookup);
704+
super.saveAdditional(nbt, registryLookup);
703705
nbt.putInt("crafting_time", this.craftingTime);
704706
nbt.putInt("crafting_time_total", this.craftingTimeTotal);
705707
nbt.putInt("current_item_processing_time", this.currentItemProcessingTime);
@@ -721,12 +723,6 @@ public void saveAdditional(CompoundTag nbt, HolderLookup.Provider registryLookup
721723
}
722724
}
723725

724-
@Override
725-
public void updateInClientWorld() {
726-
if (level != null)
727-
level.sendBlockUpdated(worldPosition, level.getBlockState(worldPosition), level.getBlockState(worldPosition), Block.UPDATE_INVISIBLE);
728-
}
729-
730726
public Direction getItemFacingDirection() {
731727
// if placed via pipe or other sources
732728
return Objects.requireNonNullElse(this.itemFacing, Direction.NORTH);
@@ -844,7 +840,7 @@ public UpgradeHolder getUpgradeHolder() {
844840

845841
@Override
846842
public int @NotNull [] getSlotsForFace(Direction direction) {
847-
return direction == Direction.UP ? new int[1] : new int[0];
843+
return direction == Direction.UP ? new int[]{1} : new int[]{0};
848844
}
849845

850846
@Override

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelTransmissionLogic.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package de.dafuqs.spectrum.blocks.pastel_network.network;
22

3-
import com.mojang.serialization.*;
4-
import com.mojang.serialization.codecs.*;
53
import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
64
import de.dafuqs.spectrum.helpers.*;
75
import de.dafuqs.spectrum.networking.s2c_payloads.*;
@@ -137,7 +135,6 @@ private boolean transferBetween(PastelNodeBlockEntity sourceNode, IItemHandler s
137135
proposals.put(stack, proposals.getOrDefault(stack, 0L) + stack.getCount());
138136
}
139137

140-
// TODO: this is jank. Improve
141138
for (ItemStack stack : proposals.keySet()) {
142139
long proposedAmount = Math.min(Math.min(proposals.get(stack), sourceNode.getMaxTransferredAmount()), totalAvailableStorage);
143140
if (proposedAmount == 0)
@@ -152,28 +149,24 @@ private boolean transferBetween(PastelNodeBlockEntity sourceNode, IItemHandler s
152149

153150
Optional<PastelTransmission> transmission = createTransmissionOnValidPath(sourceNode, destinationNode, new PastelPayload.ItemPastelPayload(proposedStack.copyWithCount(simulatedAmount)), sourceNode.getTransferTime());
154151
if (transmission.isPresent()) {
155-
int toRemove = simulatedAmount;
156-
while (toRemove > 0) {
157-
for (ItemStack matchingStack : matchingStacks.getB()) {
158-
int amountToShrink = Math.min(toRemove, matchingStack.getCount());
159-
matchingStack.shrink(amountToShrink);
160-
toRemove -= amountToShrink;
161-
}
152+
int extracted = 0;
153+
for (int i = 0; i < sourceStorage.getSlots(); i++) {
154+
if (ItemStack.isSameItemSameComponents(proposedStack, sourceStorage.getStackInSlot(i)))
155+
extracted += sourceStorage.extractItem(i, simulatedAmount - extracted, false).getCount();
156+
if (extracted == simulatedAmount)
157+
break;
162158
}
163159

164-
Optional<PastelTransmission> optionalTransmission = createTransmissionOnValidPath(sourceNode, destinationNode, new PastelPayload.ItemPastelPayload(proposedStack.copyWithCount(simulatedAmount)), sourceNode.getTransferTime());
165-
if (optionalTransmission.isPresent()) {
166-
PastelTransmission trans = optionalTransmission.get();
167-
int travelTime = trans.getTransmissionDuration();
168-
this.network.addTransmission(trans, travelTime);
169-
PastelTransmissionPayload.sendPastelTransmissionParticle(this.network, trans.getTransmissionDuration(), transmission.get());
170-
171-
destinationNode.markTransferred(transferMode != TransferMode.PUSH);
172-
sourceNode.markTransferred(transferMode != TransferMode.PULL);
173-
174-
destinationNode.addItemCountUnderway(simulatedAmount);
175-
return true;
176-
}
160+
PastelTransmission trans = transmission.get();
161+
int travelTime = trans.getTransmissionDuration();
162+
this.network.addTransmission(trans, travelTime);
163+
PastelTransmissionPayload.sendPastelTransmissionParticle(this.network, trans.getTransmissionDuration(), transmission.get());
164+
165+
destinationNode.markTransferred(transferMode != TransferMode.PUSH);
166+
sourceNode.markTransferred(transferMode != TransferMode.PULL);
167+
168+
destinationNode.addItemCountUnderway(simulatedAmount);
169+
return true;
177170
}
178171
}
179172
return false;

src/main/java/de/dafuqs/spectrum/capabilities/SpectrumCapabilities.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class SpectrumCapabilities {
2525
public static void register(RegisterCapabilitiesEvent event) {
2626
// ItemHandler.BLOCK
2727
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.FUSION_SHRINE.get(), (@NotNull FusionShrineBlockEntity blockEntity, Direction direction) -> new InvWrapper(blockEntity));;
28-
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.PEDESTAL.get(), (@NotNull PedestalBlockEntity blockEntity, Direction direction) -> new InvWrapper(blockEntity));
28+
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.PEDESTAL.get(), (@NotNull PedestalBlockEntity blockEntity, Direction direction) -> new SidedInvWrapper(blockEntity, direction));
2929
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.SPIRIT_INSTILLER.get(), (@NotNull SpiritInstillerBlockEntity blockEntity, Direction direction) -> new InvWrapper(blockEntity));
30-
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.ENCHANTER.get(), (@NotNull EnchanterBlockEntity blockEntity, Direction direction) -> new InvWrapper(blockEntity));
30+
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.ENCHANTER.get(), (@NotNull EnchanterBlockEntity blockEntity, Direction direction) -> new SidedInvWrapper(blockEntity, direction));
3131
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.CINDERHEARTH.get(), (@NotNull CinderhearthBlockEntity blockEntity, Direction direction) -> new SidedInvWrapper(blockEntity, direction));
3232
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.CRYSTALLARIEUM.get(), (@NotNull CrystallarieumBlockEntity blockEntity, Direction direction) -> new InvWrapper(blockEntity));
3333
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, SpectrumBlockEntities.TITRATION_BARREL.get(), (titrationBarrel, direction) -> {

0 commit comments

Comments
 (0)