fix world delete errors, fix comparator update on item pickup

This commit is contained in:
jascotty2 2019-08-18 17:46:35 -05:00
parent 8e80c97747
commit 7854f01496
4 changed files with 59 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import org.bukkit.World;
public class DataManager {
@ -94,15 +95,19 @@ public class DataManager {
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery(selectSpawners);
while (result.next()) {
World world = Bukkit.getWorld(result.getString("world"));
if(world == null)
continue;
int spawnerId = result.getInt("id");
int amount = result.getInt("amount");
String world = result.getString("world");
int x = result.getInt("x");
int y = result.getInt("y");
int z = result.getInt("z");
Location location = new Location(Bukkit.getWorld(world), x, y, z);
Location location = new Location(world, x, y, z);
SpawnerStack spawnerStack = new SpawnerStack(location, amount);
spawnerStack.setId(spawnerId);

View File

@ -18,6 +18,7 @@ import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import org.bukkit.block.BlockState;
public class ItemListeners implements Listener {
@ -56,12 +57,12 @@ public class ItemListeners implements Listener {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onInvPickup(InventoryPickupItemEvent event) {
if (!Setting.STACK_ITEMS.getBoolean()) return;
int amount = Methods.getActualItemAmount(event.getItem());
if (amount <= 32) return;
if (!Setting.STACK_ITEMS.getBoolean() || !Methods.hasCustomAmount(event.getItem())) return;
event.setCancelled(true);
Methods.updateInventory(event.getItem(), event.getInventory());
if (event.getInventory().getHolder() instanceof BlockState)
Methods.updateAdjacentComparators(((BlockState)event.getInventory().getHolder()).getLocation());
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)

View File

@ -10,6 +10,7 @@ import org.bukkit.World;
import org.bukkit.block.CreatureSpawner;
import java.util.Random;
import java.util.logging.Level;
public class SpawnerStack {
@ -52,7 +53,7 @@ public class SpawnerStack {
Random random = new Random();
int count = 0;
for (int i = 0; i < getAmount(); i++) {
count += random.nextInt(3 - 1 + 1) + 1;
count += random.nextInt(3) + 1;
}
return count;
}

View File

@ -2,6 +2,7 @@ package com.songoda.ultimatestacker.utils;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.utils.settings.Setting;
import java.lang.reflect.Method;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;
@ -24,9 +25,10 @@ public class Methods {
public static void updateInventory(Item item, Inventory inventory) {
int amount = Methods.getActualItemAmount(item);
ItemStack itemStack = item.getItemStack();
final int maxStack = itemStack.getMaxStackSize();
while (amount > 0) {
int subtract = Math.min(amount, 64);
int subtract = Math.min(amount, maxStack);
amount -= subtract;
ItemStack newItem = itemStack.clone();
newItem.setAmount(subtract);
@ -43,6 +45,42 @@ public class Methods {
Methods.updateItemAmount(item, itemStack, amount);
}
private static Class<?> clazzCraftWorld, clazzCraftBlock, clazzBlockPosition;
private static Method getHandle, updateAdjacentComparators, getNMSBlock;
public static void updateAdjacentComparators(Location location) {
try {
// Cache reflection.
if (clazzCraftWorld == null) {
String ver = Bukkit.getServer().getClass().getPackage().getName().substring(23);
clazzCraftWorld = Class.forName("org.bukkit.craftbukkit." + ver + ".CraftWorld");
clazzCraftBlock = Class.forName("org.bukkit.craftbukkit." + ver + ".block.CraftBlock");
clazzBlockPosition = Class.forName("net.minecraft.server." + ver + ".BlockPosition");
Class<?> clazzWorld = Class.forName("net.minecraft.server." + ver + ".World");
Class<?> clazzBlock = Class.forName("net.minecraft.server." + ver + ".Block");
getHandle = clazzCraftWorld.getMethod("getHandle");
updateAdjacentComparators = clazzWorld.getMethod("updateAdjacentComparators", clazzBlockPosition, clazzBlock);
getNMSBlock = clazzCraftBlock.getDeclaredMethod("getNMSBlock");
getNMSBlock.setAccessible(true);
}
// invoke and cast objects.
Object craftWorld = clazzCraftWorld.cast(location.getWorld());
Object world = getHandle.invoke(craftWorld);
Object craftBlock = clazzCraftBlock.cast(location.getBlock());
// Invoke final method.
updateAdjacentComparators
.invoke(world, clazzBlockPosition.getConstructor(double.class, double.class, double.class)
.newInstance(location.getX(), location.getY(), location.getZ()),
getNMSBlock.invoke(craftBlock));
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
}
public static boolean isMaterialBlacklisted(Material type) {
List<String> whitelist = Setting.ITEM_WHITELIST.getStringList();
List<String> blacklist = Setting.ITEM_BLACKLIST.getStringList();
@ -85,6 +123,13 @@ public class Methods {
}
}
public static boolean hasCustomAmount(Item item) {
if (item.hasMetadata("US_AMT")) {
return item.getItemStack().getAmount() != item.getMetadata("US_AMT").get(0).asInt();
}
return false;
}
public static String compileItemName(ItemStack item, int amount) {
String nameFormat = Setting.NAME_FORMAT_ITEM.getString();
String displayName = Methods.formatText(UltimateStacker.getInstance().getItemFile().getConfig()