PrettyPipes/src/main/java/de/ellpeck/prettypipes/pressurizer/PressurizerBlockEntity.java

163 lines
5.4 KiB
Java
Raw Normal View History

2020-10-13 18:11:40 +02:00
package de.ellpeck.prettypipes.pressurizer;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
2020-10-17 14:29:37 +02:00
import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable;
2021-12-02 15:25:46 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2021-12-02 12:31:04 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-02 15:25:46 +01:00
import net.minecraft.network.Connection;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.level.block.entity.BlockEntity;
2021-12-02 15:25:46 +01:00
import net.minecraft.world.level.block.state.BlockState;
2020-10-13 18:11:40 +02:00
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;
import net.minecraftforge.energy.IEnergyStorage;
import javax.annotation.Nullable;
2021-12-02 15:25:46 +01:00
public class PressurizerBlockEntity extends BlockEntity implements MenuProvider, IPipeConnectable {
2020-10-13 18:11:40 +02:00
private final ModifiableEnergyStorage storage = new ModifiableEnergyStorage(64000, 512, 0);
2020-10-19 21:31:23 +02:00
private final LazyOptional<IEnergyStorage> lazyStorage = LazyOptional.of(() -> this.storage);
private final LazyOptional<IPipeConnectable> lazyThis = LazyOptional.of(() -> this);
2020-10-13 18:11:40 +02:00
private int lastEnergy;
2021-12-02 16:55:04 +01:00
public PressurizerBlockEntity(BlockPos pos, BlockState state) {
super(Registry.pressurizerBlockEntity, pos, state);
2020-10-13 18:11:40 +02:00
}
public boolean pressurizeItem(ItemStack stack, boolean simulate) {
int amount = 100 * stack.getCount();
return this.storage.extractInternal(amount, simulate) >= amount;
}
public float getEnergyPercentage() {
return this.getEnergy() / (float) this.getMaxEnergy();
}
public int getEnergy() {
return this.storage.getEnergyStored();
}
public int getMaxEnergy() {
return this.storage.getMaxEnergyStored();
}
@Override
2021-12-02 15:25:46 +01:00
public CompoundTag save(CompoundTag compound) {
2020-10-13 18:11:40 +02:00
compound.putInt("energy", this.getEnergy());
2021-12-02 15:25:46 +01:00
return super.save(compound);
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 15:25:46 +01:00
public void load(CompoundTag nbt) {
2020-10-13 18:11:40 +02:00
this.storage.setEnergyStored(nbt.getInt("energy"));
2021-12-02 15:25:46 +01:00
super.load(nbt);
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 12:31:04 +01:00
public CompoundTag getUpdateTag() {
2021-12-02 15:25:46 +01:00
return this.save(new CompoundTag());
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 15:25:46 +01:00
public void handleUpdateTag(CompoundTag tag) {
this.load(tag);
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 15:25:46 +01:00
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
this.load(pkt.getTag());
2020-10-13 18:11:40 +02:00
}
@Override
2021-12-02 15:25:46 +01:00
public Component getDisplayName() {
return new TranslatableComponent("container." + PrettyPipes.ID + ".pressurizer");
2020-10-13 18:11:40 +02:00
}
@Nullable
@Override
2021-12-02 15:25:46 +01:00
public AbstractContainerMenu createMenu(int window, Inventory inv, Player player) {
return new PressurizerContainer(Registry.pressurizerContainer, window, player, this.worldPosition);
2020-10-13 18:11:40 +02:00
}
@Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
if (cap == CapabilityEnergy.ENERGY) {
2020-10-19 21:31:23 +02:00
return this.lazyStorage.cast();
2020-10-17 14:29:37 +02:00
} else if (cap == Registry.pipeConnectableCapability) {
2020-10-19 21:31:23 +02:00
return this.lazyThis.cast();
2020-10-13 18:11:40 +02:00
} else {
return LazyOptional.empty();
}
}
2020-10-19 21:31:23 +02:00
@Override
2021-12-02 15:25:46 +01:00
public void setRemoved() {
super.setRemoved();
2020-10-19 21:31:23 +02:00
this.lazyStorage.invalidate();
this.lazyThis.invalidate();
}
2021-12-02 15:25:46 +01:00
// TODO tick
/*@Override
2020-10-13 18:11:40 +02:00
public void tick() {
if (this.world.isRemote)
return;
// notify pipes in network about us
if (this.world.getGameTime() % 10 == 0) {
PipeNetwork network = PipeNetwork.get(this.world);
for (Direction dir : Direction.values()) {
BlockPos offset = this.pos.offset(dir);
for (BlockPos node : network.getOrderedNetworkNodes(offset)) {
2020-10-20 01:31:36 +02:00
if (!this.world.isBlockLoaded(node))
continue;
2021-12-02 14:44:26 +01:00
PipeBlockEntity pipe = network.getPipe(node);
if (pipe != null)
pipe.pressurizer = this;
}
}
}
// send energy update
2020-10-13 18:11:40 +02:00
if (this.lastEnergy != this.storage.getEnergyStored() && this.world.getGameTime() % 10 == 0) {
this.lastEnergy = this.storage.getEnergyStored();
2021-12-02 12:31:04 +01:00
Utility.sendBlockEntityToClients(this);
2020-10-13 18:11:40 +02:00
}
2021-12-02 15:25:46 +01:00
}*/
2020-10-13 18:11:40 +02:00
2020-10-17 14:29:37 +02:00
@Override
public ConnectionType getConnectionType(BlockPos pipePos, Direction direction) {
return ConnectionType.CONNECTED;
}
2020-10-13 18:11:40 +02:00
private static class ModifiableEnergyStorage extends EnergyStorage {
public ModifiableEnergyStorage(int capacity, int maxReceive, int maxExtract) {
super(capacity, maxReceive, maxExtract);
}
private void setEnergyStored(int energy) {
this.energy = energy;
}
private int extractInternal(int maxExtract, boolean simulate) {
int energyExtracted = Math.min(this.energy, maxExtract);
if (!simulate)
this.energy -= energyExtracted;
return energyExtracted;
}
}
}