Fix automatic lapis in enchanting tables
This commit is contained in:
1 parent
4aa3c1db59
commit
5e5e5ba435
1 file changed
+96
-2
@@ -1,14 +1,26 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
package net.trueog.oldenchantog;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.enchantment.EnchantItemEvent;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryAction;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent;
|
||||
import org.bukkit.inventory.EnchantingInventory;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
/**
|
||||
@@ -20,6 +32,9 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
*/
|
||||
public class OldEnchantOG extends JavaPlugin implements Listener {
|
||||
|
||||
private static final int ENCHANTING_LAPIS_SLOT = 1;
|
||||
private static final int AUTO_LAPIS_AMOUNT = 64;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
@@ -27,16 +42,59 @@ public class OldEnchantOG extends JavaPlugin implements Listener {
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void inventoryOpen(InventoryOpenEvent event) {
|
||||
|
||||
fillUpEnchantingTable(event.getInventory());
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void inventoryClick(InventoryClickEvent event) {
|
||||
|
||||
Inventory inventory = event.getInventory();
|
||||
if (inventory.getType().equals(InventoryType.ENCHANTING) && event.getRawSlot() == 1) {
|
||||
if (!inventory.getType().equals(InventoryType.ENCHANTING)) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if (event.getRawSlot() == ENCHANTING_LAPIS_SLOT) {
|
||||
|
||||
event.setCancelled(true);
|
||||
|
||||
} else if (event.getClick() == ClickType.DOUBLE_CLICK && isLapis(event.getCursor())) {
|
||||
|
||||
event.setCancelled(true);
|
||||
|
||||
} else if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && isLapis(event.getCurrentItem())) {
|
||||
|
||||
event.setCancelled(true);
|
||||
|
||||
}
|
||||
|
||||
refillEnchantingTableNextTick(inventory);
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void inventoryDrag(InventoryDragEvent event) {
|
||||
|
||||
Inventory inventory = event.getInventory();
|
||||
if (!inventory.getType().equals(InventoryType.ENCHANTING)) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
if (event.getRawSlots().contains(ENCHANTING_LAPIS_SLOT) || isLapis(event.getOldCursor())) {
|
||||
|
||||
event.setCancelled(true);
|
||||
|
||||
}
|
||||
|
||||
refillEnchantingTableNextTick(inventory);
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@@ -45,7 +103,7 @@ public class OldEnchantOG extends JavaPlugin implements Listener {
|
||||
Inventory inventory = event.getInventory();
|
||||
if (inventory.getType().equals(InventoryType.ENCHANTING)) {
|
||||
|
||||
inventory.setItem(1, null);
|
||||
((EnchantingInventory) inventory).setSecondary(null);
|
||||
|
||||
}
|
||||
|
||||
@@ -59,6 +117,42 @@ public class OldEnchantOG extends JavaPlugin implements Listener {
|
||||
|
||||
event.getEnchanter().setLevel(newLevel);
|
||||
event.setExpLevelCost(1);
|
||||
((EnchantingInventory) event.getInventory()).setSecondary(getLapis());
|
||||
|
||||
}
|
||||
|
||||
private void fillUpEnchantingTable(Inventory inventory) {
|
||||
|
||||
if (inventory == null || inventory.getType() != InventoryType.ENCHANTING) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
EnchantingInventory enchantingInventory = (EnchantingInventory) inventory;
|
||||
if (!isLapis(enchantingInventory.getSecondary()) || enchantingInventory.getSecondary().getAmount() != AUTO_LAPIS_AMOUNT) {
|
||||
|
||||
enchantingInventory.setSecondary(getLapis());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void refillEnchantingTableNextTick(Inventory inventory) {
|
||||
|
||||
Bukkit.getScheduler().runTask(this, () -> fillUpEnchantingTable(inventory));
|
||||
|
||||
}
|
||||
|
||||
private ItemStack getLapis() {
|
||||
|
||||
return new ItemStack(Material.LAPIS_LAZULI, AUTO_LAPIS_AMOUNT);
|
||||
|
||||
}
|
||||
|
||||
private boolean isLapis(ItemStack item) {
|
||||
|
||||
return item != null && item.getType() == Material.LAPIS_LAZULI;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user