|
| 1 | +package com.github.gtexpert.core.common.metatileentities.multi; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import net.minecraft.client.resources.I18n; |
| 7 | +import net.minecraft.item.ItemStack; |
| 8 | +import net.minecraft.nbt.NBTTagCompound; |
| 9 | +import net.minecraft.network.PacketBuffer; |
| 10 | +import net.minecraft.util.EnumFacing; |
| 11 | +import net.minecraft.util.ResourceLocation; |
| 12 | +import net.minecraft.util.text.ITextComponent; |
| 13 | +import net.minecraft.util.text.TextComponentString; |
| 14 | +import net.minecraft.util.text.TextFormatting; |
| 15 | +import net.minecraft.world.World; |
| 16 | +import net.minecraftforge.common.capabilities.Capability; |
| 17 | +import net.minecraftforge.fml.relauncher.Side; |
| 18 | +import net.minecraftforge.fml.relauncher.SideOnly; |
| 19 | + |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | + |
| 23 | +import gregtech.api.GTValues; |
| 24 | +import gregtech.api.capability.GregtechDataCodes; |
| 25 | +import gregtech.api.capability.GregtechTileCapabilities; |
| 26 | +import gregtech.api.capability.IControllable; |
| 27 | +import gregtech.api.capability.IEnergyContainer; |
| 28 | +import gregtech.api.capability.impl.EnergyContainerList; |
| 29 | +import gregtech.api.metatileentity.MetaTileEntity; |
| 30 | +import gregtech.api.metatileentity.interfaces.IGregTechTileEntity; |
| 31 | +import gregtech.api.metatileentity.multiblock.IMultiblockPart; |
| 32 | +import gregtech.api.metatileentity.multiblock.MultiblockAbility; |
| 33 | +import gregtech.api.metatileentity.multiblock.MultiblockDisplayText; |
| 34 | +import gregtech.api.metatileentity.multiblock.MultiblockWithDisplayBase; |
| 35 | +import gregtech.api.pattern.BlockPattern; |
| 36 | +import gregtech.api.pattern.FactoryBlockPattern; |
| 37 | +import gregtech.api.pattern.PatternMatchContext; |
| 38 | +import gregtech.api.util.GTUtility; |
| 39 | +import gregtech.api.util.TextComponentUtil; |
| 40 | +import gregtech.api.util.TextFormattingUtil; |
| 41 | +import gregtech.client.renderer.ICubeRenderer; |
| 42 | +import gregtech.client.renderer.texture.Textures; |
| 43 | +import gregtech.common.blocks.BlockMetalCasing; |
| 44 | +import gregtech.common.blocks.MetaBlocks; |
| 45 | + |
| 46 | +import codechicken.lib.render.CCRenderState; |
| 47 | +import codechicken.lib.render.pipeline.IVertexOperation; |
| 48 | +import codechicken.lib.vec.Matrix4; |
| 49 | + |
| 50 | +public class MetaTileEntityLargeTransformer extends MultiblockWithDisplayBase implements IControllable { |
| 51 | + |
| 52 | + private EnergyContainerList input; |
| 53 | + private ExtendedEnergyContainerList output; |
| 54 | + private boolean isWorkingEnabled = true; |
| 55 | + private boolean isActive = true; |
| 56 | + |
| 57 | + public MetaTileEntityLargeTransformer(ResourceLocation metaTileEntityId) { |
| 58 | + super(metaTileEntityId); |
| 59 | + this.input = new EnergyContainerList(new ArrayList<>()); |
| 60 | + this.output = new ExtendedEnergyContainerList(new ArrayList<>()); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) { |
| 65 | + return new MetaTileEntityLargeTransformer(metaTileEntityId); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void updateFormedValid() { |
| 70 | + if (isWorkingEnabled()) { |
| 71 | + long canDrain = input.getEnergyStored(); |
| 72 | + long totalDrained = output.changeEnergy(canDrain); |
| 73 | + input.removeEnergy(totalDrained); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected @NotNull BlockPattern createStructurePattern() { |
| 79 | + return FactoryBlockPattern.start() |
| 80 | + .aisle("XSX", "XXX") |
| 81 | + .where('X', abilities(MultiblockAbility.MAINTENANCE_HATCH).setExactLimit(1) |
| 82 | + .or(abilities(MultiblockAbility.INPUT_ENERGY).setExactLimit(1)) |
| 83 | + .or(abilities(MultiblockAbility.OUTPUT_ENERGY).setExactLimit(1)) |
| 84 | + .or(states( |
| 85 | + MetaBlocks.METAL_CASING.getState(BlockMetalCasing.MetalCasingType.PALLADIUM_SUBSTATION)) |
| 86 | + .setExactLimit(2))) |
| 87 | + .where('S', selfPredicate()) |
| 88 | + .build(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void formStructure(PatternMatchContext context) { |
| 93 | + super.formStructure(context); |
| 94 | + List<IEnergyContainer> powerInput = new ArrayList<>(getAbilities(MultiblockAbility.INPUT_ENERGY)); |
| 95 | + |
| 96 | + List<IEnergyContainer> powerOutput = new ArrayList<>(getAbilities(MultiblockAbility.OUTPUT_ENERGY)); |
| 97 | + |
| 98 | + // Invalidate the structure if there is not at least one output and one input |
| 99 | + if (powerInput.isEmpty() || powerOutput.isEmpty()) { |
| 100 | + this.invalidateStructure(); |
| 101 | + } |
| 102 | + |
| 103 | + this.input = new EnergyContainerList(powerInput); |
| 104 | + this.output = new ExtendedEnergyContainerList(powerOutput); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void invalidateStructure() { |
| 109 | + super.invalidateStructure(); |
| 110 | + this.input = new EnergyContainerList(new ArrayList<>()); |
| 111 | + this.output = new ExtendedEnergyContainerList(new ArrayList<>()); |
| 112 | + setActive(false); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected boolean shouldShowVoidingModeButton() { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public boolean isWorkingEnabled() { |
| 122 | + return isWorkingEnabled; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void setWorkingEnabled(boolean isWorkingAllowed) { |
| 127 | + this.isWorkingEnabled = isWorkingAllowed; |
| 128 | + markDirty(); |
| 129 | + World world = getWorld(); |
| 130 | + if (world != null && !world.isRemote) { |
| 131 | + writeCustomData(GregtechDataCodes.WORKING_ENABLED, buf -> buf.writeBoolean(isWorkingEnabled)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean isActive() { |
| 137 | + return super.isActive() && this.isWorkingEnabled; |
| 138 | + } |
| 139 | + |
| 140 | + public void setActive(boolean active) { |
| 141 | + if (this.isActive != active) { |
| 142 | + this.isActive = active; |
| 143 | + markDirty(); |
| 144 | + World world = getWorld(); |
| 145 | + if (world != null && !world.isRemote) { |
| 146 | + writeCustomData(GregtechDataCodes.WORKABLE_ACTIVE, buf -> buf.writeBoolean(active)); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public NBTTagCompound writeToNBT(NBTTagCompound data) { |
| 153 | + super.writeToNBT(data); |
| 154 | + data.setBoolean("isActive", this.isActive); |
| 155 | + data.setBoolean("isWorkingEnabled", this.isWorkingEnabled); |
| 156 | + return data; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void readFromNBT(NBTTagCompound data) { |
| 161 | + super.readFromNBT(data); |
| 162 | + this.isActive = data.getBoolean("isActive"); |
| 163 | + this.isWorkingEnabled = data.getBoolean("isWorkingEnabled"); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void writeInitialSyncData(PacketBuffer buf) { |
| 168 | + super.writeInitialSyncData(buf); |
| 169 | + buf.writeBoolean(this.isActive); |
| 170 | + buf.writeBoolean(this.isWorkingEnabled); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void receiveInitialSyncData(PacketBuffer buf) { |
| 175 | + super.receiveInitialSyncData(buf); |
| 176 | + this.isActive = buf.readBoolean(); |
| 177 | + this.isWorkingEnabled = buf.readBoolean(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) { |
| 182 | + super.receiveCustomData(dataId, buf); |
| 183 | + if (dataId == GregtechDataCodes.WORKABLE_ACTIVE) { |
| 184 | + this.isActive = buf.readBoolean(); |
| 185 | + scheduleRenderUpdate(); |
| 186 | + } else if (dataId == GregtechDataCodes.WORKING_ENABLED) { |
| 187 | + this.isWorkingEnabled = buf.readBoolean(); |
| 188 | + scheduleRenderUpdate(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public <T> T getCapability(Capability<T> capability, EnumFacing side) { |
| 194 | + if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) { |
| 195 | + return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this); |
| 196 | + } |
| 197 | + return super.getCapability(capability, side); |
| 198 | + } |
| 199 | + |
| 200 | + @SideOnly(Side.CLIENT) |
| 201 | + @Override |
| 202 | + public ICubeRenderer getBaseTexture(IMultiblockPart sourcePart) { |
| 203 | + return Textures.PALLADIUM_SUBSTATION_CASING; |
| 204 | + } |
| 205 | + |
| 206 | + @SideOnly(Side.CLIENT) |
| 207 | + @Override |
| 208 | + protected @NotNull ICubeRenderer getFrontOverlay() { |
| 209 | + return Textures.DATA_BANK_OVERLAY; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) { |
| 214 | + super.renderMetaTileEntity(renderState, translation, pipeline); |
| 215 | + getFrontOverlay().renderOrientedState(renderState, translation, pipeline, getFrontFacing(), this.isActive(), |
| 216 | + this.isWorkingEnabled()); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + protected void addDisplayText(List<ITextComponent> textList) { |
| 221 | + MultiblockDisplayText.builder(textList, isStructureFormed()) |
| 222 | + .setWorkingStatus(true, isActive() && isWorkingEnabled()) |
| 223 | + .addCustom(tl -> { |
| 224 | + if (isStructureFormed()) { |
| 225 | + // input |
| 226 | + String inputEnergyFormatted = TextFormattingUtil.formatNumbers(getInputVolt()); |
| 227 | + ITextComponent inputVoltageName = new TextComponentString( |
| 228 | + GTValues.VNF[GTUtility.getFloorTierByVoltage(getInputVolt())]); |
| 229 | + ITextComponent inputAmp = TextComponentUtil.stringWithColor( |
| 230 | + TextFormatting.YELLOW, |
| 231 | + TextFormattingUtil.formatNumbers(getInputAmp())); |
| 232 | + tl.add(TextComponentUtil.translationWithColor( |
| 233 | + TextFormatting.GRAY, |
| 234 | + "gtexpert.multiblock.large_transformer.max_input_energy_per_tick_amps", |
| 235 | + inputEnergyFormatted, inputVoltageName, inputAmp)); |
| 236 | + // output |
| 237 | + String outputEnergyFormatted = TextFormattingUtil.formatNumbers(getOutputVolt()); |
| 238 | + ITextComponent outputVoltageName = new TextComponentString( |
| 239 | + GTValues.VNF[GTUtility.getFloorTierByVoltage(getOutputVolt())]); |
| 240 | + ITextComponent outputAmp = TextComponentUtil.stringWithColor( |
| 241 | + TextFormatting.YELLOW, |
| 242 | + TextFormattingUtil.formatNumbers(getOutputAmp())); |
| 243 | + tl.add(TextComponentUtil.translationWithColor( |
| 244 | + TextFormatting.GRAY, |
| 245 | + "gtexpert.multiblock.large_transformer.max_output_energy_per_tick_amps", |
| 246 | + outputEnergyFormatted, outputVoltageName, outputAmp)); |
| 247 | + } |
| 248 | + }) |
| 249 | + .addWorkingStatusLine(); |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + protected void addWarningText(List<ITextComponent> textList) { |
| 254 | + super.addWarningText(textList); |
| 255 | + if (isStructureFormed() && this.getOutput() > this.getInput()) { |
| 256 | + textList.add(TextComponentUtil.translationWithColor( |
| 257 | + TextFormatting.YELLOW, |
| 258 | + "gtexpert.multiblock.large_transformer.not_enough_input")); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public void addInformation(ItemStack stack, @Nullable World world, @NotNull List<String> tooltip, |
| 264 | + boolean advanced) { |
| 265 | + tooltip.add(I18n.format("gtexpert.machine.large_transformer.tooltip.1")); |
| 266 | + tooltip.add(I18n.format("gtexpert.machine.large_transformer.tooltip.2")); |
| 267 | + } |
| 268 | + |
| 269 | + private long getInput() { |
| 270 | + return input.getInputVoltage() * input.getInputAmperage(); |
| 271 | + } |
| 272 | + |
| 273 | + private long getOutput() { |
| 274 | + return output.getOutputVoltage() * output.getOutputAmperage(); |
| 275 | + } |
| 276 | + |
| 277 | + private long getInputAmp() { |
| 278 | + return getInput() / input.getHighestInputVoltage(); |
| 279 | + } |
| 280 | + |
| 281 | + private long getInputVolt() { |
| 282 | + return input.getHighestInputVoltage(); |
| 283 | + } |
| 284 | + |
| 285 | + private long getOutputAmp() { |
| 286 | + return getOutput() / output.getHighestOutputVoltage(); |
| 287 | + } |
| 288 | + |
| 289 | + private long getOutputVolt() { |
| 290 | + return output.getHighestOutputVoltage(); |
| 291 | + } |
| 292 | + |
| 293 | + private static class ExtendedEnergyContainerList extends EnergyContainerList { |
| 294 | + |
| 295 | + private final List<IEnergyContainer> energyContainerList; |
| 296 | + /** The highest single energy container's input voltage in the list. */ |
| 297 | + private final long highestOutputVoltage; |
| 298 | + /** The number of energy containers at the highest input voltage in the list. */ |
| 299 | + private final int numHighestOutputContainers; |
| 300 | + |
| 301 | + public ExtendedEnergyContainerList(@NotNull List<IEnergyContainer> energyContainerList) { |
| 302 | + super(energyContainerList); |
| 303 | + this.energyContainerList = energyContainerList; |
| 304 | + long highestOutputVoltage = 0; |
| 305 | + int numHighestOutputContainers = 0; |
| 306 | + for (IEnergyContainer container : energyContainerList) { |
| 307 | + if (container.getOutputVoltage() > highestOutputVoltage) { |
| 308 | + highestOutputVoltage = container.getOutputVoltage(); |
| 309 | + } |
| 310 | + } |
| 311 | + for (IEnergyContainer container : energyContainerList) { |
| 312 | + if (container.getOutputVoltage() == highestOutputVoltage) { |
| 313 | + numHighestOutputContainers++; |
| 314 | + } |
| 315 | + } |
| 316 | + this.highestOutputVoltage = highestOutputVoltage; |
| 317 | + this.numHighestOutputContainers = numHighestOutputContainers; |
| 318 | + } |
| 319 | + |
| 320 | + /** The highest single voltage of an energy container in this list. */ |
| 321 | + public long getHighestOutputVoltage() { |
| 322 | + return highestOutputVoltage; |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * The number of parts with voltage specified in {@link ExtendedEnergyContainerList#getHighestOutputVoltage()} |
| 327 | + * in this list. |
| 328 | + */ |
| 329 | + public int getNumHighestOutputContainers() { |
| 330 | + return numHighestOutputContainers; |
| 331 | + } |
| 332 | + } |
| 333 | +} |
0 commit comments