123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package me.km.blocks.cookingpot;
- import java.awt.Color;
- import java.util.LinkedList;
- import me.km.KajetansMod;
- import net.minecraft.block.BlockState;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.INBT;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.nbt.ListNBT;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.play.server.SUpdateTileEntityPacket;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.tileentity.TileEntityType;
- public class TileEntityCookingPot extends TileEntity {
- public static final TileEntityType<TileEntityCookingPot> COOKING_POT
- = (TileEntityType<TileEntityCookingPot>) TileEntityType.Builder
- .create(TileEntityCookingPot::new).build(null)
- .setRegistryName(KajetansMod.MODID, "cooking_pot");
- private final CookingPotColorMixer mixer;
- public TileEntityCookingPot() {
- super(COOKING_POT);
- mixer = new CookingPotColorMixer();
- }
- @Override
- public CompoundNBT write(CompoundNBT com) {
- super.write(com);
- ListNBT list = new ListNBT();
- LinkedList<ItemStack> items = mixer.getItems();
- items.stream().forEach(stack -> {
- CompoundNBT nbttagcompound = new CompoundNBT();
- stack.write(nbttagcompound);
- list.add(nbttagcompound);
- });
- com.put("items", list);
- return com;
- }
- @Override
- public void read(BlockState state, CompoundNBT nbt) {
- super.read(state, nbt);
- readItems(nbt);
- }
- private void readItems(CompoundNBT com) {
- if(com.contains("items")) {
- mixer.reset();
- ListNBT list = com.getList("items", 10);
- LinkedList<ItemStack> items = mixer.getItems();
- for(INBT base : list) {
- items.add(ItemStack.read((CompoundNBT) base));
- }
- mixer.cache();
- }
- }
- public boolean hasDefaultColor() {
- return mixer.getItems().isEmpty();
- }
- public int getNumberOfIngredients() {
- return mixer.getItems().size();
- }
- public LinkedList<ItemStack> getIngredients() {
- return mixer.getItems();
- }
- public Color getColor() {
- return mixer.getMixedColor();
- }
- public void reset() {
- mixer.reset();
- }
- public boolean addItemStack(ItemStack stack, boolean server) {
- Color c = mixer.getItemColor(stack);
- if(c == null) {
- return false;
- }
- if(server) {
- mixer.addColor(stack, c);
- notifyBlockUpdate();
- markDirty();
- }
- return true;
- }
- public void setColor(int red, int green, int blue) {
- mixer.reset(new Color(red, green, blue));
- notifyBlockUpdate();
- markDirty();
- }
- private void notifyBlockUpdate() {
- BlockState state = world.getBlockState(pos);
- this.world.notifyBlockUpdate(pos, state, state, 3);
- }
- @Override
- public SUpdateTileEntityPacket getUpdatePacket() {
- return new SUpdateTileEntityPacket(this.pos, -1, this.getUpdateTag());
- }
- @Override
- public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
- readItems(pkt.getNbtCompound());
- notifyBlockUpdate();
- }
- @Override
- public CompoundNBT getUpdateTag() {
- return write(new CompoundNBT());
- }
- }
|