Guest User

ModTileEntity

a guest
Oct 30th, 2020
10
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ModTileEntity extends LockableLootTileEntity {
  2.  
  3. @Nullable
  4. private NonNullList<ItemStack> contents;
  5.  
  6. private BlockPos blockPos;
  7. private int numPlayersUsing;
  8. private int containerInd;
  9.  
  10. public int getContainerInd() {
  11. return this.containerInd;
  12. }
  13.  
  14. public ModTileEntity() {
  15. super(ModTileEntities.MOD_TE);
  16. this.containerInd = this.getCapability(ModTeIDProvider.MOD_TE_ID).orElseThrow(
  17. () -> new IllegalArgumentException("LazyOptional cannot be empty!")).get()[3];
  18. }
  19.  
  20. @Override
  21. public NonNullList<ItemStack> getItems() {
  22. if (this.contents == null) {
  23. this.contents = NonNullList.withSize(27, ItemStack.EMPTY);
  24. }
  25. return this.contents;
  26. }
  27.  
  28. @Override
  29. public CompoundNBT write(CompoundNBT compound) {
  30. super.write(compound);
  31. if (!checkLootAndWrite(compound)) {
  32. ItemStackHelper.saveAllItems(compound, getItems());
  33. }
  34. return compound;
  35. }
  36.  
  37. @Override
  38. public void func_230337_a_(BlockState blockState, CompoundNBT compound) {
  39. super.func_230337_a_(blockState, compound);
  40. this.contents = NonNullList.withSize(27, ItemStack.EMPTY);
  41. if (!checkLootAndRead(compound)) {
  42. ItemStackHelper.loadAllItems(compound, this.contents);
  43. }
  44. }
  45.  
  46. @Override
  47. protected void setItems(NonNullList<ItemStack> itemsIn) {
  48. this.contents = itemsIn;
  49. }
  50.  
  51. @Override
  52. protected Container createMenu(int id, PlayerInventory player) {
  53. this.blockPos = new BlockPos(this.pos);
  54. return new ModContainer(ModContainerTypes.MULTI_9X3, id, player, this, this, 3);
  55. }
  56.  
  57. @Override
  58. public void openInventory(PlayerEntity player) {
  59. if (!player.isSpectator()) {
  60. if (numPlayersUsing < 0) {
  61. numPlayersUsing = 0;
  62. }
  63. numPlayersUsing++;
  64. BlockState blockstate = getBlockState();
  65. if (!blockstate.get(ModBlock.PROPERTY_OPEN)) {
  66. playSound(blockstate, ModSounds.MOD_BLOCK_ON);
  67. setOpenProperty(blockstate, true);
  68. }
  69. scheduleTick();
  70. }
  71. }
  72.  
  73. private void scheduleTick() {
  74. world.getPendingBlockTicks().scheduleTick(getPos(), getBlockState().getBlock(), 5);
  75. }
  76.  
  77. public void modTETick() {
  78. int x = pos.getX();
  79. int y = pos.getY();
  80. int z = pos.getZ();
  81.  
  82. System.out.println("Pos: " + pos);
  83.  
  84. this.containerInd = this.getCapability(ModTeIDProvider.MOD_TE_ID).orElseThrow(
  85. () -> new IllegalArgumentException("LazyOptional cannot be empty!")).get()[3];
  86.  
  87. numPlayersUsing = calculatePlayersUsing(world, this, x, y, z);
  88. if (numPlayersUsing > 0) {
  89. scheduleTick();
  90. } else {
  91. BlockState blockstate = getBlockState();
  92. if (!(blockstate.getBlock() instanceof ModBlock)) {
  93. remove();
  94. return;
  95. }
  96. boolean flag = blockstate.get(ModBlock.PROPERTY_OPEN);
  97. if (flag) {
  98. playSound(blockstate, ModSounds.MOD_BLOCK_OFF);
  99. setOpenProperty(blockstate, false);
  100. }
  101. }
  102. }
  103.  
  104. public BlockPos getBlockPos() {
  105. return this.blockPos;
  106. }
  107.  
  108. public static int calculatePlayersUsing(World p_213976_0_, LockableTileEntity p_213976_1_, int p_213976_2_, int p_213976_3_, int p_213976_4_) {
  109. int i = 0;
  110. float f = 5.0F;
  111. for(PlayerEntity playerentity : p_213976_0_.getEntitiesWithinAABB(PlayerEntity.class, new AxisAlignedBB((float)p_213976_2_ - f, (float)p_213976_3_ - f, (float)p_213976_4_ - f, (double)((float)(p_213976_2_ + 1) + f), (float)(p_213976_3_ + 1) + f, (float)(p_213976_4_ + 1) + f))) {
  112. if (playerentity.openContainer instanceof ModContainer) {
  113. IInventory iinventory = ((ModContainer)playerentity.openContainer).getLowerChestInventory();
  114. if (iinventory == p_213976_1_ || iinventory instanceof DoubleSidedInventory && ((DoubleSidedInventory) iinventory).isPartOfLargeChest(p_213976_1_)) {
  115. ++i;
  116. }
  117. }
  118. }
  119. return i;
  120. }
  121.  
  122. @Override
  123. public void closeInventory(PlayerEntity player) {
  124. if (!player.isSpectator()) {
  125. numPlayersUsing--;
  126. }
  127. }
  128.  
  129. private void setOpenProperty(BlockState blockState, boolean open) {
  130. world.setBlockState(getPos(), blockState.with(ModBlock.PROPERTY_OPEN, open), 3);
  131. }
  132.  
  133. private void playSound(BlockState blockState, SoundEvent soundEvent) {
  134. Vector3i vec3i = blockState.get(ModBlock.PROPERTY_FACING).getDirectionVec();
  135. double x = (double) this.pos.getX() + 0.5D + (double) vec3i.getX() / 2D;
  136. double y = (double) this.pos.getY() + 0.5D + (double) vec3i.getY() / 2D;
  137. double z = (double) this.pos.getZ() + 0.5D + (double) vec3i.getZ() / 2D;
  138. world.playSound(null, x, y, z, soundEvent, SoundCategory.BLOCKS, 0.5F, 1.0F);
  139. }
  140.  
  141. @Override
  142. protected ITextComponent getDefaultName() {
  143. return new TranslationTextComponent(getBlockState().getBlock().getTranslationKey());
  144. }
  145.  
  146. @Override
  147. public SUpdateTileEntityPacket getUpdatePacket() {
  148. return new SUpdateTileEntityPacket(pos, 1, getUpdateTag());
  149. }
  150.  
  151. @Override
  152. public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
  153. func_230337_a_(getBlockState(), pkt.getNbtCompound());
  154. }
  155.  
  156. @Override
  157. public CompoundNBT getUpdateTag() {
  158. return write(new CompoundNBT());
  159. }
  160.  
  161. }
RAW Paste Data