Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ModTileEntity extends LockableLootTileEntity {
- @Nullable
- private NonNullList<ItemStack> contents;
- private BlockPos blockPos;
- private int numPlayersUsing;
- private int containerInd;
- public int getContainerInd() {
- return this.containerInd;
- }
- public ModTileEntity() {
- super(ModTileEntities.MOD_TE);
- this.containerInd = this.getCapability(ModTeIDProvider.MOD_TE_ID).orElseThrow(
- () -> new IllegalArgumentException("LazyOptional cannot be empty!")).get()[3];
- }
- @Override
- public NonNullList<ItemStack> getItems() {
- if (this.contents == null) {
- this.contents = NonNullList.withSize(27, ItemStack.EMPTY);
- }
- return this.contents;
- }
- @Override
- public CompoundNBT write(CompoundNBT compound) {
- super.write(compound);
- if (!checkLootAndWrite(compound)) {
- ItemStackHelper.saveAllItems(compound, getItems());
- }
- return compound;
- }
- @Override
- public void func_230337_a_(BlockState blockState, CompoundNBT compound) {
- super.func_230337_a_(blockState, compound);
- this.contents = NonNullList.withSize(27, ItemStack.EMPTY);
- if (!checkLootAndRead(compound)) {
- ItemStackHelper.loadAllItems(compound, this.contents);
- }
- }
- @Override
- protected void setItems(NonNullList<ItemStack> itemsIn) {
- this.contents = itemsIn;
- }
- @Override
- protected Container createMenu(int id, PlayerInventory player) {
- this.blockPos = new BlockPos(this.pos);
- return new ModContainer(ModContainerTypes.MULTI_9X3, id, player, this, this, 3);
- }
- @Override
- public void openInventory(PlayerEntity player) {
- if (!player.isSpectator()) {
- if (numPlayersUsing < 0) {
- numPlayersUsing = 0;
- }
- numPlayersUsing++;
- BlockState blockstate = getBlockState();
- if (!blockstate.get(ModBlock.PROPERTY_OPEN)) {
- playSound(blockstate, ModSounds.MOD_BLOCK_ON);
- setOpenProperty(blockstate, true);
- }
- scheduleTick();
- }
- }
- private void scheduleTick() {
- world.getPendingBlockTicks().scheduleTick(getPos(), getBlockState().getBlock(), 5);
- }
- public void modTETick() {
- int x = pos.getX();
- int y = pos.getY();
- int z = pos.getZ();
- System.out.println("Pos: " + pos);
- this.containerInd = this.getCapability(ModTeIDProvider.MOD_TE_ID).orElseThrow(
- () -> new IllegalArgumentException("LazyOptional cannot be empty!")).get()[3];
- numPlayersUsing = calculatePlayersUsing(world, this, x, y, z);
- if (numPlayersUsing > 0) {
- scheduleTick();
- } else {
- BlockState blockstate = getBlockState();
- if (!(blockstate.getBlock() instanceof ModBlock)) {
- remove();
- return;
- }
- boolean flag = blockstate.get(ModBlock.PROPERTY_OPEN);
- if (flag) {
- playSound(blockstate, ModSounds.MOD_BLOCK_OFF);
- setOpenProperty(blockstate, false);
- }
- }
- }
- public BlockPos getBlockPos() {
- return this.blockPos;
- }
- public static int calculatePlayersUsing(World p_213976_0_, LockableTileEntity p_213976_1_, int p_213976_2_, int p_213976_3_, int p_213976_4_) {
- int i = 0;
- float f = 5.0F;
- 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))) {
- if (playerentity.openContainer instanceof ModContainer) {
- IInventory iinventory = ((ModContainer)playerentity.openContainer).getLowerChestInventory();
- if (iinventory == p_213976_1_ || iinventory instanceof DoubleSidedInventory && ((DoubleSidedInventory) iinventory).isPartOfLargeChest(p_213976_1_)) {
- ++i;
- }
- }
- }
- return i;
- }
- @Override
- public void closeInventory(PlayerEntity player) {
- if (!player.isSpectator()) {
- numPlayersUsing--;
- }
- }
- private void setOpenProperty(BlockState blockState, boolean open) {
- world.setBlockState(getPos(), blockState.with(ModBlock.PROPERTY_OPEN, open), 3);
- }
- private void playSound(BlockState blockState, SoundEvent soundEvent) {
- Vector3i vec3i = blockState.get(ModBlock.PROPERTY_FACING).getDirectionVec();
- double x = (double) this.pos.getX() + 0.5D + (double) vec3i.getX() / 2D;
- double y = (double) this.pos.getY() + 0.5D + (double) vec3i.getY() / 2D;
- double z = (double) this.pos.getZ() + 0.5D + (double) vec3i.getZ() / 2D;
- world.playSound(null, x, y, z, soundEvent, SoundCategory.BLOCKS, 0.5F, 1.0F);
- }
- @Override
- protected ITextComponent getDefaultName() {
- return new TranslationTextComponent(getBlockState().getBlock().getTranslationKey());
- }
- @Override
- public SUpdateTileEntityPacket getUpdatePacket() {
- return new SUpdateTileEntityPacket(pos, 1, getUpdateTag());
- }
- @Override
- public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
- func_230337_a_(getBlockState(), pkt.getNbtCompound());
- }
- @Override
- public CompoundNBT getUpdateTag() {
- return write(new CompoundNBT());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement