[Fix] Barrel Plays closing sound early (/quiqueck/BetterEnd#20, paulevsGitch/EdenRing#41)
This commit is contained in:
parent
59756e6dca
commit
d5cf63427b
2 changed files with 90 additions and 55 deletions
|
@ -1,6 +1,5 @@
|
|||
package org.betterx.bclib.blockentities;
|
||||
|
||||
import org.betterx.bclib.blocks.BaseBarrelBlock;
|
||||
import org.betterx.bclib.registry.BaseBlockEntities;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
|
@ -11,21 +10,49 @@ import net.minecraft.network.chat.Component;
|
|||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundEvents;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.ContainerHelper;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.inventory.ChestMenu;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.BarrelBlock;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
||||
import net.minecraft.world.level.block.entity.ContainerOpenersCounter;
|
||||
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
|
||||
public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
|
||||
private NonNullList<ItemStack> inventory;
|
||||
private int viewerCount;
|
||||
private ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() {
|
||||
|
||||
@Override
|
||||
protected void onOpen(Level level, BlockPos blockPos, BlockState blockState) {
|
||||
BaseBarrelBlockEntity.this.playSound(blockState, SoundEvents.BARREL_OPEN);
|
||||
BaseBarrelBlockEntity.this.updateBlockState(blockState, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onClose(Level level, BlockPos blockPos, BlockState blockState) {
|
||||
BaseBarrelBlockEntity.this.playSound(blockState, SoundEvents.BARREL_CLOSE);
|
||||
BaseBarrelBlockEntity.this.updateBlockState(blockState, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void openerCountChanged(Level level, BlockPos blockPos, BlockState blockState, int i, int j) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isOwnContainer(Player player) {
|
||||
if (player.containerMenu instanceof ChestMenu) {
|
||||
Container container = ((ChestMenu) player.containerMenu).getContainer();
|
||||
return container == BaseBarrelBlockEntity.this;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
private BaseBarrelBlockEntity(BlockEntityType<?> type, BlockPos blockPos, BlockState blockState) {
|
||||
super(type, blockPos, blockState);
|
||||
|
@ -42,10 +69,9 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
|
|||
if (!this.trySaveLootTable(tag)) {
|
||||
ContainerHelper.saveAllItems(tag, this.inventory);
|
||||
}
|
||||
|
||||
//return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(CompoundTag tag) {
|
||||
super.load(tag);
|
||||
this.inventory = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
||||
|
@ -54,43 +80,51 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getContainerSize() {
|
||||
return 27;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NonNullList<ItemStack> getItems() {
|
||||
return this.inventory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setItems(NonNullList<ItemStack> list) {
|
||||
this.inventory = list;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Component getDefaultName() {
|
||||
return Component.translatable("container.barrel");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) {
|
||||
return ChestMenu.threeRows(syncId, playerInventory, this);
|
||||
}
|
||||
|
||||
public void startOpen(Player player) {
|
||||
if (!player.isSpectator()) {
|
||||
if (viewerCount < 0) {
|
||||
viewerCount = 0;
|
||||
}
|
||||
|
||||
++viewerCount;
|
||||
BlockState blockState = this.getBlockState();
|
||||
if (!blockState.getValue(BarrelBlock.OPEN)) {
|
||||
playSound(blockState, SoundEvents.BARREL_OPEN);
|
||||
setOpen(blockState, true);
|
||||
}
|
||||
|
||||
if (level != null) {
|
||||
scheduleUpdate();
|
||||
}
|
||||
if (!this.remove && !player.isSpectator()) {
|
||||
this.openersCounter.incrementOpeners(player, this.getLevel(), this.getBlockPos(), this.getBlockState());
|
||||
}
|
||||
// if (!player.isSpectator()) {
|
||||
// if (viewerCount < 0) {
|
||||
// viewerCount = 0;
|
||||
// }
|
||||
//
|
||||
// ++viewerCount;
|
||||
// BlockState blockState = this.getBlockState();
|
||||
// if (!blockState.getValue(BarrelBlock.OPEN)) {
|
||||
// playSound(blockState, SoundEvents.BARREL_OPEN);
|
||||
// updateBlockState(blockState, true);
|
||||
// }
|
||||
//
|
||||
// if (level != null) {
|
||||
// scheduleUpdate();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
private void scheduleUpdate() {
|
||||
|
@ -98,31 +132,38 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
|
|||
}
|
||||
|
||||
public void tick() {
|
||||
if (level != null) {
|
||||
viewerCount = ChestBlockEntity.getOpenCount(level, worldPosition);
|
||||
if (viewerCount > 0) {
|
||||
scheduleUpdate();
|
||||
} else {
|
||||
BlockState blockState = getBlockState();
|
||||
if (!(blockState.getBlock() instanceof BaseBarrelBlock)) {
|
||||
setRemoved();
|
||||
return;
|
||||
}
|
||||
if (blockState.getValue(BarrelBlock.OPEN)) {
|
||||
playSound(blockState, SoundEvents.BARREL_CLOSE);
|
||||
setOpen(blockState, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (level != null) {
|
||||
// viewerCount = ChestBlockEntity.getOpenCount(level, worldPosition);
|
||||
// if (viewerCount > 0) {
|
||||
// scheduleUpdate();
|
||||
// } else {
|
||||
// BlockState blockState = getBlockState();
|
||||
// if (!(blockState.getBlock() instanceof BaseBarrelBlock)) {
|
||||
// setRemoved();
|
||||
// return;
|
||||
// }
|
||||
// if (blockState.getValue(BarrelBlock.OPEN)) {
|
||||
// playSound(blockState, SoundEvents.BARREL_CLOSE);
|
||||
// updateBlockState(blockState, false);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopOpen(Player player) {
|
||||
if (!player.isSpectator()) {
|
||||
--this.viewerCount;
|
||||
if (!this.remove && !player.isSpectator()) {
|
||||
this.openersCounter.decrementOpeners(player, this.getLevel(), this.getBlockPos(), this.getBlockState());
|
||||
}
|
||||
}
|
||||
|
||||
private void setOpen(BlockState state, boolean open) {
|
||||
public void recheckOpen() {
|
||||
if (!this.remove) {
|
||||
this.openersCounter.recheckOpeners(this.getLevel(), this.getBlockPos(), this.getBlockState());
|
||||
}
|
||||
}
|
||||
|
||||
private void updateBlockState(BlockState state, boolean open) {
|
||||
if (level != null) {
|
||||
level.setBlock(this.getBlockPos(), state.setValue(BarrelBlock.OPEN, open), 3);
|
||||
}
|
||||
|
@ -130,19 +171,13 @@ public class BaseBarrelBlockEntity extends RandomizableContainerBlockEntity {
|
|||
|
||||
private void playSound(BlockState blockState, SoundEvent soundEvent) {
|
||||
if (level != null) {
|
||||
Vec3i vec3i = blockState.getValue(BarrelBlock.FACING).getNormal();
|
||||
double d = (double) this.worldPosition.getX() + 0.5D + (double) vec3i.getX() / 2.0D;
|
||||
double e = (double) this.worldPosition.getY() + 0.5D + (double) vec3i.getY() / 2.0D;
|
||||
double f = (double) this.worldPosition.getZ() + 0.5D + (double) vec3i.getZ() / 2.0D;
|
||||
Vec3i facingDir = blockState.getValue(BarrelBlock.FACING).getNormal();
|
||||
double x = this.worldPosition.getX() + 0.5D + facingDir.getX() / 2.0D;
|
||||
double y = this.worldPosition.getY() + 0.5D + facingDir.getY() / 2.0D;
|
||||
double z = this.worldPosition.getZ() + 0.5D + facingDir.getZ() / 2.0D;
|
||||
level.playSound(
|
||||
null,
|
||||
d,
|
||||
e,
|
||||
f,
|
||||
soundEvent,
|
||||
SoundSource.BLOCKS,
|
||||
0.5F,
|
||||
this.level.random.nextFloat() * 0.1F + 0.9F
|
||||
null, x, y, z,
|
||||
soundEvent, SoundSource.BLOCKS, 0.5F, this.level.random.nextFloat() * 0.1F + 0.9F
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,16 +66,16 @@ public class BaseBarrelBlock extends BarrelBlock implements BlockModelProvider {
|
|||
@Override
|
||||
public InteractionResult use(
|
||||
BlockState state,
|
||||
Level world,
|
||||
Level level,
|
||||
BlockPos pos,
|
||||
Player player,
|
||||
InteractionHand hand,
|
||||
BlockHitResult hit
|
||||
) {
|
||||
if (world.isClientSide) {
|
||||
if (level.isClientSide) {
|
||||
return InteractionResult.SUCCESS;
|
||||
} else {
|
||||
BlockEntity blockEntity = world.getBlockEntity(pos);
|
||||
BlockEntity blockEntity = level.getBlockEntity(pos);
|
||||
if (blockEntity instanceof BaseBarrelBlockEntity) {
|
||||
player.openMenu((BaseBarrelBlockEntity) blockEntity);
|
||||
player.awardStat(Stats.OPEN_BARREL);
|
||||
|
@ -90,7 +90,7 @@ public class BaseBarrelBlock extends BarrelBlock implements BlockModelProvider {
|
|||
public void tick(BlockState state, ServerLevel world, BlockPos pos, RandomSource random) {
|
||||
BlockEntity blockEntity = world.getBlockEntity(pos);
|
||||
if (blockEntity instanceof BaseBarrelBlockEntity) {
|
||||
((BaseBarrelBlockEntity) blockEntity).tick();
|
||||
((BaseBarrelBlockEntity) blockEntity).recheckOpen();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue