|
1 | 1 | package igentuman.nc.block.entity.kugelblitz; |
2 | 2 |
|
3 | 3 | import igentuman.nc.block.entity.NuclearCraftBE; |
4 | | -import igentuman.nc.handler.sided.SidedContentHandler; |
| 4 | +import igentuman.nc.block.entity.fusion.FusionCoreProxyBE; |
| 5 | +import igentuman.nc.client.particle.FusionBeamParticleData; |
| 6 | +import igentuman.nc.multiblock.AbstractNCMultiblock; |
| 7 | +import igentuman.nc.multiblock.MultiblockHandler; |
| 8 | +import igentuman.nc.multiblock.kugelblitz.KugelblitzMultiblock; |
| 9 | +import igentuman.nc.multiblock.kugelblitz.KugelblitzRegistration; |
5 | 10 | import igentuman.nc.util.CustomEnergyStorage; |
| 11 | +import igentuman.nc.util.NCBlockPos; |
| 12 | +import igentuman.nc.util.annotation.NBTField; |
| 13 | +import mekanism.api.math.FloatingLong; |
6 | 14 | import net.minecraft.core.BlockPos; |
| 15 | +import net.minecraft.core.Direction; |
| 16 | +import net.minecraft.server.level.ServerLevel; |
| 17 | +import net.minecraft.server.level.ServerPlayer; |
| 18 | +import net.minecraft.world.level.block.Block; |
| 19 | +import net.minecraft.world.level.block.entity.BlockEntity; |
7 | 20 | import net.minecraft.world.level.block.entity.BlockEntityType; |
8 | 21 | import net.minecraft.world.level.block.state.BlockState; |
| 22 | +import net.minecraft.world.phys.Vec3; |
9 | 23 | import net.minecraftforge.common.util.LazyOptional; |
10 | 24 | import net.minecraftforge.energy.IEnergyStorage; |
| 25 | +import net.minecraftforge.registries.ForgeRegistries; |
11 | 26 |
|
12 | | -import static igentuman.nc.handler.sided.SlotModePair.SlotMode.INPUT; |
13 | | -import static igentuman.nc.handler.sided.SlotModePair.SlotMode.OUTPUT; |
| 27 | +import static igentuman.nc.block.fission.FissionControllerBlock.POWERED; |
| 28 | +import static igentuman.nc.handler.config.KugelblitzConfig.KUGELBLITZ_CONFIG; |
14 | 29 | import static igentuman.nc.multiblock.kugelblitz.KugelblitzRegistration.EXPL_BE; |
| 30 | +import static igentuman.nc.setup.registration.NCSounds.LASER_SHOOT; |
| 31 | +import static igentuman.nc.util.ModUtil.isMekanismLoaded; |
| 32 | +import static net.minecraft.world.level.block.DirectionalBlock.FACING; |
15 | 33 |
|
16 | 34 | public class EXPLBE extends NuclearCraftBE { |
17 | 35 |
|
| 36 | + @NBTField |
| 37 | + public int pulseEnergy; |
| 38 | + @NBTField |
| 39 | + public int inputRedstoneSignal = 0; |
| 40 | + @NBTField |
| 41 | + public boolean activated = false; |
| 42 | + @NBTField |
18 | 43 | public int energyPerTick; |
| 44 | + @NBTField |
| 45 | + public int pulseTime = 0; |
| 46 | + |
19 | 47 | protected final LazyOptional<IEnergyStorage> energy; |
20 | | - public final SidedContentHandler contentHandler; |
21 | 48 | public final CustomEnergyStorage energyStorage; |
| 49 | + private EXPLProxyBE[] proxyBES; |
| 50 | + private boolean energyTransfered = false; |
22 | 51 |
|
23 | 52 | public EXPLBE(BlockPos pPos, BlockState pBlockState) { |
24 | 53 | super(EXPL_BE.get(), pPos, pBlockState); |
25 | 54 | energyStorage = createEnergy(); |
26 | 55 | energy = LazyOptional.of(() -> energyStorage); |
27 | | - contentHandler = new SidedContentHandler( |
28 | | - 0, 0, |
29 | | - 1, 1, 10, 10); |
30 | | - contentHandler().setBlockEntity(this); |
31 | | - contentHandler().fluidCapability.setGlobalMode(0, INPUT); |
32 | | - contentHandler().fluidCapability.setGlobalMode(1, OUTPUT); |
33 | 56 | } |
34 | 57 |
|
35 | 58 | public EXPLBE(BlockEntityType<?> pType, BlockPos pPos, BlockState pBlockState) { |
36 | 59 | this(pPos, pBlockState); |
37 | 60 | } |
38 | 61 |
|
| 62 | + protected void sendBeamData(FusionBeamParticleData data, BlockPos from) { |
| 63 | + Vec3 vec = Vec3.atCenterOf(from); |
| 64 | + if (!getLevel().isClientSide() && level instanceof ServerLevel serverWorld) { |
| 65 | + for (ServerPlayer player : serverWorld.players()) { |
| 66 | + serverWorld.sendParticles(player, data, true, vec.x, vec.y, vec.z, 1, 0, 0, 0, 0); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + protected void renderBeam() { |
| 72 | + int beamLength = 12; |
| 73 | + sendBeamData(new FusionBeamParticleData(getFacing(), beamLength, energyStorage().getEnergyStored()/(float)energyStorage().getMaxEnergyStored()*0.5f), |
| 74 | + getBlockPos().relative(getFacing()) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + private Direction getFacing() { |
| 79 | + return getBlockState().getValue(FACING); |
| 80 | + } |
| 81 | + |
39 | 82 | protected CustomEnergyStorage createEnergy() { |
40 | | - return new CustomEnergyStorage(2_048_000_000, 5000000, 0) { |
| 83 | + return new CustomEnergyStorage(2_048_000_000, 1000000, 0) { |
41 | 84 | @Override |
42 | 85 | protected void onEnergyChanged() { |
43 | 86 | setChanged(); |
44 | 87 | } |
45 | 88 | }; |
46 | 89 | } |
47 | 90 |
|
| 91 | + protected EXPLProxyBE[] getProxies() { |
| 92 | + if(proxyBES == null) { |
| 93 | + //proxy block placement depends on facing |
| 94 | + Direction facing = getFacing(); |
| 95 | + int minX = -1, maxX = 0, minY = -1, minZ = 2, maxY = 4, maxZ = 2; |
| 96 | + switch (facing) { |
| 97 | + case UP: |
| 98 | + minX = -1; minY = 0; minZ = -1; maxX = 2; maxY = 4; maxZ = 2; |
| 99 | + break; |
| 100 | + case DOWN: |
| 101 | + minX = -1; minY = -3; minZ = -1; maxX = 2; maxY = 1; maxZ = 2; |
| 102 | + break; |
| 103 | + case NORTH: |
| 104 | + minX = -1; minY = -1; minZ = -3; maxX = 2; maxY = 2; maxZ = 1; |
| 105 | + break; |
| 106 | + case SOUTH: |
| 107 | + minX = -1; minY = -1; minZ = 0; maxX = 2; maxY = 2; maxZ = 4; |
| 108 | + break; |
| 109 | + case WEST: |
| 110 | + minX = -3; minY = -1; minZ = -1; maxX = 1; maxY = 2; maxZ = 2; |
| 111 | + break; |
| 112 | + case EAST: |
| 113 | + minX = 0; minY = -1; minZ = -1; maxX = 4; maxY = 2; maxZ = 2; |
| 114 | + break; |
| 115 | + } |
| 116 | + proxyBES = new EXPLProxyBE[35]; |
| 117 | + int i = 0; |
| 118 | + |
| 119 | + for (int x = minX; x < maxX; x++) { |
| 120 | + for (int z = minZ; z < maxZ; z++) { |
| 121 | + for (int y = minY; y < maxY; y++) { |
| 122 | + BlockEntity be = blockEntity(getBlockPos().offset(x, y, z)); |
| 123 | + if (be instanceof EXPLProxyBE explProxy) { |
| 124 | + proxyBES[i] = explProxy; |
| 125 | + i++; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return proxyBES; |
| 132 | + } |
| 133 | + |
| 134 | + private void tickProxyBlocks() { |
| 135 | + for(EXPLProxyBE proxy: getProxies()) { |
| 136 | + if(proxy == null) continue; |
| 137 | + proxy.forceTickServer(this); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void tickServer() { |
| 143 | + inputRedstoneSignal = 0; |
| 144 | + tickProxyBlocks(); |
| 145 | + activated = activated || inputRedstoneSignal > 0; |
| 146 | + pulseEnergy = energyStorage().getEnergyStored(); |
| 147 | + if (activated && pulseEnergy == energyStorage().getMaxEnergyStored()) { |
| 148 | + if (pulseTime == 0) { |
| 149 | + pulseTime = 80; |
| 150 | + } |
| 151 | + } |
| 152 | + if(pulseTime > 0) { |
| 153 | + level.setBlockAndUpdate(worldPosition, getBlockState()); |
| 154 | + level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), Block.UPDATE_NEIGHBORS); |
| 155 | + pulseTime--; |
| 156 | + setChanged(); |
| 157 | + if(pulseTime < 40) { |
| 158 | + renderBeam(); |
| 159 | + energyStorage().setEnergy(energyStorage().getEnergyStored()/2); |
| 160 | + } |
| 161 | + if(pulseTime < 35 && pulseTime > 30) { |
| 162 | + transferEnergy(); |
| 163 | + } |
| 164 | + if (pulseTime < 10) { |
| 165 | + energyStorage().setEnergy(0); |
| 166 | + energyTransfered = false; |
| 167 | + } |
| 168 | + } |
| 169 | + if(activated && pulseTime < 1) { |
| 170 | + activated = false; |
| 171 | + setChanged(); |
| 172 | + level.setBlockAndUpdate(worldPosition, getBlockState()); |
| 173 | + level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), Block.UPDATE_NEIGHBORS); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private void transferEnergy() { |
| 178 | + if (energyTransfered) return; |
| 179 | + energyTransfered = true; |
| 180 | + for (int i = 4; i <= KUGELBLITZ_CONFIG.LASER_DISTANCE.get()+4; i++) { |
| 181 | + BlockPos pos = getBlockPos().relative(getFacing(), i); |
| 182 | + BlockEntity be = level.getExistingBlockEntity(pos); |
| 183 | + if (be instanceof PhotonConcentratorBE photonConcentrator) { |
| 184 | + AbstractNCMultiblock multiblock = photonConcentrator.getMultiblock(); |
| 185 | + if (multiblock instanceof KugelblitzMultiblock kugelblitzMultiblock) { |
| 186 | + kugelblitzMultiblock.addPulseEnergy(pulseEnergy, getFacing()); |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + if (isMekanismLoaded() && be instanceof mekanism.generators.common.tile.fusion.TileEntityLaserFocusMatrix matrixBe) { |
| 191 | + matrixBe.receiveLaserEnergy(FloatingLong.create(pulseEnergy)); |
| 192 | + break; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
48 | 197 | @Override |
49 | | - public SidedContentHandler contentHandler() { |
50 | | - return contentHandler; |
| 198 | + public void tickClient() { |
| 199 | + if(pulseEnergy > 0 && pulseTime > 78) { |
| 200 | + playSound(LASER_SHOOT, 0.9f); |
| 201 | + } |
| 202 | + if(pulseTime < 1) { |
| 203 | + stopSound(); |
| 204 | + } |
51 | 205 | } |
52 | 206 |
|
53 | 207 | @Override |
|
0 commit comments