131 lines
3.7 KiB
Java
131 lines
3.7 KiB
Java
package com.thebrokenrail.reliccraft.block;
|
|
|
|
import com.thebrokenrail.reliccraft.RelicCraft;
|
|
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.inventory.Inventories;
|
|
import net.minecraft.inventory.Inventory;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.Items;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.util.DefaultedList;
|
|
import net.minecraft.util.Tickable;
|
|
|
|
import java.util.Objects;
|
|
|
|
public class DragonEggHolderBlockEntity extends BlockEntity implements Inventory, Tickable {
|
|
public DragonEggHolderBlockEntity() {
|
|
super(RelicCraft.DRAGON_EGG_HOLDER_BLOCK_ENTITY);
|
|
}
|
|
|
|
@Override
|
|
public void fromTag(CompoundTag tag) {
|
|
super.fromTag(tag);
|
|
DefaultedList<ItemStack> list = DefaultedList.ofSize(getInvSize(), ItemStack.EMPTY);
|
|
Inventories.fromTag(tag, list);
|
|
stack = list.get(0);
|
|
}
|
|
|
|
@Override
|
|
public CompoundTag toTag(CompoundTag tag) {
|
|
DefaultedList<ItemStack> list = DefaultedList.ofSize(getInvSize(), ItemStack.EMPTY);
|
|
list.set(0, stack);
|
|
Inventories.toTag(tag, list);
|
|
return super.toTag(tag);
|
|
}
|
|
|
|
private ItemStack stack = ItemStack.EMPTY;
|
|
|
|
@Override
|
|
public int getInvSize() {
|
|
return 1;
|
|
}
|
|
|
|
@Override
|
|
public int getInvMaxStackAmount() {
|
|
return 1;
|
|
}
|
|
|
|
@Override
|
|
public boolean isInvEmpty() {
|
|
return stack.isEmpty();
|
|
}
|
|
|
|
@Override
|
|
public ItemStack getInvStack(int slot) {
|
|
return stack;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack takeInvStack(int slot, int amount) {
|
|
ItemStack newStack = getInvStack(0).split(amount);
|
|
markDirty();
|
|
return newStack;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack removeInvStack(int slot) {
|
|
ItemStack newStack = getInvStack(0).copy();
|
|
setInvStack(0, ItemStack.EMPTY);
|
|
return newStack;
|
|
}
|
|
|
|
@Override
|
|
public void setInvStack(int slot, ItemStack stack) {
|
|
this.stack = stack;
|
|
if (stack.getCount() > getInvMaxStackAmount()) {
|
|
stack.setCount(getInvMaxStackAmount());
|
|
}
|
|
markDirty();
|
|
}
|
|
|
|
@Override
|
|
public boolean canPlayerUseInv(PlayerEntity player) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void clear() {
|
|
setInvStack(0, ItemStack.EMPTY);
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
if (hasWorld()) {
|
|
assert getWorld() != null;
|
|
Block block = getWorld().getBlockState(getPos()).getBlock();
|
|
if (block instanceof AbstractDragonEggHolderBlock && !getWorld().isClient()) {
|
|
((AbstractDragonEggHolderBlock) block).tick(getWorld(), getPos());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isValidInvStack(int slot, ItemStack stack) {
|
|
return stack.getItem() == Items.DRAGON_EGG;
|
|
}
|
|
|
|
@Override
|
|
public void markDirty() {
|
|
super.markDirty();
|
|
if (hasWorld() && !Objects.requireNonNull(getWorld()).isClient()) {
|
|
updateBlockState();
|
|
}
|
|
}
|
|
|
|
private void updateBlockState() {
|
|
if (hasWorld()) {
|
|
assert getWorld() != null;
|
|
BlockState state = getWorld().getBlockState(pos);
|
|
boolean active = !getInvStack(0).isEmpty();
|
|
if (state.get(TeleportationRestrictorBlock.ACTIVE) != active) {
|
|
getWorld().setBlockState(getPos(), state.with(TeleportationRestrictorBlock.ACTIVE, active));
|
|
RelicCraft.playInteractTeleportRestrictorSound(getWorld(), getPos());
|
|
}
|
|
}
|
|
}
|
|
}
|