Fix use item trigger for block interactions; resolves #593

This commit is contained in:
Daniel Saukel 2019-06-19 02:39:28 +02:00
parent 0684e3b34a
commit 0380e5c527
2 changed files with 51 additions and 31 deletions

View File

@ -25,7 +25,6 @@ import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.config.MainConfig;
import de.erethon.dungeonsxl.game.Game;
import de.erethon.dungeonsxl.mob.DMob;
import de.erethon.dungeonsxl.trigger.UseItemTrigger;
import de.erethon.dungeonsxl.util.ParsingUtil;
import de.erethon.dungeonsxl.world.DEditWorld;
import de.erethon.dungeonsxl.world.DGameWorld;
@ -58,7 +57,6 @@ 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;
/**
* @author Daniel Saukel, Frank Baumann, Milan Albrecht
@ -586,34 +584,6 @@ public class DPlayerListener implements Listener {
}
}
}
// Trigger UseItem Signs
DGameWorld gameWorld = DGameWorld.getByWorld(player.getWorld());
if (gameWorld != null) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
String name = null;
if (item.hasItemMeta()) {
if (item.getItemMeta().hasDisplayName()) {
name = item.getItemMeta().getDisplayName();
} else if (VanillaItem.WRITTEN_BOOK.is(item) || VanillaItem.WRITABLE_BOOK.is(item)) {
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.getByName(name, gameWorld);
if (trigger != null) {
trigger.onTrigger(player);
}
}
}
}
}

View File

@ -16,11 +16,18 @@
*/
package de.erethon.dungeonsxl.trigger;
import de.erethon.caliburn.item.ExItem;
import de.erethon.caliburn.item.VanillaItem;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.world.DGameWorld;
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.block.BlockRedstoneEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.scheduler.BukkitRunnable;
/**
@ -35,7 +42,7 @@ public class TriggerListener implements Listener {
}
@EventHandler
public void onRedstoneEvent(final BlockRedstoneEvent event) {
public void onBlockRedstone(final BlockRedstoneEvent event) {
new BukkitRunnable() {
@Override
public void run() {
@ -48,4 +55,47 @@ public class TriggerListener implements Listener {
}.runTaskLater(plugin, 1L);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
DGameWorld gameWorld = DGameWorld.getByWorld(player.getWorld());
if (gameWorld == null) {
return;
}
if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_AIR) {
return;
}
ItemStack item = event.getItem();
if (item == null) {
return;
}
String name = null;
if (item.hasItemMeta()) {
if (item.getItemMeta().hasDisplayName()) {
name = item.getItemMeta().getDisplayName();
} else if (VanillaItem.WRITTEN_BOOK.is(item) || VanillaItem.WRITABLE_BOOK.is(item)) {
if (item.getItemMeta() instanceof BookMeta) {
BookMeta meta = (BookMeta) item.getItemMeta();
if (meta.hasTitle()) {
name = meta.getTitle();
}
}
}
}
if (name == null) {
ExItem exItem = plugin.getCaliburn().getExItem(item);
if (item != null) {
name = exItem.getName();
}
}
UseItemTrigger trigger = UseItemTrigger.getByName(name, gameWorld);
if (trigger != null) {
trigger.onTrigger(player);
}
}
}