Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/net/coreprotect/CoreProtect.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public final class CoreProtect extends JavaPlugin {

private static CoreProtect instance;
private boolean advancedChestsEnabled = false;

/**
* Get the instance of CoreProtect
Expand Down Expand Up @@ -43,6 +44,7 @@ public void onEnable() {
instance = this;
ConfigHandler.path = this.getDataFolder().getPath() + File.separator;

advancedChestsEnabled = getServer().getPluginManager().getPlugin("AdvancedChests") != null;
// Initialize plugin using the initialization service
boolean initialized = PluginInitializationService.initializePlugin(this);

Expand All @@ -57,4 +59,8 @@ public void onEnable() {
public void onDisable() {
ShutdownService.safeShutdown(this);
}

public boolean isAdvancedChestsEnabled(){
return advancedChestsEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import net.coreprotect.utility.ItemUtils;
import net.coreprotect.utility.Util;
import net.coreprotect.utility.Validate;
import us.lynuxcraft.deadsilenceiv.advancedchests.AdvancedChestsAPI;
import us.lynuxcraft.deadsilenceiv.advancedchests.chest.AdvancedChest;

public final class InventoryChangeListener extends Queue implements Listener {

Expand Down Expand Up @@ -70,141 +72,147 @@
return false;
}

static boolean onInventoryInteract(String user, final Inventory inventory, ItemStack[] inventoryData, Material containerType, Location location, boolean aSync) {
if (inventory != null && location != null) {
World world = location.getWorld();

if (Config.getConfig(world).ITEM_TRANSACTIONS) {
Material type = Material.CHEST;
Location playerLocation = null;

if (aSync) {
playerLocation = location;
if (containerType != null) {
type = containerType;
}
}
else {
} else {
InventoryHolder inventoryHolder = inventory.getHolder();
if (inventoryHolder == null) {
return false;
if (CoreProtect.getInstance().isAdvancedChestsEnabled()) {
AdvancedChest<?, ?> advancedChest = AdvancedChestsAPI.getInventoryManager().getAdvancedChest(inventory);
if (advancedChest != null) {
playerLocation = advancedChest.getLocation();
} else {
return false;
}
} else {
return false;
}
}

if (inventoryHolder instanceof BlockState) {
BlockState state = (BlockState) inventoryHolder;
type = state.getType();
if (BlockGroup.CONTAINERS.contains(type)) {
playerLocation = state.getLocation();
}
}
else if (inventoryHolder instanceof DoubleChest) {
} else if (inventoryHolder instanceof DoubleChest) {
DoubleChest state = (DoubleChest) inventoryHolder;
playerLocation = state.getLocation();
}
}

if (playerLocation != null) {
if (inventoryData == null) {
inventoryData = inventory.getContents();
}

int x = playerLocation.getBlockX();
int y = playerLocation.getBlockY();
int z = playerLocation.getBlockZ();

String transactingChestId = playerLocation.getWorld().getUID().toString() + "." + x + "." + y + "." + z;
String loggingChestId = user.toLowerCase(Locale.ROOT) + "." + x + "." + y + "." + z;
for (String loggingChestIdViewer : ConfigHandler.oldContainer.keySet()) {
if (loggingChestIdViewer.equals(loggingChestId) || !loggingChestIdViewer.endsWith("." + x + "." + y + "." + z)) {
continue;
}

if (ConfigHandler.oldContainer.get(loggingChestIdViewer) != null) { // player has pending consumer item
int sizeOld = ConfigHandler.oldContainer.get(loggingChestIdViewer).size();
ConfigHandler.forceContainer.computeIfAbsent(loggingChestIdViewer, k -> new ArrayList<>());
List<ItemStack[]> list = ConfigHandler.forceContainer.get(loggingChestIdViewer);

if (list != null && list.size() < sizeOld) {
ItemStack[] containerState = ItemUtils.getContainerState(inventoryData);

// If items have been removed by a hopper, merge into containerState
List<Object> transactingChest = ConfigHandler.transactingChest.get(transactingChestId);
if (transactingChest != null) {
List<Object> transactingChestList = Collections.synchronizedList(new ArrayList<>(transactingChest));
if (!transactingChestList.isEmpty()) {
ItemStack[] newState = new ItemStack[containerState.length + transactingChestList.size()];
int count = 0;

for (int j = 0; j < containerState.length; j++) {
newState[j] = containerState[j];
count++;
}

for (Object item : transactingChestList) {
ItemStack addItem = null;
ItemStack removeItem = null;
if (item instanceof ItemStack) {
addItem = (ItemStack) item;
}
else {
addItem = ((ItemStack[]) item)[0];
removeItem = ((ItemStack[]) item)[1];
}

// item was removed by hopper, add back to state
if (addItem != null) {
newState[count] = addItem;
count++;
}

// item was added by hopper, remove from state
if (removeItem != null) {
for (ItemStack check : newState) {
if (check != null && check.isSimilar(removeItem)) {
check.setAmount(check.getAmount() - 1);
break;
}
}
}
}
containerState = newState;
}
}

modifyForceContainer(loggingChestIdViewer, containerState);
}
}
}

int chestId = getChestId(loggingChestId);
if (chestId > 0) {
List<ItemStack[]> forceList = ConfigHandler.forceContainer.get(loggingChestId);
if (forceList != null) {
int forceSize = forceList.size();
List<ItemStack[]> list = ConfigHandler.oldContainer.get(loggingChestId);

if (list != null && list.size() <= forceSize) {
list.add(ItemUtils.getContainerState(inventoryData));
ConfigHandler.oldContainer.put(loggingChestId, list);
}
}
}
else {
List<ItemStack[]> list = new ArrayList<>();
list.add(ItemUtils.getContainerState(inventoryData));
ConfigHandler.oldContainer.put(loggingChestId, list);
}

ConfigHandler.transactingChest.computeIfAbsent(transactingChestId, k -> Collections.synchronizedList(new ArrayList<>()));
Queue.queueContainerTransaction(user, playerLocation, type, inventory, chestId);
return true;
}
}
}

return false;
}

Check notice on line 215 in src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java#L75-L215

Complex Method
static void onInventoryInteractAsync(Player player, Inventory inventory, boolean enderChest) {
if (inventory == null) {
return;
Expand All @@ -217,6 +225,17 @@
catch (Exception e) {
return;
}

if (location == null && !CoreProtect.getInstance().isAdvancedChestsEnabled()) {
return;
}
if (CoreProtect.getInstance().isAdvancedChestsEnabled()) {
AdvancedChest<?,?> chest = AdvancedChestsAPI.getInventoryManager().getAdvancedChest(inventory);
if (chest != null) {
location = chest.getLocation();
}
}

if (location == null) {
return;
}
Expand Down Expand Up @@ -249,56 +268,64 @@
});
}

@EventHandler(priority = EventPriority.LOWEST)
protected void onInventoryClick(InventoryClickEvent event) {
InventoryAction inventoryAction = event.getAction();
if (inventoryAction == InventoryAction.NOTHING) {
return;
}

boolean enderChest = false;
boolean advancedChest;
if (inventoryAction != InventoryAction.MOVE_TO_OTHER_INVENTORY && inventoryAction != InventoryAction.COLLECT_TO_CURSOR && inventoryAction != InventoryAction.UNKNOWN) {
// Perform this check to prevent triggering onInventoryInteractAsync when a user is just clicking items in their own inventory
Inventory inventory = null;
try {
try {
inventory = event.getView().getInventory(event.getRawSlot());
}
catch (IncompatibleClassChangeError e) {
inventory = event.getClickedInventory();
}
}
catch (Exception e) {
return;
}
if (inventory == null) {
return;
}

InventoryHolder inventoryHolder = inventory.getHolder();
enderChest = inventory.equals(event.getWhoClicked().getEnderChest());
if ((inventoryHolder == null || !(inventoryHolder instanceof BlockInventoryHolder || inventoryHolder instanceof DoubleChest)) && !enderChest) {
advancedChest = isAdvancedChest(inventory);
if ((!(inventoryHolder instanceof BlockInventoryHolder || inventoryHolder instanceof DoubleChest)) && !enderChest && !advancedChest) {
return;
}
}
else {
if(advancedChest && event.getSlot() > inventory.getSize() - 10){
return;
}
} else {
// Perform standard inventory holder check on primary inventory
Inventory inventory = event.getInventory();
if (inventory == null) {
return;
}

InventoryHolder inventoryHolder = inventory.getHolder();
enderChest = inventory.equals(event.getWhoClicked().getEnderChest());
if ((inventoryHolder == null || !(inventoryHolder instanceof BlockInventoryHolder || inventoryHolder instanceof DoubleChest)) && !enderChest) {
advancedChest = isAdvancedChest(inventory);
if ((!(inventoryHolder instanceof BlockInventoryHolder || inventoryHolder instanceof DoubleChest)) && !enderChest && !advancedChest) {
return;
}
if(advancedChest && event.getSlot() > inventory.getSize() - 10){
return;
}
}

Player player = (Player) event.getWhoClicked();
onInventoryInteractAsync(player, event.getInventory(), enderChest);
}

Check notice on line 328 in src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java#L271-L328

Complex Method
@EventHandler(priority = EventPriority.LOWEST)
protected void onInventoryDragEvent(InventoryDragEvent event) {
boolean movedItem = false;
Expand All @@ -311,7 +338,7 @@
}

enderChest = inventory.equals(event.getWhoClicked().getEnderChest());
if ((inventoryHolder != null && (inventoryHolder instanceof BlockInventoryHolder || inventoryHolder instanceof DoubleChest)) || enderChest) {
if (((inventoryHolder instanceof BlockInventoryHolder || inventoryHolder instanceof DoubleChest)) || enderChest || isAdvancedChest(inventory)) {
movedItem = true;
}

Expand All @@ -323,63 +350,68 @@
onInventoryInteractAsync(player, event.getInventory(), enderChest);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
protected void onInventoryMoveItemEvent(InventoryMoveItemEvent event) {
if (event.isCancelled()) {
return;
}

Inventory sourceInventory = event.getSource();
if (sourceInventory == null) {
return;
}

Location location = sourceInventory.getLocation();
if (location == null) {
return;
}

boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS;
if (!hopperTransactions && !Config.getConfig(location.getWorld()).ITEM_TRANSACTIONS) {
return;
}

InventoryHolder sourceHolder = PaperAdapter.ADAPTER.getHolder(sourceInventory, false);
if (sourceHolder == null) {
return;
}

InventoryHolder destinationHolder = PaperAdapter.ADAPTER.getHolder(event.getDestination(), false);
if (destinationHolder == null) {
return;
}

if (hopperTransactions) {
if (Validate.isHopper(destinationHolder) && (Validate.isContainer(sourceHolder) && !Validate.isHopper(sourceHolder))) {
HopperPullListener.processHopperPull(location, "#hopper", sourceHolder, destinationHolder, event.getItem());
}
else if (Validate.isHopper(sourceHolder) && (Validate.isContainer(destinationHolder) && !Validate.isHopper(destinationHolder))) {
HopperPushListener.processHopperPush(location, "#hopper", sourceHolder, destinationHolder, event.getItem());
}
else if (Validate.isDropper(sourceHolder) && (Validate.isContainer(destinationHolder))) {
HopperPullListener.processHopperPull(location, "#dropper", sourceHolder, destinationHolder, event.getItem());
if (!Validate.isHopper(destinationHolder)) {
HopperPushListener.processHopperPush(location, "#dropper", sourceHolder, destinationHolder, event.getItem());
}
}

return;
}

if (destinationHolder instanceof Player || (!(sourceHolder instanceof BlockInventoryHolder) && !(sourceHolder instanceof DoubleChest))) {
return;
}

List<Object> list = ConfigHandler.transactingChest.get(location.getWorld().getUID().toString() + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ());
if (list == null) {
return;
}

HopperPullListener.processHopperPull(location, "#hopper", sourceHolder, destinationHolder, event.getItem());
}

Check notice on line 412 in src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java#L353-L412

Complex Method
private boolean isAdvancedChest(Inventory inventory) {
return CoreProtect.getInstance().isAdvancedChestsEnabled() && AdvancedChestsAPI.getInventoryManager().getAdvancedChest(inventory) != null;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ api-version: 1.13
folia-supported: true
website: http://coreprotect.net
author: Intelli
softdepend: [WorldEdit]
softdepend: [WorldEdit,AdvancedChests]
description: >
Provides block protection for your server.
commands:
Expand Down
Loading