|
2 | 2 |
|
3 | 3 | import dan200.computercraft.api.peripheral.IComputerAccess; |
4 | 4 | import dan200.computercraft.api.peripheral.IPeripheral; |
5 | | -import de.srendi.advancedperipherals.AdvancedPeripherals; |
6 | 5 | import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral; |
| 6 | +import de.srendi.advancedperipherals.lib.peripherals.DisabledPeripheral; |
7 | 7 | import de.srendi.advancedperipherals.lib.peripherals.IPeripheralTileEntity; |
8 | 8 | import net.minecraft.core.BlockPos; |
9 | 9 | import net.minecraft.core.Direction; |
|
29 | 29 | import org.jetbrains.annotations.Nullable; |
30 | 30 |
|
31 | 31 | import java.util.Collections; |
| 32 | +import java.util.Optional; |
32 | 33 |
|
33 | 34 | public abstract class PeripheralBlockEntity<T extends BasePeripheral<?>> extends BaseContainerBlockEntity implements WorldlyContainer, MenuProvider, IPeripheralTileEntity, ICapabilityProvider { |
34 | 35 | private static final String PERIPHERAL_SETTINGS_KEY = "peripheralSettings"; |
35 | | - protected CompoundTag peripheralSettings; |
| 36 | + protected CompoundTag peripheralSettings = new CompoundTag(); |
36 | 37 | protected NonNullList<ItemStack> items; |
37 | 38 | @Nullable |
38 | | - protected T peripheral = null; |
39 | | - private IItemHandler itemHandler; |
40 | | - private IFluidHandler fluidHandler; |
| 39 | + private IItemHandler itemHandler = null; |
| 40 | + private IFluidHandler fluidHandler = null; |
| 41 | + private IPeripheral peripheral = null; |
41 | 42 |
|
42 | | - public PeripheralBlockEntity(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, BlockState state) { |
| 43 | + protected PeripheralBlockEntity(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, BlockState state) { |
43 | 44 | super(tileEntityTypeIn, pos, state); |
44 | 45 | if (this instanceof IInventoryBlock<?> inventoryBlock) { |
45 | 46 | items = NonNullList.withSize(inventoryBlock.getInvSize(), ItemStack.EMPTY); |
46 | 47 | } else { |
47 | 48 | items = NonNullList.withSize(0, ItemStack.EMPTY); |
48 | 49 | } |
49 | | - peripheralSettings = new CompoundTag(); |
50 | 50 | } |
51 | 51 |
|
52 | 52 | @Nullable |
53 | 53 | @Override |
54 | 54 | public IPeripheral createPeripheralCap(@Nullable Direction side) { |
55 | | - if (peripheral == null) |
56 | | - // Perform later peripheral creation, because creating peripheral |
57 | | - // on init of tile entity cause some infinity loop, if peripheral |
58 | | - // are depend on tile entity data |
59 | | - this.peripheral = this.createPeripheral(); |
60 | | - if (peripheral.isEnabled()) { |
61 | | - return peripheral; |
62 | | - } else { |
63 | | - AdvancedPeripherals.debug(peripheral.getType() + " is disabled, you can enable it in the Configuration."); |
| 55 | + // Perform later peripheral creation, because creating peripheral |
| 56 | + // on init of tile entity cause some infinity loop, if peripheral |
| 57 | + // are depend on tile entity data |
| 58 | + if (this.peripheral == null) { |
| 59 | + // Recreate peripheral to allow CC: Tweaked correctly handle |
| 60 | + // peripheral update logic, so new peripheral and old one will be |
| 61 | + // different |
| 62 | + this.peripheral = this.createPeripheralDisable(); |
64 | 63 | } |
65 | | - return null; |
| 64 | + return this.peripheral; |
66 | 65 | } |
67 | 66 |
|
68 | 67 | @Nullable |
69 | 68 | @Override |
70 | 69 | public IFluidHandler createFluidHandlerCap(@Nullable Direction side) { |
71 | | - if (fluidHandler == null) |
72 | | - fluidHandler = new FluidTank(0); |
73 | | - return fluidHandler; |
| 70 | + if (this.fluidHandler == null) { |
| 71 | + this.fluidHandler = new FluidTank(0); |
| 72 | + } |
| 73 | + return this.fluidHandler; |
74 | 74 | } |
75 | 75 |
|
76 | 76 | @Nullable |
77 | 77 | @Override |
78 | 78 | public IItemHandler createItemHandlerCap(@Nullable Direction side) { |
79 | | - if (itemHandler == null) |
80 | | - itemHandler = new SidedInvWrapper(this, null); |
81 | | - return itemHandler; |
| 79 | + if (this.itemHandler == null) { |
| 80 | + this.itemHandler = new SidedInvWrapper(this, null); |
| 81 | + } |
| 82 | + return this.itemHandler; |
82 | 83 | } |
83 | 84 |
|
84 | 85 | @NotNull |
85 | 86 | protected abstract T createPeripheral(); |
86 | 87 |
|
| 88 | + protected IPeripheral createPeripheralDisable() { |
| 89 | + T peripheral = this.createPeripheral(); |
| 90 | + if (peripheral.isEnabled()) { |
| 91 | + return peripheral; |
| 92 | + } |
| 93 | + return new DisabledPeripheral(peripheral); |
| 94 | + } |
| 95 | + |
87 | 96 | public Iterable<IComputerAccess> getConnectedComputers() { |
88 | | - if (peripheral == null) // just avoid some NPE in strange cases |
89 | | - return Collections.emptyList(); |
90 | | - return peripheral.getConnectedComputers(); |
| 97 | + return this.getPeripheralOptional().map(BasePeripheral::getConnectedComputers).orElse(Collections.emptyList()); |
| 98 | + } |
| 99 | + |
| 100 | + @Nullable |
| 101 | + public T getPeripheral() { |
| 102 | + IPeripheral peripheral = this.createPeripheralCap(null); |
| 103 | + if (peripheral == null || peripheral instanceof DisabledPeripheral) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + return (T) peripheral; |
| 107 | + } |
| 108 | + |
| 109 | + public Optional<T> getPeripheralOptional() { |
| 110 | + return Optional.ofNullable(this.getPeripheral()); |
91 | 111 | } |
92 | 112 |
|
93 | 113 | /*@Override |
@@ -210,7 +230,6 @@ public CompoundTag getPeripheralSettings() { |
210 | 230 |
|
211 | 231 | @Override |
212 | 232 | public void markSettingsChanged() { |
213 | | - setChanged(); |
| 233 | + this.setChanged(); |
214 | 234 | } |
215 | 235 | } |
216 | | - |
|
0 commit comments