EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserBlockEntity.java

112 lines
3.9 KiB
Java

package com.thebrokenrail.energonrelics.block.entity.infuser;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.api.energy.Action;
import com.thebrokenrail.energonrelics.block.InfuserBlock;
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import com.thebrokenrail.energonrelics.item.MultimeterItem;
import com.thebrokenrail.energonrelics.registry.infuser.data.InfuserAction;
import com.thebrokenrail.energonrelics.registry.infuser.data.InfuserEntry;
import com.thebrokenrail.energonrelics.registry.infuser.InfuserRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.Item;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import java.util.Objects;
public class InfuserBlockEntity extends EnergyReceiverBlockEntity implements MultimeterItem.MultimeterExtra {
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());
}
}
@Override
public MutableText getExtra() {
return new TranslatableText("text." + EnergonRelics.NAMESPACE + ".infuser_progress", new LiteralText(String.valueOf(((float) progress / (float) HardcodedConfig.INFUSER_TIME) * 100f)).formatted(Formatting.WHITE));
}
}