This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/entity/infuser/InfuserBlockEntity.java

112 lines
3.9 KiB
Java
Raw Normal View History

2020-07-30 02:51:29 +00:00
package com.thebrokenrail.energonrelics.block.entity.infuser;
2020-07-30 19:49:18 +00:00
import com.thebrokenrail.energonrelics.EnergonRelics;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.api.energy.Action;
2020-08-16 03:01:24 +00:00
import com.thebrokenrail.energonrelics.api.item.MultimeterExtra;
2020-07-30 02:51:29 +00:00
import com.thebrokenrail.energonrelics.block.InfuserBlock;
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.registry.infuser.data.InfuserAction;
import com.thebrokenrail.energonrelics.registry.infuser.data.InfuserEntry;
import com.thebrokenrail.energonrelics.registry.infuser.InfuserRegistry;
2020-07-30 02:51:29 +00:00
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.Item;
import net.minecraft.nbt.CompoundTag;
2020-07-30 19:49:18 +00:00
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
2020-07-30 02:51:29 +00:00
import java.util.Objects;
2020-08-16 03:01:24 +00:00
public class InfuserBlockEntity extends EnergyReceiverBlockEntity implements MultimeterExtra {
2020-07-30 02:51:29 +00:00
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());
}
}
2020-07-30 19:49:18 +00:00
@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));
}
2020-07-30 02:51:29 +00:00
}