Skip to content
This repository was archived by the owner on Dec 16, 2024. It is now read-only.

Commit 71a54a2

Browse files
committed
support for even more versions + run command
1 parent bd588f6 commit 71a54a2

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>lol.hyper</groupId>
88
<artifactId>lecterncrashfix</artifactId>
9-
<version>1.1</version>
9+
<version>1.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>LecternCrashFix</name>

src/main/java/lol/hyper/lecterncrashfix/LecternCrashFix.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,32 @@
77
import com.comphenix.protocol.events.PacketEvent;
88
import lol.hyper.lecterncrashfix.wrapper.WrapperPlayClientWindowClick;
99
import org.bukkit.Bukkit;
10+
import org.bukkit.configuration.file.FileConfiguration;
11+
import org.bukkit.configuration.file.YamlConfiguration;
1012
import org.bukkit.entity.Player;
1113
import org.bukkit.event.inventory.InventoryType;
1214
import org.bukkit.inventory.InventoryView;
1315
import org.bukkit.plugin.java.JavaPlugin;
1416

17+
import java.io.File;
1518
import java.util.logging.Logger;
1619

1720
public final class LecternCrashFix extends JavaPlugin {
1821

1922
private final Logger logger = this.getLogger();
23+
final int CONFIG_VERSION = 1;
24+
private final File configFile = new File(this.getDataFolder(), "config.yml");
25+
public FileConfiguration config;
2026

2127
@Override
2228
public void onEnable() {
23-
String bukkitPackageName = Bukkit.getServer().getClass().getPackage().getName();
24-
String bukkitVersion = bukkitPackageName.substring(bukkitPackageName.lastIndexOf(".") + 1);
25-
int ver = Integer.parseInt(bukkitVersion.split("_")[1]);
29+
if (!configFile.exists()) {
30+
this.saveResource("config.yml", true);
31+
}
32+
config = YamlConfiguration.loadConfiguration(configFile);
33+
if (config.getInt("config-version") != CONFIG_VERSION) {
34+
logger.warning("Your config file is outdated! Please regenerate the config.");
35+
}
2636

2737
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(this, ListenerPriority.HIGHEST, PacketType.Play.Client.WINDOW_CLICK) {
2838
@Override
@@ -31,16 +41,37 @@ public void onPacketReceiving(PacketEvent event) {
3141
return;
3242
}
3343

34-
WrapperPlayClientWindowClick packet = new WrapperPlayClientWindowClick(event.getPacket(), ver);
44+
WrapperPlayClientWindowClick packet = new WrapperPlayClientWindowClick(event.getPacket());
3545
Player player = event.getPlayer();
3646
InventoryView inv = player.getOpenInventory();
3747
if (inv.getType() == InventoryType.LECTERN) {
3848
if (packet.getShift() == WrapperPlayClientWindowClick.InventoryClickType.QUICK_MOVE) {
3949
event.setCancelled(true);
4050
logger.warning(player.getName() + " tried to illegally click a slot in a lectern! Location: " + player.getLocation());
51+
if (config.getBoolean("run-command")) {
52+
runCommand(player);
53+
}
4154
}
4255
}
4356
}
4457
});
4558
}
59+
60+
private void runCommand(Player player) {
61+
String command = config.getString("command");
62+
if (command == null || command.isEmpty()) {
63+
return;
64+
}
65+
if (command.contains("%player%")) {
66+
command = command.replace("%player%", player.getName());
67+
}
68+
if (command.contains("%uuid%")) {
69+
command = command.replace("%uuid%", player.getUniqueId().toString());
70+
}
71+
if (command.contains("%location%")) {
72+
command = command.replace("%location%", player.getLocation().toString());
73+
}
74+
String finalCommand = command;
75+
Bukkit.getScheduler().runTaskLater(this, ()-> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), finalCommand), 20);
76+
}
4677
}

src/main/java/lol/hyper/lecterncrashfix/wrapper/WrapperPlayClientWindowClick.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@
2525

2626
public class WrapperPlayClientWindowClick extends AbstractPacket {
2727
public static final PacketType TYPE = PacketType.Play.Client.WINDOW_CLICK;
28-
private final int VERSION;
2928

30-
public WrapperPlayClientWindowClick(int version) {
29+
public WrapperPlayClientWindowClick() {
3130
super(new PacketContainer(TYPE), TYPE);
32-
VERSION = version;
3331
handle.getModifier().writeDefaults();
3432
}
3533

36-
public WrapperPlayClientWindowClick(PacketContainer packet, int version) {
34+
public WrapperPlayClientWindowClick(PacketContainer packet) {
3735
super(packet, TYPE);
38-
VERSION = version;
3936
}
4037

4138
/**
@@ -138,15 +135,18 @@ public void setClickedItem(ItemStack value) {
138135
}
139136

140137
public InventoryClickType getShift() {
141-
// index changes here because versions use different ones idk
142-
int index = 0;
143-
if (VERSION == 16) {
144-
index = 5;
138+
InventoryClickType inventoryClickType = null;
139+
// cycle through the packet
140+
// see if any data matches a click type
141+
// this should help with older versions
142+
for (int i = 0; i < 6; i++) {
143+
try {
144+
inventoryClickType = handle.getEnumModifier(InventoryClickType.class, i).read(0);
145+
} catch (Exception ignored) {
146+
// ignore any exceptions here
147+
}
145148
}
146-
if (VERSION >= 17) {
147-
index = 4;
148-
}
149-
return handle.getEnumModifier(InventoryClickType.class, index).read(0);
149+
return inventoryClickType;
150150
}
151151

152152
public void setShift(InventoryClickType value) {

src/main/resources/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Run a command when a player tries to do the exploit.
2+
run-command: true
3+
4+
# Command to run.
5+
# %player%
6+
# %location%
7+
# %uuid%
8+
command: "ban %player%"
9+
10+
config-version: 1

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: LecternCrashFix
22
version: '${project.version}'
33
main: lol.hyper.lecterncrashfix.LecternCrashFix
4-
api-version: 1.16
4+
api-version: 1.14
55
depend: [ProtocolLib]

0 commit comments

Comments
 (0)