mirror of
https://github.com/songoda/UltimateStacker.git
synced 2024-09-29 06:57:33 +02:00
fix world delete errors, fix comparator update on item pickup
This commit is contained in:
parent
8e80c97747
commit
7854f01496
@ -10,6 +10,7 @@ import java.util.Collection;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import org.bukkit.World;
|
||||||
|
|
||||||
public class DataManager {
|
public class DataManager {
|
||||||
|
|
||||||
@ -94,15 +95,19 @@ public class DataManager {
|
|||||||
try (Statement statement = connection.createStatement()) {
|
try (Statement statement = connection.createStatement()) {
|
||||||
ResultSet result = statement.executeQuery(selectSpawners);
|
ResultSet result = statement.executeQuery(selectSpawners);
|
||||||
while (result.next()) {
|
while (result.next()) {
|
||||||
|
World world = Bukkit.getWorld(result.getString("world"));
|
||||||
|
|
||||||
|
if(world == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
int spawnerId = result.getInt("id");
|
int spawnerId = result.getInt("id");
|
||||||
|
|
||||||
int amount = result.getInt("amount");
|
int amount = result.getInt("amount");
|
||||||
|
|
||||||
String world = result.getString("world");
|
|
||||||
int x = result.getInt("x");
|
int x = result.getInt("x");
|
||||||
int y = result.getInt("y");
|
int y = result.getInt("y");
|
||||||
int z = result.getInt("z");
|
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 spawnerStack = new SpawnerStack(location, amount);
|
||||||
spawnerStack.setId(spawnerId);
|
spawnerStack.setId(spawnerId);
|
||||||
|
@ -18,6 +18,7 @@ import org.bukkit.event.player.PlayerPickupItemEvent;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.bukkit.block.BlockState;
|
||||||
|
|
||||||
public class ItemListeners implements Listener {
|
public class ItemListeners implements Listener {
|
||||||
|
|
||||||
@ -56,12 +57,12 @@ public class ItemListeners implements Listener {
|
|||||||
|
|
||||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||||
public void onInvPickup(InventoryPickupItemEvent event) {
|
public void onInvPickup(InventoryPickupItemEvent event) {
|
||||||
if (!Setting.STACK_ITEMS.getBoolean()) return;
|
if (!Setting.STACK_ITEMS.getBoolean() || !Methods.hasCustomAmount(event.getItem())) return;
|
||||||
int amount = Methods.getActualItemAmount(event.getItem());
|
|
||||||
if (amount <= 32) return;
|
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|
||||||
Methods.updateInventory(event.getItem(), event.getInventory());
|
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)
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||||
|
@ -10,6 +10,7 @@ import org.bukkit.World;
|
|||||||
import org.bukkit.block.CreatureSpawner;
|
import org.bukkit.block.CreatureSpawner;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class SpawnerStack {
|
public class SpawnerStack {
|
||||||
|
|
||||||
@ -52,7 +53,7 @@ public class SpawnerStack {
|
|||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (int i = 0; i < getAmount(); i++) {
|
for (int i = 0; i < getAmount(); i++) {
|
||||||
count += random.nextInt(3 - 1 + 1) + 1;
|
count += random.nextInt(3) + 1;
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.songoda.ultimatestacker.utils;
|
|||||||
|
|
||||||
import com.songoda.ultimatestacker.UltimateStacker;
|
import com.songoda.ultimatestacker.UltimateStacker;
|
||||||
import com.songoda.ultimatestacker.utils.settings.Setting;
|
import com.songoda.ultimatestacker.utils.settings.Setting;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import org.bukkit.*;
|
import org.bukkit.*;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.CreatureSpawner;
|
import org.bukkit.block.CreatureSpawner;
|
||||||
@ -24,9 +25,10 @@ public class Methods {
|
|||||||
public static void updateInventory(Item item, Inventory inventory) {
|
public static void updateInventory(Item item, Inventory inventory) {
|
||||||
int amount = Methods.getActualItemAmount(item);
|
int amount = Methods.getActualItemAmount(item);
|
||||||
ItemStack itemStack = item.getItemStack();
|
ItemStack itemStack = item.getItemStack();
|
||||||
|
final int maxStack = itemStack.getMaxStackSize();
|
||||||
|
|
||||||
while (amount > 0) {
|
while (amount > 0) {
|
||||||
int subtract = Math.min(amount, 64);
|
int subtract = Math.min(amount, maxStack);
|
||||||
amount -= subtract;
|
amount -= subtract;
|
||||||
ItemStack newItem = itemStack.clone();
|
ItemStack newItem = itemStack.clone();
|
||||||
newItem.setAmount(subtract);
|
newItem.setAmount(subtract);
|
||||||
@ -43,6 +45,42 @@ public class Methods {
|
|||||||
Methods.updateItemAmount(item, itemStack, amount);
|
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) {
|
public static boolean isMaterialBlacklisted(Material type) {
|
||||||
List<String> whitelist = Setting.ITEM_WHITELIST.getStringList();
|
List<String> whitelist = Setting.ITEM_WHITELIST.getStringList();
|
||||||
List<String> blacklist = Setting.ITEM_BLACKLIST.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) {
|
public static String compileItemName(ItemStack item, int amount) {
|
||||||
String nameFormat = Setting.NAME_FORMAT_ITEM.getString();
|
String nameFormat = Setting.NAME_FORMAT_ITEM.getString();
|
||||||
String displayName = Methods.formatText(UltimateStacker.getInstance().getItemFile().getConfig()
|
String displayName = Methods.formatText(UltimateStacker.getInstance().getItemFile().getConfig()
|
||||||
|
Loading…
Reference in New Issue
Block a user