mirror of
https://gitlab.com/phoenix-dvpmt/mmocore.git
synced 2024-11-27 00:45:40 +01:00
Moved armor listener to MMOLib 1.0.3
This commit is contained in:
parent
7102cad2eb
commit
fcaaecd441
@ -1,97 +0,0 @@
|
||||
package com.codingforcookies.armorequip;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* @author Arnah
|
||||
* @since Jul 30, 2015
|
||||
*/
|
||||
public final class ArmorEquipEvent extends PlayerEvent {
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
// private boolean cancelled = false;
|
||||
private final ArmorType type;
|
||||
private final ItemStack oldArmor, newArmor;
|
||||
|
||||
/**
|
||||
* Constructor for the ArmorEquipEvent.
|
||||
*
|
||||
* @param player
|
||||
* The player who put on / removed the armor.
|
||||
* @param type
|
||||
* The ArmorType of the armor added
|
||||
* @param oldArmorPiece
|
||||
* The ItemStack of the armor removed.
|
||||
* @param newArmorPiece
|
||||
* The ItemStack of the armor added.
|
||||
*/
|
||||
public ArmorEquipEvent(Player player, ArmorType type, ItemStack oldArmor, ItemStack newArmor) {
|
||||
super(player);
|
||||
|
||||
this.type = type;
|
||||
this.oldArmor = oldArmor;
|
||||
this.newArmor = newArmor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of handlers handling this event.
|
||||
*
|
||||
* @return A list of handlers handling this event.
|
||||
*/
|
||||
public final static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of handlers handling this event.
|
||||
*
|
||||
* @return A list of handlers handling this event.
|
||||
*/
|
||||
@Override
|
||||
public final HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets if this event should be cancelled.
|
||||
*
|
||||
* @param cancel
|
||||
* If this event should be cancelled.
|
||||
*/
|
||||
// public final void setCancelled(final boolean cancelled){
|
||||
// this.cancelled = cancelled;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Gets if this event is cancelled.
|
||||
*
|
||||
* @return If this event is cancelled
|
||||
*/
|
||||
// public final boolean isCancelled(){
|
||||
// return cancelled;
|
||||
// }
|
||||
|
||||
public final ArmorType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last equipped armor piece, could be a piece of armor,
|
||||
* {@link Material#Air}, or null.
|
||||
*/
|
||||
public final ItemStack getOldArmor() {
|
||||
return oldArmor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the newly equipped armor, could be a piece of armor,
|
||||
* {@link Material#Air}, or null.
|
||||
*/
|
||||
public final ItemStack getNewArmorPiece() {
|
||||
return newArmor;
|
||||
}
|
||||
}
|
@ -1,186 +0,0 @@
|
||||
package com.codingforcookies.armorequip;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.block.BlockDispenseArmorEvent;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryAction;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.event.inventory.InventoryType.SlotType;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerItemBreakEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* @author Arnah
|
||||
* @since Jul 30, 2015
|
||||
*/
|
||||
public class ArmorListener implements Listener {
|
||||
|
||||
// private final List<String> blockedMaterials;
|
||||
|
||||
// public ArmorListener(List<String> blockedMaterials){
|
||||
// this.blockedMaterials = blockedMaterials;
|
||||
// }
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onClick(InventoryClickEvent event) {
|
||||
if (event.isCancelled() || event.getAction() == InventoryAction.NOTHING)
|
||||
return;
|
||||
|
||||
final boolean shift = event.getClick().equals(ClickType.SHIFT_LEFT) || event.getClick().equals(ClickType.SHIFT_RIGHT), numberkey = event.getClick().equals(ClickType.NUMBER_KEY);
|
||||
|
||||
if (event.getSlotType() != SlotType.ARMOR && event.getSlotType() != SlotType.QUICKBAR && event.getSlotType() != SlotType.CONTAINER)
|
||||
return;
|
||||
if (event.getClickedInventory() != null && !event.getClickedInventory().getType().equals(InventoryType.PLAYER))
|
||||
return;
|
||||
if (!event.getInventory().getType().equals(InventoryType.CRAFTING) && !event.getInventory().getType().equals(InventoryType.PLAYER))
|
||||
return;
|
||||
if (!(event.getWhoClicked() instanceof Player))
|
||||
return;
|
||||
ArmorType newArmorType = ArmorType.matchType(shift ? event.getCurrentItem() : event.getCursor());
|
||||
if (!shift && newArmorType != null && event.getRawSlot() != newArmorType.getSlot()) {
|
||||
// Used for drag and drop checking to make sure you aren't trying to
|
||||
// place a helmet in the boots slot.
|
||||
return;
|
||||
}
|
||||
|
||||
if (shift) {
|
||||
newArmorType = ArmorType.matchType(event.getCurrentItem());
|
||||
if (newArmorType != null) {
|
||||
final boolean equipping = event.getRawSlot() != newArmorType.getSlot();
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
|
||||
if (equipping ? isAirOrNull(newArmorType.getItem(player)) : !isAirOrNull(newArmorType.getItem(player)))
|
||||
Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent((Player) event.getWhoClicked(), newArmorType, equipping ? null : event.getCurrentItem(), equipping ? event.getCurrentItem() : null));
|
||||
// if(armorEquipEvent.isCancelled()){
|
||||
// event.setCancelled(true);
|
||||
// }
|
||||
}
|
||||
|
||||
} else {
|
||||
ItemStack newArmorPiece = event.getCursor();
|
||||
ItemStack oldArmorPiece = event.getCurrentItem();
|
||||
if (numberkey) {
|
||||
|
||||
// prevents shit in the 2v2 crafting
|
||||
if (event.getClickedInventory().getType().equals(InventoryType.PLAYER)) {
|
||||
// event.getClickedInventory() == The players inventory
|
||||
// event.getHotBarButton() == key people are pressing to
|
||||
// equip
|
||||
// or unequip the item to or from.
|
||||
// event.getRawSlot() == The slot the item is going to.
|
||||
// event.getSlot() == Armor slot, can't use
|
||||
// event.getRawSlot() as
|
||||
// that gives a hotbar slot ;-;
|
||||
ItemStack hotbarItem = event.getClickedInventory().getItem(event.getHotbarButton());
|
||||
if (!isAirOrNull(hotbarItem)) {// Equipping
|
||||
newArmorType = ArmorType.matchType(hotbarItem);
|
||||
newArmorPiece = hotbarItem;
|
||||
oldArmorPiece = event.getClickedInventory().getItem(event.getSlot());
|
||||
} else // Unequipping
|
||||
newArmorType = ArmorType.matchType(!isAirOrNull(event.getCurrentItem()) ? event.getCurrentItem() : event.getCursor());
|
||||
}
|
||||
|
||||
// equip with no new item going into the slot
|
||||
} else if (isAirOrNull(event.getCursor()) && !isAirOrNull(event.getCurrentItem()))
|
||||
newArmorType = ArmorType.matchType(event.getCurrentItem());
|
||||
|
||||
// event.getCurrentItem() == Unequip
|
||||
// event.getCursor() == Equip
|
||||
// newArmorType =
|
||||
// ArmorType.matchType(!isAirOrNull(event.getCurrentItem()) ?
|
||||
// event.getCurrentItem() : event.getCursor());
|
||||
if (newArmorType != null && event.getRawSlot() == newArmorType.getSlot())
|
||||
Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent((Player) event.getWhoClicked(), newArmorType, oldArmorPiece, newArmorPiece));
|
||||
// if(armorEquipEvent.isCancelled()){
|
||||
// event.setCancelled(true);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
if (event.getAction() == Action.PHYSICAL || (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK))
|
||||
return;
|
||||
|
||||
ArmorType type = ArmorType.matchType(event.getItem());
|
||||
if (type != null && isAirOrNull(type.getItem(event.getPlayer())))
|
||||
Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent(event.getPlayer(), ArmorType.matchType(event.getItem()), null, event.getItem()));
|
||||
// if(armorEquipEvent.isCancelled()){
|
||||
// event.setCancelled(true);
|
||||
// player.updateInventory();
|
||||
// }
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryDrag(InventoryDragEvent event) {
|
||||
// getType() seems to always be even.
|
||||
// Old Cursor gives the item you are equipping
|
||||
// Raw slot is the ArmorType slot
|
||||
// Can't replace armor using this method making getCursor() useless.
|
||||
ArmorType type = ArmorType.matchType(event.getOldCursor());
|
||||
if (event.getRawSlots().isEmpty())
|
||||
return;// Idk if this will ever happen
|
||||
if (type != null && type.getSlot() == event.getRawSlots().stream().findFirst().orElse(0))
|
||||
Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent((Player) event.getWhoClicked(), type, null, event.getOldCursor()));
|
||||
// if(armorEquipEvent.isCancelled()){
|
||||
// event.setResult(Result.DENY);
|
||||
// event.setCancelled(true);
|
||||
// }
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onItemBreak(PlayerItemBreakEvent event) {
|
||||
ArmorType type = ArmorType.matchType(event.getBrokenItem());
|
||||
if (type != null)
|
||||
Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent(event.getPlayer(), type, event.getBrokenItem(), null));
|
||||
// if(armorEquipEvent.isCancelled()){
|
||||
// ItemStack i = event.getBrokenItem().clone();
|
||||
// i.setAmount(1);
|
||||
// i.setDurability((short) (i.getDurability() - 1));
|
||||
// if(type.equals(ArmorType.HELMET)){
|
||||
// p.getInventory().setHelmet(i);
|
||||
// }else if(type.equals(ArmorType.CHESTPLATE)){
|
||||
// p.getInventory().setChestplate(i);
|
||||
// }else if(type.equals(ArmorType.LEGGINGS)){
|
||||
// p.getInventory().setLeggings(i);
|
||||
// }else if(type.equals(ArmorType.BOOTS)){
|
||||
// p.getInventory().setBoots(i);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDeath(PlayerDeathEvent event) {
|
||||
Player player = event.getEntity();
|
||||
for (ItemStack item : player.getInventory().getArmorContents())
|
||||
if (!isAirOrNull(item))
|
||||
Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent(player, ArmorType.matchType(item), item, null));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDispense(BlockDispenseArmorEvent event) {
|
||||
ArmorType type = ArmorType.matchType(event.getItem());
|
||||
if (type != null && event.getTargetEntity() instanceof Player)
|
||||
Bukkit.getServer().getPluginManager().callEvent(new ArmorEquipEvent((Player) event.getTargetEntity(), type, null, event.getItem()));
|
||||
// if(armorEquipEvent.isCancelled()){
|
||||
// event.setCancelled(true);
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* A utility method to support versions that use null or air ItemStacks.
|
||||
*/
|
||||
private boolean isAirOrNull(ItemStack item) {
|
||||
return item == null || item.getType().equals(Material.AIR);
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
package com.codingforcookies.armorequip;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
|
||||
import net.mmogroup.mmolib.version.VersionMaterial;
|
||||
|
||||
/**
|
||||
* @author Arnah
|
||||
* @since Jul 30, 2015
|
||||
*/
|
||||
public enum ArmorType {
|
||||
HELMET(5, (inv) -> inv.getHelmet()),
|
||||
CHESTPLATE(6, (inv) -> inv.getChestplate()),
|
||||
LEGGINGS(7, (inv) -> inv.getLeggings()),
|
||||
BOOTS(8, (inv) -> inv.getBoots());
|
||||
|
||||
private final int slot;
|
||||
private final Function<PlayerInventory, ItemStack> handler;
|
||||
|
||||
private ArmorType(int slot, Function<PlayerInventory, ItemStack> handler) {
|
||||
this.slot = slot;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public int getSlot() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
public ItemStack getItem(Player player) {
|
||||
return handler.apply(player.getInventory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to match the ArmorType for the specified ItemStack.
|
||||
*
|
||||
* @param itemStack
|
||||
* The ItemStack to parse the type of.
|
||||
* @return The parsed ArmorType. (null if none were found.)
|
||||
*/
|
||||
public static ArmorType matchType(ItemStack item) {
|
||||
if (item == null || item.getType().equals(Material.AIR))
|
||||
return null;
|
||||
|
||||
Material type = item.getType();
|
||||
String name = type.name();
|
||||
if (name.endsWith("HELMET") || name.endsWith("SKULL") || name.endsWith("HEAD") || type == VersionMaterial.PLAYER_HEAD.toMaterial() || type == Material.PUMPKIN)
|
||||
return HELMET;
|
||||
|
||||
else if (name.endsWith("CHESTPLATE"))
|
||||
return CHESTPLATE;
|
||||
|
||||
else if (name.endsWith("LEGGINGS"))
|
||||
return LEGGINGS;
|
||||
|
||||
else if (name.endsWith("BOOTS"))
|
||||
return BOOTS;
|
||||
|
||||
else
|
||||
return null;
|
||||
}
|
||||
}
|
@ -11,8 +11,6 @@ import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import com.codingforcookies.armorequip.ArmorListener;
|
||||
|
||||
import net.Indyuce.mmocore.api.ConfigFile;
|
||||
import net.Indyuce.mmocore.api.PlayerActionBar;
|
||||
import net.Indyuce.mmocore.api.debug.DebugMode;
|
||||
@ -284,8 +282,6 @@ public class MMOCore extends JavaPlugin {
|
||||
Bukkit.getPluginManager().registerEvents(new FishingListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new PlayerCollectStats(), this);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new ArmorListener(), this);
|
||||
|
||||
/*
|
||||
* initialize player data from all online players. this is very
|
||||
* important to do that after registering all the professses otherwise
|
||||
|
@ -12,12 +12,9 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import com.codingforcookies.armorequip.ArmorEquipEvent;
|
||||
|
||||
import net.Indyuce.mmocore.api.event.PlayerCombatEvent;
|
||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||
import net.Indyuce.mmocore.gui.api.PluginInventory;
|
||||
import net.mmogroup.mmolib.api.stat.SharedStat;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
@ -80,13 +77,4 @@ public class PlayerListener implements Listener {
|
||||
if (!event.entersCombat())
|
||||
event.getData().getSkillData().resetData();
|
||||
}
|
||||
|
||||
/*
|
||||
* updates the player's movement speed when equipping an armor to update the
|
||||
* speed malus reduction from the armors.
|
||||
*/
|
||||
@EventHandler
|
||||
public void g(ArmorEquipEvent event) {
|
||||
PlayerData.get(event.getPlayer()).getStats().getMap().update(SharedStat.MOVEMENT_SPEED);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user