Skip to content

Commit

Permalink
adjustable transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 committed Aug 12, 2021
1 parent 45e27fa commit 6acee86
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 66 deletions.
4 changes: 4 additions & 0 deletions src/main/java/gregtech/api/render/Textures.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public class Textures {
public static final SimpleOverlayRenderer ENERGY_IN = new SimpleOverlayRenderer("overlay/machine/overlay_energy_in");
public static final SimpleOverlayRenderer ENERGY_OUT_MULTI = new SimpleOverlayRenderer("overlay/machine/overlay_energy_out_multi");
public static final SimpleOverlayRenderer ENERGY_IN_MULTI = new SimpleOverlayRenderer("overlay/machine/overlay_energy_in_multi");
public static final SimpleOverlayRenderer ENERGY_OUT_HI = new SimpleOverlayRenderer("overlay/machine/overlay_energy_out_hi");
public static final SimpleOverlayRenderer ENERGY_IN_HI = new SimpleOverlayRenderer("overlay/machine/overlay_energy_in_hi");
public static final SimpleOverlayRenderer ENERGY_OUT_ULTRA = new SimpleOverlayRenderer("overlay/machine/overlay_energy_out_ultra");
public static final SimpleOverlayRenderer ENERGY_IN_ULTRA = new SimpleOverlayRenderer("overlay/machine/overlay_energy_in_ultra");
public static final SimpleOverlayRenderer CONVEYOR_OVERLAY = new SimpleOverlayRenderer("cover/overlay_conveyor");
public static final SimpleOverlayRenderer ARM_OVERLAY = new SimpleOverlayRenderer("cover/overlay_arm");
public static final SimpleOverlayRenderer PUMP_OVERLAY = new SimpleOverlayRenderer("cover/overlay_pump");
Expand Down
120 changes: 66 additions & 54 deletions src/main/java/gregtech/common/metatileentities/MetaTileEntities.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package gregtech.common.metatileentities.electric;

import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import gregtech.api.GTValues;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.impl.EnergyContainerHandler;
import gregtech.api.capability.tool.ISoftHammerItem;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.render.SimpleOverlayRenderer;
import gregtech.api.render.Textures;
import gregtech.api.util.PipelineUtil;
import gregtech.common.tools.DamageValues;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;

public class MetaTileEntityAdjustableTransformer extends MetaTileEntityTransformer {

private static final int[] hiAmpsRange = {1, 2, 4, 16};
private static final int[] loAmpsRange = {4, 8, 16, 64};
private int ampIndex;

public MetaTileEntityAdjustableTransformer(ResourceLocation metaTileEntityId, int tier) {
super(metaTileEntityId, tier);
this.ampIndex = 0;
}

@Override
public MetaTileEntity createMetaTileEntity(MetaTileEntityHolder holder) {
return new MetaTileEntityAdjustableTransformer(metaTileEntityId, getTier());
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
data.setInteger("hiAmpIndex", ampIndex);
return data;
}

@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
this.ampIndex = data.getInteger("hiAmpIndex");
reinitializeEnergyContainer();
}

@Override
public void writeInitialSyncData(PacketBuffer buf) {
super.writeInitialSyncData(buf);
buf.writeInt(ampIndex);
}

@Override
public void receiveInitialSyncData(PacketBuffer buf) {
super.receiveInitialSyncData(buf);
this.ampIndex = buf.readInt();
}

@Override
public void receiveCustomData(int dataId, PacketBuffer buf) {
super.receiveCustomData(dataId, buf);
if (dataId == 101) {
this.ampIndex = buf.readInt();
scheduleRenderUpdate();
}
}

protected void incrementAmpIndex() {
this.ampIndex = (this.ampIndex + 1) % hiAmpsRange.length;
if (!getWorld().isRemote) {
reinitializeEnergyContainer();
writeCustomData(101, b -> b.writeInt(ampIndex));
getHolder().notifyBlockUpdate();
markDirty();
}
}

@Override
protected void reinitializeEnergyContainer() {
long tierVoltage = GTValues.V[getTier()];
if (isInverted()) {
//storage = 1 amp high; input = tier / 4; amperage = X; output = tier; amperage = Y
this.energyContainer = new EnergyContainerHandler(this, tierVoltage * 128L, tierVoltage, loAmpsRange[ampIndex], tierVoltage * 4, hiAmpsRange[ampIndex]);
((EnergyContainerHandler) this.energyContainer).setSideInputCondition(s -> s != getFrontFacing());
((EnergyContainerHandler) this.energyContainer).setSideOutputCondition(s -> s == getFrontFacing());
} else {
//storage = 1 amp high; input = tier; amperage = Y; output = tier / 4; amperage = X
this.energyContainer = new EnergyContainerHandler(this, tierVoltage * 128L, tierVoltage * 4, hiAmpsRange[ampIndex], tierVoltage, loAmpsRange[ampIndex]);
((EnergyContainerHandler) this.energyContainer).setSideInputCondition(s -> s == getFrontFacing());
((EnergyContainerHandler) this.energyContainer).setSideOutputCondition(s -> s != getFrontFacing());
}
}

@Override
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
super.renderMetaTileEntity(renderState, translation, pipeline);

SimpleOverlayRenderer otherFaceTexture;
SimpleOverlayRenderer frontFaceTexture;
switch (this.ampIndex) {
case 1:
otherFaceTexture = isInverted() ? Textures.ENERGY_IN_MULTI : Textures.ENERGY_OUT_MULTI;
frontFaceTexture = isInverted() ? Textures.ENERGY_IN_HI : Textures.ENERGY_IN_HI;
break;
case 2:
otherFaceTexture = isInverted() ? Textures.ENERGY_IN_HI : Textures.ENERGY_OUT_HI;
frontFaceTexture = isInverted() ? Textures.ENERGY_OUT_ULTRA : Textures.ENERGY_IN_ULTRA;
break;
case 3:
otherFaceTexture = isInverted() ? Textures.ENERGY_IN_ULTRA : Textures.ENERGY_OUT_ULTRA;
frontFaceTexture = isInverted() ? Textures.ENERGY_OUT_ULTRA : Textures.ENERGY_IN_ULTRA;
break;
default:
otherFaceTexture = isInverted() ? Textures.ENERGY_IN : Textures.ENERGY_OUT;
frontFaceTexture = isInverted() ? Textures.ENERGY_OUT_MULTI : Textures.ENERGY_IN_MULTI;
}

frontFaceTexture.renderSided(frontFacing, renderState, translation, PipelineUtil.color(pipeline, GTValues.VC[getTier() + 1]));
Arrays.stream(EnumFacing.values()).filter(f -> f != frontFacing)
.forEach((f -> otherFaceTexture.renderSided(f, renderState, translation, PipelineUtil.color(pipeline, GTValues.VC[getTier()]))));
}

@Override
public boolean onRightClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
ItemStack itemStack = playerIn.getHeldItem(hand);
if (!itemStack.isEmpty() && itemStack.hasCapability(GregtechCapabilities.CAPABILITY_MALLET, null)) {
ISoftHammerItem softHammerItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_MALLET, null);

if (getWorld().isRemote) {
scheduleRenderUpdate();
return true;
}
if (!softHammerItem.damageItem(DamageValues.DAMAGE_FOR_SOFT_HAMMER, false)) {
return false;
}

if (isInverted()) {
setTransformUp(false);
playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.transformer.message_transform_down",
energyContainer.getInputVoltage(), energyContainer.getInputAmperage(), energyContainer.getOutputVoltage(), energyContainer.getOutputAmperage()));
} else {
setTransformUp(true);
playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.transformer.message_transform_up",
energyContainer.getInputVoltage(), energyContainer.getInputAmperage(), energyContainer.getOutputVoltage(), energyContainer.getOutputAmperage()));
}
return true;
}
return false;
}

@Override
public boolean onScrewdriverClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
if (getWorld().isRemote) {
scheduleRenderUpdate();
return true;
}

incrementAmpIndex();
playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.transformer_adjustable.message_adjust",
energyContainer.getInputVoltage(), energyContainer.getInputAmperage(), energyContainer.getOutputVoltage(), energyContainer.getOutputAmperage()));

return true;
}

@Override
public void addInformation(ItemStack stack, @Nullable World player, List<String> tooltip, boolean advanced) {
String lowerTierName = GTValues.VN[getTier()];
String higherTierName = GTValues.VN[getTier() + 1];
long lowerVoltage = energyContainer.getOutputVoltage();
long higherVoltage = energyContainer.getInputVoltage();
long lowerAmperage = energyContainer.getInputAmperage();
long higherAmperage = energyContainer.getOutputAmperage();

tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_tool_usage"));
tooltip.add(I18n.format("gregtech.machine.transformer_adjustable.tooltip_tool_usage"));
tooltip.add(I18n.format("gregtech.universal.tooltip.energy_storage_capacity", energyContainer.getEnergyCapacity()));
tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_transform_down", lowerAmperage, higherVoltage, higherTierName, higherAmperage, lowerVoltage, lowerTierName));
tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_transform_up", higherAmperage, lowerVoltage, lowerTierName, lowerAmperage, higherVoltage, higherTierName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,7 @@ public void addInformation(ItemStack stack, @Nullable World player, List<String>

tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_tool_usage"));
tooltip.add(I18n.format("gregtech.universal.tooltip.energy_storage_capacity", energyContainer.getEnergyCapacity()));
tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_transform_down"));
tooltip.add(I18n.format("gregtech.universal.tooltip.voltage_in", higherVoltage, higherTierName));
tooltip.add(I18n.format("gregtech.universal.tooltip.voltage_out", lowerVoltage, lowerTierName));
tooltip.add(I18n.format("gregtech.universal.tooltip.amperage_in", lowerAmperage));
tooltip.add(I18n.format("gregtech.universal.tooltip.amperage_out", higherAmperage));
tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_transform_up"));
tooltip.add(I18n.format("gregtech.universal.tooltip.voltage_in", lowerVoltage, lowerTierName));
tooltip.add(I18n.format("gregtech.universal.tooltip.voltage_out", higherVoltage, higherTierName));
tooltip.add(I18n.format("gregtech.universal.tooltip.amperage_in", higherAmperage));
tooltip.add(I18n.format("gregtech.universal.tooltip.amperage_out", lowerAmperage));
tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_transform_down", lowerAmperage, higherVoltage, higherTierName, higherAmperage, lowerVoltage, lowerTierName));
tooltip.add(I18n.format("gregtech.machine.transformer.tooltip_transform_up", higherAmperage, lowerVoltage, lowerTierName, lowerAmperage, higherVoltage, higherTierName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package gregtech.common.metatileentities.electric.multiblockpart;

import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Matrix4;
import gregtech.api.GTValues;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.impl.EnergyContainerHandler;
import gregtech.api.gui.ModularUI;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart;
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
import gregtech.api.render.SimpleOverlayRenderer;
import gregtech.api.render.Textures;
import gregtech.api.util.PipelineUtil;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class MetaTileEntityAdjustableEnergyHatch extends MetaTileEntityMultiblockPart implements IMultiblockAbilityPart<IEnergyContainer> {

private final boolean isExportHatch;
private final IEnergyContainer energyContainer;

public MetaTileEntityAdjustableEnergyHatch(ResourceLocation metaTileEntityId, int tier, boolean isExportHatch) {
super(metaTileEntityId, tier);
this.isExportHatch = isExportHatch;
if (isExportHatch) {
this.energyContainer = EnergyContainerHandler.emitterContainer(this, GTValues.V[tier] * 128L, GTValues.V[tier], 1);
((EnergyContainerHandler) this.energyContainer).setSideOutputCondition(s -> s == getFrontFacing());
} else {
this.energyContainer = EnergyContainerHandler.receiverContainer(this, GTValues.V[tier] * 16L, GTValues.V[tier], 2);
}
}

@Override
public MetaTileEntity createMetaTileEntity(MetaTileEntityHolder holder) {
return new MetaTileEntityAdjustableEnergyHatch(metaTileEntityId, getTier(), isExportHatch);
}

@Override
public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) {
super.renderMetaTileEntity(renderState, translation, pipeline);
if (shouldRenderOverlay()) {
SimpleOverlayRenderer renderer = isExportHatch ? Textures.ENERGY_OUT_MULTI : Textures.ENERGY_IN_MULTI;
renderer.renderSided(getFrontFacing(), renderState, translation, PipelineUtil.color(pipeline, GTValues.VC[getTier()]));
}
}

@Override
public MultiblockAbility<IEnergyContainer> getAbility() {
return isExportHatch ? MultiblockAbility.OUTPUT_ENERGY : MultiblockAbility.INPUT_ENERGY;
}

@Override
public void registerAbilities(List<IEnergyContainer> abilityList) {
abilityList.add(energyContainer);
}

@Override
protected boolean openGUIOnRightClick() {
return false;
}

@Override
protected ModularUI createUI(EntityPlayer entityPlayer) {
return null;
}

@Override
public void addInformation(ItemStack stack, @Nullable World player, List<String> tooltip, boolean advanced) {
String tierName = GTValues.VN[getTier()];

if (isExportHatch) {
tooltip.add(I18n.format("gregtech.machine.energy_hatch.output.tooltip"));
tooltip.add(I18n.format("gregtech.universal.tooltip.voltage_out", energyContainer.getOutputVoltage(), tierName));
tooltip.add(I18n.format("gregtech.universal.tooltip.amperage_out_till", energyContainer.getOutputAmperage()));
} else {
tooltip.add(I18n.format("gregtech.machine.energy_hatch.input.tooltip"));
tooltip.add(I18n.format("gregtech.universal.tooltip.voltage_in", energyContainer.getInputVoltage(), tierName));
tooltip.add(I18n.format("gregtech.universal.tooltip.amperage_in_till", energyContainer.getInputAmperage()));
}
tooltip.add(I18n.format("gregtech.universal.tooltip.energy_storage_capacity", energyContainer.getEnergyCapacity()));
tooltip.add(I18n.format("gregtech.universal.enabled"));
}
}
Loading

0 comments on commit 6acee86

Please sign in to comment.