From bfe1fcdc5332da563b1f450895b5b5f0e22e780f Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:40:45 +0100 Subject: [PATCH] use nbt keys and put move back --- .../java/com/enderio/core/CoreNBTKeys.java | 1 + .../common/blockentity/DrainBlockEntity.java | 22 ------------------ .../blockentity/FluidTankBlockEntity.java | 21 ----------------- .../blockentity/SoulBinderBlockEntity.java | 22 ------------------ .../blockentity/SoulEngineBlockEntity.java | 22 ------------------ .../blockentity/XPObeliskBlockEntity.java | 22 ------------------ .../blockentity/XPVacuumBlockEntity.java | 23 ------------------- .../blockentity/base/MachineBlockEntity.java | 23 ++++++++++++++----- .../common/io/fluid/MachineFluidHandler.java | 7 +++--- .../machines/common/item/FluidTankItem.java | 9 ++++---- 10 files changed, 26 insertions(+), 146 deletions(-) diff --git a/src/core/java/com/enderio/core/CoreNBTKeys.java b/src/core/java/com/enderio/core/CoreNBTKeys.java index ed34130843..de3f600e1b 100644 --- a/src/core/java/com/enderio/core/CoreNBTKeys.java +++ b/src/core/java/com/enderio/core/CoreNBTKeys.java @@ -11,6 +11,7 @@ public class CoreNBTKeys { public static final String ITEMS = "Items"; public static final String FLUID = "Fluid"; public static final String FLUIDS = "Fluids"; + public static final String TANKS = "Tanks"; public static final String ENERGY = "Energy"; public static final String BLOCK_ENTITY_TAG = BlockItem.BLOCK_ENTITY_TAG; diff --git a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java index 2d39467069..70a7ed2355 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/DrainBlockEntity.java @@ -15,7 +15,6 @@ import com.enderio.machines.common.init.MachineAttachments; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.io.FixedIOConfig; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; @@ -24,7 +23,6 @@ import com.enderio.machines.common.menu.DrainMenu; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -35,7 +33,6 @@ import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; import net.neoforged.neoforge.fluids.capability.IFluidHandler; @@ -253,23 +250,4 @@ public void load(CompoundTag pTag) { consumed = pTag.getInt(CONSUMED); loadTank(pTag); } - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java index 0efae3fa80..9516ad9eac 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/FluidTankBlockEntity.java @@ -7,7 +7,6 @@ import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.init.MachineRecipes; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.IFluidItemInteractive; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; @@ -18,7 +17,6 @@ import com.enderio.machines.common.menu.FluidTankMenu; import com.enderio.machines.common.recipe.TankRecipe; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; @@ -371,23 +369,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java index 28207c9159..cab79b52fa 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulBinderBlockEntity.java @@ -16,7 +16,6 @@ import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; import com.enderio.machines.common.init.MachineRecipes; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; @@ -28,7 +27,6 @@ import com.enderio.machines.common.recipe.RecipeCaches; import com.enderio.machines.common.recipe.SoulBindingRecipe; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -39,7 +37,6 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluids; import net.neoforged.fml.LogicalSide; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; @@ -228,23 +225,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java index 9691e9600c..1e3804b9ac 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/SoulEngineBlockEntity.java @@ -13,7 +13,6 @@ import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.energy.MachineEnergyStorage; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; @@ -23,7 +22,6 @@ import com.enderio.machines.common.menu.SoulEngineMenu; import com.enderio.machines.common.souldata.EngineSoul; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.core.Holder; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; @@ -40,7 +38,6 @@ import net.minecraft.world.level.material.Fluids; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.Mod; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.client.event.RecipesUpdatedEvent; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.FluidType; @@ -257,23 +254,4 @@ public void setLevel(Level level) { static void onReload(RecipesUpdatedEvent event) { reload = !reload; } - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java index ea14646edb..26d5731829 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPObeliskBlockEntity.java @@ -7,21 +7,18 @@ import com.enderio.machines.common.attachment.IFluidTankUser; import com.enderio.machines.common.blockentity.base.MachineBlockEntity; import com.enderio.machines.common.init.MachineBlockEntities; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.menu.XPObeliskMenu; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluids; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; @@ -139,23 +136,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java index a7899ac67a..8f78cfa148 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/XPVacuumBlockEntity.java @@ -6,23 +6,19 @@ import com.enderio.machines.common.blockentity.base.VacuumMachineBlockEntity; import com.enderio.machines.common.config.MachinesConfig; import com.enderio.machines.common.init.MachineBlockEntities; -import com.enderio.machines.common.io.TransferUtil; import com.enderio.machines.common.io.fluid.MachineFluidHandler; import com.enderio.machines.common.io.fluid.MachineFluidTank; import com.enderio.machines.common.io.fluid.MachineTankLayout; import com.enderio.machines.common.io.fluid.TankAccess; import com.enderio.machines.common.menu.XPVacuumMenu; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.ExperienceOrb; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.level.block.state.BlockState; -import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.fluids.FluidStack; -import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.fluids.capability.IFluidHandler.FluidAction; import static com.enderio.base.common.util.ExperienceUtil.EXP_TO_FLUID; @@ -105,23 +101,4 @@ public void load(CompoundTag pTag) { } // endregion - - /** - * Move fluids to and fro via the given side. - */ - private void moveFluids(Direction side) { - IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); - IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); - if (selfHandler == null || otherHandler == null) { - return; - } - - TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); - } - - @Override - public void moveResource(Direction direction) { - super.moveResource(direction); - moveFluids(direction); - } } diff --git a/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java index 875be55dfe..09402ca59a 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/base/MachineBlockEntity.java @@ -39,6 +39,7 @@ import net.neoforged.neoforge.capabilities.ICapabilityProvider; import net.neoforged.neoforge.client.model.data.ModelData; import net.neoforged.neoforge.client.model.data.ModelProperty; +import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.items.IItemHandler; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -338,17 +339,14 @@ private void forceResources() { for (Direction direction : Direction.values()) { if (ioConfig.getMode(direction).canForce()) { // TODO: Maybe some kind of resource distributor so that items are transmitted evenly around? rather than taking the order of Direction.values() - moveResource(direction); + moveItems(direction); + moveFluids(direction); } } } - public void moveResource(Direction direction) { - moveItems(direction); - } - /** - * Move items to and fro via the given side. + * Move items to and from via the given side. */ private void moveItems(Direction side) { IItemHandler selfHandler = getSelfCapability(Capabilities.ItemHandler.BLOCK, side); @@ -360,6 +358,19 @@ private void moveItems(Direction side) { TransferUtil.distributeItems(ioConfig.getMode(side), selfHandler, otherHandler); } + /** + * Move fluids to and from via the given side. + */ + private void moveFluids(Direction side) { + IFluidHandler selfHandler = getSelfCapability(Capabilities.FluidHandler.BLOCK, side); + IFluidHandler otherHandler = getNeighbouringCapability(Capabilities.FluidHandler.BLOCK, side); + if (selfHandler == null || otherHandler == null) { + return; + } + + TransferUtil.distributeFluids(getIOConfig().getMode(side), selfHandler, otherHandler); + } + // endregion // region Serialization diff --git a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java index 6ce65c0dee..1ea899392b 100644 --- a/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java +++ b/src/machines/java/com/enderio/machines/common/io/fluid/MachineFluidHandler.java @@ -1,6 +1,7 @@ package com.enderio.machines.common.io.fluid; import com.enderio.api.io.IIOConfig; +import com.enderio.core.CoreNBTKeys; import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; @@ -11,7 +12,6 @@ import net.neoforged.neoforge.fluids.capability.IFluidHandler; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,7 +23,6 @@ public class MachineFluidHandler implements IFluidHandler, INBTSerializable { public static final String TANK_INDEX = "Index"; - public static final String TANKS = "Tanks"; private final IIOConfig config; private final MachineTankLayout layout; private Map tanks = new HashMap<>(); @@ -219,13 +218,13 @@ public CompoundTag serializeNBT() { nbtTagList.add(tankTag); } CompoundTag nbt = new CompoundTag(); - nbt.put(TANKS, nbtTagList); + nbt.put(CoreNBTKeys.TANKS, nbtTagList); return nbt; } @Override public void deserializeNBT(CompoundTag nbt) { - ListTag tagList = nbt.getList(TANKS, Tag.TAG_COMPOUND); + ListTag tagList = nbt.getList(CoreNBTKeys.TANKS, Tag.TAG_COMPOUND); for (int i = 0; i < tagList.size(); i++) { CompoundTag tankTag = tagList.getCompound(i); int index = tankTag.getInt(TANK_INDEX); diff --git a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java index 58800ae639..56617379c3 100644 --- a/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java +++ b/src/machines/java/com/enderio/machines/common/item/FluidTankItem.java @@ -1,6 +1,7 @@ package com.enderio.machines.common.item; import com.enderio.base.common.lang.EIOLang; +import com.enderio.core.CoreNBTKeys; import com.enderio.core.client.item.IAdvancedTooltipProvider; import com.enderio.core.common.util.TooltipUtil; import com.enderio.machines.client.rendering.item.FluidTankBEWLR; @@ -93,7 +94,7 @@ public FluidStack getFluid() { return tagCompoundOptional .map(tagCompound -> tagCompound.getCompound(BLOCK_ENTITY_TAG)) .map(blockEntityTag -> blockEntityTag.getCompound(MachineNBTKeys.FLUIDS)) - .map(fluidTag -> fluidTag.getList("Tanks", Tag.TAG_COMPOUND)) + .map(fluidTag -> fluidTag.getList(CoreNBTKeys.TANKS, Tag.TAG_COMPOUND)) .map(tank -> tank.getCompound(0)) .map(FluidStack::loadFluidStackFromNBT) .orElse(FluidStack.EMPTY); @@ -109,7 +110,7 @@ protected void setFluid(FluidStack fluid) { ListTag listTag = new ListTag(); listTag.add(0, fluidTag); CompoundTag tanks = new CompoundTag(); - tanks.put("Tanks", listTag); + tanks.put(CoreNBTKeys.TANKS, listTag); blockEntityTag.put(MachineNBTKeys.FLUIDS, tanks); } @@ -118,8 +119,8 @@ protected void setContainerToEmpty() { CompoundTag tagCompound = container.getTag(); if (tagCompound != null) { CompoundTag blockEntityTag = tagCompound.getCompound(BLOCK_ENTITY_TAG); - if (blockEntityTag.contains("Tanks")) { - blockEntityTag.remove("Tanks"); + if (blockEntityTag.contains(CoreNBTKeys.TANKS)) { + blockEntityTag.remove(CoreNBTKeys.TANKS); } } }