Skip to content

Commit

Permalink
Fixed item frame & armor stand logging under Folia
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed Mar 25, 2023
1 parent 7170e29 commit ace870b
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 75 deletions.
7 changes: 4 additions & 3 deletions src/main/java/net/coreprotect/database/Rollback.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.paper.PaperAdapter;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Chat;
Expand Down Expand Up @@ -650,7 +651,7 @@ else if ((rowType == Material.ARMOR_STAND)) {

if (!exists) {
Entity entity = block.getLocation().getWorld().spawnEntity(location1, EntityType.ARMOR_STAND);
entity.teleport(location1);
PaperAdapter.ADAPTER.teleportAsync(entity, location1);
}
}
else if ((rowType == Material.END_CRYSTAL)) {
Expand All @@ -671,7 +672,7 @@ else if ((rowType == Material.END_CRYSTAL)) {
Entity entity = block.getLocation().getWorld().spawnEntity(location1, EntityType.ENDER_CRYSTAL);
EnderCrystal enderCrystal = (EnderCrystal) entity;
enderCrystal.setShowingBottom((rowData != 0));
entity.teleport(location1);
PaperAdapter.ADAPTER.teleportAsync(entity, location1);
}
}
else if ((rowType == Material.AIR) && ((oldTypeMaterial == Material.WATER))) {
Expand Down Expand Up @@ -728,7 +729,7 @@ else if (BlockGroup.CONTAINERS.contains(Material.ARMOR_STAND)) {
}

entityLocation.setY(entityLocation.getY() - 1.99);
entity.teleport(entityLocation);
PaperAdapter.ADAPTER.teleportAsync(entity, entityLocation);
entity.remove();
}
}
Expand Down
20 changes: 6 additions & 14 deletions src/main/java/net/coreprotect/database/logger/ContainerLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.ItemFrame;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

Expand All @@ -34,19 +33,12 @@ public static void log(PreparedStatement preparedStmtContainer, PreparedStatemen
ItemStack[] contents = null;
String faceData = null;

if (type == Material.ARMOR_STAND) {
EntityEquipment equipment = (EntityEquipment) container;
if (equipment != null) {
contents = Util.getArmorStandContents(equipment);
}
}
else if (type == Material.ITEM_FRAME) {
ItemFrame itemFrame = (ItemFrame) container;
contents = Util.getItemFrameItem(itemFrame);
faceData = itemFrame.getFacing().name();
if (type == Material.ITEM_FRAME) {
contents = (ItemStack[]) ((Object[]) container)[1];
faceData = ((BlockFace) ((Object[]) container)[2]).name();
}
else if (type == Material.JUKEBOX) {
contents = new ItemStack[] { ((ItemStack[]) container)[1] };
else if (type == Material.JUKEBOX || type == Material.ARMOR_STAND) {
contents = (ItemStack[]) ((Object[]) container)[1];
}
else {
Inventory inventory = (Inventory) container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.inventory.ItemStack;

import net.coreprotect.config.Config;
import net.coreprotect.consumer.Queue;
Expand Down Expand Up @@ -45,7 +46,9 @@ protected void onEntityDamageByBlock(EntityDamageByBlockEvent event) {
if (entity instanceof ItemFrame && Config.getConfig(entity.getWorld()).ITEM_TRANSACTIONS) {
ItemFrame frame = (ItemFrame) entity;
if (frame.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueContainerSingleItem(user, Material.ITEM_FRAME, frame, frame.getLocation(), false);
ItemStack[] oldState = new ItemStack[] { frame.getItem().clone() };
ItemStack[] newState = new ItemStack[] { new ItemStack(Material.AIR) };
PlayerInteractEntityListener.queueContainerSpecifiedItems(user, Material.ITEM_FRAME, new Object[] { oldState, newState, frame.getFacing() }, frame.getLocation(), false);
}
}
else if (entity instanceof ArmorStand && Config.getConfig(entity.getWorld()).BLOCK_BREAK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ else if (damager.getType() != null) {
if (entity instanceof ItemFrame && Config.getConfig(entityLocation.getWorld()).ITEM_TRANSACTIONS) {
ItemFrame frame = (ItemFrame) entity;
if (frame.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueContainerSingleItem(user, Material.ITEM_FRAME, frame, frame.getLocation(), logDrops);
ItemStack[] oldState = new ItemStack[] { frame.getItem().clone() };
ItemStack[] newState = new ItemStack[] { new ItemStack(Material.AIR) };
PlayerInteractEntityListener.queueContainerSpecifiedItems(user, Material.ITEM_FRAME, new Object[] { oldState, newState, frame.getFacing() }, frame.getLocation(), logDrops);
}
}
else if (entity instanceof EnderCrystal && Config.getConfig(entity.getWorld()).BLOCK_BREAK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.inventory.ItemStack;

import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
Expand Down Expand Up @@ -133,7 +134,9 @@ else if (remover.getType() != null) {

if (!event.isCancelled() && Config.getConfig(entity.getWorld()).ITEM_TRANSACTIONS && !inspecting) {
if (itemframe.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueContainerSingleItem(culprit, Material.ITEM_FRAME, itemframe, itemframe.getLocation(), logDrops);
ItemStack[] oldState = new ItemStack[] { itemframe.getItem().clone() };
ItemStack[] newState = new ItemStack[] { new ItemStack(Material.AIR) };
PlayerInteractEntityListener.queueContainerSpecifiedItems(culprit, Material.ITEM_FRAME, new Object[] { oldState, newState, itemframe.getFacing() }, itemframe.getLocation(), logDrops);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.inventory.ItemStack;

import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
Expand Down Expand Up @@ -61,7 +62,9 @@ else if (cause.equals(HangingBreakEvent.RemoveCause.OBSTRUCTION)) {

if (!event.isCancelled() && Config.getConfig(entity.getWorld()).ITEM_TRANSACTIONS) {
if (itemframe.getItem().getType() != Material.AIR) {
PlayerInteractEntityListener.queueContainerSingleItem(causeName, Material.ITEM_FRAME, itemframe, itemframe.getLocation(), logDrops);
ItemStack[] oldState = new ItemStack[] { itemframe.getItem().clone() };
ItemStack[] newState = new ItemStack[] { new ItemStack(Material.AIR) };
PlayerInteractEntityListener.queueContainerSpecifiedItems(causeName, Material.ITEM_FRAME, new Object[] { oldState, newState, itemframe.getFacing() }, itemframe.getLocation(), logDrops);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -87,7 +84,8 @@ protected void onPlayerArmorStandManipulateEvent(PlayerArmorStandManipulateEvent
Player player = event.getPlayer();
final ArmorStand armorStand = event.getRightClicked();
EntityEquipment equipment = armorStand.getEquipment();
ItemStack[] contents = Util.getArmorStandContents(equipment);
ItemStack[] oldContents = Util.getArmorStandContents(equipment);
ItemStack[] newContents = oldContents.clone();
ItemStack item = event.getArmorStandItem();
ItemStack playerItem = event.getPlayerItem();

Expand All @@ -110,49 +108,45 @@ protected void onPlayerArmorStandManipulateEvent(PlayerArmorStandManipulateEvent
return;
}

if (item == null && playerItem == null) {
return;
int slot = 0;
switch (event.getSlot()) {
case LEGS:
slot = 1;
break;
case CHEST:
slot = 2;
break;
case HEAD:
slot = 3;
break;
case HAND:
slot = 4;
break;
case OFF_HAND:
slot = 5;
break;
default:
slot = 0;
}
// 0: BOOTS, 1: LEGGINGS, 2: CHESTPLATE, 3: HELMET, 4: MAINHAND, 5: OFFHAND

/*
if (item!=null && playerItem==null){
//player gets item
}
if (item==null && playerItem!=null){
//players item placed on armor stands
}
if (item!=null && playerItem!=null){
//items are swapped
}
*/

Material type = Material.ARMOR_STAND;
Location standLocation = armorStand.getLocation();
int x = standLocation.getBlockX();
int y = standLocation.getBlockY();
int z = standLocation.getBlockZ();

String transactingChestId = standLocation.getWorld().getUID().toString() + "." + x + "." + y + "." + z;
String loggingChestId = player.getName().toLowerCase(Locale.ROOT) + "." + x + "." + y + "." + z;
int chestId = Queue.getChestId(loggingChestId);
if (chestId > 0) {
if (ConfigHandler.forceContainer.get(loggingChestId) != null) {
int force_size = ConfigHandler.forceContainer.get(loggingChestId).size();
List<ItemStack[]> list = ConfigHandler.oldContainer.get(loggingChestId);

if (list.size() <= force_size) {
list.add(Util.getContainerState(contents));
ConfigHandler.oldContainer.put(loggingChestId, list);
}
}
if (item.getType() == playerItem.getType()) {
return;
}
else {
List<ItemStack[]> list = new ArrayList<>();
list.add(Util.getContainerState(contents));
ConfigHandler.oldContainer.put(loggingChestId, list);
else if (item.getType() != Material.AIR && playerItem.getType() == Material.AIR) {
oldContents[slot] = item.clone();
newContents[slot] = new ItemStack(Material.AIR);
PlayerInteractEntityListener.queueContainerSpecifiedItems(player.getName(), Material.ARMOR_STAND, new Object[] { oldContents, newContents }, armorStand.getLocation(), false);
}
else if (item.getType() == Material.AIR && playerItem.getType() != Material.AIR) {
oldContents[slot] = new ItemStack(Material.AIR);
newContents[slot] = playerItem.clone();
PlayerInteractEntityListener.queueContainerSpecifiedItems(player.getName(), Material.ARMOR_STAND, new Object[] { oldContents, newContents }, armorStand.getLocation(), false);
}
else if (item.getType() != Material.AIR && playerItem.getType() != Material.AIR) {
oldContents[slot] = item.clone();
newContents[slot] = playerItem.clone();
PlayerInteractEntityListener.queueContainerSpecifiedItems(player.getName(), Material.ARMOR_STAND, new Object[] { oldContents, newContents }, armorStand.getLocation(), false);
}

ConfigHandler.transactingChest.computeIfAbsent(transactingChestId, k -> Collections.synchronizedList(new ArrayList<>()));
Queue.queueContainerTransaction(player.getName(), standLocation, type, equipment, chestId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ protected void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
final Entity entity = event.getRightClicked(); // change item in ItemFrame, etc
if (entity instanceof ItemFrame) {
ItemFrame frame = (ItemFrame) entity;
Material handType = Material.AIR;
ItemStack handItem = new ItemStack(Material.AIR);
ItemStack mainHand = player.getInventory().getItemInMainHand();
ItemStack offHand = player.getInventory().getItemInOffHand();
if (event.getHand().equals(EquipmentSlot.HAND) && mainHand.getType() != Material.AIR) {
handType = mainHand.getType();
handItem = mainHand;
}
else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand.getType() != Material.AIR) {
handType = offHand.getType();
handItem = offHand;
}
else if (event.getHand().equals(EquipmentSlot.OFF_HAND)) {
return;
Expand All @@ -72,8 +72,10 @@ else if (event.getHand().equals(EquipmentSlot.OFF_HAND)) {
return;
}

if (frame.getItem().getType().equals(Material.AIR) && !handType.equals(Material.AIR)) {
queueContainerSingleItem(player.getName(), Material.ITEM_FRAME, frame, frame.getLocation(), false);
if (frame.getItem().getType().equals(Material.AIR) && !handItem.getType().equals(Material.AIR)) { // add item to item frame
ItemStack[] oldState = new ItemStack[] { new ItemStack(Material.AIR) };
ItemStack[] newState = new ItemStack[] { handItem.clone() };
queueContainerSpecifiedItems(player.getName(), Material.ITEM_FRAME, new Object[] { oldState, newState, frame.getFacing() }, frame.getLocation(), false);
}
}
else if (!event.isCancelled() && entity instanceof Creature && entity.getType().name().equals("ALLAY")) {
Expand Down Expand Up @@ -108,8 +110,8 @@ else if (handItem.getType().equals(Material.AIR)) {
}
}

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] };
public static void queueContainerSpecifiedItems(String user, Material type, Object container, Location location, boolean logDrop) {
ItemStack[] contents = (ItemStack[]) ((Object[]) container)[0];
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,9 @@ else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand != null && Ta

if (Config.getConfig(block.getWorld()).ITEM_TRANSACTIONS) {
boolean logDrops = player.getGameMode() != GameMode.CREATIVE;
PlayerInteractEntityListener.queueContainerSingleItem(player.getName(), Material.JUKEBOX, new ItemStack[] { oldItemState, newItemState }, jukebox.getLocation(), logDrops);
ItemStack[] oldState = new ItemStack[] { oldItemState };
ItemStack[] newState = new ItemStack[] { newItemState };
PlayerInteractEntityListener.queueContainerSpecifiedItems(player.getName(), Material.JUKEBOX, new Object[] { oldState, newState }, jukebox.getLocation(), logDrops);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/coreprotect/paper/PaperAdapter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.coreprotect.paper;

import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

Expand Down Expand Up @@ -62,4 +64,9 @@ public String getLine(Sign sign, int line) {
return sign.getLine(line);
}

@Override
public void teleportAsync(Entity entity, Location location) {
entity.teleport(location);
}

}
7 changes: 7 additions & 0 deletions src/main/java/net/coreprotect/paper/PaperHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.coreprotect.paper;

import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Entity;

public class PaperHandler extends PaperAdapter implements PaperInterface {

Expand All @@ -9,4 +11,9 @@ public boolean isStopping(Server server) {
return server.isStopping();
}

@Override
public void teleportAsync(Entity entity, Location location) {
entity.teleportAsync(location);
}

}
4 changes: 4 additions & 0 deletions src/main/java/net/coreprotect/paper/PaperInterface.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.coreprotect.paper;

import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

Expand All @@ -13,4 +15,6 @@ public interface PaperInterface {

public String getLine(Sign sign, int line);

public void teleportAsync(Entity entity, Location location);

}
4 changes: 2 additions & 2 deletions src/main/java/net/coreprotect/thread/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public static void scheduleSyncDelayedTask(CoreProtect plugin, Runnable task, Ob
else if (regionData instanceof Entity) { // ENTITY
Entity entity = (Entity) regionData;
if (delay == 0) {
entity.getScheduler().run(plugin, value -> task.run(), null);
entity.getScheduler().run(plugin, value -> task.run(), task);
}
else {
entity.getScheduler().runDelayed(plugin, value -> task.run(), null, delay);
entity.getScheduler().runDelayed(plugin, value -> task.run(), task, delay);
}
}
else { // GLOBAL
Expand Down

0 comments on commit ace870b

Please sign in to comment.