Implemented puke item despawn time, defaults to 1 min

This commit is contained in:
Sn0wStorm
2016-05-27 21:38:15 +02:00
parent 581dd69f38
commit 41c6782635
6 changed files with 128 additions and 17 deletions
+65 -15
View File
@@ -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<String, BPlayer> players = new HashMap<>();// Players name/uuid and BPlayer
private static Map<Player, Integer> pTasks = new HashMap<>();// Player and count
private static Map<Player, MutableInt> 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<Material, Integer> 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<Map.Entry<Player, MutableInt>> iter = pTasks.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<Player, MutableInt> 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());
}
}