77import com .comphenix .protocol .events .PacketEvent ;
88import lol .hyper .lecterncrashfix .wrapper .WrapperPlayClientWindowClick ;
99import org .bukkit .Bukkit ;
10+ import org .bukkit .configuration .file .FileConfiguration ;
11+ import org .bukkit .configuration .file .YamlConfiguration ;
1012import org .bukkit .entity .Player ;
1113import org .bukkit .event .inventory .InventoryType ;
1214import org .bukkit .inventory .InventoryView ;
1315import org .bukkit .plugin .java .JavaPlugin ;
1416
17+ import java .io .File ;
1518import java .util .logging .Logger ;
1619
1720public 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}
0 commit comments