diff --git a/src/main/java/de/fanta/challenges/challenges/SammelFieberChallengeEvent.java b/src/main/java/de/fanta/challenges/challenges/SammelFieberChallengeEvent.java index 651e698..58042d4 100644 --- a/src/main/java/de/fanta/challenges/challenges/SammelFieberChallengeEvent.java +++ b/src/main/java/de/fanta/challenges/challenges/SammelFieberChallengeEvent.java @@ -1,9 +1,194 @@ package de.fanta.challenges.challenges; +import de.fanta.challenges.Challenges; +import de.fanta.challenges.events.TimerChangedEvent; +import de.fanta.challenges.guis.eventgui.SammelFieberSettingsGui; +import de.fanta.challenges.utils.ChatUtil; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.boss.BarColor; +import org.bukkit.boss.BarStyle; +import org.bukkit.boss.BossBar; +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.BlockBreakEvent; +import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.event.inventory.InventoryPickupItemEvent; +import org.bukkit.event.player.PlayerAttemptPickupItemEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.Objects; public class SammelFieberChallengeEvent implements Listener { - public static String name = "Sammel Fieber"; + public static BossBar bossBar; + public static Location hopperloc; + private static Material material; + private static boolean running; + private final Challenges plugin = Challenges.getPlugin(); + private int count; + + public static Location getHopperLocation() { + return hopperloc; + } + + public static void setHopperLocation(Location loc) { + SammelFieberChallengeEvent.hopperloc = loc; + } + + public static Material getMaterial() { + return material; + } + + public void setMaterial(Material mat) { + material = mat; + } + + public static boolean isRunning() { + return running; + } + + public void setRunning(boolean run) { + running = run; + } + + @EventHandler + public void onActivation(TimerChangedEvent event) { + if (Objects.equals(plugin.getConfig().getString("event.type"), "sammelfieber")) { + if (event.isRunning()) { + Material material = SammelFieberSettingsGui.getEventItem(); + if (material != Material.AIR && getHopperLocation() != null) { + setMaterial(material); + bossBar = Bukkit.createBossBar(ChatUtil.GREEN + "Es wurden " + ChatUtil.BLUE + count + " " + new ItemStack(getMaterial()).getI18NDisplayName() + ChatUtil.GREEN + " abgegeben. Das sind " + ChatUtil.BLUE + count * SammelFieberSettingsGui.MONEY + " Cubes", BarColor.GREEN, BarStyle.SOLID); + bossBar.setVisible(true); + for (Player pl : Bukkit.getOnlinePlayers()) { + bossBar.addPlayer(pl); + } + } else { + ChatUtil.sendErrorMessage(plugin.getCurrentEditor(), "Du musst erst ein Item und Hopper festlegen."); + plugin.getTimer().stopTimer(); + return; + } + setRunning(true); + } else { + setRunning(false); + } + } + } + + @EventHandler + public void onHoppergetItem(InventoryPickupItemEvent e) { + if (!isRunning()) { + return; + } + + Location loc = e.getInventory().getLocation(); + Location blockloc = new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); + if (!getHopperLocation().equals(blockloc)) { + return; + } + + ItemStack stack = e.getItem().getItemStack(); + + if (stack.getType() != getMaterial()) { + e.setCancelled(true); + return; + } + + ItemMeta meta = stack.getItemMeta(); + + if (meta != null && meta.hasDisplayName()) { + Player player = Bukkit.getPlayer(meta.getDisplayName()); + count = count + stack.getAmount(); + plugin.getScoreManager().updateScore(player, stack.getAmount()); + player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0F, 2.0F); + bossBar.setTitle(ChatUtil.GREEN + "Es wurden " + ChatUtil.BLUE + count + " " + new ItemStack(getMaterial()).getI18NDisplayName() + ChatUtil.GREEN + " abgegeben. Das sind " + ChatUtil.BLUE + count * SammelFieberSettingsGui.MONEY + " Cubes"); + } + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> e.getInventory().clear(), 1); + } + + @EventHandler + public void onHopperMoveItem(InventoryMoveItemEvent e) { + if (!isRunning()) { + return; + } + + Location loc = e.getSource().getLocation(); + Location blockloc = new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); + if (getHopperLocation().equals(blockloc)) { + e.setCancelled(true); + } + } + + @EventHandler + public void onInteract(PlayerInteractEvent e) { + if (!isRunning()) { + return; + } + if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { + Location loc = e.getClickedBlock().getLocation(); + Location blockloc = new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); + if (getHopperLocation().equals(blockloc)) { + e.setCancelled(true); + } + } + + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent e) { + if (!isRunning()) { + return; + } + + Location loc = e.getBlock().getLocation(); + Location blockloc = new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); + if (getHopperLocation().equals(blockloc)) { + e.setCancelled(true); + } + } + + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent e) { + if (!isRunning()) { + return; + } + + ItemStack stack = e.getItemDrop().getItemStack(); + + if (stack.getType() != getMaterial()) { + return; + } + + ItemMeta meta = stack.getItemMeta(); + meta.setDisplayName(e.getPlayer().getName()); + stack.setItemMeta(meta); + } + + @EventHandler + public void onPlayerPickUpItem(PlayerAttemptPickupItemEvent e) { + if (!isRunning()) { + return; + } + + ItemStack stack = e.getItem().getItemStack(); + + if (stack.getType() != getMaterial()) { + return; + } + + ItemMeta meta = stack.getItemMeta(); + if (meta != null && meta.hasDisplayName()) { + meta.setDisplayName(""); + stack.setItemMeta(meta); + } + } } diff --git a/src/main/java/de/fanta/challenges/guis/eventgui/SammelFieberSettingsGui.java b/src/main/java/de/fanta/challenges/guis/eventgui/SammelFieberSettingsGui.java index 40cf7d8..05994c2 100644 --- a/src/main/java/de/fanta/challenges/guis/eventgui/SammelFieberSettingsGui.java +++ b/src/main/java/de/fanta/challenges/guis/eventgui/SammelFieberSettingsGui.java @@ -1,51 +1,62 @@ package de.fanta.challenges.guis.eventgui; import de.fanta.challenges.Challenges; +import de.fanta.challenges.challenges.SammelFieberChallengeEvent; import de.fanta.challenges.utils.ChatUtil; import de.fanta.challenges.utils.guiutils.GUIUtils; import de.iani.cubesideutils.bukkit.items.CustomHeads; import org.bukkit.Bukkit; +import org.bukkit.FluidCollisionMode; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; + +import java.util.Objects; public class SammelFieberSettingsGui implements Listener { - private static final Challenges plugin = Challenges.getPlugin(); - public static final int INVENTORY_SIZE = 9; public static final Inventory SAMMEL_FIEBER_SETTINGS_GUI = Bukkit.createInventory(null, InventoryType.DISPENSER, Challenges.GUIPREFIX + " >> Sammel Fieber Settings"); - - public static int MONEY = 100; - + private static final Challenges plugin = Challenges.getPlugin(); private static final int MONEY_INDEX = 5; private static final int ADD_MONEY_INDEX = 2; private static final int REMOVE_MONEY_INDEX = 8; private static final int BACK_INDEX = 6; + private static final int SET_HOPPER_INDEX = 0; + private static final int ITEM_INDEX = 4; + public static int MONEY = 100; public static void createSammelFieberSettingsGUI(Player p) { + SAMMEL_FIEBER_SETTINGS_GUI.setItem(SET_HOPPER_INDEX, GUIUtils.createGuiItem(Material.HOPPER, ChatUtil.GREEN + "Set Hopper", ChatUtil.GREEN + "Du musst auf einen Hopper schauen.")); SAMMEL_FIEBER_SETTINGS_GUI.setItem(MONEY_INDEX, GUIUtils.createGuiItem(Material.GOLD_INGOT, ChatUtil.GREEN + "Money: " + MONEY)); - SAMMEL_FIEBER_SETTINGS_GUI.setItem(ADD_MONEY_INDEX, CustomHeads.QUARTZ_ARROW_UP.getHead()); - SAMMEL_FIEBER_SETTINGS_GUI.setItem(REMOVE_MONEY_INDEX, CustomHeads.QUARTZ_ARROW_DOWN.getHead()); - SAMMEL_FIEBER_SETTINGS_GUI.setItem(BACK_INDEX, CustomHeads.QUARTZ_ARROW_LEFT.getHead()); + SAMMEL_FIEBER_SETTINGS_GUI.setItem(ADD_MONEY_INDEX, CustomHeads.QUARTZ_ARROW_UP.getHead()); + SAMMEL_FIEBER_SETTINGS_GUI.setItem(REMOVE_MONEY_INDEX, CustomHeads.QUARTZ_ARROW_DOWN.getHead()); + SAMMEL_FIEBER_SETTINGS_GUI.setItem(BACK_INDEX, CustomHeads.QUARTZ_ARROW_LEFT.getHead()); for (int i = 0; i < INVENTORY_SIZE; i++) { - if (SAMMEL_FIEBER_SETTINGS_GUI.getItem(i) == null || SAMMEL_FIEBER_SETTINGS_GUI.getItem(i).getType() == Material.AIR) { - SAMMEL_FIEBER_SETTINGS_GUI.setItem(i, GUIUtils.EMPTY_ICON); + if (i != ITEM_INDEX) { + if (SAMMEL_FIEBER_SETTINGS_GUI.getItem(i) == null || SAMMEL_FIEBER_SETTINGS_GUI.getItem(i).getType() == Material.AIR) { + SAMMEL_FIEBER_SETTINGS_GUI.setItem(i, GUIUtils.EMPTY_ICON); + } } } p.openInventory(SAMMEL_FIEBER_SETTINGS_GUI); } + public static Material getEventItem() { + return Objects.requireNonNull(SAMMEL_FIEBER_SETTINGS_GUI.getItem(ITEM_INDEX)).getType(); + } + @EventHandler public void onInventoryClick(InventoryClickEvent e) { int slot = e.getRawSlot(); Player p = (Player) e.getWhoClicked(); - if (SAMMEL_FIEBER_SETTINGS_GUI.equals(e.getClickedInventory())) { + if (SAMMEL_FIEBER_SETTINGS_GUI.equals(e.getClickedInventory()) && slot != ITEM_INDEX) { if (plugin.getConfig().getBoolean("event.enabled")) { switch (slot) { case ADD_MONEY_INDEX -> MONEY = MONEY + 10; @@ -54,6 +65,16 @@ MONEY = MONEY - 10; } } + case SET_HOPPER_INDEX -> { + Block block = p.getTargetBlockExact(5, FluidCollisionMode.NEVER); + if (block != null && block.getType() == Material.HOPPER) { + Location loc = block.getLocation(); + SammelFieberChallengeEvent.setHopperLocation(new Location(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + ChatUtil.sendNormalMessage(p, "Hopper wurde gesetzt."); + } else { + ChatUtil.sendErrorMessage(p, "Du musst auf einen Hopper schauen!"); + } + } } createSammelFieberSettingsGUI(p); if (slot == BACK_INDEX) {