From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Sun, 11 Dec 2022 18:43:35 +0800 Subject: [PATCH] Player operation limiter This patch is Powered by plusls-carpet-addition(https://github.com/plusls/plusls-carpet-addition) diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java index efd4c9882338559f8fba3b17919270f06ee5d8b6..c15eadd90e143a7b2a1974fff696b1b2426d3fb8 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayer.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java @@ -278,6 +278,10 @@ public class ServerPlayer extends Player { public final com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet cachedSingleHashSet; // Paper public PlayerNaturallySpawnCreaturesEvent playerNaturallySpawnedEvent; // Paper public org.bukkit.event.player.PlayerQuitEvent.QuitReason quitReason = null; // Paper - there are a lot of changes to do if we change all methods leading to the event + // Leaves start - player operation limiter + private int instaBreakCountPerTick = 0; + private int placeBlockCountPerTick = 0; + // Leaves end - player operation limiter public ServerPlayer(MinecraftServer server, ServerLevel world, GameProfile profile) { super(world, world.getSharedSpawnPos(), world.getSharedSpawnAngle(), profile); @@ -670,6 +674,7 @@ public class ServerPlayer extends Player { this.joining = false; } // CraftBukkit end + this.resetOperationCountPerTick(); // Leaves - player operation limiter this.gameMode.tick(); this.wardenSpawnTracker.tick(); --this.spawnInvulnerableTime; @@ -2571,5 +2576,32 @@ public class ServerPlayer extends Player { public CraftPlayer getBukkitEntity() { return (CraftPlayer) super.getBukkitEntity(); } + + // Leaves start - player operation limiter + private void resetOperationCountPerTick() { + instaBreakCountPerTick = 0; + placeBlockCountPerTick = 0; + } + + public int getInstaBreakCountPerTick() { + return instaBreakCountPerTick; + } + + public int getPlaceBlockCountPerTick() { + return placeBlockCountPerTick; + } + + public void addInstaBreakCountPerTick() { + ++instaBreakCountPerTick; + } + + public void addPlaceBlockCountPerTick() { + ++placeBlockCountPerTick; + } + + public boolean allowOperation() { + return (instaBreakCountPerTick == 0 || placeBlockCountPerTick == 0) && (instaBreakCountPerTick <= 1 && placeBlockCountPerTick <= 2); + } + // Leaves end - player operation limiter // CraftBukkit end } diff --git a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java index 1d33c02088c150189d7f4b0aa27f6a1de96b11cf..1d3b2a017b2fcabb1e19fcac7856d051932124ba 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java @@ -44,6 +44,7 @@ import org.bukkit.event.Event; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; +import top.leavesmc.leaves.event.player.PlayerOperationLimitEvent; // CraftBukkit end public class ServerPlayerGameMode { @@ -333,6 +334,19 @@ public class ServerPlayerGameMode { } public void destroyAndAck(BlockPos pos, int sequence, String reason) { + // Leaves start - player operation limiter + if (top.leavesmc.leaves.LeavesConfig.playerOperationLimiter) { + if (reason.equals("insta mine")) { + player.addInstaBreakCountPerTick(); + if (!player.allowOperation()) { + MinecraftServer.getServer().server.getPluginManager().callEvent(new PlayerOperationLimitEvent(player.getBukkitEntity(), PlayerOperationLimitEvent.Operation.MINE, CraftBlock.at(level, pos))); + this.player.connection.send(new ClientboundBlockUpdatePacket(pos, this.level.getBlockState(pos))); + this.debugLogging(pos, false, sequence, reason); + return; + } + } + } + // Leaves end - player operation limiter if (this.destroyBlock(pos)) { this.debugLogging(pos, true, sequence, reason); } else { diff --git a/src/main/java/net/minecraft/world/item/BlockItem.java b/src/main/java/net/minecraft/world/item/BlockItem.java index 1015b3c5f6969709bb8ebfbd66eb9cede444385c..437ed8d4cba5e3393ac6370f6b8e364e4b7d9042 100644 --- a/src/main/java/net/minecraft/world/item/BlockItem.java +++ b/src/main/java/net/minecraft/world/item/BlockItem.java @@ -32,9 +32,12 @@ import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.Property; import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.phys.shapes.CollisionContext; +import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.craftbukkit.block.CraftBlockState; import org.bukkit.craftbukkit.block.data.CraftBlockData; import org.bukkit.event.block.BlockCanBuildEvent; +import top.leavesmc.leaves.event.player.PlayerOperationLimitEvent; // CraftBukkit end public class BlockItem extends Item { @@ -83,6 +86,20 @@ public class BlockItem extends Item { final org.bukkit.block.BlockState oldBlockstate = blockstate != null ? blockstate : org.bukkit.craftbukkit.block.CraftBlockStates.getBlockState(blockactioncontext1.getLevel(), blockactioncontext1.getClickedPos()); // Paper // CraftBukkit end + // Leaves start - player operation limiter + if (top.leavesmc.leaves.LeavesConfig.playerOperationLimiter && !context.getLevel().isClientSide()) { + ServerPlayer player = (ServerPlayer) context.getPlayer(); + if (player != null && iblockdata != null) { + player.addPlaceBlockCountPerTick(); + if (!player.allowOperation()) { + if (blockstate != null) { + MinecraftServer.getServer().server.getPluginManager().callEvent(new PlayerOperationLimitEvent(player.getBukkitEntity(), PlayerOperationLimitEvent.Operation.PLACE, blockstate.getBlock())); + } + return InteractionResult.FAIL; + } + } + } + // Leaves end - player operation limiter if (iblockdata == null) { return InteractionResult.FAIL; } else if (!this.placeBlock(blockactioncontext1, iblockdata)) { diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index be07458a96a0e7da995ce349a70be9f1b648da23..dbcc4046b447ef6d5f22948552cc54afd9d1b6f5 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -443,6 +443,11 @@ public final class LeavesConfig { } } + public static boolean playerOperationLimiter = false; + private static void playerOperationLimiter() { + playerOperationLimiter = getBoolean("settings.modify.player-operation-limiter", playerOperationLimiter); + } + public static final class WorldConfig { public final String worldName;