Skip to content

Commit

Permalink
Add IControllable to creative energy emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Dec 3, 2023
1 parent 6082ac2 commit 8f9342e
Showing 1 changed file with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import gregtech.api.GTValues;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.ILaserContainer;
import gregtech.api.gui.GuiTextures;
Expand Down Expand Up @@ -35,16 +37,18 @@
import codechicken.lib.vec.Matrix4;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Function;

import static gregtech.api.GTValues.MAX;
import static gregtech.api.GTValues.V;
import static gregtech.api.capability.GregtechDataCodes.UPDATE_ACTIVE;
import static gregtech.api.capability.GregtechDataCodes.UPDATE_IO_SPEED;

public class MetaTileEntityCreativeEnergy extends MetaTileEntity implements ILaserContainer {
public class MetaTileEntityCreativeEnergy extends MetaTileEntity implements ILaserContainer, IControllable {

private long voltage = 0;
private int amps = 1;
Expand Down Expand Up @@ -89,6 +93,8 @@ public <T> T getCapability(Capability<T> capability, EnumFacing side) {
return GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER.cast(this);
} else if (capability == GregtechTileCapabilities.CAPABILITY_LASER) {
return GregtechTileCapabilities.CAPABILITY_LASER.cast(this);
} else if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
} else {
return super.getCapability(capability, side);
}
Expand Down Expand Up @@ -126,7 +132,7 @@ protected ModularUI createUI(EntityPlayer entityPlayer) {

builder.dynamicLabel(7, 110, () -> "Energy I/O per sec: " + this.lastEnergyIOPerSec, 0x232323);

builder.widget(new CycleButtonWidget(7, 139, 77, 20, () -> active, value -> active = value,
builder.widget(new CycleButtonWidget(7, 139, 77, 20, () -> active, this::setActive,
"gregtech.creative.activity.off", "gregtech.creative.activity.on"));
builder.widget(new CycleButtonWidget(85, 139, 77, 20, () -> source, value -> {
source = value;
Expand All @@ -144,6 +150,13 @@ protected ModularUI createUI(EntityPlayer entityPlayer) {
return builder.build(getHolder(), entityPlayer);
}

public void setActive(boolean active) {
this.active = active;
if (!getWorld().isRemote) {
writeCustomData(GregtechDataCodes.UPDATE_ACTIVE, buf -> buf.writeBoolean(active));
}
}

@Override
public void addToolUsages(ItemStack stack, @Nullable World world, List<String> tooltip, boolean advanced) {
tooltip.add(I18n.format("gregtech.tool_action.screwdriver.access_covers"));
Expand Down Expand Up @@ -297,13 +310,27 @@ public void setIOSpeed(long energyIOPerSec) {
}

@Override
public void receiveCustomData(int dataId, PacketBuffer buf) {
public void receiveCustomData(int dataId, @NotNull PacketBuffer buf) {
super.receiveCustomData(dataId, buf);
if (dataId == UPDATE_IO_SPEED) {
this.lastEnergyIOPerSec = buf.readLong();
} else if (dataId == UPDATE_ACTIVE) {
this.active = buf.readBoolean();
}
}

@Override
public void writeInitialSyncData(@NotNull PacketBuffer buf) {
super.writeInitialSyncData(buf);
buf.writeBoolean(active);
}

@Override
public void receiveInitialSyncData(@NotNull PacketBuffer buf) {
super.receiveInitialSyncData(buf);
this.active = buf.readBoolean();
}

public static Function<String, String> getTextFieldValidator() {
return val -> {
if (val.isEmpty()) {
Expand All @@ -321,4 +348,14 @@ public static Function<String, String> getTextFieldValidator() {
return val;
};
}

@Override
public boolean isWorkingEnabled() {
return active;
}

@Override
public void setWorkingEnabled(boolean isWorkingAllowed) {
setActive(isWorkingAllowed);
}
}

0 comments on commit 8f9342e

Please sign in to comment.