Skip to content

Commit

Permalink
add 1.20.6-1.21 bookdupe prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Sep 10, 2024
1 parent cb4766e commit edbdefb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.xginko.aef.modules.dupepreventions;

import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEditBook;
import me.xginko.aef.modules.packets.PacketModule;
import me.xginko.aef.utils.PlatformUtil;

public class BookTitleDupe extends PacketModule {

private final int maxTitleChars;

public BookTitleDupe() {
super("dupe-preventions.book-title-dupe", PacketListenerPriority.HIGHEST);
config.addComment(configPath + ".enable", """
Relevant for 1.20.6 - 1.21:
Will prevent players from sending book packets with a too large title,
to get disconnected and their inventories restored.""");
this.maxTitleChars = config.getInt(configPath + ".max-title-charlength", 20, """
Normal ascii like charlimit in vanilla is 15, you could need a bit more
if you want to support languages with special characters.""");
}

@Override
public boolean shouldEnable() {
return config.getBoolean(configPath + ".enable", PlatformUtil.getMinecraftPatchVersion() > 19);
}

@Override
public void onPacketReceive(PacketReceiveEvent event) {
if (event.getPacketType() != PacketType.Play.Client.EDIT_BOOK) return;

WrapperPlayClientEditBook packet = new WrapperPlayClientEditBook(event);
if (packet.getTitle() == null) return;

if (packet.getTitle().length() > maxTitleChars) {
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.xginko.aef.modules.dupepreventions;

import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEditBook;
import me.xginko.aef.modules.packets.PacketModule;
import me.xginko.aef.utils.PlatformUtil;

public class BookTitleDupe extends PacketModule {

private final int maxTitleChars;

public BookTitleDupe() {
super("dupe-preventions.book-title-dupe", PacketListenerPriority.HIGHEST);
config.addComment(configPath + ".enable",
"Relevant for 1.20.6 - 1.21:\n" +
"Will prevent players from sending book packets with a too large title,\n" +
"to get disconnected and their inventories restored.");
this.maxTitleChars = config.getInt(configPath + ".max-title-charlength", 20,
"Normal ascii like charlimit in vanilla is 15, you could need a bit more\n" +
"if you want to support languages with special characters.");
}

@Override
public boolean shouldEnable() {
return config.getBoolean(configPath + ".enable", PlatformUtil.getMinecraftPatchVersion() > 19);
}

@Override
public void onPacketReceive(PacketReceiveEvent event) {
if (event.getPacketType() != PacketType.Play.Client.EDIT_BOOK) return;

WrapperPlayClientEditBook packet = new WrapperPlayClientEditBook(event);
if (packet.getTitle() == null) return;

if (packet.getTitle().length() > maxTitleChars) {
event.setCancelled(true);
}
}
}

0 comments on commit edbdefb

Please sign in to comment.