1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Improved code

This commit is contained in:
Rothes 2021-04-20 12:48:17 +08:00 committed by GitHub
parent 68dc6d560d
commit 37bb6005e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,11 +1,14 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting; package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -14,6 +17,7 @@ import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils; import io.github.thebusybiscuit.cscorelib2.inventory.InvUtils;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.api.events.AutoEnchantEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.api.items.settings.IntRangeSetting; import io.github.thebusybiscuit.slimefun4.api.items.settings.IntRangeSetting;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
@ -24,8 +28,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
/** /**
* The {@link AutoEnchanter}, in contrast to the {@link AutoDisenchanter}, adds * The {@link AutoEnchanter}, in contrast to the {@link AutoDisenchanter}, adds
@ -70,6 +73,13 @@ public class AutoEnchanter extends AContainer {
return null; return null;
} }
AutoEnchantEvent event = new AutoEnchantEvent(target);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return null;
}
ItemStack item = menu.getItemInSlot(slot); ItemStack item = menu.getItemInSlot(slot);
if (item != null && item.getType() == Material.ENCHANTED_BOOK && target != null) { if (item != null && item.getType() == Material.ENCHANTED_BOOK && target != null) {
@ -120,21 +130,26 @@ public class AutoEnchanter extends AContainer {
return null; return null;
} }
private boolean isEnchantable(ItemStack item) { private boolean isEnchantable(@Nullable ItemStack item) {
SlimefunItem sfItem = null; if (item == null) {
if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()) { return false;
for (String lore : cantEnchantLores.getValue()) { } else {
if (item.getItemMeta().getLore().contains(ChatColors.color(lore))) { ItemMeta itemMeta = item.getItemMeta();
return false; if (itemMeta != null && itemMeta.hasLore()) {
for (String lore : cantEnchantLores.getValue()) {
if (itemMeta.getLore().contains(ChatColors.color(lore))) {
return false;
}
} }
} }
} }
// stops endless checks of getByItem for enchanted book stacks. // stops endless checks of getByItem for enchanted book stacks.
if (item != null && item.getType() != Material.ENCHANTED_BOOK) { if (item.getType() != Material.ENCHANTED_BOOK) {
sfItem = SlimefunItem.getByItem(item); SlimefunItem sfItem = SlimefunItem.getByItem(item);
return sfItem == null || sfItem.isEnchantable();
} else {
return false;
} }
return sfItem == null || sfItem.isEnchantable();
} }
@Override @Override