From 41c6782635f4b5e501e1e8088ab5c8399c2bbdd0 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Fri, 27 May 2016 21:38:15 +0200 Subject: [PATCH] Implemented puke item despawn time, defaults to 1 min --- resources/config/de/config.yml | 4 + resources/config/en/config.yml | 4 + resources/config/fr/config.yml | 4 + src/com/dre/brewery/BPlayer.java | 80 +++++++++++++++---- src/com/dre/brewery/P.java | 1 + .../dre/brewery/filedata/ConfigUpdater.java | 52 +++++++++++- 6 files changed, 128 insertions(+), 17 deletions(-) diff --git a/resources/config/de/config.yml b/resources/config/de/config.yml index 6cd7a1e..4934417 100644 --- a/resources/config/de/config.yml +++ b/resources/config/de/config.yml @@ -35,6 +35,10 @@ enablePuke: true # Item das beim Erbrechen mehrfach unaufsammelbar fallen gelassen wird [SOUL_SAND] pukeItem: SOUL_SAND +# Zeit in Sekunden bis die pukeitems despawnen, (mc standard wäre 300 = 5 min) [60] +# Wurde die item Despawnzeit in der spigot.yml verändert, verändert sich auch die pukeDespawnzeit in Abhängigkeit. +pukeDespawntime: 60 + # Konsumierbares Item/Stärke. Senkt den Alkoholpegel um wenn konsumiert. drainItems: - BREAD/4 diff --git a/resources/config/en/config.yml b/resources/config/en/config.yml index 7752e71..501510d 100644 --- a/resources/config/en/config.yml +++ b/resources/config/en/config.yml @@ -35,6 +35,10 @@ enablePuke: true # Item that is dropped multiple times uncollectable when puking [SOUL_SAND] pukeItem: SOUL_SAND +# Time in seconds until the pukeitems despawn, (mc default is 300 = 5 min) [60] +# If the item despawn time was changed in the spigot.yml, the pukeDespawntime changes as well. +pukeDespawntime: 60 + # Consumable Item/strength. Decreases the alcohol level by when consumed. (list) drainItems: - BREAD/4 diff --git a/resources/config/fr/config.yml b/resources/config/fr/config.yml index 04fbebc..8727779 100644 --- a/resources/config/fr/config.yml +++ b/resources/config/fr/config.yml @@ -35,6 +35,10 @@ enablePuke: true # L'objet utilisé pour représenter le vomit [SOUL_SAND] pukeItem: SOUL_SAND +# Time in seconds until the pukeitems despawn, (mc default is 300 = 5 min) [60] +# If the item despawn time was changed in the spigot.yml, the pukeDespawntime changes as well. +pukeDespawntime: 60 + # Consommables Objet/Force. Réduit le montant d'alcool par lors de la consommation. (list) drainItems: - BREAD/4 diff --git a/src/com/dre/brewery/BPlayer.java b/src/com/dre/brewery/BPlayer.java index 8a7ffea..f49e964 100644 --- a/src/com/dre/brewery/BPlayer.java +++ b/src/com/dre/brewery/BPlayer.java @@ -1,10 +1,6 @@ package com.dre.brewery; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.UUID; +import org.apache.commons.lang.mutable.MutableInt; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -17,14 +13,24 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffectType; import org.bukkit.util.Vector; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + public class BPlayer { private static Map players = new HashMap<>();// Players name/uuid and BPlayer - private static Map pTasks = new HashMap<>();// Player and count + private static Map pTasks = new HashMap<>();// Player and count private static int taskId; + private static boolean modAge = true; + private static Random pukeRand; + private static Method gh; + private static Field age; // Settings public static Map drainItems = new HashMap<>();// DrainItem Material and Strength public static Material pukeItem; + public static int pukeDespawntime; public static int hangoverTime; public static boolean overdrinkKick; public static boolean enableHome; @@ -379,17 +385,21 @@ public class BPlayer { } }, 1L, 1L); } - pTasks.put(player, count); + pTasks.put(player, new MutableInt(count)); } public static void pukeTask() { - for (Player player : pTasks.keySet()) { + for (Iterator> iter = pTasks.entrySet().iterator(); iter.hasNext(); ) { + Map.Entry entry = iter.next(); + Player player = entry.getKey(); + MutableInt count = entry.getValue(); + if (!player.isValid() || !player.isOnline()) { + iter.remove(); + } puke(player); - int newCount = pTasks.get(player) - 1; - if (newCount == 0) { - pTasks.remove(player); - } else { - pTasks.put(player, newCount); + count.decrement(); + if (count.intValue() <= 0) { + iter.remove(); } } if (pTasks.isEmpty()) { @@ -398,15 +408,55 @@ public class BPlayer { } public static void puke(Player player) { + if (pukeRand == null) { + pukeRand = new Random(); + } + if (pukeItem == null || pukeItem == Material.AIR) { + pukeItem = Material.SOUL_SAND; + } Location loc = player.getLocation(); + loc.setY(loc.getY() + 1.1); + loc.setPitch(loc.getPitch() - 10 + pukeRand.nextInt(20)); + loc.setYaw(loc.getYaw() - 10 + pukeRand.nextInt(20)); Vector direction = loc.getDirection(); direction.multiply(0.5); - loc.setY(loc.getY() + 1.5); - loc.setPitch(loc.getPitch() + 10); loc.add(direction); Item item = player.getWorld().dropItem(loc, new ItemStack(pukeItem)); item.setVelocity(direction); item.setPickupDelay(32767); // Item can never be picked up when pickup delay is 32767 + //item.setTicksLived(6000 - pukeDespawntime); // Well this does not work... + if (modAge) { + if (pukeDespawntime >= 5800) { + return; + } + try { + if (gh == null) { + gh = Class.forName(P.p.getServer().getClass().getPackage().getName() + ".entity.CraftItem").getMethod("getHandle", (Class[]) null); + } + Object entityItem = gh.invoke(item, (Object[]) null); + if (age == null) { + age = entityItem.getClass().getDeclaredField("age"); + age.setAccessible(true); + } + + // Setting the age determines when an item is despawned. At age 6000 it is removed. + if (pukeDespawntime <= 0) { + // Just show the item for a tick + age.setInt(entityItem, 5999); + } else if (pukeDespawntime <= 120) { + // it should despawn in less than 6 sec. Add up to half of that randomly + age.setInt(entityItem, 6000 - pukeDespawntime + pukeRand.nextInt((int) (pukeDespawntime / 2F))); + } else { + // Add up to 5 sec randomly + age.setInt(entityItem, 6000 - pukeDespawntime + pukeRand.nextInt(100)); + } + return; + } catch (InvocationTargetException | ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { + e.printStackTrace(); + } + modAge = false; + P.p.errorLog("Failed to set Despawn Time on item " + pukeItem.name()); + } } diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index a6e1635..0f7a01d 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -287,6 +287,7 @@ public class P extends JavaPlugin { BPlayer.enableHome = config.getBoolean("enableHome", false); BPlayer.enableLoginDisallow = config.getBoolean("enableLoginDisallow", false); BPlayer.enablePuke = config.getBoolean("enablePuke", false); + BPlayer.pukeDespawntime = config.getInt("pukeDespawntime", 60) * 20; BPlayer.homeType = config.getString("homeType", null); Brew.colorInBarrels = config.getBoolean("colorInBarrels", false); Brew.colorInBrewer = config.getBoolean("colorInBrewer", false); diff --git a/src/com/dre/brewery/filedata/ConfigUpdater.java b/src/com/dre/brewery/filedata/ConfigUpdater.java index 7792bb6..c4efec0 100644 --- a/src/com/dre/brewery/filedata/ConfigUpdater.java +++ b/src/com/dre/brewery/filedata/ConfigUpdater.java @@ -683,6 +683,30 @@ public class ConfigUpdater { setLine(index, "# Das Item kann nicht aufgesammelt werden und bleibt bis zum Despawnen liegen."); } + String[] lines = new String[] { "", + "# Zeit in Sekunden bis die pukeitems despawnen, (mc standard wäre 300 = 5 min) [60]", + "# Wurde die item Despawnzeit in der spigot.yml verändert, verändert sich auch die pukeDespawnzeit in Abhängigkeit.", + "pukeDespawntime: 60" }; + + index = indexOfStart("pukeItem:"); + if (index == -1) { + index = indexOfStart("enablePuke:"); + if (index == -1) { + index = indexOfStart("# Konsumierbares Item") - 1; + if (index == -2) { + index = indexOfStart("enableKickOnOverdrink:"); + if (index == -1) { + index = indexOfStart("language:"); + } + } + } + } + if (index == -1) { + appendLines(lines); + } else { + addLines(index + 1, lines); + } + index = indexOfStart("# Färben der Iteminformationen je nach Qualität während sie sich 1. im Fass und/oder 2. im Braustand befinden [true, false]"); if (index != -1) { setLine(index, "# Färben der Iteminformationen je nach Qualität während sie sich 1. im Fass und/oder 2. im Braustand befinden [true, true]"); @@ -698,7 +722,7 @@ public class ConfigUpdater { setLine(index, "# Eine Liste von allen Materialien kann hier gefunden werden: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html"); } - String[] lines = new String[] { "# Wenn Vault installiert ist können normale englische Item Namen verwendet werden, anstatt Material, ID und Data!", + lines = new String[] { "# Wenn Vault installiert ist können normale englische Item Namen verwendet werden, anstatt Material, ID und Data!", "# Vault erkennt Namen wie \"Jungle Leaves\" anstatt \"LEAVES,3\". Dies macht es viel einfacher!" }; index = indexOfStart("# Es kann ein Data-Wert angegeben werden, weglassen"); @@ -781,6 +805,30 @@ public class ConfigUpdater { setLine(index, "# The item can not be collected and stays on the ground until it despawns."); } + String[] lines = new String[] { "", + "# Time in seconds until the pukeitems despawn, (mc default is 300 = 5 min) [60]", + "# If the item despawn time was changed in the spigot.yml, the pukeDespawntime changes as well.", + "pukeDespawntime: 60" }; + + index = indexOfStart("pukeItem:"); + if (index == -1) { + index = indexOfStart("enablePuke:"); + if (index == -1) { + index = indexOfStart("# Consumable Item") - 1; + if (index == -2) { + index = indexOfStart("enableKickOnOverdrink:"); + if (index == -1) { + index = indexOfStart("language:"); + } + } + } + } + if (index == -1) { + appendLines(lines); + } else { + addLines(index + 1, lines); + } + index = indexOfStart("# Color the Item information (lore) depending on quality while it is 1. in a barrel and/or 2. in a brewing stand [true, false]"); if (index != -1) { setLine(index, "# Color the Item information (lore) depending on quality while it is 1. in a barrel and/or 2. in a brewing stand [true, true]"); @@ -796,7 +844,7 @@ public class ConfigUpdater { setLine(index, "# A list of materials can be found here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html"); } - String[] lines = new String[] { "# If Vault is installed normal names can be used instead of material or id, so using Vault is highly recommended.", + lines = new String[] { "# If Vault is installed normal names can be used instead of material or id, so using Vault is highly recommended.", "# Vault will recognize things like \"Jungle Leaves\" instead of \"LEAVES,3\"" }; index = indexOfStart("# You can specify a data value, omitting");