Skip to content

Commit

Permalink
Unsure - changed machine ranges into an attachment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rover656 committed Feb 19, 2024
1 parent 9f7833d commit 5af83f4
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void init() {
addRenderableOnly(new FluidStackWidget(this, getMenu().getBlockEntity()::getFluidTank, 80 + leftPos, 21 + topPos, 16, 47));

addRenderableWidget(new ToggleImageButton<>(this, leftPos + imageWidth - 6 - 16, topPos + 2*16 + 2, 16, 16, 0, 0, 16, 0, RANGE_BUTTON_TEXTURE,
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setIsRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible() ? EIOLang.HIDE_RANGE : EIOLang.SHOW_RANGE));

addRenderableWidget(new EIOImageButton(this, leftPos + imageWidth - 2 * 16, topPos + 2 + 16 * 2, 8, 8, PLUS_SPRITES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void init() {
control -> menu.getBlockEntity().setRedstoneControl(control), EIOLang.REDSTONE_MODE));

addRenderableWidget(new ToggleImageButton<>(this, leftPos + imageWidth - 6 - 16, topPos + 24, 16, 16, 0, 0, 16, 0, RANGE_BUTTON_TEXTURE,
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setIsRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible() ? EIOLang.HIDE_RANGE : EIOLang.SHOW_RANGE));

addRenderableWidget(new ActiveWidget(this, menu.getBlockEntity()::getMachineStates, leftPos + imageWidth - 6 - 16, topPos + 16*4));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void init() {
control -> menu.getBlockEntity().setRedstoneControl(control), EIOLang.REDSTONE_MODE));

addRenderableWidget(new ToggleImageButton<>(this, leftPos + imageWidth - 8 - 16 * 2 - 2, topPos + 105, 16, 16, 0, 0, 16, 0, RANGE_BUTTON_TEXTURE,
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setIsRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible() ? EIOLang.HIDE_RANGE : EIOLang.SHOW_RANGE));

addRenderableWidget(new EIOImageButton(this, leftPos + imageWidth - 8 - 8, topPos + 86, 8, 8, PLUS_SPRITES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void init() {
control -> menu.getBlockEntity().setRedstoneControl(control), EIOLang.REDSTONE_MODE));

addRenderableWidget(new ToggleImageButton<>(this, leftPos + imageWidth - 6 - 16, topPos + 34, 16, 16, 0, 0, 16, 0, RANGE_BUTTON_TEXTURE,
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setIsRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible(), state -> menu.getBlockEntity().setRangeVisible(state),
() -> menu.getBlockEntity().isRangeVisible() ? EIOLang.HIDE_RANGE : EIOLang.SHOW_RANGE));

addRenderableWidget(new EIOImageButton(this, leftPos + imageWidth - 6 - 8 - 2 - 16, topPos + 34, 8, 8, PLUS_SPRITES,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.enderio.machines.common.attachment;

import com.enderio.api.UseOnly;
import com.enderio.base.common.particle.RangeParticleData;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.neoforged.fml.LogicalSide;

public record ActionRange(int range, boolean isVisible) {
public static final Codec<ActionRange> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Codec.INT.fieldOf("range").forGetter(ActionRange::range),
Codec.BOOL.fieldOf("isVisible").forGetter(ActionRange::isVisible)
).apply(instance, ActionRange::new));

public ActionRange visible() {
return new ActionRange(range, true);
}

public ActionRange invisible() {
return new ActionRange(range, false);
}

public ActionRange increment() {
return new ActionRange(range + 1, isVisible);
}

public ActionRange decrement() {
return new ActionRange(range - 1, isVisible);
}

@UseOnly(LogicalSide.CLIENT)
public void addClientParticle(ClientLevel level, BlockPos pos, String color) {
if (!isVisible) {
return;
}

if (level.isClientSide()) {
level.addAlwaysVisibleParticle(new RangeParticleData(range, color), true, pos.getX(), pos.getY(), pos.getZ(), 0, 0, 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.enderio.machines.common.attachment;

public interface IRangedActor {

int getMaxRange();

ActionRange getActionRange();

void setActionRange(ActionRange actionRange);

default int getRange() {
return getActionRange().range();
}

default boolean isRangeVisible() {
return getActionRange().isVisible();
}

default void setRangeVisible(boolean isRangeVisible) {
if (isRangeVisible) {
setActionRange(getActionRange().visible());
} else {
setActionRange(getActionRange().invisible());
}
}

default void increaseRange() {
var actionRange = getActionRange();
if (actionRange.range() < getMaxRange()) {
setActionRange(actionRange.increment());
}
}

default void decreaseRange() {
var actionRange = getActionRange();
if (actionRange.range() > 0) {
setActionRange(actionRange.decrement());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import com.enderio.api.io.IOMode;
import com.enderio.api.io.energy.EnergyIOMode;
import com.enderio.core.common.network.slot.BooleanNetworkDataSlot;
import com.enderio.core.common.network.slot.CodecNetworkDataSlot;
import com.enderio.core.common.network.slot.FluidStackNetworkDataSlot;
import com.enderio.core.common.network.slot.IntegerNetworkDataSlot;
import com.enderio.machines.common.attachment.ActionRange;
import com.enderio.machines.common.attachment.IRangedActor;
import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity;
import com.enderio.machines.common.config.MachinesConfig;
import com.enderio.machines.common.init.MachineAttachments;
import com.enderio.machines.common.init.MachineBlockEntities;
import com.enderio.machines.common.io.FixedIOConfig;
import com.enderio.machines.common.io.fluid.MachineFluidHandler;
Expand All @@ -18,6 +22,7 @@
import com.enderio.machines.common.io.fluid.TankAccess;
import com.enderio.machines.common.io.item.MachineInventoryLayout;
import com.enderio.machines.common.menu.DrainMenu;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Inventory;
Expand All @@ -38,7 +43,7 @@
import java.util.ArrayList;
import java.util.List;

public class DrainBlockEntity extends PoweredMachineBlockEntity {
public class DrainBlockEntity extends PoweredMachineBlockEntity implements IRangedActor {
public static final String CONSUMED = "Consumed";
private static final QuadraticScalable ENERGY_CAPACITY = new QuadraticScalable(CapacitorModifier.ENERGY_CAPACITY, MachinesConfig.COMMON.ENERGY.DRAIN_CAPACITY);
private static final QuadraticScalable ENERGY_USAGE = new QuadraticScalable(CapacitorModifier.ENERGY_USE, MachinesConfig.COMMON.ENERGY.DRAIN_USAGE);
Expand All @@ -51,22 +56,43 @@ public class DrainBlockEntity extends PoweredMachineBlockEntity {
private int consumed = 0;
private Fluid type = Fluids.EMPTY;

private CodecNetworkDataSlot<ActionRange> actionRangeDataSlot;

public DrainBlockEntity(BlockPos worldPosition, BlockState blockState) {
super(EnergyIOMode.Input, ENERGY_CAPACITY, ENERGY_USAGE, MachineBlockEntities.DRAIN.get(), worldPosition, blockState);
addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), fluid -> TANK.setFluid(this, fluid)));

this.range = 5;
// TODO: rubbish way of having a default. use an interface instead?
if (!hasData(MachineAttachments.ACTION_RANGE)) {
setData(MachineAttachments.ACTION_RANGE, new ActionRange(5, false));
}

rangeDataSlot = new IntegerNetworkDataSlot(this::getRange, r -> this.range = r) {
@Override
public void updateServerCallback() {
updateLocations();
}
};
addDataSlot(rangeDataSlot);
actionRangeDataSlot = addDataSlot(new CodecNetworkDataSlot<>(this::getActionRange, this::internalSetActionRange, ActionRange.CODEC));
}

@Override
public int getMaxRange() {
return 10;
}

rangeVisibleDataSlot = new BooleanNetworkDataSlot(this::isRangeVisible, b -> this.rangeVisible = b);
addDataSlot(rangeVisibleDataSlot);
@Override
public ActionRange getActionRange() {
return getData(MachineAttachments.ACTION_RANGE);
}

@Override
public void setActionRange(ActionRange actionRange) {
if (level != null && level.isClientSide) {
clientUpdateSlot(actionRangeDataSlot, actionRange);
} else {
internalSetActionRange(actionRange);
}
}

private void internalSetActionRange(ActionRange actionRange) {
setData(MachineAttachments.ACTION_RANGE, actionRange);
updateLocations();
setChanged();
}

@Override
Expand Down Expand Up @@ -126,7 +152,7 @@ protected boolean isActive() {
}

public void drainFluids() {
int stop = Math.min(currentIndex + range, positions.size());
int stop = Math.min(currentIndex + getRange(), positions.size());
while (currentIndex < stop) {
if (currentIndex >= positions.size()) {
currentIndex--;
Expand Down Expand Up @@ -172,25 +198,17 @@ public void drainFluids() {
}
}

@Override
public int getMaxRange() {
return 10;
}

@Override
public String getColor() {
return MachinesConfig.CLIENT.BLOCKS.DRAIN_RANGE_COLOR.get();
}

@Override
public BlockPos getParticleLocation() {
return worldPosition.below(range + 1);
return worldPosition.below(getRange() + 1);
}

@Override
public void setRange(int range) {
super.setRange(range);
updateLocations();
public void clientTick() {
if (level.isClientSide && level instanceof ClientLevel clientLevel) {
getActionRange().addClientParticle(clientLevel, getParticleLocation(), MachinesConfig.CLIENT.BLOCKS.DRAIN_RANGE_COLOR.get());
}

super.clientTick();
}

@Override
Expand All @@ -202,6 +220,7 @@ public void onLoad() {
private void updateLocations() {
positions = new ArrayList<>();
currentIndex = 0;
int range = getRange();
for (BlockPos pos : BlockPos.betweenClosed(worldPosition.offset(-range,-range*2 - 1,-range), worldPosition.offset(range,-1,range))) {
positions.add(pos.immutable()); //Need to make it immutable
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
import com.enderio.api.capacitor.QuadraticScalable;
import com.enderio.api.io.energy.EnergyIOMode;
import com.enderio.core.common.network.slot.BooleanNetworkDataSlot;
import com.enderio.core.common.network.slot.CodecNetworkDataSlot;
import com.enderio.core.common.network.slot.ResourceLocationNetworkDataSlot;
import com.enderio.machines.common.MachineNBTKeys;
import com.enderio.machines.common.attachment.ActionRange;
import com.enderio.machines.common.attachment.IRangedActor;
import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity;
import com.enderio.machines.common.blockentity.task.IMachineTask;
import com.enderio.machines.common.blockentity.task.SpawnerMachineTask;
import com.enderio.machines.common.blockentity.task.host.MachineTaskHost;
import com.enderio.machines.common.config.MachinesConfig;
import com.enderio.machines.common.init.MachineAttachments;
import com.enderio.machines.common.init.MachineBlockEntities;
import com.enderio.machines.common.io.item.MachineInventoryLayout;
import com.enderio.machines.common.lang.MachineLang;
import com.enderio.machines.common.menu.PoweredSpawnerMenu;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
Expand All @@ -32,7 +37,7 @@
import java.util.Optional;
// TODO: I want to revisit the powered spawner and task
// But there's not enough time before alpha, so just porting as-is.
public class PoweredSpawnerBlockEntity extends PoweredMachineBlockEntity {
public class PoweredSpawnerBlockEntity extends PoweredMachineBlockEntity implements IRangedActor {

public static final QuadraticScalable CAPACITY = new QuadraticScalable(CapacitorModifier.ENERGY_CAPACITY, MachinesConfig.COMMON.ENERGY.POWERED_SPAWNER_CAPACITY);
public static final QuadraticScalable USAGE = new QuadraticScalable(CapacitorModifier.ENERGY_USE, MachinesConfig.COMMON.ENERGY.POWERED_SPAWNER_USAGE);
Expand All @@ -41,18 +46,22 @@ public class PoweredSpawnerBlockEntity extends PoweredMachineBlockEntity {
private SpawnerBlockedReason reason = SpawnerBlockedReason.NONE;
private final MachineTaskHost taskHost;

private CodecNetworkDataSlot<ActionRange> actionRangeDataSlot;

public PoweredSpawnerBlockEntity(BlockPos worldPosition, BlockState blockState) {
super(EnergyIOMode.Input, CAPACITY, USAGE, MachineBlockEntities.POWERED_SPAWNER.get(), worldPosition, blockState);

rangeVisibleDataSlot = new BooleanNetworkDataSlot(this::isRangeVisible, b -> this.rangeVisible = b);
addDataSlot(rangeVisibleDataSlot);
// TODO: rubbish way of having a default. use an interface instead?
if (!hasData(MachineAttachments.ACTION_RANGE)) {
setData(MachineAttachments.ACTION_RANGE, new ActionRange(4, false));
}

actionRangeDataSlot = addDataSlot(new CodecNetworkDataSlot<>(this::getActionRange, this::internalSetActionRange, ActionRange.CODEC));

addDataSlot(new ResourceLocationNetworkDataSlot(() -> this.getEntityType().orElse(NO_MOB), rl -> {
setEntityType(rl);
EnderIO.LOGGER.info("UPDATED ENTITY TYPE.");
}));
range = 4;

taskHost = new MachineTaskHost(this, this::hasEnergy) {
@Override
Expand All @@ -77,6 +86,29 @@ public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInven
return new PoweredSpawnerMenu(this, pPlayerInventory, pContainerId);
}

public int getMaxRange() {
return 3;
}

@Override
public ActionRange getActionRange() {
return getData(MachineAttachments.ACTION_RANGE);
}

@Override
public void setActionRange(ActionRange actionRange) {
if (level != null && level.isClientSide) {
clientUpdateSlot(actionRangeDataSlot, actionRange);
} else {
internalSetActionRange(actionRange);
}
}

private void internalSetActionRange(ActionRange actionRange) {
setData(MachineAttachments.ACTION_RANGE, actionRange);
setChanged();
}

@Override
public void serverTick() {
super.serverTick();
Expand All @@ -86,6 +118,15 @@ public void serverTick() {
}
}

@Override
public void clientTick() {
if (level.isClientSide && level instanceof ClientLevel clientLevel) {
getActionRange().addClientParticle(clientLevel, getBlockPos(), MachinesConfig.CLIENT.BLOCKS.POWERED_SPAWNER_RANGE_COLOR.get());
}

super.clientTick();
}

@Override
public void onLoad() {
super.onLoad();
Expand All @@ -107,16 +148,6 @@ protected void onInventoryContentsChanged(int slot) {

// endregion

@Override
public String getColor() {
return MachinesConfig.CLIENT.BLOCKS.POWERED_SPAWNER_RANGE_COLOR.get();
}

@Override
public int getMaxRange() {
return 3;
}

// region Task

public float getSpawnProgress() {
Expand Down
Loading

0 comments on commit 5af83f4

Please sign in to comment.