Add logging and rollback support for jukebox transactions

This commit is contained in:
Intelli 2022-12-15 15:25:53 -07:00
parent 272ecae858
commit 9da1343f7f
10 changed files with 93 additions and 18 deletions

View File

@ -30,8 +30,10 @@ import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.Banner;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.CommandBlock;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Jukebox;
import org.bukkit.block.ShulkerBox;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.data.Bisected;
@ -1142,7 +1144,14 @@ public class Rollback extends Queue {
}
if (BlockGroup.CONTAINERS.contains(block.getType())) {
container = Util.getContainerInventory(block.getState(), false);
BlockState blockState = block.getState();
if (blockState instanceof Jukebox) {
container = blockState;
}
else {
container = Util.getContainerInventory(blockState, false);
}
containerType = block.getType();
}
else if (BlockGroup.CONTAINERS.contains(Material.ARMOR_STAND) || BlockGroup.CONTAINERS.contains(Material.ITEM_FRAME)) {
@ -1631,6 +1640,21 @@ public class Rollback extends Queue {
frame.setItem(itemstack);
}
}
else if (type != null && type.equals(Material.JUKEBOX)) {
Jukebox jukebox = (Jukebox) container;
if (jukebox != null) {
if (action == 1 && Tag.ITEMS_MUSIC_DISCS.isTagged(itemstack.getType())) {
itemstack.setAmount(1);
}
else {
itemstack.setType(Material.AIR);
itemstack.setAmount(0);
}
jukebox.setRecord(itemstack);
jukebox.update();
}
}
else {
Inventory inventory = (Inventory) container;
if (inventory != null) {

View File

@ -45,6 +45,9 @@ public class ContainerLogger extends Queue {
contents = Util.getItemFrameItem(itemFrame);
faceData = itemFrame.getFacing().name();
}
else if (type == Material.JUKEBOX) {
contents = new ItemStack[] { ((ItemStack[]) container)[1] };
}
else {
Inventory inventory = (Inventory) container;
if (inventory != null) {

View File

@ -45,7 +45,7 @@ public final class EntityDamageByBlockListener extends Queue implements Listener
if (entity instanceof ItemFrame && Config.getConfig(entity.getWorld()).ITEM_TRANSACTIONS) {
ItemFrame frame = (ItemFrame) entity;
if (frame.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueFrameTransaction(user, frame, false);
PlayerInteractEntityListener.queueContainerSingleItem(user, Material.ITEM_FRAME, frame, frame.getLocation(), false);
}
}
else if (entity instanceof ArmorStand && Config.getConfig(entity.getWorld()).BLOCK_BREAK) {

View File

@ -113,7 +113,7 @@ public final class EntityDamageByEntityListener extends Queue implements Listene
if (entity instanceof ItemFrame && Config.getConfig(entityLocation.getWorld()).ITEM_TRANSACTIONS) {
ItemFrame frame = (ItemFrame) entity;
if (frame.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueFrameTransaction(user, frame, logDrops);
PlayerInteractEntityListener.queueContainerSingleItem(user, Material.ITEM_FRAME, frame, frame.getLocation(), logDrops);
}
}
else if (entity instanceof EnderCrystal && Config.getConfig(entity.getWorld()).BLOCK_BREAK) {

View File

@ -133,7 +133,7 @@ public final class HangingBreakByEntityListener extends Queue implements Listene
if (!event.isCancelled() && Config.getConfig(entity.getWorld()).ITEM_TRANSACTIONS && !inspecting) {
if (itemframe.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueFrameTransaction(culprit, itemframe, logDrops);
PlayerInteractEntityListener.queueContainerSingleItem(culprit, Material.ITEM_FRAME, itemframe, itemframe.getLocation(), logDrops);
}
}
}

View File

@ -61,7 +61,7 @@ public final class HangingBreakListener extends Queue implements Listener {
if (!event.isCancelled() && Config.getConfig(entity.getWorld()).ITEM_TRANSACTIONS) {
if (itemframe.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueFrameTransaction(causeName, itemframe, logDrops);
PlayerInteractEntityListener.queueContainerSingleItem(causeName, Material.ITEM_FRAME, itemframe, itemframe.getLocation(), logDrops);
}
}
}

View File

@ -73,7 +73,7 @@ public final class PlayerInteractEntityListener extends Queue implements Listene
}
if (frame.getItem().getType().equals(Material.AIR) && !handType.equals(Material.AIR)) {
queueFrameTransaction(player.getName(), frame, false);
queueContainerSingleItem(player.getName(), Material.ITEM_FRAME, frame, frame.getLocation(), false);
}
}
else if (!event.isCancelled() && entity instanceof Creature && entity.getType().name().equals("ALLAY")) {
@ -108,15 +108,13 @@ public final class PlayerInteractEntityListener extends Queue implements Listene
}
}
public static void queueFrameTransaction(String user, ItemFrame frame, boolean logDrop) {
ItemStack[] contents = Util.getItemFrameItem(frame);
Material type = Material.ITEM_FRAME;
Location frameLocation = frame.getLocation();
int x = frameLocation.getBlockX();
int y = frameLocation.getBlockY();
int z = frameLocation.getBlockZ();
public static void queueContainerSingleItem(String user, Material type, Object container, Location location, boolean logDrop) {
ItemStack[] contents = type == Material.ITEM_FRAME ? Util.getItemFrameItem((ItemFrame) container) : new ItemStack[] { ((ItemStack[]) container)[0] };
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
String transactingChestId = frameLocation.getWorld().getUID().toString() + "." + x + "." + y + "." + z;
String transactingChestId = location.getWorld().getUID().toString() + "." + x + "." + y + "." + z;
String loggingChestId = user.toLowerCase(Locale.ROOT) + "." + x + "." + y + "." + z;
int chestId = Queue.getChestId(loggingChestId);
if (chestId > 0) {
@ -137,15 +135,15 @@ public final class PlayerInteractEntityListener extends Queue implements Listene
}
ConfigHandler.transactingChest.computeIfAbsent(transactingChestId, k -> Collections.synchronizedList(new ArrayList<>()));
Queue.queueContainerTransaction(user, frameLocation, type, frame, chestId);
Queue.queueContainerTransaction(user, location, type, container, chestId);
if (logDrop) {
ItemStack dropItem = frame.getItem();
ItemStack dropItem = contents[0];
if (dropItem.getType() == Material.AIR) {
return;
}
PlayerDropItemListener.playerDropItem(frame.getLocation(), user, dropItem);
PlayerDropItemListener.playerDropItem(location, user, dropItem);
}
}
}

View File

@ -19,6 +19,7 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Jukebox;
import org.bukkit.block.Sign;
import org.bukkit.block.data.Bisected;
import org.bukkit.block.data.Bisected.Half;
@ -623,6 +624,39 @@ public final class PlayerInteractListener extends Queue implements Listener {
isCake = type.name().endsWith(Material.CAKE.name());
}
else if (type == Material.JUKEBOX && Config.getConfig(block.getWorld()).ITEM_TRANSACTIONS) {
BlockState blockState = block.getState();
if (blockState instanceof Jukebox) {
Jukebox jukebox = (Jukebox) blockState;
ItemStack jukeboxRecord = jukebox.getRecord();
ItemStack oldItemState = jukeboxRecord.clone();
ItemStack newItemState = new ItemStack(Material.AIR);
if (jukeboxRecord.getType() == Material.AIR) {
ItemStack handItem = null;
ItemStack mainHand = player.getInventory().getItemInMainHand();
ItemStack offHand = player.getInventory().getItemInOffHand();
if (event.getHand().equals(EquipmentSlot.HAND) && mainHand != null && Tag.ITEMS_MUSIC_DISCS.isTagged(mainHand.getType())) {
handItem = mainHand;
}
else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand != null && Tag.ITEMS_MUSIC_DISCS.isTagged(offHand.getType())) {
handItem = offHand;
}
else {
return;
}
oldItemState = new ItemStack(Material.AIR);
newItemState = handItem.clone();
}
if (!oldItemState.equals(newItemState)) {
boolean logDrops = player.getGameMode() != GameMode.CREATIVE;
PlayerInteractEntityListener.queueContainerSingleItem(player.getName(), Material.JUKEBOX, new ItemStack[] { oldItemState, newItemState }, jukebox.getLocation(), logDrops);
}
}
}
if (isCake || type == Material.CAKE) {
boolean placeCandle = false;

View File

@ -17,7 +17,7 @@ public final class BlockGroup {
public static Set<Material> TRACK_BOTTOM = new HashSet<>(Arrays.asList());
public static Set<Material> TRACK_SIDE = new HashSet<>(Arrays.asList(Material.WALL_TORCH, Material.REDSTONE_WALL_TORCH, Material.RAIL, Material.POWERED_RAIL, Material.DETECTOR_RAIL, Material.ACTIVATOR_RAIL, Material.WHITE_BED, Material.ORANGE_BED, Material.MAGENTA_BED, Material.LIGHT_BLUE_BED, Material.YELLOW_BED, Material.LIME_BED, Material.PINK_BED, Material.GRAY_BED, Material.LIGHT_GRAY_BED, Material.CYAN_BED, Material.PURPLE_BED, Material.BLUE_BED, Material.BROWN_BED, Material.GREEN_BED, Material.RED_BED, Material.BLACK_BED, Material.LADDER, Material.ACACIA_WALL_SIGN, Material.BIRCH_WALL_SIGN, Material.DARK_OAK_WALL_SIGN, Material.JUNGLE_WALL_SIGN, Material.OAK_WALL_SIGN, Material.SPRUCE_WALL_SIGN, Material.VINE, Material.COCOA, Material.TRIPWIRE_HOOK, Material.WHITE_WALL_BANNER, Material.ORANGE_WALL_BANNER, Material.MAGENTA_WALL_BANNER, Material.LIGHT_BLUE_WALL_BANNER, Material.YELLOW_WALL_BANNER, Material.LIME_WALL_BANNER, Material.PINK_WALL_BANNER, Material.GRAY_WALL_BANNER, Material.LIGHT_GRAY_WALL_BANNER, Material.CYAN_WALL_BANNER, Material.PURPLE_WALL_BANNER, Material.BLUE_WALL_BANNER, Material.BROWN_WALL_BANNER, Material.GREEN_WALL_BANNER, Material.RED_WALL_BANNER, Material.BLACK_WALL_BANNER));
public static Set<Material> SHULKER_BOXES = new HashSet<>(Arrays.asList(Material.SHULKER_BOX, Material.BLACK_SHULKER_BOX, Material.BLUE_SHULKER_BOX, Material.BROWN_SHULKER_BOX, Material.CYAN_SHULKER_BOX, Material.GRAY_SHULKER_BOX, Material.GREEN_SHULKER_BOX, Material.LIGHT_BLUE_SHULKER_BOX, Material.LIME_SHULKER_BOX, Material.MAGENTA_SHULKER_BOX, Material.ORANGE_SHULKER_BOX, Material.PINK_SHULKER_BOX, Material.PURPLE_SHULKER_BOX, Material.RED_SHULKER_BOX, Material.LIGHT_GRAY_SHULKER_BOX, Material.WHITE_SHULKER_BOX, Material.YELLOW_SHULKER_BOX));
public static Set<Material> CONTAINERS = new HashSet<>(Arrays.asList(Material.DISPENSER, Material.CHEST, Material.FURNACE, Material.BREWING_STAND, Material.TRAPPED_CHEST, Material.HOPPER, Material.DROPPER, Material.ARMOR_STAND, Material.ITEM_FRAME, Material.SHULKER_BOX, Material.BLACK_SHULKER_BOX, Material.BLUE_SHULKER_BOX, Material.BROWN_SHULKER_BOX, Material.CYAN_SHULKER_BOX, Material.GRAY_SHULKER_BOX, Material.GREEN_SHULKER_BOX, Material.LIGHT_BLUE_SHULKER_BOX, Material.LIME_SHULKER_BOX, Material.MAGENTA_SHULKER_BOX, Material.ORANGE_SHULKER_BOX, Material.PINK_SHULKER_BOX, Material.PURPLE_SHULKER_BOX, Material.RED_SHULKER_BOX, Material.LIGHT_GRAY_SHULKER_BOX, Material.WHITE_SHULKER_BOX, Material.YELLOW_SHULKER_BOX, Material.BARREL, Material.BLAST_FURNACE, Material.SMOKER, Material.LECTERN));
public static Set<Material> CONTAINERS = new HashSet<>(Arrays.asList(Material.JUKEBOX, Material.DISPENSER, Material.CHEST, Material.FURNACE, Material.BREWING_STAND, Material.TRAPPED_CHEST, Material.HOPPER, Material.DROPPER, Material.ARMOR_STAND, Material.ITEM_FRAME, Material.SHULKER_BOX, Material.BLACK_SHULKER_BOX, Material.BLUE_SHULKER_BOX, Material.BROWN_SHULKER_BOX, Material.CYAN_SHULKER_BOX, Material.GRAY_SHULKER_BOX, Material.GREEN_SHULKER_BOX, Material.LIGHT_BLUE_SHULKER_BOX, Material.LIME_SHULKER_BOX, Material.MAGENTA_SHULKER_BOX, Material.ORANGE_SHULKER_BOX, Material.PINK_SHULKER_BOX, Material.PURPLE_SHULKER_BOX, Material.RED_SHULKER_BOX, Material.LIGHT_GRAY_SHULKER_BOX, Material.WHITE_SHULKER_BOX, Material.YELLOW_SHULKER_BOX, Material.BARREL, Material.BLAST_FURNACE, Material.SMOKER, Material.LECTERN));
public static Set<Material> DOORS = new HashSet<>(Arrays.asList(Material.OAK_DOOR, Material.SPRUCE_DOOR, Material.BIRCH_DOOR, Material.JUNGLE_DOOR, Material.ACACIA_DOOR, Material.DARK_OAK_DOOR));
public static Set<Material> BUTTONS = new HashSet<>(Arrays.asList(Material.STONE_BUTTON, Material.OAK_BUTTON, Material.ACACIA_BUTTON, Material.BIRCH_BUTTON, Material.DARK_OAK_BUTTON, Material.JUNGLE_BUTTON, Material.SPRUCE_BUTTON));
public static Set<Material> PRESSURE_PLATES = new HashSet<>(Arrays.asList(Material.STONE_PRESSURE_PLATE, Material.ACACIA_PRESSURE_PLATE, Material.BIRCH_PRESSURE_PLATE, Material.DARK_OAK_PRESSURE_PLATE, Material.HEAVY_WEIGHTED_PRESSURE_PLATE, Material.JUNGLE_PRESSURE_PLATE, Material.LIGHT_WEIGHTED_PRESSURE_PLATE, Material.OAK_PRESSURE_PLATE, Material.SPRUCE_PRESSURE_PLATE));

View File

@ -28,6 +28,7 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.CommandBlock;
import org.bukkit.block.Jukebox;
import org.bukkit.block.ShulkerBox;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.data.BlockData;
@ -719,6 +720,10 @@ public class Util extends Queue {
ItemFrame entity = (ItemFrame) container;
contents = Util.getItemFrameItem(entity);
}
else if (type == Material.JUKEBOX) {
Jukebox blockState = (Jukebox) ((Block) container).getState();
contents = Util.getJukeboxItem(blockState);
}
else {
Block block = (Block) container;
Inventory inventory = Util.getContainerInventory(block.getState(), true);
@ -796,6 +801,17 @@ public class Util extends Queue {
return contents;
}
public static ItemStack[] getJukeboxItem(Jukebox blockState) {
ItemStack[] contents = null;
try {
contents = new ItemStack[] { blockState.getRecord() };
}
catch (Exception e) {
e.printStackTrace();
}
return contents;
}
public static int getEntityId(EntityType type) {
return getEntityId(type.name(), true);
}