Added post-rollback sorting for player inventories

This commit is contained in:
Intelli 2022-03-20 17:34:24 -06:00
parent 840eb3b4ee
commit 5861570f8a
2 changed files with 56 additions and 9 deletions

View File

@ -1038,6 +1038,7 @@ public class Rollback extends Queue {
} }
chunkChanges.clear(); chunkChanges.clear();
Map<Player, List<Integer>> sortPlayers = new HashMap<>();
Object container = null; Object container = null;
Material containerType = null; Material containerType = null;
boolean containerInit = false; boolean containerInit = false;
@ -1101,7 +1102,14 @@ public class Rollback extends Queue {
if (rowAction == ItemLogger.ITEM_REMOVE_ENDER || rowAction == ItemLogger.ITEM_ADD_ENDER) { if (rowAction == ItemLogger.ITEM_REMOVE_ENDER || rowAction == ItemLogger.ITEM_ADD_ENDER) {
modifyContainerItems(containerType, player.getEnderChest(), (Integer) populatedStack[0], ((ItemStack) populatedStack[2]).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[2], action); int modifiedArmor = modifyContainerItems(containerType, player.getInventory(), (Integer) populatedStack[0], (ItemStack) populatedStack[2], action);
if (modifiedArmor > -1) {
List<Integer> currentSortList = sortPlayers.getOrDefault(player, new ArrayList<>());
if (!currentSortList.contains(modifiedArmor)) {
currentSortList.add(modifiedArmor);
}
sortPlayers.put(player, currentSortList);
}
itemCount1 = itemCount1 + rowAmount; itemCount1 = itemCount1 + rowAmount;
ConfigHandler.rollbackHash.put(finalUserString, new int[] { itemCount1, blockCount1, entityCount1, 0 }); ConfigHandler.rollbackHash.put(finalUserString, new int[] { itemCount1, blockCount1, entityCount1, 0 });
@ -1186,6 +1194,11 @@ public class Rollback extends Queue {
} }
itemData.clear(); itemData.clear();
for (Entry<Player, List<Integer>> sortEntry : sortPlayers.entrySet()) {
sortContainerItems(sortEntry.getKey().getInventory(), sortEntry.getValue());
}
sortPlayers.clear();
int[] rollbackHashData1 = ConfigHandler.rollbackHash.get(finalUserString); int[] rollbackHashData1 = ConfigHandler.rollbackHash.get(finalUserString);
int itemCount1 = rollbackHashData1[0]; int itemCount1 = rollbackHashData1[0];
int blockCount1 = rollbackHashData1[1]; int blockCount1 = rollbackHashData1[1];
@ -1567,7 +1580,8 @@ public class Rollback extends Queue {
} }
} }
static void modifyContainerItems(Material type, Object container, int slot, ItemStack itemstack, int action) { static int modifyContainerItems(Material type, Object container, int slot, ItemStack itemstack, int action) {
int modifiedArmor = -1;
try { try {
ItemStack[] contents = null; ItemStack[] contents = null;
@ -1629,7 +1643,9 @@ public class Rollback extends Queue {
while (count < amount) { while (count < amount) {
boolean addedItem = false; boolean addedItem = false;
if (isPlayerInventory) { if (isPlayerInventory) {
addedItem = Util.setPlayerArmor((PlayerInventory) inventory, itemstack); int setArmor = Util.setPlayerArmor((PlayerInventory) inventory, itemstack);
addedItem = (setArmor > -1);
modifiedArmor = addedItem ? setArmor : modifiedArmor;
} }
if (!addedItem) { if (!addedItem) {
addedItem = (inventory.addItem(itemstack).size() == 0); addedItem = (inventory.addItem(itemstack).size() == 0);
@ -1710,6 +1726,37 @@ public class Rollback extends Queue {
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return modifiedArmor;
}
public static void sortContainerItems(PlayerInventory inventory, List<Integer> modifiedArmorSlots) {
try {
ItemStack[] armorContents = inventory.getArmorContents();
ItemStack[] storageContents = inventory.getStorageContents();
for (int armor = 0; armor < armorContents.length; armor++) {
ItemStack armorItem = armorContents[armor];
if (armorItem == null || !modifiedArmorSlots.contains(armor)) {
continue;
}
for (int storage = 0; storage < storageContents.length; storage++) {
ItemStack storageItem = storageContents[storage];
if (storageItem == null) {
storageContents[storage] = armorItem;
armorContents[armor] = null;
break;
}
}
}
inventory.setArmorContents(armorContents);
inventory.setStorageContents(storageContents);
}
catch (Exception e) {
e.printStackTrace();
}
} }
private static void buildFireworkEffect(Builder effectBuilder, Material rowType, ItemStack itemstack) { private static void buildFireworkEffect(Builder effectBuilder, Material rowType, ItemStack itemstack) {

View File

@ -656,7 +656,7 @@ public class Util extends Queue {
return artname; return artname;
} }
public static boolean setPlayerArmor(PlayerInventory inventory, ItemStack itemStack) { public static int setPlayerArmor(PlayerInventory inventory, ItemStack itemStack) {
String itemName = itemStack.getType().name(); String itemName = itemStack.getType().name();
boolean isHelmet = (itemName.endsWith("_HELMET") || itemName.endsWith("_HEAD") || itemName.endsWith("_SKULL") || itemName.endsWith("_PUMPKIN")); boolean isHelmet = (itemName.endsWith("_HELMET") || itemName.endsWith("_HEAD") || itemName.endsWith("_SKULL") || itemName.endsWith("_PUMPKIN"));
boolean isChestplate = (itemName.endsWith("_CHESTPLATE")); boolean isChestplate = (itemName.endsWith("_CHESTPLATE"));
@ -665,22 +665,22 @@ public class Util extends Queue {
if (isHelmet && inventory.getHelmet() == null) { if (isHelmet && inventory.getHelmet() == null) {
inventory.setHelmet(itemStack); inventory.setHelmet(itemStack);
return true; return 3;
} }
else if (isChestplate && inventory.getChestplate() == null) { else if (isChestplate && inventory.getChestplate() == null) {
inventory.setChestplate(itemStack); inventory.setChestplate(itemStack);
return true; return 2;
} }
else if (isLeggings && inventory.getLeggings() == null) { else if (isLeggings && inventory.getLeggings() == null) {
inventory.setLeggings(itemStack); inventory.setLeggings(itemStack);
return true; return 1;
} }
else if (isBoots && inventory.getBoots() == null) { else if (isBoots && inventory.getBoots() == null) {
inventory.setBoots(itemStack); inventory.setBoots(itemStack);
return true; return 0;
} }
return false; return -1;
} }
public static ItemStack[] getArmorStandContents(EntityEquipment equipment) { public static ItemStack[] getArmorStandContents(EntityEquipment equipment) {