From 527be90249ec2f365219eba91640b77ff07ba4fc Mon Sep 17 00:00:00 2001 From: Intelli Date: Fri, 18 Feb 2022 17:31:35 -0700 Subject: [PATCH] Fixed item frame transactions not logging BlockFace data --- .../net/coreprotect/bukkit/Bukkit_v1_17.java | 2 +- .../database/ContainerRollback.java | 21 +++++++++++- .../net/coreprotect/database/Rollback.java | 32 +++++++++++++------ .../database/logger/ContainerBreakLogger.java | 2 +- .../database/logger/ContainerLogger.java | 11 ++++--- .../database/logger/ItemLogger.java | 2 +- .../listener/entity/EntityDeathListener.java | 4 +-- .../java/net/coreprotect/utility/Util.java | 12 +++---- .../utility/entity/EntityUtil.java | 4 +-- .../utility/serialize/ItemMetaHandler.java | 12 +++++-- 10 files changed, 72 insertions(+), 30 deletions(-) diff --git a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_17.java b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_17.java index 259bf26..0dfb626 100644 --- a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_17.java +++ b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_17.java @@ -123,7 +123,7 @@ public class Bukkit_v1_17 extends Bukkit_v1_16 implements BukkitInterface { if (subMeta.hasItems()) { list = new ArrayList<>(); for (ItemStack itemStack : subMeta.getItems()) { - Map itemMap = Util.serializeItemStack(itemStack, slot); + Map itemMap = Util.serializeItemStack(itemStack, null, slot); if (itemMap.size() > 0) { list.add(itemMap); } diff --git a/src/main/java/net/coreprotect/database/ContainerRollback.java b/src/main/java/net/coreprotect/database/ContainerRollback.java index 5415b09..8e062f1 100644 --- a/src/main/java/net/coreprotect/database/ContainerRollback.java +++ b/src/main/java/net/coreprotect/database/ContainerRollback.java @@ -1,6 +1,7 @@ package net.coreprotect.database; import java.sql.Statement; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -8,6 +9,7 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.command.CommandSender; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; @@ -60,6 +62,7 @@ public class ContainerRollback extends Queue { } Object container = null; Material type = block.getType(); + List matchingFrames = new ArrayList<>(); if (BlockGroup.CONTAINERS.contains(type)) { container = Util.getContainerInventory(block.getState(), false); @@ -74,6 +77,7 @@ public class ContainerRollback extends Queue { else if (entity instanceof ItemFrame) { type = Material.ITEM_FRAME; container = entity; + matchingFrames.add((ItemFrame) entity); } } } @@ -113,12 +117,27 @@ public class ContainerRollback extends Queue { ItemStack itemstack = new ItemStack(rowType, rowAmount, (short) rowData); Object[] populatedStack = Rollback.populateItemStack(itemstack, rowMetadata); int slot = (Integer) populatedStack[0]; - itemstack = (ItemStack) populatedStack[1]; + String faceData = (String) populatedStack[1]; + itemstack = (ItemStack) populatedStack[2]; + + if (type == Material.ITEM_FRAME && faceData.length() > 0) { + BlockFace blockFace = BlockFace.valueOf(faceData); + ItemFrame itemFrame = (ItemFrame) container; + if (blockFace != itemFrame.getFacing()) { + for (ItemFrame frame : matchingFrames) { + if (blockFace == frame.getFacing()) { + container = frame; + break; + } + } + } + } Rollback.modifyContainerItems(type, container, slot, itemstack, action); } } } + matchingFrames.clear(); ConfigHandler.rollbackHash.put(finalUserString, new int[] { itemCount, modifyCount, entityCount, 1 }); } diff --git a/src/main/java/net/coreprotect/database/Rollback.java b/src/main/java/net/coreprotect/database/Rollback.java index ad62773..687d552 100644 --- a/src/main/java/net/coreprotect/database/Rollback.java +++ b/src/main/java/net/coreprotect/database/Rollback.java @@ -1010,6 +1010,7 @@ public class Rollback extends Queue { int lastY = 0; int lastZ = 0; int lastWorldId = 0; + String lastFace = ""; for (Object[] row : itemData) { int[] rollbackHashData1 = ConfigHandler.rollbackHash.get(finalUserString); @@ -1028,7 +1029,7 @@ public class Rollback extends Queue { byte[] rowMetadata = (byte[]) row[12]; Material rowType = Util.getType(rowTypeRaw); - if ((rollbackType == 0 && rowRolledBack == 0) || (rollbackType == 1 && rowRolledBack == 1)) { + if (rowType != null && ((rollbackType == 0 && rowRolledBack == 0) || (rollbackType == 1 && rowRolledBack == 1))) { if (inventoryRollback) { int rowUserId = (Integer) row[2]; String rowUser = ConfigHandler.playerIdCacheReversed.get(rowUserId); @@ -1061,9 +1062,9 @@ public class Rollback extends Queue { ItemStack itemstack = new ItemStack(rowType, rowAmount, (short) rowData); Object[] populatedStack = populateItemStack(itemstack, rowMetadata); if (rowAction == ItemLogger.ITEM_REMOVE_ENDER || rowAction == ItemLogger.ITEM_ADD_ENDER) { - modifyContainerItems(containerType, player.getEnderChest(), (Integer) populatedStack[0], ((ItemStack) populatedStack[1]).clone(), action ^ 1); + modifyContainerItems(containerType, player.getEnderChest(), (Integer) populatedStack[0], ((ItemStack) populatedStack[2]).clone(), action ^ 1); } - modifyContainerItems(containerType, player.getInventory(), (Integer) populatedStack[0], (ItemStack) populatedStack[1], action); + modifyContainerItems(containerType, player.getInventory(), (Integer) populatedStack[0], (ItemStack) populatedStack[2], action); itemCount1 = itemCount1 + rowAmount; ConfigHandler.rollbackHash.put(finalUserString, new int[] { itemCount1, blockCount1, entityCount1, 0 }); @@ -1074,7 +1075,11 @@ public class Rollback extends Queue { continue; // skip inventory & ender chest transactions } - if (!containerInit || rowX != lastX || rowY != lastY || rowZ != lastZ || rowWorldId != lastWorldId) { + ItemStack itemstack = new ItemStack(rowType, rowAmount, (short) rowData); + Object[] populatedStack = populateItemStack(itemstack, rowMetadata); + String faceData = (String) populatedStack[1]; + + if (!containerInit || rowX != lastX || rowY != lastY || rowZ != lastZ || rowWorldId != lastWorldId || !faceData.equals(lastFace)) { container = null; // container patch 2.14.0 String world = Util.getWorldName(rowWorldId); if (world.length() == 0) { @@ -1095,6 +1100,7 @@ public class Rollback extends Queue { containerType = block.getType(); } else if (BlockGroup.CONTAINERS.contains(Material.ARMOR_STAND) || BlockGroup.CONTAINERS.contains(Material.ITEM_FRAME)) { + BlockFace blockFace = BlockFace.valueOf(faceData); for (Entity entity : block.getChunk().getEntities()) { if (entity.getLocation().getBlockX() == rowX && entity.getLocation().getBlockY() == rowY && entity.getLocation().getBlockZ() == rowZ) { if (entity instanceof ArmorStand) { @@ -1104,6 +1110,9 @@ public class Rollback extends Queue { else if (entity instanceof ItemFrame) { container = entity; containerType = Material.ITEM_FRAME; + if (blockFace == ((ItemFrame) entity).getFacing()) { + break; + } } } } @@ -1113,9 +1122,10 @@ public class Rollback extends Queue { lastY = rowY; lastZ = rowZ; lastWorldId = rowWorldId; + lastFace = faceData; } - if (container != null && rowType != null) { + if (container != null) { int action = 0; if (rollbackType == 0 && rowAction == 0) { action = 1; @@ -1125,10 +1135,8 @@ public class Rollback extends Queue { action = 1; } - ItemStack itemstack = new ItemStack(rowType, rowAmount, (short) rowData); - Object[] populatedStack = populateItemStack(itemstack, rowMetadata); int slot = (Integer) populatedStack[0]; - itemstack = (ItemStack) populatedStack[1]; + itemstack = (ItemStack) populatedStack[2]; modifyContainerItems(containerType, container, slot, itemstack, action); itemCount1 = itemCount1 + rowAmount; @@ -1652,6 +1660,7 @@ public class Rollback extends Queue { @SuppressWarnings("unchecked") public static Object[] populateItemStack(ItemStack itemstack, Object list) { int slot = 0; + String faceData = ""; try { /* @@ -1682,6 +1691,9 @@ public class Rollback extends Queue { if (mapData.get("slot") != null) { slot = (Integer) mapData.get("slot"); } + else if (mapData.get("facing") != null) { + faceData = (String) mapData.get("facing"); + } else if (mapData.get("modifiers") != null) { ItemMeta itemMeta = itemstack.getItemMeta(); if (itemMeta.hasAttributeModifiers()) { @@ -1798,7 +1810,7 @@ public class Rollback extends Queue { catch (Exception e) { e.printStackTrace(); } - return new Object[] { slot, itemstack }; + return new Object[] { slot, faceData, itemstack }; } public static Object[] populateItemStack(ItemStack itemstack, byte[] metadata) { @@ -1817,7 +1829,7 @@ public class Rollback extends Queue { } } - return new Object[] { 0, itemstack }; + return new Object[] { 0, "", itemstack }; } } diff --git a/src/main/java/net/coreprotect/database/logger/ContainerBreakLogger.java b/src/main/java/net/coreprotect/database/logger/ContainerBreakLogger.java index 752e8eb..31e33dc 100644 --- a/src/main/java/net/coreprotect/database/logger/ContainerBreakLogger.java +++ b/src/main/java/net/coreprotect/database/logger/ContainerBreakLogger.java @@ -19,7 +19,7 @@ public class ContainerBreakLogger { public static void log(PreparedStatement preparedStmt, int batchCount, String player, Location l, Material type, ItemStack[] oldInventory) { try { Util.mergeItems(type, oldInventory); - ContainerLogger.logTransaction(preparedStmt, batchCount, player, type, oldInventory, 0, l); + ContainerLogger.logTransaction(preparedStmt, batchCount, player, type, null, oldInventory, 0, l); String loggingContainerId = player.toLowerCase(Locale.ROOT) + "." + l.getBlockX() + "." + l.getBlockY() + "." + l.getBlockZ(); // If there was a pending chest transaction, it would have already been processed. diff --git a/src/main/java/net/coreprotect/database/logger/ContainerLogger.java b/src/main/java/net/coreprotect/database/logger/ContainerLogger.java index 0b6312b..4df28c7 100644 --- a/src/main/java/net/coreprotect/database/logger/ContainerLogger.java +++ b/src/main/java/net/coreprotect/database/logger/ContainerLogger.java @@ -32,6 +32,8 @@ public class ContainerLogger extends Queue { public static void log(PreparedStatement preparedStmtContainer, PreparedStatement preparedStmtItems, int batchCount, String player, Material type, Object container, Location location) { try { ItemStack[] contents = null; + String faceData = null; + if (type == Material.ARMOR_STAND) { EntityEquipment equipment = (EntityEquipment) container; if (equipment != null) { @@ -41,6 +43,7 @@ public class ContainerLogger extends Queue { else if (type == Material.ITEM_FRAME) { ItemFrame itemFrame = (ItemFrame) container; contents = Util.getItemFrameItem(itemFrame); + faceData = itemFrame.getFacing().name(); } else { Inventory inventory = (Inventory) container; @@ -138,8 +141,8 @@ public class ContainerLogger extends Queue { Util.mergeItems(type, newInventory); if (type != Material.ENDER_CHEST) { - logTransaction(preparedStmtContainer, batchCount, player, type, oldInventory, 0, location); - logTransaction(preparedStmtContainer, batchCount, player, type, newInventory, 1, location); + logTransaction(preparedStmtContainer, batchCount, player, type, faceData, oldInventory, 0, location); + logTransaction(preparedStmtContainer, batchCount, player, type, faceData, newInventory, 1, location); } else { // pass ender chest transactions to item logger ItemLogger.logTransaction(preparedStmtItems, batchCount, player, location, oldInventory, ItemLogger.ITEM_REMOVE_ENDER); @@ -154,7 +157,7 @@ public class ContainerLogger extends Queue { } } - protected static void logTransaction(PreparedStatement preparedStmt, int batchCount, String user, Material type, ItemStack[] items, int action, Location location) { + protected static void logTransaction(PreparedStatement preparedStmt, int batchCount, String user, Material type, String faceData, ItemStack[] items, int action, Location location) { try { if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) { return; @@ -164,7 +167,7 @@ public class ContainerLogger extends Queue { if (item != null) { if (item.getAmount() > 0 && !Util.isAir(item.getType())) { // Object[] metadata = new Object[] { slot, item.getItemMeta() }; - List>> metadata = ItemMetaHandler.seralize(item, type, slot); + List>> metadata = ItemMetaHandler.seralize(item, type, faceData, slot); if (metadata.size() == 0) { metadata = null; } diff --git a/src/main/java/net/coreprotect/database/logger/ItemLogger.java b/src/main/java/net/coreprotect/database/logger/ItemLogger.java index c82c5a9..f640b19 100644 --- a/src/main/java/net/coreprotect/database/logger/ItemLogger.java +++ b/src/main/java/net/coreprotect/database/logger/ItemLogger.java @@ -63,7 +63,7 @@ public class ItemLogger { for (ItemStack item : items) { if (item != null && item.getAmount() > 0 && !Util.isAir(item.getType())) { // Object[] metadata = new Object[] { slot, item.getItemMeta() }; - List>> data = ItemMetaHandler.seralize(item, null, 0); + List>> data = ItemMetaHandler.seralize(item, null, null, 0); if (data.size() == 0) { data = null; } diff --git a/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java b/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java index c044137..794cebb 100644 --- a/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java +++ b/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java @@ -317,7 +317,7 @@ public final class EntityDeathListener extends Queue implements Listener { List ingredients = new ArrayList<>(); List itemMap = new ArrayList<>(); ItemStack item = merchantRecipe.getResult().clone(); - List>> metadata = ItemMetaHandler.seralize(item, item.getType(), 0); + List>> metadata = ItemMetaHandler.seralize(item, item.getType(), null, 0); item.setItemMeta(null); itemMap.add(item.serialize()); itemMap.add(metadata); @@ -329,7 +329,7 @@ public final class EntityDeathListener extends Queue implements Listener { for (ItemStack ingredient : merchantRecipe.getIngredients()) { itemMap = new ArrayList<>(); item = ingredient.clone(); - metadata = ItemMetaHandler.seralize(item, item.getType(), 0); + metadata = ItemMetaHandler.seralize(item, item.getType(), null, 0); item.setItemMeta(null); itemMap.add(item.serialize()); itemMap.add(metadata); diff --git a/src/main/java/net/coreprotect/utility/Util.java b/src/main/java/net/coreprotect/utility/Util.java index 486e39f..96ee70c 100755 --- a/src/main/java/net/coreprotect/utility/Util.java +++ b/src/main/java/net/coreprotect/utility/Util.java @@ -1220,9 +1220,9 @@ public class Util extends Queue { return newVersion(convertArray(oldVersionSplit), convertArray(currentVersionSplit)); } - public static Map serializeItemStackLegacy(ItemStack itemStack, int slot) { + public static Map serializeItemStackLegacy(ItemStack itemStack, String faceData, int slot) { Map result = new HashMap<>(); - Map itemMap = serializeItemStack(itemStack, slot); + Map itemMap = serializeItemStack(itemStack, faceData, slot); if (itemMap.size() > 1) { result.put(0, itemMap.get("0")); result.put(1, itemMap.get("1")); @@ -1245,11 +1245,11 @@ public class Util extends Queue { return result; } - public static Map serializeItemStack(ItemStack itemStack, int slot) { + public static Map serializeItemStack(ItemStack itemStack, String faceData, int slot) { Map itemMap = new HashMap<>(); if (itemStack != null && !itemStack.getType().equals(Material.AIR)) { ItemStack item = itemStack.clone(); - List>> metadata = ItemMetaHandler.seralize(item, null, slot); + List>> metadata = ItemMetaHandler.seralize(item, null, faceData, slot); item.setItemMeta(null); itemMap.put("0", item.serialize()); itemMap.put("1", metadata); @@ -1269,7 +1269,7 @@ public class Util extends Queue { List>> metadata = (List>>) itemMap.get("1"); Object[] populatedStack = Rollback.populateItemStack(item, metadata); - result = (ItemStack) populatedStack[1]; + result = (ItemStack) populatedStack[2]; } return result; @@ -1298,7 +1298,7 @@ public class Util extends Queue { ItemStack[] inventory = shulkerBox.getSnapshotInventory().getStorageContents(); int slot = 0; for (ItemStack itemStack : inventory) { - Map itemMap = serializeItemStackLegacy(itemStack, slot); + Map itemMap = serializeItemStackLegacy(itemStack, null, slot); if (itemMap.size() > 0) { meta.add(itemMap); } diff --git a/src/main/java/net/coreprotect/utility/entity/EntityUtil.java b/src/main/java/net/coreprotect/utility/entity/EntityUtil.java index cf23678..6fe3da8 100644 --- a/src/main/java/net/coreprotect/utility/entity/EntityUtil.java +++ b/src/main/java/net/coreprotect/utility/entity/EntityUtil.java @@ -350,7 +350,7 @@ public class EntityUtil { @SuppressWarnings("unchecked") List>> metadata = (List>>) itemMap.get(1); Object[] populatedStack = Rollback.populateItemStack(result, metadata); - result = (ItemStack) populatedStack[1]; + result = (ItemStack) populatedStack[2]; int uses = (int) recipe.get(1); int maxUses = (int) recipe.get(2); boolean experienceReward = (boolean) recipe.get(3); @@ -365,7 +365,7 @@ public class EntityUtil { @SuppressWarnings("unchecked") List>> itemMetaData = (List>>) ingredientMap.get(1); populatedStack = Rollback.populateItemStack(item, itemMetaData); - item = (ItemStack) populatedStack[1]; + item = (ItemStack) populatedStack[2]; merchantIngredients.add(item); } MerchantRecipe merchantRecipe = new MerchantRecipe(result, uses, maxUses, experienceReward); diff --git a/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java b/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java index 419c8a5..40e8bb2 100644 --- a/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java +++ b/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java @@ -109,7 +109,7 @@ public class ItemMetaHandler { return result.toString(); } - public static List>> seralize(ItemStack item, Material type, int slot) { + public static List>> seralize(ItemStack item, Material type, String faceData, int slot) { List>> metadata = new ArrayList<>(); List> list = new ArrayList<>(); List modifiers = new ArrayList<>(); @@ -210,7 +210,7 @@ public class ItemMetaHandler { list = new ArrayList<>(); for (ItemStack chargedProjectile : subMeta.getChargedProjectiles()) { - Map itemMap = Util.serializeItemStack(chargedProjectile, slot); + Map itemMap = Util.serializeItemStack(chargedProjectile, null, slot); if (itemMap.size() > 0) { list.add(itemMap); } @@ -246,6 +246,14 @@ public class ItemMetaHandler { metadata.add(list); } + if (faceData != null && faceData.length() > 0) { + Map meta = new HashMap<>(); + meta.put("facing", faceData); + list = new ArrayList<>(); + list.add(meta); + metadata.add(list); + } + if (modifiers.size() > 0) { Map meta = new HashMap<>(); meta.put("modifiers", modifiers);