TileEntityCookingPot.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package me.km.blocks.cookingpot;
  2. import java.awt.Color;
  3. import java.util.LinkedList;
  4. import me.km.KajetansMod;
  5. import net.minecraft.block.BlockState;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.INBT;
  8. import net.minecraft.nbt.CompoundNBT;
  9. import net.minecraft.nbt.ListNBT;
  10. import net.minecraft.network.NetworkManager;
  11. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.tileentity.TileEntityType;
  14. public class TileEntityCookingPot extends TileEntity {
  15. public static final TileEntityType<TileEntityCookingPot> COOKING_POT
  16. = (TileEntityType<TileEntityCookingPot>) TileEntityType.Builder
  17. .create(TileEntityCookingPot::new).build(null)
  18. .setRegistryName(KajetansMod.MODID, "cooking_pot");
  19. private final CookingPotColorMixer mixer;
  20. public TileEntityCookingPot() {
  21. super(COOKING_POT);
  22. mixer = new CookingPotColorMixer();
  23. }
  24. @Override
  25. public CompoundNBT write(CompoundNBT com) {
  26. super.write(com);
  27. ListNBT list = new ListNBT();
  28. LinkedList<ItemStack> items = mixer.getItems();
  29. items.stream().forEach(stack -> {
  30. CompoundNBT nbttagcompound = new CompoundNBT();
  31. stack.write(nbttagcompound);
  32. list.add(nbttagcompound);
  33. });
  34. com.put("items", list);
  35. return com;
  36. }
  37. @Override
  38. public void read(BlockState state, CompoundNBT nbt) {
  39. super.read(state, nbt);
  40. readItems(nbt);
  41. }
  42. private void readItems(CompoundNBT com) {
  43. if(com.contains("items")) {
  44. mixer.reset();
  45. ListNBT list = com.getList("items", 10);
  46. LinkedList<ItemStack> items = mixer.getItems();
  47. for(INBT base : list) {
  48. items.add(ItemStack.read((CompoundNBT) base));
  49. }
  50. mixer.cache();
  51. }
  52. }
  53. public boolean hasDefaultColor() {
  54. return mixer.getItems().isEmpty();
  55. }
  56. public int getNumberOfIngredients() {
  57. return mixer.getItems().size();
  58. }
  59. public LinkedList<ItemStack> getIngredients() {
  60. return mixer.getItems();
  61. }
  62. public Color getColor() {
  63. return mixer.getMixedColor();
  64. }
  65. public void reset() {
  66. mixer.reset();
  67. }
  68. public boolean addItemStack(ItemStack stack, boolean server) {
  69. Color c = mixer.getItemColor(stack);
  70. if(c == null) {
  71. return false;
  72. }
  73. if(server) {
  74. mixer.addColor(stack, c);
  75. notifyBlockUpdate();
  76. markDirty();
  77. }
  78. return true;
  79. }
  80. public void setColor(int red, int green, int blue) {
  81. mixer.reset(new Color(red, green, blue));
  82. notifyBlockUpdate();
  83. markDirty();
  84. }
  85. private void notifyBlockUpdate() {
  86. BlockState state = world.getBlockState(pos);
  87. this.world.notifyBlockUpdate(pos, state, state, 3);
  88. }
  89. @Override
  90. public SUpdateTileEntityPacket getUpdatePacket() {
  91. return new SUpdateTileEntityPacket(this.pos, -1, this.getUpdateTag());
  92. }
  93. @Override
  94. public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
  95. readItems(pkt.getNbtCompound());
  96. notifyBlockUpdate();
  97. }
  98. @Override
  99. public CompoundNBT getUpdateTag() {
  100. return write(new CompoundNBT());
  101. }
  102. }