Skip to content

Commit

Permalink
Added inventory transaction logging for placing items on a campfire (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed Jul 13, 2023
1 parent 5973206 commit e7a6f21
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/net/coreprotect/listener/ListenerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.bukkit.plugin.PluginManager;

import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.listener.block.BlockBreakListener;
import net.coreprotect.listener.block.BlockBurnListener;
import net.coreprotect.listener.block.BlockDispenseListener;
Expand All @@ -15,6 +17,7 @@
import net.coreprotect.listener.block.BlockPistonListener;
import net.coreprotect.listener.block.BlockPlaceListener;
import net.coreprotect.listener.block.BlockSpreadListener;
import net.coreprotect.listener.block.CampfireStartListener;
import net.coreprotect.listener.channel.PluginChannelHandshakeListener;
import net.coreprotect.listener.channel.PluginChannelListener;
import net.coreprotect.listener.entity.CreatureSpawnListener;
Expand Down Expand Up @@ -83,6 +86,9 @@ public ListenerHandler(CoreProtect plugin) {
pluginManager.registerEvents(new BlockPistonListener(), plugin);
pluginManager.registerEvents(new BlockPlaceListener(), plugin);
pluginManager.registerEvents(new BlockSpreadListener(), plugin);
if (ConfigHandler.SERVER_VERSION >= BukkitAdapter.BUKKIT_V1_20) {
pluginManager.registerEvents(new CampfireStartListener(), plugin);
}

// Entity Listeners
pluginManager.registerEvents(new CreatureSpawnListener(), plugin);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.coreprotect.listener.block;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.CampfireStartEvent;
import org.bukkit.inventory.ItemStack;

import net.coreprotect.consumer.Queue;
import net.coreprotect.listener.player.PlayerDropItemListener;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.utility.Util;

public final class CampfireStartListener extends Queue implements Listener {

@EventHandler(priority = EventPriority.MONITOR)
protected void onCampfireStart(CampfireStartEvent event) {
Block block = event.getBlock();
Location location = block.getLocation();
int worldId = Util.getWorldId(location.getWorld().getName());
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
String coordinates = x + "." + y + "." + z + "." + worldId + "." + block.getType().name();
String user = "#entity";

Object[] data = CacheHandler.interactCache.get(coordinates);
if (data != null && data[1].equals(event.getSource())) {
long newTime = System.currentTimeMillis();
long oldTime = (long) data[0];
if ((newTime - oldTime) < 20) { // 50ms = 1 tick
user = (String) data[2];
}
CacheHandler.interactCache.remove(coordinates);
}

if (user.equals("#entity")) {
return;
}

ItemStack itemStack = event.getSource().clone();
itemStack.setAmount(1);
PlayerDropItemListener.playerDropItem(event.getBlock().getLocation(), user, itemStack);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public final class PlayerDropItemListener extends Queue implements Listener {

protected static void playerDropItem(Location location, String user, ItemStack itemStack) {
public static void playerDropItem(Location location, String user, ItemStack itemStack) {
if (!Config.getConfig(location.getWorld()).ITEM_DROPS || itemStack == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,32 @@ else if (BlockGroup.LIGHTABLES.contains(type)) { // extinguishing a lit block su
});
*/
}
else if (type == Material.CAMPFIRE || type == Material.SOUL_CAMPFIRE) {
ItemStack handItem = null;
ItemStack mainHand = player.getInventory().getItemInMainHand();
ItemStack offHand = player.getInventory().getItemInOffHand();

if (event.getHand().equals(EquipmentSlot.HAND) && mainHand != null && mainHand.getType() != Material.BUCKET) {
handItem = mainHand;
}
else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand != null) {
handItem = offHand;
}
else {
return;
}

if (player.getGameMode() != GameMode.CREATIVE) {
Location location = block.getLocation();
long time = System.currentTimeMillis();
int wid = Util.getWorldId(location.getWorld().getName());
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
String coordinates = x + "." + y + "." + z + "." + wid + "." + type.name();
CacheHandler.interactCache.put(coordinates, new Object[] { time, handItem, player.getName() });
}
}

isCake = type.name().endsWith(Material.CAKE.name());
}
Expand Down

0 comments on commit e7a6f21

Please sign in to comment.