From e8fa5ead913d26c89c2db45ecb9541cf3ac10c00 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 2 Feb 2022 09:34:04 +0100 Subject: [PATCH] Add EventType.DropItem Update FF Event Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 3 +- .../bausystem/features/script/EventType.java | 56 +++++++++---------- .../features/script/ScriptCommand.java | 2 +- .../features/script/ScriptEventListener.java | 24 ++++---- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 41329a18..870a1838 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -440,8 +440,9 @@ SCRIPT_GUI_CUSTOM_EVENTS_LORE_16 = §eTNTExplode §8-§7 x, y, z SCRIPT_GUI_CUSTOM_EVENTS_LORE_17 = §eTNTExplodeInBuild §8-§7 x, y, z SCRIPT_GUI_CUSTOM_EVENTS_LORE_18 = §eSelfJoin §8-§7 x, y, z, playerName SCRIPT_GUI_CUSTOM_EVENTS_LORE_19 = §eSelfLeave §8-§7 x, y, z, playerName +SCRIPT_GUI_CUSTOM_EVENTS_LORE_20 = §eDropItem §8-§7 material, x, y, z SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_1 = §e* §8-§7 Alles in den Klammern ist nur gesetzt, -SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_2 = §7wenn die Variable dafür gesetzt ist. +SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_2 = §7wenn die Variable davor auf §etrue§7 gesetzt ist. SCRIPT_GUI_OTHER = §eOther SCRIPT_GUI_OTHER_LORE_1 = §7Kommentare fangen mit §e#§7 an. diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/EventType.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/EventType.java index ee4e70e4..5aad2069 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/EventType.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/EventType.java @@ -21,15 +21,13 @@ package de.steamwar.bausystem.features.script; import de.steamwar.bausystem.features.script.variables.Value; import lombok.Getter; +import org.bukkit.Location; import org.bukkit.event.Event; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntitySpawnEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.player.PlayerSwapHandItemsEvent; +import org.bukkit.event.player.*; import java.util.HashMap; import java.util.Map; @@ -40,17 +38,13 @@ public enum EventType { FF(PlayerSwapHandItemsEvent.class, event -> null), PlaceBlock(BlockPlaceEvent.class, event -> { Map valueMap = new HashMap<>(); - valueMap.put("blockX", new Value.LongValue(event.getBlockPlaced().getX())); - valueMap.put("blockY", new Value.LongValue(event.getBlockPlaced().getY())); - valueMap.put("blockZ", new Value.LongValue(event.getBlockPlaced().getZ())); + addBlockXYZ(valueMap, event.getBlockPlaced().getLocation()); valueMap.put("blockType", new Value.StringValue(event.getBlockPlaced().getType().name())); return valueMap; }), BreakBlock(BlockBreakEvent.class, event -> { Map valueMap = new HashMap<>(); - valueMap.put("blockX", new Value.LongValue(event.getBlock().getX())); - valueMap.put("blockY", new Value.LongValue(event.getBlock().getY())); - valueMap.put("blockZ", new Value.LongValue(event.getBlock().getZ())); + addBlockXYZ(valueMap, event.getBlock().getLocation()); valueMap.put("blockType", new Value.StringValue(event.getBlock().getType().name())); return valueMap; }), @@ -61,9 +55,7 @@ public enum EventType { valueMap.put("handType", new Value.StringValue(event.getMaterial().name())); valueMap.put("hasBlock", new Value.BooleanValue(event.hasBlock())); if (event.hasBlock()) { - valueMap.put("blockX", new Value.LongValue(event.getClickedBlock().getX())); - valueMap.put("blockY", new Value.LongValue(event.getClickedBlock().getY())); - valueMap.put("blockZ", new Value.LongValue(event.getClickedBlock().getZ())); + addBlockXYZ(valueMap, event.getClickedBlock().getLocation()); valueMap.put("blockFace", new Value.StringValue(event.getBlockFace().name())); } return valueMap; @@ -75,9 +67,7 @@ public enum EventType { valueMap.put("handType", new Value.StringValue(event.getMaterial().name())); valueMap.put("hasBlock", new Value.BooleanValue(event.hasBlock())); if (event.hasBlock()) { - valueMap.put("blockX", new Value.LongValue(event.getClickedBlock().getX())); - valueMap.put("blockY", new Value.LongValue(event.getClickedBlock().getY())); - valueMap.put("blockZ", new Value.LongValue(event.getClickedBlock().getZ())); + addBlockXYZ(valueMap, event.getClickedBlock().getLocation()); valueMap.put("blockFace", new Value.StringValue(event.getBlockFace().name())); } return valueMap; @@ -85,33 +75,43 @@ public enum EventType { TNTSpawn(EntitySpawnEvent.class, event -> null), TNTExplode(EntityExplodeEvent.class, event -> { Map valueMap = new HashMap<>(); - valueMap.put("x", new Value.DoubleValue(event.getLocation().getX())); - valueMap.put("y", new Value.DoubleValue(event.getLocation().getY())); - valueMap.put("z", new Value.DoubleValue(event.getLocation().getZ())); + addXYZ(valueMap, event.getLocation()); return valueMap; }), TNTExplodeInBuild(EntityExplodeEvent.class, event -> { Map valueMap = new HashMap<>(); - valueMap.put("x", new Value.DoubleValue(event.getLocation().getX())); - valueMap.put("y", new Value.DoubleValue(event.getLocation().getY())); - valueMap.put("z", new Value.DoubleValue(event.getLocation().getZ())); + addXYZ(valueMap, event.getLocation()); return valueMap; }), SelfJoin(PlayerJoinEvent.class, event -> { Map valueMap = new HashMap<>(); - valueMap.put("x", new Value.DoubleValue(event.getPlayer().getLocation().getX())); - valueMap.put("y", new Value.DoubleValue(event.getPlayer().getLocation().getY())); - valueMap.put("z", new Value.DoubleValue(event.getPlayer().getLocation().getZ())); + addXYZ(valueMap, event.getPlayer().getLocation()); return valueMap; }), SelfLeave(PlayerQuitEvent.class, event -> { Map valueMap = new HashMap<>(); - valueMap.put("x", new Value.DoubleValue(event.getPlayer().getLocation().getX())); - valueMap.put("y", new Value.DoubleValue(event.getPlayer().getLocation().getY())); - valueMap.put("z", new Value.DoubleValue(event.getPlayer().getLocation().getZ())); + addXYZ(valueMap, event.getPlayer().getLocation()); + return valueMap; + }), + DropItem(PlayerDropItemEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("material", new Value.StringValue(event.getItemDrop().getItemStack().getType().name())); + addXYZ(valueMap, event.getItemDrop().getLocation()); return valueMap; }); + private static void addXYZ(Map valueMap, Location location) { + valueMap.put("x", new Value.DoubleValue(location.getX())); + valueMap.put("y", new Value.DoubleValue(location.getY())); + valueMap.put("z", new Value.DoubleValue(location.getZ())); + } + + private static void addBlockXYZ(Map valueMap, Location location) { + valueMap.put("blockX", new Value.LongValue(location.getBlockX())); + valueMap.put("blockY", new Value.LongValue(location.getBlockY())); + valueMap.put("blockZ", new Value.LongValue(location.getBlockZ())); + } + private Class eventType; private Function> eventValues; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index e185ff3d..b12fd889 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -55,7 +55,7 @@ public class ScriptCommand extends SWCommand { } swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_COMMANDS", p), loreBuilder(p, "SCRIPT_GUI_CUSTOM_COMMANDS_LORE_", 1, 13), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS", p), loreBuilder(p, "SCRIPT_GUI_CUSTOM_EVENTS_LORE_", 1, 19, loreBuilder(p, "SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_", 1, 2)), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_CUSTOM_EVENTS", p), loreBuilder(p, "SCRIPT_GUI_CUSTOM_EVENTS_LORE_", 1, 20, loreBuilder(p, "SCRIPT_GUI_CUSTOM_EVENTS_LORE_STAR_", 1, 2)), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, BauSystem.MESSAGE.parse("SCRIPT_GUI_OTHER", p), loreBuilder(p, "SCRIPT_GUI_OTHER_LORE_", 1, 9), false, clickType -> { }), null)); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java index fc7ebde8..442af91a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java @@ -36,13 +36,10 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntitySpawnEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.player.PlayerSwapHandItemsEvent; +import org.bukkit.event.player.*; -import java.util.HashSet; -import java.util.Set; +import java.util.HashMap; +import java.util.Map; @Linked(LinkageType.LISTENER) public class ScriptEventListener implements Listener { @@ -50,10 +47,12 @@ public class ScriptEventListener implements Listener { @LinkedInstance private CustomScriptManager customScriptManager; - private static final Set LAST_FS = new HashSet<>(); + private static final Map LAST_FS = new HashMap<>(); { - Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), LAST_FS::clear, 20, 20); + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { + LAST_FS.entrySet().removeIf(entry -> System.currentTimeMillis() - entry.getValue() > 200); + }, 2, 2); } @EventHandler @@ -68,10 +67,10 @@ public class ScriptEventListener implements Listener { @EventHandler public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { - if (LAST_FS.contains(event.getPlayer())) { + if (LAST_FS.containsKey(event.getPlayer())) { customScriptManager.callEvent(EventType.FF, event.getPlayer(), event); } else { - LAST_FS.add(event.getPlayer()); + LAST_FS.put(event.getPlayer(), System.currentTimeMillis()); } } @@ -122,4 +121,9 @@ public class ScriptEventListener implements Listener { } } } + + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) { + customScriptManager.callEvent(EventType.DropItem, event.getPlayer(), event); + } }