From 446ecc4838e6957e79f50f832b267ff55e78512a Mon Sep 17 00:00:00 2001 From: RXJpaw Date: Sun, 17 Dec 2023 18:11:15 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Limited=20simultaneous=20quarry?= =?UTF-8?q?=20breaking=20sounds=20to=2010=20per=20tick.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blockentity/QuarryBlockEntity.java | 66 +++++++++++++------ .../network/PacketQuarryBlockBreak.java | 22 ++++--- .../network/packet/BlockPosStatePacket.java | 37 ----------- .../packet/BooleanBlockPosStatePacket.java | 41 ++++++++++++ .../java/pw/rxj/iron_quarry/util/ZUtil.java | 15 +++++ 5 files changed, 117 insertions(+), 64 deletions(-) delete mode 100644 src/main/java/pw/rxj/iron_quarry/network/packet/BlockPosStatePacket.java create mode 100644 src/main/java/pw/rxj/iron_quarry/network/packet/BooleanBlockPosStatePacket.java diff --git a/src/main/java/pw/rxj/iron_quarry/blockentity/QuarryBlockEntity.java b/src/main/java/pw/rxj/iron_quarry/blockentity/QuarryBlockEntity.java index 99fd724..27909df 100644 --- a/src/main/java/pw/rxj/iron_quarry/blockentity/QuarryBlockEntity.java +++ b/src/main/java/pw/rxj/iron_quarry/blockentity/QuarryBlockEntity.java @@ -36,17 +36,18 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.util.math.Direction; -import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.random.Random; import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; import net.minecraft.world.chunk.WorldChunk; import org.jetbrains.annotations.Nullable; +import oshi.util.tuples.Pair; import pw.rxj.iron_quarry.block.QuarryBlock; import pw.rxj.iron_quarry.item.BlueprintItem; import pw.rxj.iron_quarry.network.PacketQuarryBlockBreak; import pw.rxj.iron_quarry.network.ZNetwork; +import pw.rxj.iron_quarry.render.RenderUtil; import pw.rxj.iron_quarry.screenhandler.QuarryBlockScreenHandler; import pw.rxj.iron_quarry.types.Face; import pw.rxj.iron_quarry.types.IoState; @@ -55,6 +56,7 @@ import team.reborn.energy.api.EnergyStorageUtil; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; public class QuarryBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory { @@ -386,21 +388,43 @@ public static void tick(World thisWorld, BlockPos thisPos, BlockState thisBlockS return; } + List> minedBlocks = thisBlockEntity.drillQueue(threads, thisBlock, thisBlockState, thisPos, thisBlockEntity, serverWorldToBreak, upgradesUtil); + HashSet shouldPlaySound = ZUtil.getNSpacedIndexes(minedBlocks.size(), 10); + + for (int i = 0; i < minedBlocks.size(); i++) { + BlockPos blockPos = minedBlocks.get(i).getA(); + BlockState blockState = minedBlocks.get(i).getB(); + boolean sound = shouldPlaySound.contains(i); + + ZNetwork.sendToAround(serverWorldToBreak, blockPos, 64.0, player -> { + return PacketQuarryBlockBreak.bake(blockPos, blockState, sound); + }); + } + } + + private List> drillQueue(int threads, QuarryBlock quarryBlock, BlockState quarryBlockState, BlockPos quarryPos, QuarryBlockEntity quarryBlockEntity, ServerWorld serverWorldToBreak, MachineUpgradesUtil upgradesUtil) { + List> minedBlocks = new ArrayList<>(); + ArrayList MiningQueue = quarryBlockEntity.MiningQueue; + for (int i = 0; i < threads; i++) { - if(MiningQueue.isEmpty()) return; + if(MiningQueue.isEmpty()) { + return minedBlocks; + } BlockPos blockPosToBreak = MiningQueue.remove(MiningQueue.size() - 1); WorldChunk worldChunkToBreak = serverWorldToBreak.getChunkManager().getWorldChunk(blockPosToBreak.getX() >> 4, blockPosToBreak.getZ() >> 4); if(worldChunkToBreak == null) { - MiningQueue.add(blockPosToBreak); return; + MiningQueue.add(blockPosToBreak); + return minedBlocks; } BlockState blockStateToBreak = worldChunkToBreak.getBlockState(blockPosToBreak); Block blockToBreak = blockStateToBreak.getBlock(); if(!blockStateToBreak.getFluidState().isEmpty()) { - serverWorldToBreak.setBlockState(blockPosToBreak, Blocks.AIR.getDefaultState(), 2, 0); continue; + serverWorldToBreak.setBlockState(blockPosToBreak, Blocks.AIR.getDefaultState(), 2, 0); + continue; } if(blockStateToBreak.isAir()) continue; @@ -408,14 +432,16 @@ public static void tick(World thisWorld, BlockPos thisPos, BlockState thisBlockS if(ZUtil.isActualBlockEntity(serverWorldToBreak, blockStateToBreak, blockPosToBreak)) continue; //Energy - long actualEnergyConsumption = thisBlock.getActualEnergyConsumption(upgradesUtil, blockToBreak); + long actualEnergyConsumption = quarryBlock.getActualEnergyConsumption(upgradesUtil, blockToBreak); - if(thisBlockEntity.EnergyContainer.getStored() < actualEnergyConsumption) { - MiningQueue.add(blockPosToBreak); return; + if(quarryBlockEntity.EnergyContainer.getStored() < actualEnergyConsumption) { + MiningQueue.add(blockPosToBreak); + return minedBlocks; } //Loot ItemStack breakingItem = Items.NETHERITE_PICKAXE.getDefaultStack(); + if(upgradesUtil.hasSilkTouch()) { breakingItem.addEnchantment(Enchantments.SILK_TOUCH, 1); } else { @@ -425,33 +451,35 @@ public static void tick(World thisWorld, BlockPos thisPos, BlockState thisBlockS List droppingItems = blockStateToBreak.getDroppedStacks( new LootContext.Builder(serverWorldToBreak) - .parameter(LootContextParameters.BLOCK_STATE, thisBlockState) - .parameter(LootContextParameters.ORIGIN, new Vec3d(thisPos.getX(), thisPos.getY(), thisPos.getZ())) + .parameter(LootContextParameters.BLOCK_STATE, quarryBlockState) + .parameter(LootContextParameters.ORIGIN, RenderUtil.vec3dFrom(quarryPos)) .parameter(LootContextParameters.TOOL, breakingItem) ); List stacksToAward = new ArrayList<>(); - for (ItemStack droppingItem : droppingItems) { - if(!thisBlockEntity.OutputInventory.canInsert(droppingItem)) { - MiningQueue.add(blockPosToBreak); return; + //TODO: can result in loss of drops + for (ItemStack drop : droppingItems) { + if(!quarryBlockEntity.OutputInventory.canInsert(drop)) { + MiningQueue.add(blockPosToBreak); + return minedBlocks; } else { - stacksToAward.add(droppingItem); + stacksToAward.add(drop); } } - stacksToAward.forEach(thisBlockEntity.OutputInventory::addStack); + stacksToAward.forEach(quarryBlockEntity.OutputInventory::addStack); //Mining serverWorldToBreak.setBlockState(blockPosToBreak, Blocks.AIR.getDefaultState(), 2, 0); - thisBlockEntity.EnergyContainer.useEnergy(actualEnergyConsumption); - - ZNetwork.sendToAround(serverWorldToBreak, blockPosToBreak, 32.0, player -> { - return PacketQuarryBlockBreak.bake(blockPosToBreak, blockStateToBreak); - }); + quarryBlockEntity.EnergyContainer.useEnergy(actualEnergyConsumption); + minedBlocks.add(new Pair<>(blockPosToBreak, blockStateToBreak)); } + + return minedBlocks; } + //NBT Handling @Override public void readNbt(NbtCompound tag) { diff --git a/src/main/java/pw/rxj/iron_quarry/network/PacketQuarryBlockBreak.java b/src/main/java/pw/rxj/iron_quarry/network/PacketQuarryBlockBreak.java index 0639157..fafc846 100644 --- a/src/main/java/pw/rxj/iron_quarry/network/PacketQuarryBlockBreak.java +++ b/src/main/java/pw/rxj/iron_quarry/network/PacketQuarryBlockBreak.java @@ -12,9 +12,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import pw.rxj.iron_quarry.Main; -import pw.rxj.iron_quarry.network.packet.BlockPosStatePacket; +import pw.rxj.iron_quarry.network.packet.BooleanBlockPosStatePacket; -public class PacketQuarryBlockBreak extends ComplexPacketHandler { +public class PacketQuarryBlockBreak extends ComplexPacketHandler { protected static PacketQuarryBlockBreak INSTANCE = new PacketQuarryBlockBreak(); private PacketQuarryBlockBreak() { } @@ -24,12 +24,12 @@ public Identifier getChannelId() { return Identifier.of(Main.MOD_ID, "quarry_block_break"); } @Override - public @Nullable BlockPosStatePacket read(PacketByteBuf buf) { - return BlockPosStatePacket.read(buf); + public @Nullable BooleanBlockPosStatePacket read(PacketByteBuf buf) { + return BooleanBlockPosStatePacket.read(buf); } @Override - protected void receiveFromServer(MinecraftClient client, ClientPlayNetworkHandler handler, @NotNull BlockPosStatePacket packet, PacketSender response) { + protected void receiveFromServer(MinecraftClient client, ClientPlayNetworkHandler handler, @NotNull BooleanBlockPosStatePacket packet, PacketSender response) { if(client.world == null) return; BlockPos blockPos = packet.blockPos; @@ -37,12 +37,18 @@ protected void receiveFromServer(MinecraftClient client, ClientPlayNetworkHandle if (!blockState.isAir() && blockState.getFluidState().isEmpty()) { BlockSoundGroup blockSoundGroup = blockState.getSoundGroup(); - client.world.playSound(blockPos, blockSoundGroup.getBreakSound(), SoundCategory.BLOCKS, (blockSoundGroup.getVolume() + 1.0F) / 2.0F, blockSoundGroup.getPitch() * 0.8F, false); client.world.addBlockBreakParticles(blockPos, blockState); + + if(packet.bool) { + float volume = (blockSoundGroup.getVolume() + 1.0F) / 2.0F; + float pitch = blockSoundGroup.getPitch() * 0.8F; + + client.world.playSound(blockPos, blockSoundGroup.getBreakSound(), SoundCategory.BLOCKS, volume, pitch, false); + } } } - public static PacketByteBuf bake(BlockPos blockPos, BlockState blockState) { - return BlockPosStatePacket.write(INSTANCE.getChannelId(), blockPos, blockState); + public static PacketByteBuf bake(BlockPos blockPos, BlockState blockState, boolean sound) { + return BooleanBlockPosStatePacket.write(INSTANCE.getChannelId(), sound, blockPos, blockState); } } diff --git a/src/main/java/pw/rxj/iron_quarry/network/packet/BlockPosStatePacket.java b/src/main/java/pw/rxj/iron_quarry/network/packet/BlockPosStatePacket.java deleted file mode 100644 index 58de61c..0000000 --- a/src/main/java/pw/rxj/iron_quarry/network/packet/BlockPosStatePacket.java +++ /dev/null @@ -1,37 +0,0 @@ -package pw.rxj.iron_quarry.network.packet; - -import io.netty.buffer.Unpooled; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; - -public class BlockPosStatePacket { - public final BlockPos blockPos; - public final BlockState blockState; - - private BlockPosStatePacket(BlockPos blockPos, BlockState blockState) { - this.blockPos = blockPos; - this.blockState = blockState; - } - - public static BlockPosStatePacket read(PacketByteBuf buf) { - try { - BlockPos blockPos = buf.readBlockPos(); - BlockState blockState = Block.getStateFromRawId(buf.readInt()); - - return new BlockPosStatePacket(blockPos, blockState); - } catch(Exception ignored) { - return null; - } - } - public static PacketByteBuf write(Identifier channel, BlockPos blockPos, BlockState blockState) { - PacketByteBuf packet = new PacketByteBuf(Unpooled.buffer()); - packet.writeIdentifier(channel); - packet.writeBlockPos(blockPos); - packet.writeInt(Block.getRawIdFromState(blockState)); - - return packet; - } - } \ No newline at end of file diff --git a/src/main/java/pw/rxj/iron_quarry/network/packet/BooleanBlockPosStatePacket.java b/src/main/java/pw/rxj/iron_quarry/network/packet/BooleanBlockPosStatePacket.java new file mode 100644 index 0000000..eadfa98 --- /dev/null +++ b/src/main/java/pw/rxj/iron_quarry/network/packet/BooleanBlockPosStatePacket.java @@ -0,0 +1,41 @@ +package pw.rxj.iron_quarry.network.packet; + +import io.netty.buffer.Unpooled; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.BlockPos; + +public class BooleanBlockPosStatePacket { + public final Boolean bool; + public final BlockPos blockPos; + public final BlockState blockState; + + private BooleanBlockPosStatePacket(Boolean bool, BlockPos blockPos, BlockState blockState) { + this.bool = bool; + this.blockPos = blockPos; + this.blockState = blockState; + } + + public static BooleanBlockPosStatePacket read(PacketByteBuf buf) { + try { + Boolean bool = buf.readBoolean(); + BlockPos blockPos = buf.readBlockPos(); + BlockState blockState = Block.getStateFromRawId(buf.readInt()); + + return new BooleanBlockPosStatePacket(bool, blockPos, blockState); + } catch(Exception ignored) { + return null; + } + } + public static PacketByteBuf write(Identifier channel, Boolean bool, BlockPos blockPos, BlockState blockState) { + PacketByteBuf packet = new PacketByteBuf(Unpooled.buffer()); + packet.writeIdentifier(channel); + packet.writeBoolean(bool); + packet.writeBlockPos(blockPos); + packet.writeInt(Block.getRawIdFromState(blockState)); + + return packet; + } +} \ No newline at end of file diff --git a/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java b/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java index 07e584f..68ea26b 100644 --- a/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java +++ b/src/main/java/pw/rxj/iron_quarry/util/ZUtil.java @@ -16,6 +16,7 @@ import org.jetbrains.annotations.Nullable; import java.text.DecimalFormat; +import java.util.HashSet; public class ZUtil { public static Object getBlockOrItem(ItemStack stack) { @@ -137,4 +138,18 @@ public static boolean isInsideWorldBounds(BlockPos blockPos, World world) { return world.getWorldBorder().contains(blockPos); } + + public static HashSet getNSpacedIndexes(int listSize, int n) { + if(listSize <= 0 || n <= 0) return new HashSet<>(); + + HashSet indexSet = new HashSet<>(); + float step = (listSize - 1.0F) / (n - 1.0F); + + for (int i = 0; i < n; i++) { + int index = Math.round(step * i); + indexSet.add(index); + } + + return indexSet; + } }