Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create StorageBlock registry #10254

Draft
wants to merge 17 commits into
base: version/main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/com/minecolonies/api/IMinecoloniesAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.minecolonies.api.research.ModResearchCostTypes.ResearchCostType;
import com.minecolonies.api.research.effects.registry.ResearchEffectEntry;
import com.minecolonies.api.research.registry.ResearchRequirementEntry;
import com.minecolonies.api.tileentities.storageblocks.IStorageBlockNotificationManager;
import com.minecolonies.api.tileentities.storageblocks.registry.StorageBlockEntry;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.NewRegistryEvent;

Expand Down Expand Up @@ -101,4 +103,8 @@ static IMinecoloniesAPI getInstance()
void onRegistryNewRegistry(NewRegistryEvent event);

IForgeRegistry<EquipmentTypeEntry> getEquipmentTypeRegistry();

IForgeRegistry<StorageBlockEntry> getStorageBlockRegistry();

IStorageBlockNotificationManager getStorageBlockNotificationManager();
}
14 changes: 14 additions & 0 deletions src/main/java/com/minecolonies/api/MinecoloniesAPIProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.minecolonies.api.research.ModResearchCostTypes.ResearchCostType;
import com.minecolonies.api.research.effects.registry.ResearchEffectEntry;
import com.minecolonies.api.research.registry.ResearchRequirementEntry;
import com.minecolonies.api.tileentities.storageblocks.IStorageBlockNotificationManager;
import com.minecolonies.api.tileentities.storageblocks.registry.StorageBlockEntry;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.NewRegistryEvent;

Expand Down Expand Up @@ -235,4 +237,16 @@ public IForgeRegistry<EquipmentTypeEntry> getEquipmentTypeRegistry()
{
return apiInstance.getEquipmentTypeRegistry();
}

@Override
public IForgeRegistry<StorageBlockEntry> getStorageBlockRegistry()
{
return apiInstance.getStorageBlockRegistry();
}

@Override
public IStorageBlockNotificationManager getStorageBlockNotificationManager()
{
return apiInstance.getStorageBlockNotificationManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraftforge.event.TickEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/com/minecolonies/api/colony/buildings/IBuilding.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.minecolonies.api.colony.requestsystem.resolver.IRequestResolverProvider;
import com.minecolonies.api.colony.requestsystem.token.IToken;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.tileentities.storageblocks.InsertNotifier;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.Tuple;
Expand All @@ -37,7 +38,7 @@
import static com.minecolonies.api.util.constant.EquipmentLevelConstants.BASIC_TOOL_LEVEL;
import static com.minecolonies.api.util.constant.EquipmentLevelConstants.TOOL_LEVEL_MAXIMUM;

public interface IBuilding extends IBuildingContainer, IModuleContainer<IBuildingModule>, IRequestResolverProvider, IRequester, ISchematicProvider
public interface IBuilding extends IBuildingContainer, IModuleContainer<IBuildingModule>, IRequestResolverProvider, IRequester, ISchematicProvider, InsertNotifier.IInsertListener
{
/**
* Minimal level to ask for wood tools. (WOOD_HUT_LEVEL + 1 == stone)
Expand Down Expand Up @@ -263,16 +264,6 @@ default int buildingRequiresCertainAmountOfItem(ItemStack stack, List<ItemStorag
*/
Map<Predicate<ItemStack>, Tuple<Integer, Boolean>> getRequiredItemsAndAmount();

/**
* Try to transfer a stack to one of the inventories of the building and force the transfer.
*
* @param stack the stack to transfer.
* @param world the world to do it in.
* @return the itemStack which has been replaced or the itemStack which could not be transfered
*/
@Nullable
ItemStack forceTransferStack(ItemStack stack, Level world);

/**
* Create a request for a citizen.
*
Expand Down Expand Up @@ -503,4 +494,17 @@ default boolean canAssignCitizens()
* @return true if so.
*/
boolean canEat(final ItemStack stack);

/**
* Handles when a new item is inserted into a block this building is
* listening to.
*
* @param insertPos The position of the storageblock the item was inserted
* @param itemStack The item that was inserted.
*/
@Override
default void onInsert(BlockPos insertPos, ItemStack itemStack) {
overruleNextOpenRequestWithStack(itemStack);
markDirty();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.minecolonies.api.colony.buildings;

import com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding;
import com.minecolonies.api.tileentities.storageblocks.AbstractStorageBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -56,7 +57,7 @@ public interface IBuildingContainer extends ISchematicProvider, ICapabilityProvi
*
* @return a copy of the list to avoid currentModification exception.
*/
List<BlockPos> getContainers();
List<AbstractStorageBlock> getContainers();

/**
* Register a blockState and position. We suppress this warning since this parameter will be used in child classes which override this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.minecolonies.api.colony.requestsystem.request.IRequest;
import com.minecolonies.api.colony.requestsystem.requester.IRequester;
import com.minecolonies.api.colony.requestsystem.token.IToken;
import com.minecolonies.api.tileentities.storageblocks.AbstractStorageBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -55,11 +56,11 @@ public interface IBuildingView extends IRequester, IModuleContainerView
int getBuildingLevel();

/**
* Get the BlockPos of the Containers.
* Get the storage interfaces of the Containers.
*
* @return containerList.
*/
List<BlockPos> getContainerList();
List<AbstractStorageBlock> getContainerList();

/**
* Get the max level of the building.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.minecolonies.api.colony.event;

import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.eventbus.api.Event;

/**
* An event used to notify listeners when an inventory
* is updated.
*/
public class StorageBlockStackInsertEvent extends Event
{
/**
* The position of the storage block that was updated.
*/
final BlockPos storageBlockPos;

/**
* The dimension that the storage block is located in.
*/
final ResourceKey<Level> dimension;

/**
* The stack that was inserted.
*/
final ItemStack stack;

/**
* Constructor.
*
* @param dimension The dimension of the updated storage block
* @param pos The position of the updated storage block
* @param stack The stack that was inserted into the storage block.
*/
public StorageBlockStackInsertEvent(ResourceKey<Level> dimension, BlockPos pos, ItemStack stack)
{
this.storageBlockPos = pos;
this.dimension = dimension;
this.stack = stack;
}

/**
* The storage block position.
*
* @return The storage block position
*/
public BlockPos getPosition()
{
return storageBlockPos;
}

/**
* The stack that was inserted into the storage block.
*
* @return The inserted stack.
*/
public ItemStack getInsertedStack()
{
return stack;
}

/**
* The dimension of the storage block.
*
* @return The dimension of the storage block.
*/
public ResourceKey<Level> getDimension()
{
return dimension;
}

@Override
public String toString()
{
return String.format("StorageBlockStackInsertEvent: %s (%d, %d, %d) -> %s", dimension.location(), storageBlockPos.getX(), storageBlockPos.getY(), storageBlockPos.getZ(), stack.getItem().getDescription().getString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.minecolonies.api.blocks.AbstractBlockMinecoloniesRack;
import com.minecolonies.api.blocks.types.RackType;
import com.minecolonies.api.colony.event.StorageBlockStackInsertEvent;
import com.minecolonies.api.inventory.ModContainers;
import com.minecolonies.api.tileentities.AbstractTileEntityRack;
import com.minecolonies.api.tileentities.storageblocks.IStorageBlockNotificationManager;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.api.util.Log;

import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -14,6 +18,7 @@
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
Expand Down Expand Up @@ -227,11 +232,13 @@ protected boolean moveItemStackTo(final ItemStack stack, final int startIndex, f
private void updateRacks(final ItemStack stack)
{
rack.updateItemStorage();
rack.updateWarehouseIfAvailable(stack);
StorageBlockStackInsertEvent insertEvent = new StorageBlockStackInsertEvent(rack.getLevel().dimension(), rack.getBlockPos(), stack);
MinecraftForge.EVENT_BUS.post(insertEvent);
if (neighborRack != null)
{
neighborRack.updateItemStorage();
neighborRack.updateWarehouseIfAvailable(stack);
insertEvent = new StorageBlockStackInsertEvent(neighborRack.getLevel().dimension(), neighborRack.getBlockPos(), stack);
MinecraftForge.EVENT_BUS.post(insertEvent);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.minecolonies.api.tileentities;

import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.api.colony.requestsystem.requestable.IDeliverable;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.util.ItemStackUtils;
import com.minecolonies.api.util.Log;
import com.minecolonies.api.colony.event.StorageBlockStackInsertEvent;
import net.minecraft.core.BlockPos;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,16 +21,6 @@

public abstract class AbstractTileEntityRack extends BlockEntity implements MenuProvider
{
/**
* whether this rack is in a warehouse or not. defaults to not set by the warehouse building upon being built
*/
protected boolean inWarehouse = false;

/**
* Pos of the owning building.
*/
protected BlockPos buildingPos = BlockPos.ZERO;

/**
* The inventory of the tileEntity.
*/
Expand Down Expand Up @@ -90,7 +78,12 @@ public void setStackInSlot(final int slot, final @Nonnull ItemStack stack)
{
onContentsChanged(slot);
}
updateWarehouseIfAvailable(stack);

if (level != null)
{
StorageBlockStackInsertEvent insertEvent = new StorageBlockStackInsertEvent(level.dimension(), worldPosition, stack);
MinecraftForge.EVENT_BUS.post(insertEvent);
}
}

@Nonnull
Expand All @@ -100,7 +93,8 @@ public ItemStack insertItem(final int slot, @Nonnull final ItemStack stack, fina
final ItemStack result = super.insertItem(slot, stack, simulate);
if ((result.isEmpty() || result.getCount() < stack.getCount()) && !simulate)
{
updateWarehouseIfAvailable(stack);
StorageBlockStackInsertEvent insertEvent = new StorageBlockStackInsertEvent(level.dimension(), worldPosition, stack);
MinecraftForge.EVENT_BUS.post(insertEvent);
}
return result;
}
Expand All @@ -114,51 +108,6 @@ public ItemStack insertItem(final int slot, @Nonnull final ItemStack stack, fina
*/
public abstract ItemStackHandler createInventory(final int slots);

/**
* Update the warehouse if available with the updated stack.
*
* @param stack the incoming stack.
*/
public void updateWarehouseIfAvailable(final ItemStack stack)
{
if (!ItemStackUtils.isEmpty(stack) && level != null && !level.isClientSide)
{
if (inWarehouse || !buildingPos.equals(BlockPos.ZERO))
{
if (IColonyManager.getInstance().isCoordinateInAnyColony(level, worldPosition))
{
final IColony colony = IColonyManager.getInstance().getClosestColony(level, worldPosition);
if (colony == null)
{
return;
}

if (inWarehouse)
{
colony.getRequestManager().onColonyUpdate(request ->
request.getRequest() instanceof IDeliverable && ((IDeliverable) request.getRequest()).matches(stack));
}
else
{
final IBuilding building = colony.getBuildingManager().getBuilding(buildingPos);
if (building != null)
{
building.overruleNextOpenRequestWithStack(stack);
building.markDirty();
}
}
}
}
}
}

/**
* Set the value for inWarehouse
*
* @param isInWarehouse is this rack in a warehouse?
*/
public abstract void setInWarehouse(Boolean isInWarehouse);

/**
* Get the amount of free slots in the inventory. This method checks the content list, it is therefore extremely fast.
*
Expand Down Expand Up @@ -215,20 +164,6 @@ public void updateWarehouseIfAvailable(final ItemStack stack)
*/
public abstract void upgradeRackSize();

/**
* Set the building pos it belongs to.
*
* @param pos the pos of the building.
*/
public void setBuildingPos(final BlockPos pos)
{
if (level != null && (buildingPos == null || !buildingPos.equals(pos)))
{
setChanged();
}
this.buildingPos = pos;
}

/**
* Get the upgrade size.
*
Expand Down
Loading