Skip to content

Commit

Permalink
make beehive module more secure against NPEs
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 27, 2023
1 parent 4067a65 commit 45bf84e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.moomoo.anarchyexploitfixes.modules.patches;

import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTCompoundList;
import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
Expand Down Expand Up @@ -37,7 +39,8 @@ public class BeehiveCoordinates implements AnarchyExploitFixesModule, Listener {
public BeehiveCoordinates() {
this.plugin = AnarchyExploitFixes.getInstance();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("patches.remove-beehive-coordinates.enable", "Patches a coordinate exploit by looking at Beehive NBT data using hacked clients.");
config.addComment("patches.remove-beehive-coordinates.enable",
"Patches a coordinate exploit by looking at Beehive NBT data using hacked clients.");
this.logIsEnabled = config.getBoolean("patches.remove-beehive-coordinates.log", false);
config.getList("patches.remove-beehive-coordinates.beehive-types-to-check",
List.of("BEEHIVE", "BEE_NEST")
Expand Down Expand Up @@ -83,18 +86,25 @@ private void handleBeehiveIfPresent(ItemStack item) {
NBTItem nbtBeehive = new NBTItem(item);
if (!nbtBeehive.hasTag("BlockEntityTag")) return;

if (logIsEnabled) moduleLog(Level.INFO, name(), "Found Beehive Item with stored Bees, removing coordinate data.");
NBTCompound blockEntityCompound = nbtBeehive.getCompound("BlockEntityTag");
if (blockEntityCompound == null) return;

NBTCompoundList beeList = blockEntityCompound.getCompoundList("Bees");
if (beeList == null) return;

for (ReadWriteNBT nbtBee : beeList) {
ReadWriteNBT beeEntityData = nbtBee.getCompound("EntityData");
if (beeEntityData == null) continue;

nbtBeehive.getCompound("BlockEntityTag").getCompoundList("Bees").forEach(bee -> {
ReadWriteNBT beeEntityData = bee.getCompound("EntityData");
beeEntityData.removeKey("FlowerPos");
beeEntityData.removeKey("Paper.Origin");
beeEntityData.removeKey("Paper.OriginWorld");
beeEntityData.removeKey("WorldUUIDMost");
beeEntityData.removeKey("WorldUUIDLeast");
});
}

nbtBeehive.applyNBT(item);
if (logIsEnabled) moduleLog(Level.INFO, name(), "Found and removed coordinate data from beehive.");
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.moomoo.anarchyexploitfixes.modules.patches;

import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTCompoundList;
import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
Expand Down Expand Up @@ -37,11 +39,11 @@ public class BeehiveCoordinates implements AnarchyExploitFixesModule, Listener {
public BeehiveCoordinates() {
this.plugin = AnarchyExploitFixes.getInstance();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("patches.remove-beehive-coordinates.enable", "Patches a coordinate exploit by looking at Beehive NBT data using hacked clients.");
config.addComment("patches.remove-beehive-coordinates.enable",
"Patches a coordinate exploit by looking at Beehive NBT data using hacked clients.");
this.logIsEnabled = config.getBoolean("patches.remove-beehive-coordinates.log", false);
List<String> configuredStorageTypes = config.getList("patches.remove-beehive-coordinates.beehive-types-to-check", Arrays.asList(
"BEEHIVE", "BEE_NEST"
));
List<String> configuredStorageTypes = config.getList("patches.remove-beehive-coordinates.beehive-types-to-check",
Arrays.asList("BEEHIVE", "BEE_NEST"));
try {
Class.forName("org.bukkit.block.Beehive");
this.serverHasBeehives = true;
Expand Down Expand Up @@ -85,18 +87,24 @@ private void handleBeehiveIfPresent(ItemStack item) {
NBTItem nbtBeehive = new NBTItem(item);
if (!nbtBeehive.hasTag("BlockEntityTag")) return;

if (logIsEnabled) moduleLog(Level.INFO, name(), "Found Beehive Item with stored Bees, removing coordinate data.");
NBTCompound blockEntityCompound = nbtBeehive.getCompound("BlockEntityTag");
if (blockEntityCompound == null) return;

for (ReadWriteNBT nbtBee : nbtBeehive.getCompound("BlockEntityTag").getCompoundList("Bees")) {
NBTCompoundList beeList = blockEntityCompound.getCompoundList("Bees");
if (beeList == null) return;

for (ReadWriteNBT nbtBee : beeList) {
ReadWriteNBT beeEntityData = nbtBee.getCompound("EntityData");
if (beeEntityData == null) continue;

beeEntityData.removeKey("FlowerPos");
beeEntityData.removeKey("Paper.Origin");
beeEntityData.removeKey("Paper.OriginWorld");
beeEntityData.removeKey("WorldUUIDMost");
beeEntityData.removeKey("WorldUUIDLeast");
}

nbtBeehive.applyNBT(item);
if (logIsEnabled) moduleLog(Level.INFO, name(), "Found and removed coordinate data from beehive.");
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
Expand Down

0 comments on commit 45bf84e

Please sign in to comment.