block entity?
This commit is contained in:
parent
0a23ce2964
commit
72045c2db1
1 changed files with 58 additions and 1 deletions
|
@ -5,7 +5,10 @@ import net.minecraft.block.entity.BlockEntity;
|
|||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.inventory.Inventories;
|
||||
import net.minecraft.inventory.SimpleInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.nbt.NbtCompound;
|
||||
import net.minecraft.screen.NamedScreenHandlerFactory;
|
||||
import net.minecraft.screen.PropertyDelegate;
|
||||
|
@ -15,6 +18,7 @@ import net.minecraft.util.collection.DefaultedList;
|
|||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import xyz.limepot.stellarworks.item.ModItems;
|
||||
|
||||
public class ArcFurnaceBlockEntity extends BlockEntity implements NamedScreenHandlerFactory, ImplementedInventory {
|
||||
private final DefaultedList<ItemStack> inventory =
|
||||
|
@ -104,6 +108,59 @@ public class ArcFurnaceBlockEntity extends BlockEntity implements NamedScreenHan
|
|||
progress = nbt.getInt("arc_furnace.progress");
|
||||
}
|
||||
|
||||
public static void tick(World world, BlockPos blockPos, BlockState blockState, ArcFurnaceBlockEntity entity) {
|
||||
private void resetProgress() {
|
||||
this.progress = 0;
|
||||
}
|
||||
|
||||
public static void tick(World world, BlockPos blockPos, BlockState state, ArcFurnaceBlockEntity entity) {
|
||||
//maybe not the correct way? but it works for now.
|
||||
if(world.isClient()) {
|
||||
return;
|
||||
}
|
||||
|
||||
//calculate recipe progress
|
||||
if(hasRecipe(entity)) {
|
||||
entity.progress++;
|
||||
markDirty(world, blockPos, state);
|
||||
if(entity.progress >= entity.maxProgress) {
|
||||
craftItem(entity);
|
||||
}
|
||||
} else {
|
||||
entity.resetProgress();
|
||||
markDirty(world, blockPos, state);
|
||||
}
|
||||
}
|
||||
|
||||
private static void craftItem(ArcFurnaceBlockEntity entity) {
|
||||
SimpleInventory inventory = new SimpleInventory(entity.size());
|
||||
for (int i = 0; i < entity.size(); i++) {
|
||||
inventory.setStack(i, entity.getStack(i));
|
||||
}
|
||||
|
||||
if(hasRecipe(entity)) {
|
||||
entity.removeStack(1, 1);
|
||||
|
||||
entity.setStack(2, new ItemStack(ModItems.PIG_IRON_INGOT, entity.getStack(2).getCount() + 1));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasRecipe(ArcFurnaceBlockEntity entity) {
|
||||
SimpleInventory inventory = new SimpleInventory(entity.size());
|
||||
for (int i = 0; i < entity.size(); i++) {
|
||||
inventory.setStack(i, entity.getStack(i));
|
||||
}
|
||||
|
||||
boolean hasArcSmeltableInFirstSlot = entity.getStack(1).getItem() == Items.IRON_INGOT;
|
||||
|
||||
return hasArcSmeltableInFirstSlot && canInsertAmountIntoOutputSlot(inventory, 1)
|
||||
&& canInsertItemIntoOutputSlot(inventory, ModItems.PIG_IRON_INGOT);
|
||||
}
|
||||
|
||||
private static boolean canInsertItemIntoOutputSlot(SimpleInventory inventory, Item output) {
|
||||
return inventory.getStack(2).getItem() == output || inventory.getStack(2).isEmpty();
|
||||
}
|
||||
|
||||
private static boolean canInsertAmountIntoOutputSlot(SimpleInventory inventory, int count) {
|
||||
return inventory.getStack(2).getMaxCount() > inventory.getStack(2).getCount() + count;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue