UltimateRepairing/src/main/java/com/craftaro/ultimaterepairing/listeners/InteractListeners.java

72 lines
2.7 KiB
Java

package com.craftaro.ultimaterepairing.listeners;
import com.craftaro.ultimaterepairing.UltimateRepairing;
import com.craftaro.ultimaterepairing.anvil.UAnvil;
import com.craftaro.ultimaterepairing.gui.AnvilSettingsGui;
import com.craftaro.ultimaterepairing.gui.RepairGui;
import com.craftaro.core.gui.GuiManager;
import com.craftaro.ultimaterepairing.settings.Settings;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
/**
* Created by songoda on 2/25/2017.
*/
public class InteractListeners implements Listener {
private final UltimateRepairing plugin;
private final GuiManager guiManager;
public InteractListeners(UltimateRepairing plugin, GuiManager guiManager) {
this.plugin = plugin;
this.guiManager = guiManager;
}
@EventHandler
public void onAnvilClick(PlayerInteractEvent event) {
boolean ourRepair = false;
Player player = event.getPlayer();
Block block = event.getClickedBlock();
if (block == null) return;
UAnvil anvil1 = null;
if (!block.getType().name().contains("ANVIL") // don't pay attention if it's not an anvil
// also don't handle if we don't have perms to use this repair anvil
|| (Settings.PERMISSION_ANVIL_PLACE.getBoolean()
&& !(anvil1 = plugin.getAnvilManager().getAnvil(block)).isPermPlaced())) {
return;
}
anvil1 = anvil1 != null ? anvil1 : plugin.getAnvilManager().getAnvil(block);
// check if we should process this as a right click
boolean rightClick = (event.getAction() == Action.RIGHT_CLICK_BLOCK) ^ (Settings.SWAP_LEFT_RIGHT.getBoolean());
// admin interface?
if (rightClick && player.isSneaking() && player.hasPermission("ultimaterepairing.admin")) {
guiManager.showGUI(player, new AnvilSettingsGui(anvil1));
event.setCancelled(true);
} else if (!Settings.ENABLE_ANVIL_DEFAULT_FUNCTION.getBoolean()) {
// if not allowing default anvil, then always use ourRepair
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
event.setCancelled(true);
}
// replace our functions for vanilla mechanics only
if (!rightClick) {
return;
}
ourRepair = true;
} else if (!rightClick && !player.isSneaking()) {
// that's us!
ourRepair = true;
}
if (ourRepair) {
RepairGui.newGui(player, anvil1.getLocation());
event.setCancelled(true);
}
}
}