9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00
Files
LeavesMC/patches/server/0047-Player-operation-limiter.patch
2023-09-28 18:12:42 +08:00

134 lines
7.0 KiB
Diff

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 ae6fe20cac39766bd1cde1adfd93072ce4a8f44c..4f47b1123eb517ffda3c1a0e61434e459b38d679 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -273,6 +273,10 @@ public class ServerPlayer extends Player {
public final com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet<ServerPlayer> 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
// Paper start - replace player chunk loader
private final java.util.concurrent.atomic.AtomicReference<io.papermc.paper.chunk.system.RegionizedPlayerChunkLoader.ViewDistances> viewDistances = new java.util.concurrent.atomic.AtomicReference<>(new io.papermc.paper.chunk.system.RegionizedPlayerChunkLoader.ViewDistances(-1, -1, -1));
@@ -714,6 +718,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;
@@ -2657,5 +2662,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 b2c2bd5ec0afd479973f7237a5c610f21231c505..63f89a7fb6309c2d5b7a3f75fdb21d85ddbfa80a 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 {
@@ -334,6 +335,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 39a8fb7a73362beab1e4a53b6297a054a159bb80..e38967daceee9a510f0021ef43f6324478088603 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)) {