98 lines
3.1 KiB
Java
98 lines
3.1 KiB
Java
|
package com.thebrokenrail.energonrelics.block.entity.infuser;
|
||
|
|
||
|
import com.thebrokenrail.energonrelics.block.InfuserBlock;
|
||
|
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
|
||
|
import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity;
|
||
|
import com.thebrokenrail.energonrelics.energy.core.util.Action;
|
||
|
import net.minecraft.block.BlockState;
|
||
|
import net.minecraft.block.entity.BlockEntityType;
|
||
|
import net.minecraft.item.Item;
|
||
|
import net.minecraft.nbt.CompoundTag;
|
||
|
|
||
|
import java.util.Objects;
|
||
|
|
||
|
public class InfuserBlockEntity extends EnergyReceiverBlockEntity {
|
||
|
public InfuserBlockEntity(BlockEntityType<?> type) {
|
||
|
super(type);
|
||
|
}
|
||
|
|
||
|
private InfuserEntry entry = null;
|
||
|
private int progress = 0;
|
||
|
|
||
|
private static final InfuserAction explosion = new InfuserAction.ExplosionAction();
|
||
|
|
||
|
@Override
|
||
|
protected void energyTick() {
|
||
|
assert getWorld() != null;
|
||
|
|
||
|
if (getCachedState().get(InfuserBlock.POWERED)) {
|
||
|
if (entry != null) {
|
||
|
if (progress >= HardcodedConfig.INFUSER_TIME) {
|
||
|
getWorld().setBlockState(getPos(), getCachedState().with(InfuserBlock.POWERED, false));
|
||
|
|
||
|
entry.run(Objects.requireNonNull(getWorld()), getPos());
|
||
|
|
||
|
progress = 0;
|
||
|
entry = null;
|
||
|
markDirty();
|
||
|
} else {
|
||
|
addAction(new Action(entry.cost, (world, pos, state) -> {
|
||
|
progress++;
|
||
|
markDirty();
|
||
|
}, (world, pos, state) -> explosion.run(world, pos)));
|
||
|
}
|
||
|
} else {
|
||
|
explosion.run(getWorld(), getPos());
|
||
|
}
|
||
|
} else {
|
||
|
if (progress != 0 || entry != null) {
|
||
|
progress = 0;
|
||
|
entry = null;
|
||
|
markDirty();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public CompoundTag toTag(CompoundTag tag) {
|
||
|
super.toTag(tag);
|
||
|
tag.putString("Entry", InfuserRegistry.toString(entry));
|
||
|
tag.putInt("Progress", progress);
|
||
|
return tag;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void fromTag(BlockState state, CompoundTag tag) {
|
||
|
super.fromTag(state, tag);
|
||
|
entry = InfuserRegistry.fromString(tag.getString("Entry"));
|
||
|
progress = tag.getInt("Progress");
|
||
|
}
|
||
|
|
||
|
public boolean insert(Item item) {
|
||
|
assert getWorld() != null;
|
||
|
|
||
|
if (getCachedState().get(InfuserBlock.POWERED)) {
|
||
|
return false;
|
||
|
} else {
|
||
|
InfuserEntry newEntry = InfuserRegistry.get(item);
|
||
|
if (newEntry != null) {
|
||
|
if (!getWorld().isClient()) {
|
||
|
getWorld().setBlockState(getPos(), getCachedState().with(InfuserBlock.POWERED, true));
|
||
|
entry = newEntry;
|
||
|
progress = 0;
|
||
|
markDirty();
|
||
|
}
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void onBreak() {
|
||
|
if (getCachedState().get(InfuserBlock.POWERED)) {
|
||
|
explosion.run(getWorld(), getPos());
|
||
|
}
|
||
|
}
|
||
|
}
|