Skip to content

Commit 42ea4d1

Browse files
committed
Refactor how containers are implemented and exposed as capabilities on itemstacks to properly use the data component system
1 parent 84a3f2b commit 42ea4d1

File tree

137 files changed

+4761
-1919
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+4761
-1919
lines changed

src/api/java/mekanism/api/chemical/ChemicalStack.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ public void encode(RegistryFriendlyByteBuf buf, STACK stack) {
163163
case PIGMENT -> PigmentStack.MAP_CODEC;
164164
case SLURRY -> SlurryStack.MAP_CODEC;
165165
});
166+
/**
167+
* Codec to get any kind of chemical stack, based on a "chemicalType" field.
168+
*
169+
* @see ChemicalType
170+
* @see mekanism.api.chemical.merged.BoxedChemicalStack
171+
* @since 10.6.0
172+
*/
173+
//TODO - 1.20.5: Re-evaluate if we wan this defaulting to an empty gas stack or to try and get the same stack type as it was?
174+
public static final Codec<ChemicalStack<?>> BOXED_OPTIONAL_CODEC = ExtraCodecs.optionalEmptyMap(BOXED_CODEC).xmap(optional -> optional.orElse(GasStack.EMPTY),
175+
stack -> stack.isEmpty() ? Optional.empty() : Optional.of(stack));
166176
/**
167177
* StreamCodec to get any kind of chemical stack (that does not accept empty stacks), based on a "chemicalType" field.
168178
*

src/api/java/mekanism/api/energy/IEnergyContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ default void setEmpty() {
154154
* @return Amount of energy needed
155155
*/
156156
default FloatingLong getNeeded() {
157-
return FloatingLong.ZERO.max(getMaxEnergy().subtract(getEnergy()));
157+
return getMaxEnergy().subtract(getEnergy());
158158
}
159159

160160
@Override

src/api/java/mekanism/api/fluid/IExtendedFluidTank.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ default int getNeeded() {
233233
return Math.max(0, getCapacity() - getFluidAmount());
234234
}
235235

236+
@Override
237+
default int getFluidAmount() {
238+
return getFluid().getAmount();
239+
}
240+
236241
@Override
237242
default CompoundTag serializeNBT(HolderLookup.Provider provider) {
238243
CompoundTag nbt = new CompoundTag();

src/api/java/mekanism/api/inventory/IInventorySlot.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ default ItemStack extractItem(int amount, Action action, AutomationType automati
125125
ItemStack current = getStack();
126126
//Ensure that if this slot allows going past the max stack size of an item, that when extracting we don't act as if we have more than
127127
// the max stack size, as the JavaDoc for IItemHandler requires that the returned stack is not larger than its stack size
128-
int currentAmount = Math.min(getCount(), current.getMaxStackSize());
128+
int currentAmount = Math.min(current.getCount(), current.getMaxStackSize());
129129
if (currentAmount < amount) {
130130
//If we are trying to extract more than we have, just change it so that we are extracting it all
131131
amount = currentAmount;
@@ -179,7 +179,9 @@ default ItemStack extractItem(int amount, Action action, AutomationType automati
179179
* @return A slot for use in a container that represents this {@link IInventorySlot}, or null if this slot should not be added.
180180
*/
181181
@Nullable
182-
Slot createContainerSlot();
182+
default Slot createContainerSlot() {
183+
return null;
184+
}
183185

184186
/**
185187
* Convenience method for modifying the size of the stored stack.
@@ -312,6 +314,7 @@ default CompoundTag serializeNBT(HolderLookup.Provider provider) {
312314
* @since 10.5.0
313315
*/
314316
default boolean isCompatible(IInventorySlot other) {
317+
//TODO - 1.20.5: Remove this? I don't think it is necessary anymore
315318
return getClass() == other.getClass() && ItemStack.matches(getStack(), other.getStack());
316319
}
317320
}

src/datagen/main/java/mekanism/common/loot/table/BaseBlockLootTables.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import mekanism.api.annotations.NothingNullByDefault;
1212
import mekanism.api.providers.IBlockProvider;
1313
import mekanism.common.Mekanism;
14-
import mekanism.common.attachments.containers.AttachedContainers;
1514
import mekanism.common.attachments.containers.ContainerType;
1615
import mekanism.common.block.BlockRadioactiveWasteBarrel;
1716
import mekanism.common.block.attribute.Attribute;
@@ -183,29 +182,28 @@ protected void dropSelfWithContents(Collection<? extends Holder<Block>> blockPro
183182
boolean hasContainers = false;
184183
CopyContainersLootFunction.Builder containerBuilder = CopyContainersLootFunction.builder();
185184
for (ContainerType<?, ?, ?> type : ContainerType.TYPES) {
186-
AttachedContainers<?> attachment = type.getAttachment(stack);
187185
List<?> containers = tileEntity.persists(type) ? type.getContainers(tileEntity) : Collections.emptyList();
188-
List<?> attachmentContainers = attachment == null ? Collections.emptyList() : attachment.getContainers();
189-
if (containers.size() == attachmentContainers.size()) {
186+
int attachmentContainers = type.getContainerCount(stack);
187+
if (containers.size() == attachmentContainers) {
190188
if (!containers.isEmpty()) {
191189
containerBuilder.copy(type);
192190
hasContainers = true;
193191
if (type != ContainerType.ENERGY && type != ContainerType.HEAT) {
194192
hasContents = true;
195193
}
196194
}
197-
} else if (attachmentContainers.isEmpty()) {
195+
} else if (attachmentContainers == 0) {
198196
//TODO: Improve how we handle skipping warnings for known missing types
199197
if (type == ContainerType.ITEM && block.asItem() instanceof ItemBlockPersonalStorage) {
200198
//We don't want explosions causing personal storage items to be directly destroyed. It is also known that the attachment is missing
201199
hasContents = true;
202200
} else if (type != ContainerType.GAS || !(block instanceof BlockRadioactiveWasteBarrel)) {
203-
Mekanism.logger.warn("Container type: {}, item missing attachments: {}", type.getAttachmentName(), RegistryUtils.getName(block));
201+
Mekanism.logger.warn("Container type: {}, item missing attachments: {}", type.getComponentName(), RegistryUtils.getName(block));
204202
}
205203
} else if (containers.isEmpty()) {
206-
Mekanism.logger.warn("Container type: {}, item has attachments but block doesn't have containers: {}", type.getAttachmentName(), RegistryUtils.getName(block));
204+
Mekanism.logger.warn("Container type: {}, item has attachments but block doesn't have containers: {}", type.getComponentName(), RegistryUtils.getName(block));
207205
} else {
208-
Mekanism.logger.warn("Container type: {}, has {} item attachments and block has {} containers: {}", type.getAttachmentName(), attachmentContainers.size(),
206+
Mekanism.logger.warn("Container type: {}, has {} item attachments and block has {} containers: {}", type.getComponentName(), attachmentContainers,
209207
containers.size(), RegistryUtils.getName(block));
210208
}
211209
}

src/generators/java/mekanism/generators/common/registries/GeneratorsBlocks.java

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package mekanism.generators.common.registries;
22

33
import java.util.function.Supplier;
4-
import mekanism.api.chemical.ChemicalTankBuilder;
54
import mekanism.api.chemical.gas.attribute.GasAttributes.Fuel;
65
import mekanism.common.attachments.containers.ContainerType;
6+
import mekanism.common.attachments.containers.chemical.gas.GasTanksBuilder;
7+
import mekanism.common.attachments.containers.fluid.FluidTanksBuilder;
8+
import mekanism.common.attachments.containers.heat.HeatCapacitorsBuilder;
9+
import mekanism.common.attachments.containers.item.ItemSlotsBuilder;
710
import mekanism.common.block.basic.BlockStructuralGlass;
811
import mekanism.common.block.interfaces.IHasDescription;
912
import mekanism.common.block.prefab.BlockBasicMultiblock;
1013
import mekanism.common.block.prefab.BlockTile;
1114
import mekanism.common.block.prefab.BlockTile.BlockTileModel;
12-
import mekanism.common.capabilities.chemical.variable.RateLimitGasTank;
13-
import mekanism.common.capabilities.fluid.BasicFluidTank;
14-
import mekanism.common.capabilities.fluid.item.RateLimitFluidTank;
15-
import mekanism.common.capabilities.heat.BasicHeatCapacitor;
1615
import mekanism.common.content.blocktype.BlockTypeTile;
17-
import mekanism.common.inventory.slot.ItemSlotsBuilder;
18-
import mekanism.common.inventory.slot.chemical.GasInventorySlot;
1916
import mekanism.common.item.block.ItemBlockTooltip;
2017
import mekanism.common.registration.impl.BlockDeferredRegister;
2118
import mekanism.common.registration.impl.BlockRegistryObject;
@@ -30,7 +27,6 @@
3027
import mekanism.generators.common.item.ItemBlockFissionLogicAdapter;
3128
import mekanism.generators.common.item.ItemBlockFusionLogicAdapter;
3229
import mekanism.generators.common.item.generator.ItemBlockWindGenerator;
33-
import mekanism.generators.common.slot.FluidFuelInventorySlot;
3430
import mekanism.generators.common.tile.TileEntityAdvancedSolarGenerator;
3531
import mekanism.generators.common.tile.TileEntityBioGenerator;
3632
import mekanism.generators.common.tile.TileEntityGasGenerator;
@@ -55,9 +51,7 @@
5551
import mekanism.generators.common.tile.turbine.TileEntityTurbineVent;
5652
import net.minecraft.tags.FluidTags;
5753
import net.minecraft.world.level.block.Block;
58-
import net.minecraft.world.level.material.Fluids;
5954
import net.minecraft.world.level.material.MapColor;
60-
import net.neoforged.neoforge.fluids.FluidStack;
6155

6256
public class GeneratorsBlocks {
6357

@@ -69,54 +63,50 @@ private GeneratorsBlocks() {
6963
public static final BlockRegistryObject<BlockTileModel<TileEntityHeatGenerator, Generator<TileEntityHeatGenerator>>, ItemBlockTooltip<BlockTileModel<TileEntityHeatGenerator, Generator<TileEntityHeatGenerator>>>> HEAT_GENERATOR =
7064
BLOCKS.register("heat_generator", () -> new BlockTileModel<>(GeneratorsBlockTypes.HEAT_GENERATOR, properties -> properties.mapColor(MapColor.METAL)), ItemBlockTooltip::new)
7165
.forItemHolder(holder -> holder
72-
.addAttachmentOnlyContainer(ContainerType.FLUID, stack -> RateLimitFluidTank.createBasicItem(MekanismGeneratorsConfig.generators.heatTankCapacity,
73-
BasicFluidTank.manualOnly, BasicFluidTank.alwaysTrueBi,
74-
fluidStack -> fluidStack.is(FluidTags.LAVA)
75-
)).addAttachmentOnlyContainer(ContainerType.HEAT, stack -> BasicHeatCapacitor.createBasicItem(TileEntityHeatGenerator.HEAT_CAPACITY,
76-
TileEntityHeatGenerator.INVERSE_CONDUCTION_COEFFICIENT, TileEntityHeatGenerator.INVERSE_INSULATION_COEFFICIENT
77-
)).addAttachmentOnlyContainers(ContainerType.ITEM, stack -> ItemSlotsBuilder.builder(stack)
78-
.addFluidSlot(0, (tank, listener, x, y) -> FluidFuelInventorySlot.forFuel(tank,
79-
s -> s.getBurnTime(null) / 20,
80-
size -> new FluidStack(Fluids.LAVA, size),
81-
listener, x, y)
82-
).addEnergy()
66+
.addAttachmentOnlyContainers(ContainerType.FLUID, () -> FluidTanksBuilder.builder()
67+
.addBasic(MekanismGeneratorsConfig.generators.heatTankCapacity, fluid -> fluid.is(FluidTags.LAVA))
68+
.build()
69+
).addAttachmentOnlyContainers(ContainerType.HEAT, () -> HeatCapacitorsBuilder.builder()
70+
.addBasic(TileEntityHeatGenerator.HEAT_CAPACITY, TileEntityHeatGenerator.INVERSE_CONDUCTION_COEFFICIENT, TileEntityHeatGenerator.INVERSE_INSULATION_COEFFICIENT)
71+
.build()
72+
).addAttachmentOnlyContainers(ContainerType.ITEM, () -> ItemSlotsBuilder.builder()
73+
.addFluidFuelSlot(0, s -> s.getBurnTime(null) > 0)
74+
.addEnergy()
8375
.build()
8476
)
8577
);
8678
public static final BlockRegistryObject<BlockTileModel<TileEntitySolarGenerator, Generator<TileEntitySolarGenerator>>, ItemBlockTooltip<BlockTileModel<TileEntitySolarGenerator, Generator<TileEntitySolarGenerator>>>> SOLAR_GENERATOR =
8779
BLOCKS.register("solar_generator", () -> new BlockTileModel<>(GeneratorsBlockTypes.SOLAR_GENERATOR, properties -> properties.mapColor(MapColor.COLOR_BLUE)), ItemBlockTooltip::new)
88-
.forItemHolder(holder -> holder.addAttachmentOnlyContainers(ContainerType.ITEM, stack -> ItemSlotsBuilder.builder(stack).addEnergy().build()));
80+
.forItemHolder(holder -> holder.addAttachmentOnlyContainers(ContainerType.ITEM, () -> ItemSlotsBuilder.builder().addEnergy().build()));
8981
public static final BlockRegistryObject<BlockTileModel<TileEntityGasGenerator, Generator<TileEntityGasGenerator>>, ItemBlockTooltip<BlockTileModel<TileEntityGasGenerator, Generator<TileEntityGasGenerator>>>> GAS_BURNING_GENERATOR =
9082
BLOCKS.register("gas_burning_generator", () -> new BlockTileModel<>(GeneratorsBlockTypes.GAS_BURNING_GENERATOR, properties -> properties.mapColor(BlockResourceInfo.STEEL.getMapColor())), ItemBlockTooltip::new)
9183
.forItemHolder(holder -> holder
92-
.addAttachmentOnlyContainer(ContainerType.GAS, stack -> RateLimitGasTank.createBasicItem(MekanismGeneratorsConfig.generators.gbgTankCapacity,
93-
ChemicalTankBuilder.GAS.manualOnly, ChemicalTankBuilder.GAS.alwaysTrueBi,
94-
gas -> gas.has(Fuel.class)
95-
)).addAttachmentOnlyContainers(ContainerType.ITEM, stack -> ItemSlotsBuilder.builder(stack)
96-
.addGasSlot(0, GasInventorySlot::fill)
84+
.addAttachmentOnlyContainers(ContainerType.GAS, () -> GasTanksBuilder.builder()
85+
.addBasic(MekanismGeneratorsConfig.generators.gbgTankCapacity, gas -> gas.has(Fuel.class))
86+
.build()
87+
).addAttachmentOnlyContainers(ContainerType.ITEM, () -> ItemSlotsBuilder.builder()
88+
.addGasFillSlot(0)
9789
.addEnergy()
9890
.build()
9991
)
10092
);
10193
public static final BlockRegistryObject<BlockTileModel<TileEntityBioGenerator, Generator<TileEntityBioGenerator>>, ItemBlockTooltip<BlockTileModel<TileEntityBioGenerator, Generator<TileEntityBioGenerator>>>> BIO_GENERATOR =
10294
BLOCKS.register("bio_generator", () -> new BlockTileModel<>(GeneratorsBlockTypes.BIO_GENERATOR, properties -> properties.mapColor(BlockResourceInfo.STEEL.getMapColor())), ItemBlockTooltip::new)
10395
.forItemHolder(holder -> holder
104-
.addAttachmentOnlyContainer(ContainerType.FLUID, stack -> RateLimitFluidTank.createBasicItem(MekanismGeneratorsConfig.generators.bioTankCapacity,
105-
BasicFluidTank.manualOnly, BasicFluidTank.alwaysTrueBi,
106-
fluidStack -> fluidStack.is(GeneratorTags.Fluids.BIOETHANOL)
107-
)).addAttachmentOnlyContainers(ContainerType.ITEM, stack -> ItemSlotsBuilder.builder(stack)
108-
.addFluidSlot(0, (tank, listener, x, y) -> FluidFuelInventorySlot.forFuel(tank, s -> s.is(MekanismTags.Items.FUELS_BIO) ? 200 : s.is(MekanismTags.Items.FUELS_BLOCK_BIO) ? 200 * 9 : 0,
109-
GeneratorsFluids.BIOETHANOL::getFluidStack,
110-
listener, x, y)
111-
).addEnergy()
96+
.addAttachmentOnlyContainers(ContainerType.FLUID, () -> FluidTanksBuilder.builder()
97+
.addBasic(MekanismGeneratorsConfig.generators.bioTankCapacity, fluid -> fluid.is(GeneratorTags.Fluids.BIOETHANOL))
98+
.build()
99+
).addAttachmentOnlyContainers(ContainerType.ITEM, () -> ItemSlotsBuilder.builder()
100+
.addFluidFuelSlot(0, s -> s.is(MekanismTags.Items.FUELS_BIO) || s.is(MekanismTags.Items.FUELS_BLOCK_BIO))
101+
.addEnergy()
112102
.build()
113103
)
114104
);
115105
public static final BlockRegistryObject<BlockTileModel<TileEntityAdvancedSolarGenerator, Generator<TileEntityAdvancedSolarGenerator>>, ItemBlockTooltip<BlockTileModel<TileEntityAdvancedSolarGenerator, Generator<TileEntityAdvancedSolarGenerator>>>> ADVANCED_SOLAR_GENERATOR =
116106
BLOCKS.register("advanced_solar_generator", () -> new BlockTileModel<>(GeneratorsBlockTypes.ADVANCED_SOLAR_GENERATOR, properties -> properties.mapColor(MapColor.COLOR_BLUE)), ItemBlockTooltip::new)
117-
.forItemHolder(holder -> holder.addAttachmentOnlyContainers(ContainerType.ITEM, stack -> ItemSlotsBuilder.builder(stack).addEnergy().build()));
107+
.forItemHolder(holder -> holder.addAttachmentOnlyContainers(ContainerType.ITEM, () -> ItemSlotsBuilder.builder().addEnergy().build()));
118108
public static final BlockRegistryObject<BlockTileModel<TileEntityWindGenerator, Generator<TileEntityWindGenerator>>, ItemBlockWindGenerator> WIND_GENERATOR = BLOCKS.register("wind_generator", () -> new BlockTileModel<>(GeneratorsBlockTypes.WIND_GENERATOR, properties -> properties.mapColor(MapColor.METAL)), ItemBlockWindGenerator::new)
119-
.forItemHolder(holder -> holder.addAttachmentOnlyContainers(ContainerType.ITEM, stack -> ItemSlotsBuilder.builder(stack).addEnergy().build()));
109+
.forItemHolder(holder -> holder.addAttachmentOnlyContainers(ContainerType.ITEM, () -> ItemSlotsBuilder.builder().addEnergy().build()));
120110

121111
public static final BlockRegistryObject<BlockTurbineRotor, ItemBlockTooltip<BlockTurbineRotor>> TURBINE_ROTOR = registerTooltipBlock("turbine_rotor", BlockTurbineRotor::new);
122112
public static final BlockRegistryObject<BlockTile<TileEntityRotationalComplex, BlockTypeTile<TileEntityRotationalComplex>>, ItemBlockTooltip<BlockTile<TileEntityRotationalComplex, BlockTypeTile<TileEntityRotationalComplex>>>> ROTATIONAL_COMPLEX = registerTooltipBlock("rotational_complex", () -> new BlockTile<>(GeneratorsBlockTypes.ROTATIONAL_COMPLEX, properties -> properties.mapColor(BlockResourceInfo.STEEL.getMapColor())));

src/generators/java/mekanism/generators/common/registries/GeneratorsItems.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package mekanism.generators.common.registries;
22

33
import mekanism.common.attachments.containers.ContainerType;
4-
import mekanism.common.capabilities.chemical.variable.RateLimitGasTank;
4+
import mekanism.common.attachments.containers.chemical.gas.GasTanksBuilder;
55
import mekanism.common.item.ItemModule;
66
import mekanism.common.registration.impl.ItemDeferredRegister;
77
import mekanism.common.registration.impl.ItemRegistryObject;
@@ -22,11 +22,10 @@ private GeneratorsItems() {
2222

2323
public static final ItemRegistryObject<Item> SOLAR_PANEL = ITEMS.register("solar_panel");
2424
public static final ItemRegistryObject<ItemHohlraum> HOHLRAUM = ITEMS.registerItem("hohlraum", ItemHohlraum::new)
25-
.addAttachedContainerCapability(ContainerType.GAS, stack -> RateLimitGasTank.createInternalStorage(
26-
MekanismGeneratorsConfig.generators.hohlraumFillRate,
27-
MekanismGeneratorsConfig.generators.hohlraumMaxGas,
28-
gas -> gas.is(GeneratorTags.Gases.FUSION_FUEL)
29-
), MekanismGeneratorsConfig.generators);
25+
.addAttachedContainerCapabilities(ContainerType.GAS, () -> GasTanksBuilder.builder()
26+
.addInternalStorage(MekanismGeneratorsConfig.generators.hohlraumFillRate, MekanismGeneratorsConfig.generators.hohlraumMaxGas,
27+
gas -> gas.is(GeneratorTags.Gases.FUSION_FUEL)
28+
).build(), MekanismGeneratorsConfig.generators);
3029
public static final ItemRegistryObject<ItemTurbineBlade> TURBINE_BLADE = ITEMS.registerItem("turbine_blade", ItemTurbineBlade::new);
3130

3231
public static final ItemRegistryObject<ItemModule> MODULE_SOLAR_RECHARGING = ITEMS.registerModule(GeneratorsModules.SOLAR_RECHARGING_UNIT, Rarity.RARE);

src/generators/java/mekanism/generators/common/slot/FluidFuelInventorySlot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static FluidFuelInventorySlot forFuel(IExtendedFluidTank fluidTank, ToInt
4444
//Always allow extraction if something went horribly wrong, and we are not a fluid item AND we can't provide a valid type of chemical
4545
// This might happen after a reload for example
4646
return fuelValue.applyAsInt(stack) == 0;
47-
}, fillPredicate.or(stack -> fuelValue.applyAsInt(stack) > 0), listener, x, y);
47+
}, stack -> fuelValue.applyAsInt(stack) > 0 || fillPredicate.test(stack), listener, x, y);
4848
}
4949

5050
private final IntFunction<@NotNull FluidStack> fuelCreator;

0 commit comments

Comments
 (0)