diff --git a/src/com/dre/dungeonsxl/listener/PlayerListener.java b/src/com/dre/dungeonsxl/listener/PlayerListener.java index 3dcbe548..d51e2335 100644 --- a/src/com/dre/dungeonsxl/listener/PlayerListener.java +++ b/src/com/dre/dungeonsxl/listener/PlayerListener.java @@ -24,6 +24,7 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; import com.dre.dungeonsxl.DGSign; import com.dre.dungeonsxl.DGroup; import com.dre.dungeonsxl.DLootInventory; @@ -35,6 +36,7 @@ import com.dre.dungeonsxl.LeaveSign; import com.dre.dungeonsxl.game.GameChest; import com.dre.dungeonsxl.game.GameWorld; import com.dre.dungeonsxl.trigger.InteractTrigger; +import com.dre.dungeonsxl.trigger.UseItemTrigger; public class PlayerListener implements Listener { public P p = P.p; @@ -72,7 +74,8 @@ public class PlayerListener implements Listener { // Check Portals if (event.getItem() != null) { - if (event.getItem().getType() == Material.WOOD_SWORD) { + ItemStack item = event.getItem(); + if (item.getType() == Material.WOOD_SWORD) { if (clickedBlock != null) { for (DPortal dportal : DPortal.portals) { if (!dportal.isActive) { @@ -96,7 +99,7 @@ public class PlayerListener implements Listener { // Copy/Paste a Sign and Block-info if (EditWorld.get(player.getWorld()) != null) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { - if (event.getItem().getType() == Material.STICK) { + if (item.getType() == Material.STICK) { DPlayer dplayer = DPlayer.get(player); if (dplayer != null) { dplayer.poke(clickedBlock); @@ -105,6 +108,35 @@ public class PlayerListener implements Listener { } } } + + // Trigger UseItem Signs + GameWorld gworld = GameWorld.get(player.getWorld()); + if (gworld != null) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) { + if (UseItemTrigger.hasTriggers(gworld)) { + String name = null; + if (item.hasItemMeta()) { + if (item.getItemMeta().hasDisplayName()) { + name = item.getItemMeta().getDisplayName(); + } else if (item.getType() == Material.WRITTEN_BOOK || item.getType() == Material.BOOK_AND_QUILL) { + if (item.getItemMeta() instanceof BookMeta) { + BookMeta meta = (BookMeta) item.getItemMeta(); + if (meta.hasTitle()) { + name = meta.getTitle(); + } + } + } + } + if (name == null) { + name = item.getType().toString(); + } + UseItemTrigger trigger = UseItemTrigger.get(name, gworld); + if (trigger != null) { + trigger.onTrigger(player); + } + } + } + } } // Check Signs diff --git a/src/com/dre/dungeonsxl/trigger/Trigger.java b/src/com/dre/dungeonsxl/trigger/Trigger.java index 8a8c8270..4b63bb33 100644 --- a/src/com/dre/dungeonsxl/trigger/Trigger.java +++ b/src/com/dre/dungeonsxl/trigger/Trigger.java @@ -57,6 +57,12 @@ public abstract class Trigger { trigger = MobTrigger.getOrCreate(splitted[1], dsign.getGameWorld()); } + } else if (splitted[0].equalsIgnoreCase("U")) { + + if (splitted.length > 1) { + trigger = UseItemTrigger.getOrCreate(splitted[1], dsign.getGameWorld()); + } + } } return trigger; diff --git a/src/com/dre/dungeonsxl/trigger/UseItemTrigger.java b/src/com/dre/dungeonsxl/trigger/UseItemTrigger.java new file mode 100644 index 00000000..c632c573 --- /dev/null +++ b/src/com/dre/dungeonsxl/trigger/UseItemTrigger.java @@ -0,0 +1,77 @@ +package com.dre.dungeonsxl.trigger; + +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import com.dre.dungeonsxl.game.GameWorld; + + +public class UseItemTrigger extends Trigger { + + private static Map> triggers = new HashMap>(); + + private String name; + private String matchedName; + + public UseItemTrigger(String name) { + this.name = name; + Material mat = Material.matchMaterial(name); + if (mat != null) { + this.matchedName = mat.toString(); + } + } + + public void onTrigger(Player player) { + triggered = true; + this.player = player; + updateDSigns(); + } + + public void register(GameWorld gworld) { + if (!hasTriggers(gworld)) { + ArrayList list = new ArrayList(); + list.add(this); + triggers.put(gworld, list); + } else { + triggers.get(gworld).add(this); + } + } + + public void unregister(GameWorld gworld) { + if (hasTriggers(gworld)) { + triggers.get(gworld).remove(this); + } + } + + public static UseItemTrigger getOrCreate(String name, GameWorld gworld) { + UseItemTrigger trigger = get(name, gworld); + if (trigger != null) { + return trigger; + } + return new UseItemTrigger(name); + } + + public static UseItemTrigger get(String name, GameWorld gworld) { + if (hasTriggers(gworld)) { + for (UseItemTrigger trigger : triggers.get(gworld)) { + if (trigger.name.equalsIgnoreCase(name)) { + return trigger; + } else { + if (trigger.matchedName != null) { + if (trigger.matchedName.equalsIgnoreCase(name)) { + return trigger; + } + } + } + } + } + return null; + } + + public static boolean hasTriggers(GameWorld gworld) { + return !triggers.isEmpty() && triggers.containsKey(gworld); + } + +}