Skip to content

Commit 525cdec

Browse files
authored
Make CableTier more accessible (#939)
1 parent 9374e59 commit 525cdec

File tree

7 files changed

+97
-8
lines changed

7 files changed

+97
-8
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Azercoco & Technici4n
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package aztech.modern_industrialization.api.energy;
25+
26+
public interface CableTierHolder {
27+
CableTier getCableTier();
28+
}

src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricCraftingMachineBlockEntity.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
package aztech.modern_industrialization.machines.blockentities;
2525

2626
import aztech.modern_industrialization.MICapabilities;
27+
import aztech.modern_industrialization.api.energy.CableTier;
28+
import aztech.modern_industrialization.api.energy.CableTierHolder;
2729
import aztech.modern_industrialization.api.energy.EnergyApi;
2830
import aztech.modern_industrialization.api.energy.MIEnergyStorage;
2931
import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder;
@@ -44,7 +46,7 @@
4446
import net.minecraft.world.entity.player.Player;
4547
import net.minecraft.world.level.block.entity.BlockEntityType;
4648

47-
public class ElectricCraftingMachineBlockEntity extends AbstractCraftingMachineBlockEntity implements EnergyComponentHolder {
49+
public class ElectricCraftingMachineBlockEntity extends AbstractCraftingMachineBlockEntity implements EnergyComponentHolder, CableTierHolder {
4850

4951
public ElectricCraftingMachineBlockEntity(BEP bep, MachineRecipeType recipeType, MachineInventoryComponent inventory,
5052
MachineGuiParameters guiParams, EnergyBar.Parameters energyBarParams, ProgressBar.Parameters progressBarParams,
@@ -132,4 +134,9 @@ public boolean isOverdriving() {
132134
public EnergyComponent getEnergyComponent() {
133135
return energy;
134136
}
137+
138+
@Override
139+
public CableTier getCableTier() {
140+
return casing.getCableTier();
141+
}
135142
}

src/main/java/aztech/modern_industrialization/machines/blockentities/ElectricWaterPumpBlockEntity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import aztech.modern_industrialization.MICapabilities;
2727
import aztech.modern_industrialization.api.energy.CableTier;
28+
import aztech.modern_industrialization.api.energy.CableTierHolder;
2829
import aztech.modern_industrialization.api.energy.EnergyApi;
2930
import aztech.modern_industrialization.api.energy.MIEnergyStorage;
3031
import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder;
@@ -43,7 +44,7 @@
4344
import net.minecraft.world.level.material.Fluids;
4445
import net.neoforged.neoforge.fluids.FluidType;
4546

46-
public class ElectricWaterPumpBlockEntity extends AbstractWaterPumpBlockEntity implements EnergyComponentHolder {
47+
public class ElectricWaterPumpBlockEntity extends AbstractWaterPumpBlockEntity implements EnergyComponentHolder, CableTierHolder {
4748
public ElectricWaterPumpBlockEntity(BEP bep) {
4849
super(bep, "electric_water_pump");
4950

@@ -105,4 +106,9 @@ public static void registerEnergyApi(BlockEntityType<?> bet) {
105106
event.registerBlockEntity(EnergyApi.SIDED, bet, (be, direction) -> ((ElectricWaterPumpBlockEntity) be).insertable);
106107
});
107108
}
109+
110+
@Override
111+
public CableTier getCableTier() {
112+
return CableTier.LV;
113+
}
108114
}

src/main/java/aztech/modern_industrialization/machines/blockentities/GeneratorMachineBlockEntity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import aztech.modern_industrialization.MICapabilities;
2929
import aztech.modern_industrialization.api.energy.CableTier;
30+
import aztech.modern_industrialization.api.energy.CableTierHolder;
3031
import aztech.modern_industrialization.api.energy.EnergyApi;
3132
import aztech.modern_industrialization.api.energy.MIEnergyStorage;
3233
import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder;
@@ -50,7 +51,7 @@
5051
import net.minecraft.network.chat.Component;
5152
import net.minecraft.world.level.block.entity.BlockEntityType;
5253

53-
public class GeneratorMachineBlockEntity extends MachineBlockEntity implements Tickable, EnergyComponentHolder {
54+
public class GeneratorMachineBlockEntity extends MachineBlockEntity implements Tickable, EnergyComponentHolder, CableTierHolder {
5455

5556
private final CableTier outputTier;
5657
private final MIEnergyStorage extractable;
@@ -212,4 +213,9 @@ public static void registerEnergyApi(BlockEntityType<?> bet) {
212213
public List<Component> getTooltips() {
213214
return fluidItemConsumer.getTooltips();
214215
}
216+
217+
@Override
218+
public CableTier getCableTier() {
219+
return outputTier;
220+
}
215221
}

src/main/java/aztech/modern_industrialization/machines/blockentities/StorageMachineBlockEntity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
package aztech.modern_industrialization.machines.blockentities;
2525

2626
import aztech.modern_industrialization.api.energy.CableTier;
27+
import aztech.modern_industrialization.api.energy.CableTierHolder;
2728
import aztech.modern_industrialization.machines.BEP;
2829
import net.minecraft.util.Mth;
2930

30-
public class StorageMachineBlockEntity extends AbstractStorageMachineBlockEntity {
31+
public class StorageMachineBlockEntity extends AbstractStorageMachineBlockEntity implements CableTierHolder {
3132

3233
public StorageMachineBlockEntity(BEP bep, CableTier tier, String name, long eu_capacity) {
3334
super(bep, tier, tier, name, eu_capacity);
@@ -43,4 +44,9 @@ protected int getComparatorOutput() {
4344
double fillPercentage = (double) energy.getEu() / energy.getCapacity();
4445
return Mth.floor(fillPercentage * 14) + (energy.getEu() > 0 ? 1 : 0);
4546
}
47+
48+
@Override
49+
public CableTier getCableTier() {
50+
return to;
51+
}
4652
}

src/main/java/aztech/modern_industrialization/machines/blockentities/hatches/EnergyHatch.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import aztech.modern_industrialization.MICapabilities;
2727
import aztech.modern_industrialization.api.energy.CableTier;
28+
import aztech.modern_industrialization.api.energy.CableTierHolder;
2829
import aztech.modern_industrialization.api.energy.EnergyApi;
2930
import aztech.modern_industrialization.api.energy.MIEnergyStorage;
3031
import aztech.modern_industrialization.api.machine.holder.EnergyComponentHolder;
@@ -39,12 +40,13 @@
3940
import java.util.List;
4041
import net.minecraft.world.level.block.entity.BlockEntityType;
4142

42-
public class EnergyHatch extends HatchBlockEntity implements EnergyComponentHolder {
43+
public class EnergyHatch extends HatchBlockEntity implements EnergyComponentHolder, CableTierHolder {
4344

4445
public EnergyHatch(BEP bep, String name, boolean input, CableTier tier) {
4546
super(bep, new MachineGuiParameters.Builder(name, false).build(), new OrientationComponent.Params(!input, false, false));
4647

4748
this.input = input;
49+
this.tier = tier;
4850

4951
this.energy = new EnergyComponent(this, 30 * 20 * tier.getEu());
5052
insertable = energy.buildInsertable((CableTier tier2) -> tier2 == tier);
@@ -56,10 +58,11 @@ public EnergyHatch(BEP bep, String name, boolean input, CableTier tier) {
5658
}
5759

5860
private final boolean input;
61+
private final CableTier tier;
5962

60-
protected final EnergyComponent energy;
61-
protected final MIEnergyStorage insertable;
62-
protected final MIEnergyStorage extractable;
63+
private final EnergyComponent energy;
64+
private final MIEnergyStorage insertable;
65+
private final MIEnergyStorage extractable;
6366

6467
@Override
6568
public HatchType getHatchType() {
@@ -71,6 +74,11 @@ public boolean upgradesToSteel() {
7174
return false;
7275
}
7376

77+
@Override
78+
public CableTier getCableTier() {
79+
return tier;
80+
}
81+
7482
@Override
7583
public MIInventory getInventory() {
7684
return MIInventory.EMPTY;

src/main/java/aztech/modern_industrialization/machines/components/CasingComponent.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,25 @@
4444

4545
public class CasingComponent implements IComponent, DropableComponent {
4646

47+
@FunctionalInterface
48+
public interface UpdatedCallback {
49+
void onUpdated(CableTier from, CableTier to);
50+
}
51+
52+
@Nullable
53+
private final UpdatedCallback callback;
54+
4755
private ItemStack casingStack = ItemStack.EMPTY;
4856
private CableTier currentTier = CableTier.LV;
4957

58+
public CasingComponent(@Nullable UpdatedCallback callback) {
59+
this.callback = callback;
60+
}
61+
62+
public CasingComponent() {
63+
this(null);
64+
}
65+
5066
/**
5167
* Sets the current casing stack and update {@link #currentTier} accordingly.
5268
*/
@@ -80,13 +96,18 @@ public void readClientNbt(CompoundTag tag, HolderLookup.Provider registries) {
8096
currentTier = CableTier.getTier(tag.getString("casing"));
8197
}
8298

99+
public CableTier getCableTier() {
100+
return currentTier;
101+
}
102+
83103
public void dropCasing(Level world, BlockPos pos) {
84104
Containers.dropItemStack(world, pos.getX(), pos.getY(), pos.getZ(), casingStack);
85105
}
86106

87107
public ItemInteractionResult onUse(MachineBlockEntity be, Player player, InteractionHand hand) {
88108
ItemStack stackInHand = player.getItemInHand(hand);
89109
if (stackInHand.getCount() >= 1) {
110+
var previousTier = currentTier;
90111
var newTier = getCasingTier(stackInHand.getItem());
91112
if (newTier != null && newTier != currentTier) {
92113
if (currentTier != CableTier.LV) {
@@ -103,6 +124,9 @@ public ItemInteractionResult onUse(MachineBlockEntity be, Player player, Interac
103124
be.getLevel().updateNeighborsAt(be.getBlockPos(), Blocks.AIR);
104125
// Play a nice sound :)
105126
playCasingPlaceSound(be);
127+
if (callback != null) {
128+
callback.onUpdated(previousTier, currentTier);
129+
}
106130
return ItemInteractionResult.sidedSuccess(be.getLevel().isClientSide);
107131
}
108132
}
@@ -142,11 +166,15 @@ public ItemStack getDrop() {
142166
}
143167

144168
public void setCasingServer(MachineBlockEntity be, ItemStack casing) {
169+
var previousTier = currentTier;
145170
setCasingStack(casing);
146171
be.setChanged();
147172
be.sync();
148173
be.getLevel().updateNeighborsAt(be.getBlockPos(), Blocks.AIR);
149174
playCasingPlaceSound(be);
175+
if (callback != null && previousTier != currentTier) {
176+
callback.onUpdated(previousTier, currentTier);
177+
}
150178
}
151179

152180
public MachineCasing getCasing() {

0 commit comments

Comments
 (0)