Merge branch 'ks-hl:main' into feature/folia-support

This commit is contained in:
adabugra
2025-06-16 22:52:38 +03:00
committed by GitHub
6 changed files with 85 additions and 63 deletions

5
.gitignore vendored
View File

@@ -1,10 +1,11 @@
**/target/
/test_run/
.idea*
auxprotect.iml
AuxProtect.iml
*.iml
.classpath
.project
*.bat
test_run/
dependency-reduced-pom.xml
/AuxProtect_Private
/AuxProtect_Private*

View File

@@ -41,6 +41,7 @@ import org.bukkit.inventory.ItemStack;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -235,10 +236,19 @@ public class InvCommand extends Command {
}
if (entry.getAction().equals(EntryAction.INVENTORY)) {
PlayerInventoryRecord inv_;
byte[] blob;
try {
inv_ = InvSerialization.toPlayerInventory(entry.getBlob());
blob = entry.getBlob();
} catch (Exception e1) {
plugin.warning("Error serializing inventory lookup");
plugin.warning("Error getting blob from inventory entry");
plugin.print(e1);
sender.sendLang(Language.L.ERROR);
return;
}
try {
inv_ = InvSerialization.toPlayerInventory(blob);
} catch (Exception e1) {
plugin.warning("Error deserializing inventory: " + Base64.getEncoder().encodeToString(blob));
plugin.print(e1);
sender.sendLang(Language.L.ERROR);
return;

View File

@@ -620,9 +620,9 @@ public final class AuxProtectSpigot extends JavaPlugin implements IAuxProtect {
@Override
public void print(Throwable t) {
getLogger().log(Level.WARNING, t.getMessage(), t);
String stack = StackUtil.format(t, 3);
if (stackHashHistory.add(stack.hashCode())) {
stack = StackUtil.format(t, 20);
String stack = StackUtil.format(t, Integer.MAX_VALUE);
if (!stackHashHistory.add(stack.hashCode())) {
stack = StackUtil.format(t, 3);
}
logToStackLog(stack);
}

View File

@@ -1,6 +1,6 @@
package dev.heliosares.auxprotect.utils;
import dev.heliosares.auxprotect.spigot.AuxProtectSpigot;
import dev.heliosares.auxprotect.api.AuxProtectAPI;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@@ -13,6 +13,10 @@ import org.bukkit.util.io.BukkitObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class InvSerialization {
@@ -55,10 +59,7 @@ public class InvSerialization {
} catch (Exception ignored) {
//Allows reading a single item as an array
}
if (size < 1) {
AuxProtectSpigot.getInstance().warning("Empty BLOB");
return new ItemStack[0];
}
if (size < 1) return new ItemStack[0];
ItemStack[] arrayOfItemStack = new ItemStack[size];
for (int i = 0; i < arrayOfItemStack.length; i++) {
@@ -112,36 +113,29 @@ public class InvSerialization {
if (player == null) {
return null;
}
IntSet[] skip = new IntSet[4];
for (int i = 0; i < skip.length; i++) {
skip[i] = new IntSet();
}
return playerToByteArray(new PlayerInventoryRecord(player.getInventory().getStorageContents(),
player.getInventory().getArmorContents(), player.getInventory().getExtraContents(),
player.getEnderChest().getContents(), Experience.getTotalExp(player)));
player.getEnderChest().getContents(), Experience.getTotalExp(player)), player.getName(), skip, 100);
}
public static byte[] playerToByteArray(PlayerInventoryRecord record) throws IOException {
if (record == null) {
return null;
}
private static byte[] playerToByteArray(PlayerInventoryRecord record, String playerName, IntSet[] skip, int maxRecursion) throws IOException {
if (record == null) return null;
if (maxRecursion <= 0) return null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (BukkitObjectOutputStream stream = new BukkitObjectOutputStream(byteArrayOutputStream)) {
stream.writeInt(record.storage().length);
for (ItemStack item : record.storage()) {
stream.writeObject(item);
}
stream.writeInt(record.armor().length);
for (ItemStack item : record.armor()) {
stream.writeObject(item);
}
stream.writeInt(record.extra().length);
for (ItemStack item : record.extra()) {
stream.writeObject(item);
}
stream.writeInt(record.ender().length);
for (ItemStack item : record.ender()) {
stream.writeObject(item);
try {
writeInventory(stream, record.storage(), playerName, "main inventory", skip[0]);
writeInventory(stream, record.armor(), playerName, "armor", skip[1]);
writeInventory(stream, record.extra(), playerName, "offhand/extra", skip[2]);
writeInventory(stream, record.ender(), playerName, "ender chest", skip[3]);
} catch (ItemSerializationException e) {
return playerToByteArray(record, playerName, skip, maxRecursion - 1);
}
stream.writeInt(record.exp());
@@ -151,35 +145,43 @@ public class InvSerialization {
}
}
public static void debug(byte[] bytes) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
System.out.println("Debug Byte[] Dump:");
try (BukkitObjectInputStream stream = new BukkitObjectInputStream(byteArrayInputStream)) {
boolean keep = true;
while (keep) {
keep = false;
try {
System.out.println(stream.readInt());
keep = true;
} catch (Exception ignored) {
}
try {
Object o = stream.readObject();
String out = "null";
if (o != null) {
out = o.toString();
if (out.length() > 50) {
out = out.substring(0, 50);
}
}
System.out.println(out);
keep = true;
} catch (Exception ignored) {
}
private static final Map<String, Long> spamMap = new HashMap<>();
private static void writeInventory(BukkitObjectOutputStream stream, ItemStack[] items, String playerName, String inventoryName, IntSet skip) throws IOException {
stream.writeInt(items.length);
for (int i = 0; i < items.length; i++) {
if (skip.contains(i)) {
stream.writeObject(null);
continue;
}
} catch (Exception ignored) {
ItemStack item = items[i];
writeItem(stream, item, playerName, inventoryName, i, skip);
}
}
private static class IntSet extends HashSet<Integer> {
}
private static class ItemSerializationException extends IOException {
public ItemSerializationException(Throwable cause) {
super(cause);
}
}
private static void writeItem(BukkitObjectOutputStream stream, ItemStack item, String playerName, String inventoryName, int index, Set<Integer> skip) throws IOException {
try {
stream.writeObject(item);
} catch (Throwable e) {
skip.add(index);
String msg = "Failed to serialize item in " + inventoryName + ", slot " + index + " of " + playerName + ". This is not an issue with AuxProtect, it is Bukkit failing to serialize the item correctly. The item will be logged as an empty slot. (" + e.getClass().getName() + ": " + e.getMessage() + ")";
spamMap.values().removeIf(l -> System.currentTimeMillis() - l > 60000L);
if (!spamMap.containsKey(msg)) {
spamMap.put(msg, System.currentTimeMillis());
AuxProtectAPI.warning(msg);
AuxProtectAPI.print(e);
}
throw new ItemSerializationException(e);
}
System.out.println("EOF");
}
public static PlayerInventoryRecord toPlayerInventory(byte[] bytes) throws IOException, ClassNotFoundException {

View File

@@ -23,3 +23,12 @@ maven { url 'https://jitpack.io' }
implementation 'com.github.Heliosares:AuxProtect:1.3.1'
```
## Contributors
Thank you for considering helping out. :)
Please base any PRs off of the `refactor-modules` branch, that is my working branch and has pretty significant structure changes.
### Formatting
- Ask, before you work on a PR, if the change is something I would implement. I don't want you to waste time if it's not something I want added.
- Please only change code that is necessary to your PR.
- Do not use wildcard imports. (e.g. `java.util.*`)
- Try to match the formatting of the rest of the code. (IntelliJ default autoformat with the exception of wildcard imports)

View File

@@ -18,6 +18,6 @@
<properties>
<jars>${env.USERPROFILE}/eclipse-workspace/jars</jars>
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
<revision>1.3.2</revision>
<revision>1.3.4-pre2</revision>
</properties>
</project>