From 525cdecd7e3c923287ae03bb17d0e3410885a716 Mon Sep 17 00:00:00 2001 From: Swedz Date: Mon, 2 Dec 2024 04:25:14 -0500 Subject: [PATCH] Make CableTier more accessible (#939) --- .../api/energy/CableTierHolder.java | 28 +++++++++++++++++++ .../ElectricCraftingMachineBlockEntity.java | 9 +++++- .../ElectricWaterPumpBlockEntity.java | 8 +++++- .../GeneratorMachineBlockEntity.java | 8 +++++- .../StorageMachineBlockEntity.java | 8 +++++- .../blockentities/hatches/EnergyHatch.java | 16 ++++++++--- .../machines/components/CasingComponent.java | 28 +++++++++++++++++++ 7 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/main/java/aztech/modern_industrialization/api/energy/CableTierHolder.java diff --git a/src/main/java/aztech/modern_industrialization/api/energy/CableTierHolder.java b/src/main/java/aztech/modern_industrialization/api/energy/CableTierHolder.java new file mode 100644 index 000000000..65786f18d --- /dev/null +++ b/src/main/java/aztech/modern_industrialization/api/energy/CableTierHolder.java @@ -0,0 +1,28 @@ +/* + * MIT License + * + * Copyright (c) 2020 Azercoco & Technici4n + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package aztech.modern_industrialization.api.energy; + +public interface CableTierHolder { + CableTier getCableTier(); +} diff --git a/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricCraftingMachineBlockEntity.java b/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricCraftingMachineBlockEntity.java index 1ef865c02..f04ea0604 100644 --- a/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricCraftingMachineBlockEntity.java +++ b/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricCraftingMachineBlockEntity.java @@ -24,6 +24,8 @@ package aztech.modern_industrialization.machines.blockentities; import aztech.modern_industrialization.MICapabilities; +import aztech.modern_industrialization.api.energy.CableTier; +import aztech.modern_industrialization.api.energy.CableTierHolder; import aztech.modern_industrialization.api.energy.EnergyApi; import aztech.modern_industrialization.api.energy.MIEnergyStorage; import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder; @@ -44,7 +46,7 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.block.entity.BlockEntityType; -public class ElectricCraftingMachineBlockEntity extends AbstractCraftingMachineBlockEntity implements EnergyComponentHolder { +public class ElectricCraftingMachineBlockEntity extends AbstractCraftingMachineBlockEntity implements EnergyComponentHolder, CableTierHolder { public ElectricCraftingMachineBlockEntity(BEP bep, MachineRecipeType recipeType, MachineInventoryComponent inventory, MachineGuiParameters guiParams, EnergyBar.Parameters energyBarParams, ProgressBar.Parameters progressBarParams, @@ -132,4 +134,9 @@ public boolean isOverdriving() { public EnergyComponent getEnergyComponent() { return energy; } + + @Override + public CableTier getCableTier() { + return casing.getCableTier(); + } } diff --git a/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricWaterPumpBlockEntity.java b/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricWaterPumpBlockEntity.java index d837999ab..dc542d656 100644 --- a/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricWaterPumpBlockEntity.java +++ b/src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricWaterPumpBlockEntity.java @@ -25,6 +25,7 @@ import aztech.modern_industrialization.MICapabilities; import aztech.modern_industrialization.api.energy.CableTier; +import aztech.modern_industrialization.api.energy.CableTierHolder; import aztech.modern_industrialization.api.energy.EnergyApi; import aztech.modern_industrialization.api.energy.MIEnergyStorage; import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder; @@ -43,7 +44,7 @@ import net.minecraft.world.level.material.Fluids; import net.neoforged.neoforge.fluids.FluidType; -public class ElectricWaterPumpBlockEntity extends AbstractWaterPumpBlockEntity implements EnergyComponentHolder { +public class ElectricWaterPumpBlockEntity extends AbstractWaterPumpBlockEntity implements EnergyComponentHolder, CableTierHolder { public ElectricWaterPumpBlockEntity(BEP bep) { super(bep, "electric_water_pump"); @@ -105,4 +106,9 @@ public static void registerEnergyApi(BlockEntityType bet) { event.registerBlockEntity(EnergyApi.SIDED, bet, (be, direction) -> ((ElectricWaterPumpBlockEntity) be).insertable); }); } + + @Override + public CableTier getCableTier() { + return CableTier.LV; + } } diff --git a/src/main/java/aztech/modern_industrialization/machines/blockentities/GeneratorMachineBlockEntity.java b/src/main/java/aztech/modern_industrialization/machines/blockentities/GeneratorMachineBlockEntity.java index 994a12092..e5d19728b 100644 --- a/src/main/java/aztech/modern_industrialization/machines/blockentities/GeneratorMachineBlockEntity.java +++ b/src/main/java/aztech/modern_industrialization/machines/blockentities/GeneratorMachineBlockEntity.java @@ -27,6 +27,7 @@ import aztech.modern_industrialization.MICapabilities; import aztech.modern_industrialization.api.energy.CableTier; +import aztech.modern_industrialization.api.energy.CableTierHolder; import aztech.modern_industrialization.api.energy.EnergyApi; import aztech.modern_industrialization.api.energy.MIEnergyStorage; import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder; @@ -50,7 +51,7 @@ import net.minecraft.network.chat.Component; import net.minecraft.world.level.block.entity.BlockEntityType; -public class GeneratorMachineBlockEntity extends MachineBlockEntity implements Tickable, EnergyComponentHolder { +public class GeneratorMachineBlockEntity extends MachineBlockEntity implements Tickable, EnergyComponentHolder, CableTierHolder { private final CableTier outputTier; private final MIEnergyStorage extractable; @@ -212,4 +213,9 @@ public static void registerEnergyApi(BlockEntityType bet) { public List getTooltips() { return fluidItemConsumer.getTooltips(); } + + @Override + public CableTier getCableTier() { + return outputTier; + } } diff --git a/src/main/java/aztech/modern_industrialization/machines/blockentities/StorageMachineBlockEntity.java b/src/main/java/aztech/modern_industrialization/machines/blockentities/StorageMachineBlockEntity.java index 6765d9009..fe5baf2e7 100644 --- a/src/main/java/aztech/modern_industrialization/machines/blockentities/StorageMachineBlockEntity.java +++ b/src/main/java/aztech/modern_industrialization/machines/blockentities/StorageMachineBlockEntity.java @@ -24,10 +24,11 @@ package aztech.modern_industrialization.machines.blockentities; import aztech.modern_industrialization.api.energy.CableTier; +import aztech.modern_industrialization.api.energy.CableTierHolder; import aztech.modern_industrialization.machines.BEP; import net.minecraft.util.Mth; -public class StorageMachineBlockEntity extends AbstractStorageMachineBlockEntity { +public class StorageMachineBlockEntity extends AbstractStorageMachineBlockEntity implements CableTierHolder { public StorageMachineBlockEntity(BEP bep, CableTier tier, String name, long eu_capacity) { super(bep, tier, tier, name, eu_capacity); @@ -43,4 +44,9 @@ protected int getComparatorOutput() { double fillPercentage = (double) energy.getEu() / energy.getCapacity(); return Mth.floor(fillPercentage * 14) + (energy.getEu() > 0 ? 1 : 0); } + + @Override + public CableTier getCableTier() { + return to; + } } diff --git a/src/main/java/aztech/modern_industrialization/machines/blockentities/hatches/EnergyHatch.java b/src/main/java/aztech/modern_industrialization/machines/blockentities/hatches/EnergyHatch.java index 94805acf5..82a721905 100644 --- a/src/main/java/aztech/modern_industrialization/machines/blockentities/hatches/EnergyHatch.java +++ b/src/main/java/aztech/modern_industrialization/machines/blockentities/hatches/EnergyHatch.java @@ -25,6 +25,7 @@ import aztech.modern_industrialization.MICapabilities; import aztech.modern_industrialization.api.energy.CableTier; +import aztech.modern_industrialization.api.energy.CableTierHolder; import aztech.modern_industrialization.api.energy.EnergyApi; import aztech.modern_industrialization.api.energy.MIEnergyStorage; import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder; @@ -39,12 +40,13 @@ import java.util.List; import net.minecraft.world.level.block.entity.BlockEntityType; -public class EnergyHatch extends HatchBlockEntity implements EnergyComponentHolder { +public class EnergyHatch extends HatchBlockEntity implements EnergyComponentHolder, CableTierHolder { public EnergyHatch(BEP bep, String name, boolean input, CableTier tier) { super(bep, new MachineGuiParameters.Builder(name, false).build(), new OrientationComponent.Params(!input, false, false)); this.input = input; + this.tier = tier; this.energy = new EnergyComponent(this, 30 * 20 * tier.getEu()); insertable = energy.buildInsertable((CableTier tier2) -> tier2 == tier); @@ -56,10 +58,11 @@ public EnergyHatch(BEP bep, String name, boolean input, CableTier tier) { } private final boolean input; + private final CableTier tier; - protected final EnergyComponent energy; - protected final MIEnergyStorage insertable; - protected final MIEnergyStorage extractable; + private final EnergyComponent energy; + private final MIEnergyStorage insertable; + private final MIEnergyStorage extractable; @Override public HatchType getHatchType() { @@ -71,6 +74,11 @@ public boolean upgradesToSteel() { return false; } + @Override + public CableTier getCableTier() { + return tier; + } + @Override public MIInventory getInventory() { return MIInventory.EMPTY; diff --git a/src/main/java/aztech/modern_industrialization/machines/components/CasingComponent.java b/src/main/java/aztech/modern_industrialization/machines/components/CasingComponent.java index aaabf953e..14c037c74 100644 --- a/src/main/java/aztech/modern_industrialization/machines/components/CasingComponent.java +++ b/src/main/java/aztech/modern_industrialization/machines/components/CasingComponent.java @@ -44,9 +44,25 @@ public class CasingComponent implements IComponent, DropableComponent { + @FunctionalInterface + public interface UpdatedCallback { + void onUpdated(CableTier from, CableTier to); + } + + @Nullable + private final UpdatedCallback callback; + private ItemStack casingStack = ItemStack.EMPTY; private CableTier currentTier = CableTier.LV; + public CasingComponent(@Nullable UpdatedCallback callback) { + this.callback = callback; + } + + public CasingComponent() { + this(null); + } + /** * Sets the current casing stack and update {@link #currentTier} accordingly. */ @@ -80,6 +96,10 @@ public void readClientNbt(CompoundTag tag, HolderLookup.Provider registries) { currentTier = CableTier.getTier(tag.getString("casing")); } + public CableTier getCableTier() { + return currentTier; + } + public void dropCasing(Level world, BlockPos pos) { Containers.dropItemStack(world, pos.getX(), pos.getY(), pos.getZ(), casingStack); } @@ -87,6 +107,7 @@ public void dropCasing(Level world, BlockPos pos) { public ItemInteractionResult onUse(MachineBlockEntity be, Player player, InteractionHand hand) { ItemStack stackInHand = player.getItemInHand(hand); if (stackInHand.getCount() >= 1) { + var previousTier = currentTier; var newTier = getCasingTier(stackInHand.getItem()); if (newTier != null && newTier != currentTier) { if (currentTier != CableTier.LV) { @@ -103,6 +124,9 @@ public ItemInteractionResult onUse(MachineBlockEntity be, Player player, Interac be.getLevel().updateNeighborsAt(be.getBlockPos(), Blocks.AIR); // Play a nice sound :) playCasingPlaceSound(be); + if (callback != null) { + callback.onUpdated(previousTier, currentTier); + } return ItemInteractionResult.sidedSuccess(be.getLevel().isClientSide); } } @@ -142,11 +166,15 @@ public ItemStack getDrop() { } public void setCasingServer(MachineBlockEntity be, ItemStack casing) { + var previousTier = currentTier; setCasingStack(casing); be.setChanged(); be.sync(); be.getLevel().updateNeighborsAt(be.getBlockPos(), Blocks.AIR); playCasingPlaceSound(be); + if (callback != null && previousTier != currentTier) { + callback.onUpdated(previousTier, currentTier); + } } public MachineCasing getCasing() {