Skip to content

Commit

Permalink
🚸 Limited simultaneous quarry breaking sounds to 10 per tick.
Browse files Browse the repository at this point in the history
  • Loading branch information
RXJpaw committed Dec 17, 2023
1 parent 0fcb002 commit 446ecc4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 64 deletions.
66 changes: 47 additions & 19 deletions src/main/java/pw/rxj/iron_quarry/blockentity/QuarryBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -386,36 +388,60 @@ public static void tick(World thisWorld, BlockPos thisPos, BlockState thisBlockS
return;
}

List<Pair<BlockPos, BlockState>> minedBlocks = thisBlockEntity.drillQueue(threads, thisBlock, thisBlockState, thisPos, thisBlockEntity, serverWorldToBreak, upgradesUtil);
HashSet<Integer> 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<Pair<BlockPos, BlockState>> drillQueue(int threads, QuarryBlock quarryBlock, BlockState quarryBlockState, BlockPos quarryPos, QuarryBlockEntity quarryBlockEntity, ServerWorld serverWorldToBreak, MachineUpgradesUtil upgradesUtil) {
List<Pair<BlockPos, BlockState>> minedBlocks = new ArrayList<>();
ArrayList<BlockPos> 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;
if(blockToBreak.getHardness() < 0) continue;
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 {
Expand All @@ -425,33 +451,35 @@ public static void tick(World thisWorld, BlockPos thisPos, BlockState thisBlockS

List<ItemStack> 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<ItemStack> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockPosStatePacket> {
public class PacketQuarryBlockBreak extends ComplexPacketHandler<BooleanBlockPosStatePacket> {
protected static PacketQuarryBlockBreak INSTANCE = new PacketQuarryBlockBreak();

private PacketQuarryBlockBreak() { }
Expand All @@ -24,25 +24,31 @@ 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;
BlockState blockState = packet.blockState;

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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 15 additions & 0 deletions src/main/java/pw/rxj/iron_quarry/util/ZUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -137,4 +138,18 @@ public static boolean isInsideWorldBounds(BlockPos blockPos, World world) {

return world.getWorldBorder().contains(blockPos);
}

public static HashSet<Integer> getNSpacedIndexes(int listSize, int n) {
if(listSize <= 0 || n <= 0) return new HashSet<>();

HashSet<Integer> 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;
}
}

0 comments on commit 446ecc4

Please sign in to comment.