Added logging and inventory rollback support for thrown/shot items

This commit is contained in:
Intelli 2022-02-22 19:26:17 -07:00
parent 0d7fa2dd59
commit b96f47cbac
19 changed files with 271 additions and 105 deletions

View File

@ -883,6 +883,10 @@ public class LookupCommand {
selector = (daction == 4 ? Selector.FIRST : Selector.SECOND);
tag = (daction == 4 ? Color.GREEN + "+" : Color.RED + "-");
}
else if (daction == 6 || daction == 7) { // LOOKUP_PROJECTILE
selector = Selector.SECOND;
tag = Color.RED + "-";
}
else { // LOOKUP_CONTAINER
selector = (daction == 0 ? Selector.FIRST : Selector.SECOND);
tag = (daction == 0 ? Color.GREEN + "+" : Color.RED + "-");
@ -968,6 +972,12 @@ public class LookupCommand {
tag = (daction != 4 ? Color.RED + "-" : Color.GREEN + "+");
action = "a:item";
}
else if (daction == 6 || daction == 7) {
phrase = Phrase.LOOKUP_PROJECTILE; // {threw|shot}
selector = (daction != 7 ? Selector.FIRST : Selector.SECOND);
tag = Color.RED + "-";
action = "a:item";
}
else {
phrase = Phrase.LOOKUP_CONTAINER; // {added|removed}
selector = (daction != 0 ? Selector.FIRST : Selector.SECOND);

View File

@ -88,8 +88,10 @@ public class ConfigHandler extends Queue {
public static Map<String, Integer> loggingItem = syncMap();
public static ConcurrentHashMap<String, List<Object>> transactingChest = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack[]>> oldContainer = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack>> itemsDrop = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack>> itemsPickup = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack>> itemsDrop = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack>> itemsThrown = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack>> itemsShot = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, Object[]> hopperAbort = new ConcurrentHashMap<>();
public static Map<String, List<ItemStack[]>> forceContainer = syncMap();
public static Map<String, Integer> lookupType = syncMap();

View File

@ -224,10 +224,10 @@ public class Queue {
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, location);
}
protected static void queueItemTransaction(String user, Location location, int time, int itemId) {
protected static void queueItemTransaction(String user, Location location, int time, int offset, int itemId) {
int currentConsumer = Consumer.currentConsumer;
int consumerId = Consumer.newConsumerId(currentConsumer);
addConsumer(currentConsumer, new Object[] { consumerId, Process.ITEM_TRANSACTION, null, 0, null, time, itemId, null });
addConsumer(currentConsumer, new Object[] { consumerId, Process.ITEM_TRANSACTION, null, offset, null, time, itemId, null });
queueStandardData(consumerId, currentConsumer, new String[] { user, null }, location);
}

View File

@ -11,26 +11,28 @@ import net.coreprotect.database.logger.ItemLogger;
class ItemTransactionProcess extends Queue {
static void process(PreparedStatement preparedStmt, int batchCount, int processId, int id, int forceData, int time, String user, Object object) {
static void process(PreparedStatement preparedStmt, int batchCount, int processId, int id, int forceData, int time, int offset, String user, Object object) {
if (object instanceof Location) {
Location location = (Location) object;
String loggingItemId = user.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
if (ConfigHandler.loggingItem.get(loggingItemId) != null) {
int current_chest = ConfigHandler.loggingItem.get(loggingItemId);
if (ConfigHandler.itemsDrop.get(loggingItemId) == null && ConfigHandler.itemsPickup.get(loggingItemId) == null) {
if (ConfigHandler.itemsPickup.get(loggingItemId) == null && ConfigHandler.itemsDrop.get(loggingItemId) == null && ConfigHandler.itemsThrown.get(loggingItemId) == null && ConfigHandler.itemsShot.get(loggingItemId) == null) {
return;
}
if (current_chest == forceData) {
int currentTime = (int) (System.currentTimeMillis() / 1000L);
if (currentTime > time) {
ItemLogger.log(preparedStmt, batchCount, location, user);
ConfigHandler.itemsDrop.remove(loggingItemId);
ItemLogger.log(preparedStmt, batchCount, location, offset, user);
ConfigHandler.itemsPickup.remove(loggingItemId);
ConfigHandler.itemsDrop.remove(loggingItemId);
ConfigHandler.itemsThrown.remove(loggingItemId);
ConfigHandler.itemsShot.remove(loggingItemId);
ConfigHandler.loggingItem.remove(loggingItemId);
}
else {
Queue.queueItemTransaction(user, location, time, forceData);
Queue.queueItemTransaction(user, location, time, offset, forceData);
}
}
}

View File

@ -150,7 +150,7 @@ public class Process {
ContainerTransactionProcess.process(preparedStmtContainers, preparedStmtItems, i, processId, id, blockType, forceData, user, object);
break;
case Process.ITEM_TRANSACTION:
ItemTransactionProcess.process(preparedStmtItems, i, processId, id, forceData, replaceData, user, object);
ItemTransactionProcess.process(preparedStmtItems, i, processId, id, forceData, replaceData, blockData, user, object);
break;
case Process.STRUCTURE_GROWTH:
StructureGrowthProcess.process(statement, preparedStmtBlocks, i, processId, id, user, object, forceData);

View File

@ -475,12 +475,16 @@ public class Lookup extends Queue {
if (actionTarget == ItemLogger.ITEM_ADD) {
actionText.append(",").append(ItemLogger.ITEM_DROP);
actionText.append(",").append(ItemLogger.ITEM_ADD_ENDER);
actionText.append(",").append(ItemLogger.ITEM_THROW);
actionText.append(",").append(ItemLogger.ITEM_SHOOT);
}
}
// If just looking up drops/pickups, include ender chest transactions
else if (actionList.contains(11) && !actionList.contains(4)) {
if (actionTarget == ItemLogger.ITEM_DROP) {
actionText.append(",").append(ItemLogger.ITEM_ADD_ENDER);
actionText.append(",").append(ItemLogger.ITEM_THROW);
actionText.append(",").append(ItemLogger.ITEM_SHOOT);
}
if (actionTarget == ItemLogger.ITEM_PICKUP) {
actionText.append(",").append(ItemLogger.ITEM_REMOVE_ENDER);

View File

@ -1054,7 +1054,7 @@ public class Rollback extends Queue {
}
int inventoryAction = 0;
if (rowAction == ItemLogger.ITEM_DROP || rowAction == ItemLogger.ITEM_PICKUP) {
if (rowAction == ItemLogger.ITEM_DROP || rowAction == ItemLogger.ITEM_PICKUP || rowAction == ItemLogger.ITEM_THROW || rowAction == ItemLogger.ITEM_SHOOT) {
inventoryAction = (rowAction == ItemLogger.ITEM_PICKUP ? 1 : 0);
}
else if (rowAction == ItemLogger.ITEM_REMOVE_ENDER || rowAction == ItemLogger.ITEM_ADD_ENDER) {

View File

@ -145,8 +145,8 @@ public class ContainerLogger extends Queue {
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);
ItemLogger.logTransaction(preparedStmtItems, batchCount, player, location, newInventory, ItemLogger.ITEM_ADD_ENDER);
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, player, location, oldInventory, ItemLogger.ITEM_REMOVE_ENDER);
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, player, location, newInventory, ItemLogger.ITEM_ADD_ENDER);
}
oldList.remove(0);

View File

@ -25,12 +25,14 @@ public class ItemLogger {
public static final int ITEM_PICKUP = 3;
public static final int ITEM_REMOVE_ENDER = 4;
public static final int ITEM_ADD_ENDER = 5;
public static final int ITEM_THROW = 6;
public static final int ITEM_SHOOT = 7;
private ItemLogger() {
throw new IllegalStateException("Database class");
}
public static void log(PreparedStatement preparedStmt, int batchCount, Location location, String user) {
public static void log(PreparedStatement preparedStmt, int batchCount, Location location, int offset, String user) {
try {
if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) {
return;
@ -38,27 +40,41 @@ public class ItemLogger {
String loggingItemId = user.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
List<ItemStack> dropList = ConfigHandler.itemsDrop.getOrDefault(loggingItemId, new ArrayList<>());
ItemStack[] itemDrops = new ItemStack[dropList.size()];
itemDrops = dropList.toArray(itemDrops);
dropList.clear();
List<ItemStack> pickupList = ConfigHandler.itemsPickup.getOrDefault(loggingItemId, new ArrayList<>());
ItemStack[] itemPickups = new ItemStack[pickupList.size()];
itemPickups = pickupList.toArray(itemPickups);
pickupList.clear();
Util.mergeItems(null, itemDrops);
List<ItemStack> dropList = ConfigHandler.itemsDrop.getOrDefault(loggingItemId, new ArrayList<>());
ItemStack[] itemDrops = new ItemStack[dropList.size()];
itemDrops = dropList.toArray(itemDrops);
dropList.clear();
List<ItemStack> thrownList = ConfigHandler.itemsThrown.getOrDefault(loggingItemId, new ArrayList<>());
ItemStack[] itemThrows = new ItemStack[thrownList.size()];
itemThrows = thrownList.toArray(itemThrows);
thrownList.clear();
List<ItemStack> shotList = ConfigHandler.itemsShot.getOrDefault(loggingItemId, new ArrayList<>());
ItemStack[] itemShots = new ItemStack[shotList.size()];
itemShots = shotList.toArray(itemShots);
shotList.clear();
Util.mergeItems(null, itemPickups);
logTransaction(preparedStmt, batchCount, user, location, itemDrops, ITEM_DROP);
logTransaction(preparedStmt, batchCount, user, location, itemPickups, ITEM_PICKUP);
Util.mergeItems(null, itemDrops);
Util.mergeItems(null, itemThrows);
Util.mergeItems(null, itemShots);
logTransaction(preparedStmt, batchCount, offset, user, location, itemPickups, ITEM_PICKUP);
logTransaction(preparedStmt, batchCount, offset, user, location, itemDrops, ITEM_DROP);
logTransaction(preparedStmt, batchCount, offset, user, location, itemThrows, ITEM_THROW);
logTransaction(preparedStmt, batchCount, offset, user, location, itemShots, ITEM_SHOOT);
}
catch (Exception e) {
e.printStackTrace();
}
}
protected static void logTransaction(PreparedStatement preparedStmt, int batchCount, String user, Location location, ItemStack[] items, int action) {
protected static void logTransaction(PreparedStatement preparedStmt, int batchCount, int offset, String user, Location location, ItemStack[] items, int action) {
try {
for (ItemStack item : items) {
if (item != null && item.getAmount() > 0 && !Util.isAir(item.getType())) {
@ -73,7 +89,7 @@ public class ItemLogger {
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
int time = (int) (System.currentTimeMillis() / 1000L);
int time = (int) (System.currentTimeMillis() / 1000L) - offset;
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();

View File

@ -140,6 +140,7 @@ public class Language {
phrases.put(Phrase.LOOKUP_ITEM, "{0} {picked up|dropped} {1} {2}.");
phrases.put(Phrase.LOOKUP_LOGIN, "{0} logged {in|out}.");
phrases.put(Phrase.LOOKUP_PAGE, "Page {0}");
phrases.put(Phrase.LOOKUP_PROJECTILE, "{0} {threw|shot} {1} {2}.");
phrases.put(Phrase.LOOKUP_ROWS_FOUND, "{0} {row|rows} found.");
phrases.put(Phrase.LOOKUP_SEARCHING, "Lookup searching. Please wait...");
phrases.put(Phrase.LOOKUP_STORAGE, "{0} {deposited|withdrew} {1} {2}.");

View File

@ -121,6 +121,7 @@ public enum Phrase {
LOOKUP_ITEM,
LOOKUP_LOGIN,
LOOKUP_PAGE,
LOOKUP_PROJECTILE,
LOOKUP_ROWS_FOUND,
LOOKUP_SEARCHING,
LOOKUP_STORAGE,

View File

@ -41,6 +41,7 @@ import net.coreprotect.listener.player.PlayerInteractListener;
import net.coreprotect.listener.player.PlayerJoinListener;
import net.coreprotect.listener.player.PlayerQuitListener;
import net.coreprotect.listener.player.PlayerTakeLecternBookListener;
import net.coreprotect.listener.player.ProjectileLaunchListener;
import net.coreprotect.listener.player.SignChangeListener;
import net.coreprotect.listener.world.LeavesDecayListener;
import net.coreprotect.listener.world.PortalCreateListener;
@ -97,6 +98,7 @@ public final class ListenerHandler {
pluginManager.registerEvents(new SignChangeListener(), plugin);
pluginManager.registerEvents(new PlayerInteractListener(), plugin);
pluginManager.registerEvents(new PlayerTakeLecternBookListener(), plugin);
pluginManager.registerEvents(new ProjectileLaunchListener(), plugin);
// World Listeners
pluginManager.registerEvents(new StructureGrowListener(), plugin);

View File

@ -13,15 +13,18 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockIgniteEvent.IgniteCause;
import org.bukkit.inventory.ItemStack;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.logger.ItemLogger;
import net.coreprotect.listener.player.ProjectileLaunchListener;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.utility.Util;
public final class BlockIgniteListener extends Queue implements Listener {
public final class BlockIgniteListener extends ProjectileLaunchListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
protected void onBlockIgnite(BlockIgniteEvent event) {
@ -77,6 +80,10 @@ public final class BlockIgniteListener extends Queue implements Listener {
Queue.queueBlockPlace("#fire", block.getState(), block.getType(), replacedBlock, blockIgnited, -1, 0, forceBlockData.getAsString());
}
else {
if (event.getCause() == IgniteCause.FIREBALL) {
playerLaunchProjectile(event.getPlayer().getLocation(), event.getPlayer().getName(), new ItemStack(Material.FIRE_CHARGE), 1, -1, 1, ItemLogger.ITEM_THROW);
}
Player player = event.getPlayer();
Queue.queueBlockPlace(player.getName(), block.getState(), block.getType(), replacedBlock, blockIgnited, -1, 0, forceBlockData.getAsString());
int unixtimestamp = (int) (System.currentTimeMillis() / 1000L);

View File

@ -15,7 +15,9 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.inventory.ItemStack;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue;
import net.coreprotect.listener.block.BlockUtil;
@ -25,24 +27,27 @@ public final class CreatureSpawnListener extends Queue implements Listener {
@EventHandler
public void onCreatureSpawn(CreatureSpawnEvent event) {
if (event.isCancelled()) {
if (event.isCancelled() || !event.getEntityType().equals(EntityType.ARMOR_STAND)) {
return;
}
if (event.getEntityType().equals(EntityType.ARMOR_STAND)) {
World world = event.getEntity().getWorld();
Location location = event.getEntity().getLocation();
String key = world.getName() + "-" + location.getBlockX() + "-" + location.getBlockY() + "-" + location.getBlockZ();
Iterator<Entry<UUID, Object[]>> it = ConfigHandler.entityBlockMapper.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<UUID, Object[]> pair = it.next();
UUID uuid = pair.getKey();
Object[] data = pair.getValue();
if ((data[0].equals(key) || data[1].equals(key)) && Util.getEntityMaterial(event.getEntityType()).equals(data[2])) {
Player player = Bukkit.getServer().getPlayer(uuid);
Block gravityLocation = BlockUtil.gravityScan(location, Material.ARMOR_STAND, player.getName());
Queue.queueBlockPlace(player.getName(), gravityLocation.getState(), location.getBlock().getType(), location.getBlock().getState(), (Material) data[2], (int) event.getEntity().getLocation().getYaw(), 1, null);
it.remove();
}
World world = event.getEntity().getWorld();
if (!Config.getConfig(world).BLOCK_PLACE) {
return;
}
Location location = event.getEntity().getLocation();
String key = world.getName() + "-" + location.getBlockX() + "-" + location.getBlockY() + "-" + location.getBlockZ();
Iterator<Entry<UUID, Object[]>> it = ConfigHandler.entityBlockMapper.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<UUID, Object[]> pair = it.next();
UUID uuid = pair.getKey();
Object[] data = pair.getValue();
if ((data[0].equals(key) || data[1].equals(key)) && Util.getEntityMaterial(event.getEntityType()) == ((ItemStack) data[2]).getType()) {
Player player = Bukkit.getServer().getPlayer(uuid);
Block gravityLocation = BlockUtil.gravityScan(location, Material.ARMOR_STAND, player.getName());
Queue.queueBlockPlace(player.getName(), gravityLocation.getState(), location.getBlock().getType(), location.getBlock().getState(), ((ItemStack) data[2]).getType(), (int) event.getEntity().getLocation().getYaw(), 1, null);
it.remove();
}
}
}

View File

@ -33,7 +33,7 @@ public class EntityPickupItemListener extends Queue implements Listener {
ConfigHandler.itemsPickup.put(loggingItemId, list);
int time = (int) (System.currentTimeMillis() / 1000L) + 1;
Queue.queueItemTransaction(player.getName(), location.clone(), time, itemId);
Queue.queueItemTransaction(player.getName(), location.clone(), time, 0, itemId);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)

View File

@ -18,7 +18,7 @@ import net.coreprotect.consumer.Queue;
public final class PlayerDropItemListener extends Queue implements Listener {
public static void playerDropItem(Location location, String user, ItemStack itemStack) {
protected static void playerDropItem(Location location, String user, ItemStack itemStack) {
if (!Config.getConfig(location.getWorld()).ITEM_DROPS || itemStack == null) {
return;
}
@ -31,7 +31,7 @@ public final class PlayerDropItemListener extends Queue implements Listener {
ConfigHandler.itemsDrop.put(loggingItemId, list);
int time = (int) (System.currentTimeMillis() / 1000L) + 1;
Queue.queueItemTransaction(user, location.clone(), time, itemId);
Queue.queueItemTransaction(user, location.clone(), time, 0, itemId);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)

View File

@ -528,12 +528,12 @@ public final class PlayerInteractListener extends Queue implements Listener {
}
}
}
else if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
else if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
Player player = event.getPlayer();
Block block = event.getClickedBlock();
World world = player.getWorld();
if (block != null) {
if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && block != null) {
final Material type = block.getType();
if (event.useInteractedBlock() != Event.Result.DENY) {
boolean isCake = false;
@ -647,74 +647,85 @@ public final class PlayerInteractListener extends Queue implements Listener {
CacheHandler.interactCache.put(coordinates, new Object[] { time, Material.CAKE, block.getState() });
}
}
}
}
if (event.useItemInHand() != Event.Result.DENY && Config.getConfig(world).BLOCK_PLACE) {
List<Material> entityBlockTypes = Arrays.asList(Material.ARMOR_STAND, Material.END_CRYSTAL);
Material handType = null;
ItemStack mainHand = player.getInventory().getItemInMainHand();
ItemStack offHand = player.getInventory().getItemInOffHand();
if (event.useItemInHand() != Event.Result.DENY) {
List<Material> entityBlockTypes = Arrays.asList(Material.ARMOR_STAND, Material.END_CRYSTAL, Material.TRIDENT, Material.EXPERIENCE_BOTTLE, Material.SPLASH_POTION, Material.ENDER_PEARL, Material.FIREWORK_ROCKET);
ItemStack handItem = null;
ItemStack mainHand = player.getInventory().getItemInMainHand();
ItemStack offHand = player.getInventory().getItemInOffHand();
if (event.getHand().equals(EquipmentSlot.HAND) && mainHand != null && entityBlockTypes.contains(mainHand.getType())) {
handType = mainHand.getType();
if (event.getHand().equals(EquipmentSlot.HAND) && mainHand != null && entityBlockTypes.contains(mainHand.getType())) {
handItem = mainHand;
}
else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand != null && entityBlockTypes.contains(offHand.getType())) {
handItem = offHand;
}
else {
return;
}
if (handItem.getType().equals(Material.END_CRYSTAL)) {
if (block != null && Config.getConfig(world).BLOCK_PLACE && (block.getType().equals(Material.OBSIDIAN) || block.getType().equals(Material.BEDROCK))) {
Location crystalLocation = block.getLocation().clone();
crystalLocation.setY(crystalLocation.getY() + 1);
boolean exists = false;
for (Entity entity : crystalLocation.getChunk().getEntities()) {
if (entity instanceof EnderCrystal && entity.getLocation().getBlockX() == crystalLocation.getBlockX() && entity.getLocation().getBlockY() == crystalLocation.getBlockY() && entity.getLocation().getBlockZ() == crystalLocation.getBlockZ()) {
exists = true;
break;
}
}
else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand != null && entityBlockTypes.contains(offHand.getType())) {
handType = offHand.getType();
if (!exists) {
final Player playerFinal = player;
final Location locationFinal = crystalLocation;
Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> {
try {
boolean blockExists = false;
int showingBottom = 0;
for (Entity entity : locationFinal.getChunk().getEntities()) {
if (entity instanceof EnderCrystal && entity.getLocation().getBlockX() == locationFinal.getBlockX() && entity.getLocation().getBlockY() == locationFinal.getBlockY() && entity.getLocation().getBlockZ() == locationFinal.getBlockZ()) {
EnderCrystal enderCrystal = (EnderCrystal) entity;
showingBottom = enderCrystal.isShowingBottom() ? 1 : 0;
blockExists = true;
break;
}
}
if (blockExists) {
Queue.queueBlockPlace(playerFinal.getName(), locationFinal.getBlock().getState(), locationFinal.getBlock().getType(), locationFinal.getBlock().getState(), Material.END_CRYSTAL, showingBottom, 1, null);
}
}
catch (Exception e) {
e.printStackTrace();
}
});
}
else {
}
}
else {
Location relativeBlockLocation = player.getLocation().clone();
relativeBlockLocation.setY(relativeBlockLocation.getY() + 1);
Location blockLocation = relativeBlockLocation.clone();
blockLocation.setY(blockLocation.getY() + 1);
if (handItem.getType() == Material.ARMOR_STAND || handItem.getType() == Material.FIREWORK_ROCKET) {
if (block == null) {
return;
}
if (handType.equals(Material.END_CRYSTAL)) {
Location crystalLocation = block.getLocation();
if (crystalLocation.getBlock().getType().equals(Material.OBSIDIAN) || crystalLocation.getBlock().getType().equals(Material.BEDROCK)) {
crystalLocation.setY(crystalLocation.getY() + 1);
boolean exists = false;
for (Entity entity : crystalLocation.getChunk().getEntities()) {
if (entity instanceof EnderCrystal && entity.getLocation().getBlockX() == crystalLocation.getBlockX() && entity.getLocation().getBlockY() == crystalLocation.getBlockY() && entity.getLocation().getBlockZ() == crystalLocation.getBlockZ()) {
exists = true;
break;
}
}
if (!exists) {
final Player playerFinal = player;
final Location locationFinal = crystalLocation;
Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> {
try {
boolean blockExists = false;
int showingBottom = 0;
for (Entity entity : locationFinal.getChunk().getEntities()) {
if (entity instanceof EnderCrystal && entity.getLocation().getBlockX() == locationFinal.getBlockX() && entity.getLocation().getBlockY() == locationFinal.getBlockY() && entity.getLocation().getBlockZ() == locationFinal.getBlockZ()) {
EnderCrystal enderCrystal = (EnderCrystal) entity;
showingBottom = enderCrystal.isShowingBottom() ? 1 : 0;
blockExists = true;
break;
}
}
if (blockExists) {
Queue.queueBlockPlace(playerFinal.getName(), locationFinal.getBlock().getState(), locationFinal.getBlock().getType(), locationFinal.getBlock().getState(), Material.END_CRYSTAL, showingBottom, 1, null);
}
}
catch (Exception e) {
e.printStackTrace();
}
});
}
}
}
else {
Block relativeBlock = block.getRelative(event.getBlockFace());
Location relativeBlockLocation = relativeBlock.getLocation();
Location blockLocation = block.getLocation();
String relativeBlockKey = world.getName() + "-" + relativeBlockLocation.getBlockX() + "-" + relativeBlockLocation.getBlockY() + "-" + relativeBlockLocation.getBlockZ();
String blockKey = world.getName() + "-" + blockLocation.getBlockX() + "-" + blockLocation.getBlockY() + "-" + blockLocation.getBlockZ();
Object[] keys = new Object[] { relativeBlockKey, blockKey, handType };
ConfigHandler.entityBlockMapper.put(player.getUniqueId(), keys);
}
Block relativeBlock = block.getRelative(event.getBlockFace());
relativeBlockLocation = relativeBlock.getLocation();
blockLocation = block.getLocation();
}
String relativeBlockKey = world.getName() + "-" + relativeBlockLocation.getBlockX() + "-" + relativeBlockLocation.getBlockY() + "-" + relativeBlockLocation.getBlockZ();
String blockKey = world.getName() + "-" + blockLocation.getBlockX() + "-" + blockLocation.getBlockY() + "-" + blockLocation.getBlockZ();
Object[] keys = new Object[] { relativeBlockKey, blockKey, handItem };
ConfigHandler.entityBlockMapper.put(player.getUniqueId(), keys);
}
}
}

View File

@ -0,0 +1,95 @@
package net.coreprotect.listener.player;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Egg;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.inventory.ItemStack;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.logger.ItemLogger;
import net.coreprotect.utility.Util;
public class ProjectileLaunchListener extends Queue implements Listener {
protected static void playerLaunchProjectile(Location location, String user, ItemStack itemStack, int amount, int delay, int offset, int action) {
if (!Config.getConfig(location.getWorld()).ITEM_DROPS || itemStack == null) {
return;
}
String loggingItemId = user.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
int itemId = getItemId(loggingItemId);
itemStack = itemStack.clone();
if (amount > 0) {
itemStack.setAmount(amount);
}
if (action == ItemLogger.ITEM_SHOOT) {
List<ItemStack> list = ConfigHandler.itemsShot.getOrDefault(loggingItemId, new ArrayList<>());
list.add(itemStack);
ConfigHandler.itemsShot.put(loggingItemId, list);
}
else {
List<ItemStack> list = ConfigHandler.itemsThrown.getOrDefault(loggingItemId, new ArrayList<>());
list.add(itemStack);
ConfigHandler.itemsThrown.put(loggingItemId, list);
}
int time = (int) (System.currentTimeMillis() / 1000L) + delay;
Queue.queueItemTransaction(user, location.clone(), time, offset, itemId);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
protected void onProjectileLaunch(ProjectileLaunchEvent event) {
Location location = event.getEntity().getLocation();
String key = location.getWorld().getName() + "-" + location.getBlockX() + "-" + location.getBlockY() + "-" + location.getBlockZ();
Iterator<Entry<UUID, Object[]>> it = ConfigHandler.entityBlockMapper.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<UUID, Object[]> pair = it.next();
UUID uuid = pair.getKey();
Object[] data = pair.getValue();
if ((data[0].equals(key) || data[1].equals(key)) && Util.getEntityMaterial(event.getEntityType()) == ((ItemStack) data[2]).getType()) {
Player player = Bukkit.getServer().getPlayer(uuid);
ItemStack itemStack = (ItemStack) data[2];
playerLaunchProjectile(location, player.getName(), itemStack, 1, 1, 0, (itemStack.getType() != Material.FIREWORK_ROCKET ? ItemLogger.ITEM_THROW : ItemLogger.ITEM_SHOOT));
it.remove();
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
protected void onEntityShootBow(EntityShootBowEvent event) {
if (!(event.getEntity() instanceof Player)) {
return;
}
ItemStack itemStack = event.getConsumable();
playerLaunchProjectile(event.getEntity().getLocation(), event.getEntity().getName(), itemStack, 1, 1, 0, ItemLogger.ITEM_SHOOT);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
protected void onPlayerEggThrow(PlayerEggThrowEvent event) {
Egg egg = event.getEgg();
ItemStack itemStack = egg.getItem();
playerLaunchProjectile(event.getPlayer().getLocation(), event.getPlayer().getName(), itemStack, 0, 1, 0, ItemLogger.ITEM_THROW);
}
}

View File

@ -801,6 +801,16 @@ public class Util extends Queue {
return Material.ITEM_FRAME;
case ENDER_CRYSTAL:
return Material.END_CRYSTAL;
case ENDER_PEARL:
return Material.ENDER_PEARL;
case SPLASH_POTION:
return Material.SPLASH_POTION;
case THROWN_EXP_BOTTLE:
return Material.EXPERIENCE_BOTTLE;
case TRIDENT:
return Material.TRIDENT;
case FIREWORK:
return Material.FIREWORK_ROCKET;
default:
return BukkitAdapter.ADAPTER.getFrameType(type);
}