TileEntityCookingPot.java 3.4 KB

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