use of com.elmakers.mine.bukkit.magic.Mage in project MagicPlugin by elBukkit.
the class InventoryController method onInventoryClick.
@SuppressWarnings("deprecation")
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (event.isCancelled())
return;
if (!(event.getWhoClicked() instanceof Player))
return;
Player player = (Player) event.getWhoClicked();
final Mage mage = controller.getMage(player);
GUIAction gui = mage.getActiveGUI();
if (gui != null) {
gui.clicked(event);
return;
}
// Check for temporary items and skill items
InventoryAction action = event.getAction();
InventoryType inventoryType = event.getInventory().getType();
boolean isPlayerInventory = inventoryType == InventoryType.CRAFTING || inventoryType == InventoryType.PLAYER;
ItemStack clickedItem = event.getCurrentItem();
boolean isDrop = event.getClick() == ClickType.DROP || event.getClick() == ClickType.CONTROL_DROP;
boolean isSkill = clickedItem != null && Wand.isSkill(clickedItem);
// Check for right-click-to-use
boolean isRightClick = action == InventoryAction.PICKUP_HALF;
if (isSkill && isRightClick) {
mage.useSkill(clickedItem);
player.closeInventory();
event.setCancelled(true);
return;
}
if (clickedItem != null && NMSUtils.isTemporary(clickedItem)) {
String message = NMSUtils.getTemporaryMessage(clickedItem);
if (message != null && message.length() > 1) {
mage.sendMessage(message);
}
ItemStack replacement = NMSUtils.getReplacement(clickedItem);
event.setCurrentItem(replacement);
event.setCancelled(true);
return;
}
// Check for wearing spells
ItemStack heldItem = event.getCursor();
boolean heldSpell = Wand.isSpell(heldItem);
if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
if (heldSpell) {
event.setCancelled(true);
return;
}
if (Wand.isWand(clickedItem) || Wand.isWand(heldItem)) {
controller.onArmorUpdated(mage);
}
}
boolean isHotbar = event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
if (isHotbar && event.getSlotType() == InventoryType.SlotType.ARMOR) {
int slot = event.getHotbarButton();
ItemStack item = mage.getPlayer().getInventory().getItem(slot);
if (item != null && Wand.isSpell(item)) {
event.setCancelled(true);
return;
}
controller.onArmorUpdated(mage);
}
Wand activeWand = mage.getActiveWand();
boolean isWandInventoryOpen = activeWand != null && activeWand.isInventoryOpen();
// Preventing putting skills in containers
if (isSkill && !isPlayerInventory && !isWandInventoryOpen) {
if (!isDrop) {
event.setCancelled(true);
}
return;
}
boolean isFurnace = inventoryType == InventoryType.FURNACE;
boolean isChest = inventoryType == InventoryType.CHEST || inventoryType == InventoryType.HOPPER || inventoryType == InventoryType.DISPENSER || inventoryType == InventoryType.DROPPER;
// TODO: use enum when dropping backwards compat
if (!isChest)
isChest = inventoryType.name().equals("SHULKER_BOX");
boolean clickedWand = Wand.isWand(clickedItem);
boolean isContainerSlot = event.getSlot() == event.getRawSlot();
if (isWandInventoryOpen) {
// Don't allow the offhand slot to be messed with while the spell inventory is open
if (event.getRawSlot() == 45) {
event.setCancelled(true);
return;
}
// Don't allow putting spells in a crafting slot
if (event.getSlotType() == InventoryType.SlotType.CRAFTING && heldSpell) {
event.setCancelled(true);
return;
}
if (Wand.isSpell(clickedItem) && clickedItem.getAmount() != 1) {
clickedItem.setAmount(1);
}
if (clickedWand) {
event.setCancelled(true);
if (dropChangesPages || isRightClick) {
activeWand.cycleInventory();
} else {
activeWand.cycleHotbar(1);
// There doesn't seem to be any other way to allow cycling in creative
if (inventoryType == InventoryType.PLAYER) {
activeWand.cycleInventory();
}
}
return;
}
// So many ways to try and move the wand around, that we have to watch for!
if (isHotbar && Wand.isWand(player.getInventory().getItem(event.getHotbarButton()))) {
event.setCancelled(true);
return;
}
// Can't wear spells
if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
if (controller.isWearable(clickedItem)) {
event.setCancelled(true);
return;
}
}
// but perhaps happens with lag?
if (Wand.isWand(event.getCursor())) {
activeWand.closeInventory();
event.setCursor(null);
event.setCancelled(true);
return;
}
} else if (activeWand != null) {
// Check for changes that could have been made to the active wand
int activeSlot = player.getInventory().getHeldItemSlot();
if (event.getSlot() == activeSlot || (isHotbar && event.getHotbarButton() == activeSlot)) {
mage.checkWand();
activeWand = mage.getActiveWand();
}
}
// Don't allow smelting wands
if (isFurnace && clickedWand) {
event.setCancelled(true);
return;
}
if (isFurnace && isHotbar) {
ItemStack destinationItem = player.getInventory().getItem(event.getHotbarButton());
if (Wand.isWand(destinationItem)) {
event.setCancelled(true);
return;
}
}
if (InventoryUtils.getMetaBoolean(clickedItem, "unmoveable", false)) {
event.setCancelled(true);
return;
}
if (isHotbar) {
ItemStack destinationItem = player.getInventory().getItem(event.getHotbarButton());
if (InventoryUtils.getMetaBoolean(destinationItem, "unmoveable", false)) {
event.setCancelled(true);
return;
}
if (isChest && InventoryUtils.getMetaBoolean(destinationItem, "unstashable", false) && !player.hasPermission("Magic.wand.override_stash")) {
event.setCancelled(true);
return;
}
}
// Check for unstashable wands
if (isChest && !isContainerSlot && !player.hasPermission("Magic.wand.override_stash")) {
if (InventoryUtils.getMetaBoolean(clickedItem, "unstashable", false)) {
event.setCancelled(true);
return;
}
}
// Check for taking bound wands out of chests
if (isChest && isContainerSlot && Wand.isBound(clickedItem)) {
Wand testWand = controller.getWand(clickedItem);
if (!testWand.canUse(player)) {
event.setCancelled(true);
return;
}
}
// Check for armor changing
if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
if (controller.isWearable(clickedItem)) {
controller.onArmorUpdated(mage);
}
}
// or dropping undroppable wands
if (isDrop) {
if (InventoryUtils.getMetaBoolean(clickedItem, "undroppable", false)) {
event.setCancelled(true);
if (activeWand != null) {
if (activeWand.getHotbarCount() > 1) {
activeWand.cycleHotbar(1);
} else {
activeWand.closeInventory();
}
}
return;
}
if (activeWand != null && activeWand.isInventoryOpen()) {
ItemStack droppedItem = clickedItem;
if (!Wand.isSpell(droppedItem)) {
mage.giveItem(droppedItem);
event.setCurrentItem(null);
event.setCancelled(true);
return;
}
// This is a hack to deal with spells on cooldown disappearing,
// Since the event handler doesn't match the zero-count itemstacks
Integer slot = event.getSlot();
int heldSlot = player.getInventory().getHeldItemSlot();
Inventory hotbar = activeWand.getHotbar();
if (hotbar != null && slot >= 0 && slot <= hotbar.getSize() && slot != heldSlot && activeWand.getMode() == WandMode.INVENTORY) {
if (slot > heldSlot)
slot--;
if (slot < hotbar.getSize()) {
droppedItem = hotbar.getItem(slot);
} else {
slot = null;
}
} else {
slot = null;
}
if (!controller.isSpellDroppingEnabled()) {
player.closeInventory();
String spellName = Wand.getSpell(droppedItem);
if (spellName != null && !activeWand.isManualQuickCastDisabled() && enableInventoryCasting) {
Spell spell = mage.getSpell(spellName);
if (spell != null) {
activeWand.cast(spell);
// Just in case a spell has levelled up... jeez!
if (hotbar != null && slot != null) {
droppedItem = hotbar.getItem(slot);
}
}
}
event.setCancelled(true);
// This is needed to avoid spells on cooldown disappearing from the hotbar
if (hotbar != null && slot != null && mage.getActiveGUI() == null) {
player.getInventory().setItem(event.getSlot(), droppedItem);
DeprecatedUtils.updateInventory(player);
}
return;
}
ItemStack newDrop = controller.removeItemFromWand(activeWand, droppedItem);
if (newDrop != null) {
Location location = player.getLocation();
Item item = location.getWorld().dropItem(location, newDrop);
SafetyUtils.setVelocity(item, location.getDirection().normalize());
} else {
event.setCancelled(true);
}
}
return;
}
// Check for wand cycling with active inventory
if (activeWand != null) {
WandMode wandMode = activeWand.getMode();
boolean isChestInventory = wandMode == WandMode.CHEST || wandMode == WandMode.SKILLS;
if ((wandMode == WandMode.INVENTORY && isPlayerInventory) || (isChestInventory && inventoryType == InventoryType.CHEST)) {
if (activeWand.isInventoryOpen()) {
if (event.getAction() == InventoryAction.NOTHING) {
int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
activeWand.cycleInventory(direction);
event.setCancelled(true);
return;
}
if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
event.setCancelled(true);
return;
}
// Chest mode falls back to selection from here.
boolean isInventoryQuickSelect = isRightClick && wandMode == WandMode.INVENTORY && enableInventorySelection;
if (isInventoryQuickSelect || wandMode == WandMode.CHEST) {
player.closeInventory();
mage.activateIcon(activeWand, clickedItem);
event.setCancelled(true);
}
// Prevent clicking any non-skill item when a skill inventory is open
if (wandMode == WandMode.SKILLS && clickedItem != null && clickedItem.getType() != Material.AIR && !Wand.isSkill(clickedItem)) {
event.setCancelled(true);
}
}
}
}
}
use of com.elmakers.mine.bukkit.magic.Mage in project MagicPlugin by elBukkit.
the class InventoryController method onInventoryClosed.
@EventHandler
public void onInventoryClosed(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player))
return;
// Update the active wand, it may have changed around
Player player = (Player) event.getPlayer();
Mage mage = controller.getRegisteredMage(player);
if (mage == null)
return;
Wand previousWand = mage.getActiveWand();
// Prevent spells getting smuggled out via crafting slots
Inventory inventory = event.getInventory();
if (inventory instanceof CraftingInventory && previousWand != null && previousWand.wasInventoryOpen()) {
CraftingInventory craftingInventory = (CraftingInventory) inventory;
ItemStack[] matrix = craftingInventory.getMatrix();
for (int i = 0; i < matrix.length; i++) {
matrix[i] = new ItemStack(Material.AIR);
}
craftingInventory.setMatrix(matrix);
}
GUIAction gui = mage.getActiveGUI();
if (gui != null) {
mage.onGUIDeactivate();
}
// This is just to make sure we don't lose changes made to the inventory
if (previousWand != null && previousWand.isInventoryOpen()) {
if (previousWand.getMode() == WandMode.INVENTORY) {
previousWand.saveInventory();
// Update hotbar names
previousWand.updateHotbar();
} else if (previousWand.getMode() == WandMode.CHEST || previousWand.getMode() == WandMode.SKILLS) {
// Close and save an open chest mode inventory
previousWand.closeInventory();
}
} else {
if (previousWand != null && !previousWand.wasInventoryOpen()) {
previousWand.checkInventoryForUpgrades();
}
mage.checkWand();
}
}
use of com.elmakers.mine.bukkit.magic.Mage in project MagicPlugin by elBukkit.
the class PlayerController method onPlayerTeleport.
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
Player player = event.getPlayer();
if (controller.isNPC(player))
return;
Mage mage = controller.getMage(player);
mage.onTeleport(event);
}
use of com.elmakers.mine.bukkit.magic.Mage in project MagicPlugin by elBukkit.
the class PlayerController method onPlayerSwapItem.
@EventHandler
public void onPlayerSwapItem(PlayerSwapHandItemsEvent event) {
final Player player = event.getPlayer();
Mage mage = controller.getRegisteredMage(player);
if (mage == null)
return;
final Wand activeWand = mage.getActiveWand();
final Wand offhandWand = mage.getOffhandWand();
if (activeWand == null && offhandWand == null)
return;
if (activeWand != null && activeWand.performAction(activeWand.getSwapAction())) {
event.setCancelled(true);
} else if (activeWand != null && activeWand.isInventoryOpen()) {
activeWand.closeInventory();
event.setCancelled(true);
} else if (activeWand != null || offhandWand != null || Wand.isWand(event.getMainHandItem()) || Wand.isWand(event.getOffHandItem())) {
mage.checkWandNextTick();
}
}
use of com.elmakers.mine.bukkit.magic.Mage in project MagicPlugin by elBukkit.
the class PlayerController method onPlayerEquip.
@EventHandler
public void onPlayerEquip(PlayerItemHeldEvent event) {
if (!controller.isLoaded())
return;
Player player = event.getPlayer();
PlayerInventory inventory = player.getInventory();
ItemStack next = inventory.getItem(event.getNewSlot());
Mage mage = controller.getMage(player);
// Check for self-destructing and temporary items
if (Wand.isSelfDestructWand(next)) {
mage.sendMessageKey("wand.self_destruct");
inventory.setItem(event.getNewSlot(), null);
mage.checkWand();
return;
}
if (NMSUtils.isTemporary(next)) {
inventory.setItem(event.getNewSlot(), null);
mage.checkWand();
return;
}
Wand activeWand = mage.getActiveWand();
boolean isSkill = Wand.isSkill(next);
if (isSkill && Wand.isQuickCastSkill(next)) {
mage.useSkill(next);
event.setCancelled(true);
return;
}
boolean isQuickCast = activeWand != null && activeWand.isQuickCast() && activeWand.isInventoryOpen();
if (isQuickCast) {
Spell spell = mage.getSpell(Wand.getSpell(next));
if (spell != null) {
activeWand.cast(spell);
}
event.setCancelled(true);
return;
}
// Check for active Wand
boolean isWand = Wand.isWand(next);
if (activeWand != null && activeWand.isInventoryOpen()) {
// If the wand inventory is open, we're going to let them select a spell or material
if (!isWand) {
mage.activateIcon(activeWand, next);
// Make sure we have the current wand item in the player's inventory so the
// item text gets updated on hotbar item selection "bounce"
int previousSlot = event.getPreviousSlot();
ItemStack previous = inventory.getItem(previousSlot);
if (previous != null && previous.equals(activeWand.getItem())) {
player.getInventory().setItem(previousSlot, activeWand.getItem());
}
}
event.setCancelled(true);
} else if (isWand || activeWand != null) {
// Check to see if we've switched to/from a wand
mage.checkWandNextTick();
}
// Check for map selection if no wand is active
activeWand = mage.getActiveWand();
if (activeWand == null && next != null) {
if (next.getType() == Material.MAP) {
mage.setLastHeldMapId(next.getDurability());
}
}
}
Aggregations