Skip to content

Commit

Permalink
Use capability listeners on external inventories to trigger hut capab…
Browse files Browse the repository at this point in the history
…ility invalidation
  • Loading branch information
jpenilla committed Feb 19, 2025
1 parent 8342656 commit 3dd8861
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public abstract class AbstractBuildingContainer extends AbstractSchematicProvide
*/
protected final Set<BlockPos> containerList = new HashSet<>();

/**
* Whether the container list has changed since the last capability rebuild.
*/
private boolean containerListChanged = false;

/**
* List of items the worker should keep. With the quantity and if he should keep it in the inventory as well.
*/
Expand All @@ -71,6 +76,16 @@ public AbstractBuildingContainer(final BlockPos pos, final IColony colony)
super(pos, colony);
}

public boolean pollContainerListChanged()
{
if (containerListChanged)
{
containerListChanged = false;
return true;
}
return false;
}

@Override
public void deserializeNBT(@NotNull final HolderLookup.Provider provider, final CompoundTag compound)
{
Expand All @@ -79,6 +94,7 @@ public void deserializeNBT(@NotNull final HolderLookup.Provider provider, final
final ListTag containerTagList = compound.getList(TAG_CONTAINERS, Tag.TAG_INT_ARRAY);
for (int i = 0; i < containerTagList.size(); ++i)
{
this.containerListChanged = true;
containerList.add(NBTUtils.readBlockPos(containerTagList.get(i)));
}
if (compound.contains(TAG_PRIO))
Expand Down Expand Up @@ -126,12 +142,14 @@ public void alterPickUpPriority(final int value)
@Override
public void addContainerPosition(@NotNull final BlockPos pos)
{
this.containerListChanged = true;
containerList.add(pos);
}

@Override
public void removeContainerPosition(final BlockPos pos)
{
this.containerListChanged = true;
containerList.remove(pos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.minecolonies.api.tileentities.MinecoloniesTileEntities;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.api.util.WorldUtil;
import com.minecolonies.core.colony.buildings.AbstractBuildingContainer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
Expand All @@ -32,6 +33,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand Down Expand Up @@ -444,7 +446,7 @@ public void saveAdditional(@NotNull final CompoundTag compound, @NotNull final H
@Override
public void tick()
{
if (combinedInv != null)
if (this.building instanceof AbstractBuildingContainer container && container.pollContainerListChanged())
{
invalidateCapabilities();
combinedInv = null;
Expand Down Expand Up @@ -595,6 +597,18 @@ public IItemHandler getItemHandlerCap(final Direction side)
{
handlers.add(rack.getInventory());
rack.setBuildingPos(this.getBlockPos());
if (world instanceof ServerLevel serverLevel)
{
serverLevel.registerCapabilityListener(rack.getBlockPos(), () -> {
if (!WorldUtil.isBlockLoaded(world, pos) || world.getBlockEntity(pos) != te)
{
return false;
}
this.combinedInv = null;
this.invalidateCapabilities();
return true;
});
}
}
else
{
Expand Down

0 comments on commit 3dd8861

Please sign in to comment.