Skip to content
Open
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
152 changes: 152 additions & 0 deletions core/src/main/java/com/griefcraft/listeners/LWCHopperListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.listeners;

import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCPlugin;
import com.griefcraft.model.Flag;
import com.griefcraft.model.Protection;
import com.griefcraft.util.config.Configuration;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Hopper;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

/**
*
* @author antony
*/
public class LWCHopperListener implements Listener {
private final LWCPlugin plugin;

public LWCHopperListener(LWCPlugin plugin) {
this.plugin = plugin;
}

@EventHandler(ignoreCancelled = true)
public void onMoveItem(InventoryMoveItemEvent event) {
boolean result = false;

Configuration config = LWC.getInstance().getConfiguration();

// if the initiator is the same as the source it is a dropper i.e. depositing items
if (event.getInitiator() == event.getSource()) {
if (!config.getBoolean("optional.ignoreDropperItemMoves", false)) {
result = handleMoveItemEvent(event.getInitiator(), event.getDestination());
}
} else {
if (!config.getBoolean("optional.ignoreHopperItemMoves", false)) {
result = handleMoveItemEvent(event.getInitiator(), event.getSource());
}
}

if (result) {
event.setCancelled(true);
}
}

/**
* Handle the item move event
*
* @param inventory
*/
private boolean handleMoveItemEvent(Inventory initiator, Inventory inventory) {
LWC lwc = LWC.getInstance();

if (inventory == null) {
return false;
}

Location location;
InventoryHolder holder;
Location hopperLocation = null;
InventoryHolder hopperHolder;

try {
holder = inventory.getHolder();
hopperHolder = initiator.getHolder();
} catch (AbstractMethodError e) {
return false;
}

try {
if (holder instanceof BlockState) {
location = ((BlockState) holder).getLocation();
} else if (holder instanceof DoubleChest) {
location = ((DoubleChest) holder).getLocation();
} else {
return false;
}

if (hopperHolder instanceof Hopper) {
hopperLocation = ((Hopper) hopperHolder).getLocation();
}
} catch (Exception e) {
return false;
}

// High-intensity zone: increase protection cache if it's full, otherwise
// the database will be getting rammed
lwc.getProtectionCache().increaseIfNecessary();

// Attempt to load the protection at that location
Protection protection = lwc.findProtection(location);

// If no protection was found we can safely ignore it
if (protection == null) {
return false;
}

if (hopperLocation != null && Boolean.parseBoolean(lwc.resolveProtectionConfiguration(Material.HOPPER, "enabled"))) {
Protection hopperProtection = lwc.findProtection(hopperLocation);

if (hopperProtection != null) {
// if they're owned by the same person then we can allow the move
if (hopperProtection.getOwner().equals(protection.getOwner())) {
return false;
}
}
}

boolean denyHoppers = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(Material.getMaterial(protection.getBlockId()), "denyHoppers"));

// xor = (a && !b) || (!a && b)
if (denyHoppers ^ protection.hasFlag(Flag.Type.HOPPER)) {
return true;
}

return false;
}

}
89 changes: 0 additions & 89 deletions core/src/main/java/com/griefcraft/listeners/LWCPlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,95 +74,6 @@ public LWCPlayerListener(LWCPlugin plugin) {
this.plugin = plugin;
}

@EventHandler(ignoreCancelled = true)
public void onMoveItem(InventoryMoveItemEvent event) {
boolean result;

// if the initiator is the same as the source it is a dropper i.e. depositing items
if (event.getInitiator() == event.getSource()) {
result = handleMoveItemEvent(event.getInitiator(), event.getDestination());
} else {
result = handleMoveItemEvent(event.getInitiator(), event.getSource());
}

if (result) {
event.setCancelled(true);
}
}

/**
* Handle the item move event
*
* @param inventory
*/
private boolean handleMoveItemEvent(Inventory initiator, Inventory inventory) {
LWC lwc = LWC.getInstance();

if (inventory == null) {
return false;
}

Location location;
InventoryHolder holder;
Location hopperLocation = null;
InventoryHolder hopperHolder;

try {
holder = inventory.getHolder();
hopperHolder = initiator.getHolder();
} catch (AbstractMethodError e) {
return false;
}

try {
if (holder instanceof BlockState) {
location = ((BlockState) holder).getLocation();
} else if (holder instanceof DoubleChest) {
location = ((DoubleChest) holder).getLocation();
} else {
return false;
}

if (hopperHolder instanceof Hopper) {
hopperLocation = ((Hopper) hopperHolder).getLocation();
}
} catch (Exception e) {
return false;
}

// High-intensity zone: increase protection cache if it's full, otherwise
// the database will be getting rammed
lwc.getProtectionCache().increaseIfNecessary();

// Attempt to load the protection at that location
Protection protection = lwc.findProtection(location);

// If no protection was found we can safely ignore it
if (protection == null) {
return false;
}

if (hopperLocation != null && Boolean.parseBoolean(lwc.resolveProtectionConfiguration(Material.HOPPER, "enabled"))) {
Protection hopperProtection = lwc.findProtection(hopperLocation);

if (hopperProtection != null) {
// if they're owned by the same person then we can allow the move
if (hopperProtection.getOwner().equals(protection.getOwner())) {
return false;
}
}
}

boolean denyHoppers = Boolean.parseBoolean(lwc.resolveProtectionConfiguration(Material.getMaterial(protection.getBlockId()), "denyHoppers"));

// xor = (a && !b) || (!a && b)
if (denyHoppers ^ protection.hasFlag(Flag.Type.HOPPER)) {
return true;
}

return false;
}

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerDropItem(PlayerDropItemEvent event) {
if (event.isCancelled() || !LWC.ENABLED) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/com/griefcraft/lwc/LWCPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.griefcraft.listeners.LWCBlockListener;
import com.griefcraft.listeners.LWCEntityListener;
import com.griefcraft.listeners.LWCHopperListener;
import com.griefcraft.listeners.LWCPlayerListener;
import com.griefcraft.listeners.LWCServerListener;
import com.griefcraft.scripting.event.LWCCommandEvent;
Expand Down Expand Up @@ -306,6 +307,7 @@ private void registerEvents() {
pluginManager.registerEvents(new LWCEntityListener(this), this);
pluginManager.registerEvents(new LWCBlockListener(this), this);
pluginManager.registerEvents(new LWCServerListener(this), this);
pluginManager.registerEvents(new LWCHopperListener(this), this);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/resources/config/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ optional:
# If protections limits will be enabled (defaults to unlimited protections)
useProtectionLimits: true

# Perform no protection checks on items moved by hoppers.
ignoreHopperItemMoves: false

# Perform no protection checks on items moved by droppers.
ignoreDropperItemMoves: false

# Database information for LWC
database:

Expand Down