From ec9133cf8a354f32fe9857f95896cdeb64389598 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sat, 5 Sep 2020 14:45:42 +0200 Subject: [PATCH] Small improvements --- CHANGELOG.md | 2 + .../implementation/items/RadioactiveItem.java | 3 +- .../items/electric/gadgets/SolarHelmet.java | 15 +++++--- .../generators/MagnesiumGenerator.java | 2 +- .../electric/machines/AutoDisenchanter.java | 18 +++++---- .../Slimefun/api/SlimefunItemStack.java | 37 +++++++++++-------- 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d022da502..a8440dcdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ * (API) Rewritten Block-Energy API * Removed "durability" setting from cargo nodes * Small performance improvements for radiation +* Small performance improvements for Auto Disenchanters +* Magnesium Salt in Magnesium-Salt generators now lasts longer #### Fixes * Fixed Programmable Androids rotating in the wrong direction diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/RadioactiveItem.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/RadioactiveItem.java index cd1fbe091..a17c61a3d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/RadioactiveItem.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/RadioactiveItem.java @@ -1,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.inventory.ItemStack; @@ -68,7 +69,7 @@ public class RadioactiveItem extends SlimefunItem implements Radioactive, NotPla * The recipe output */ @ParametersAreNonnullByDefault - public RadioactiveItem(Category category, Radioactivity radioactivity, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) { + public RadioactiveItem(Category category, Radioactivity radioactivity, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, @Nullable ItemStack recipeOutput) { super(category, item, recipeType, recipe, recipeOutput); this.radioactivity = radioactivity; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/SolarHelmet.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/SolarHelmet.java index a236e8512..0c88a37be 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/SolarHelmet.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/SolarHelmet.java @@ -6,6 +6,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; import io.github.thebusybiscuit.slimefun4.core.attributes.Rechargeable; @@ -51,12 +52,14 @@ public class SolarHelmet extends SlimefunItem { * The {@link Player} wearing this {@link SolarHelmet} */ public void rechargeItems(@Nonnull Player p) { - recharge(p.getInventory().getHelmet()); - recharge(p.getInventory().getChestplate()); - recharge(p.getInventory().getLeggings()); - recharge(p.getInventory().getBoots()); - recharge(p.getInventory().getItemInMainHand()); - recharge(p.getInventory().getItemInOffHand()); + PlayerInventory inv = p.getInventory(); + + recharge(inv.getHelmet()); + recharge(inv.getChestplate()); + recharge(inv.getLeggings()); + recharge(inv.getBoots()); + recharge(inv.getItemInMainHand()); + recharge(inv.getItemInOffHand()); } private void recharge(@Nullable ItemStack item) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java index 493c0a3c8..ad4974988 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/generators/MagnesiumGenerator.java @@ -18,7 +18,7 @@ public abstract class MagnesiumGenerator extends AGenerator { @Override protected void registerDefaultFuelTypes() { - registerFuel(new MachineFuel(12, SlimefunItems.MAGNESIUM_SALT)); + registerFuel(new MachineFuel(20, SlimefunItems.MAGNESIUM_SALT)); } @Override diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java index a80d022f3..53c5e61b7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoDisenchanter.java @@ -81,7 +81,7 @@ public class AutoDisenchanter extends AContainer { ItemStack target = menu.getItemInSlot(slot == getInputSlots()[0] ? getInputSlots()[1] : getInputSlots()[0]); // Disenchanting - if (item != null && target != null && target.getType() == Material.BOOK) { + if (target != null && target.getType() == Material.BOOK) { int amount = 0; for (Map.Entry entry : item.getEnchantments().entrySet()) { @@ -145,14 +145,18 @@ public class AutoDisenchanter extends AContainer { } private boolean isDisenchantable(ItemStack item) { - SlimefunItem sfItem = null; - - // stops endless checks of getByItem for empty book stacks. - if (item != null && item.getType() != Material.BOOK) { - sfItem = SlimefunItem.getByItem(item); + if (item == null) { + return false; + } + // stops endless checks of getByItem for books + else if (item.getType() != Material.BOOK) { + SlimefunItem sfItem = SlimefunItem.getByItem(item); + return sfItem == null || sfItem.isDisenchantable(); + } + else { + return true; } - return sfItem == null || sfItem.isDisenchantable(); } @Override diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java b/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java index c8dfb8ed4..916e4780c 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/SlimefunItemStack.java @@ -41,19 +41,19 @@ public class SlimefunItemStack extends CustomItem { private boolean locked = false; private String texture = null; - public SlimefunItemStack(String id, Material type, String name, String... lore) { + public SlimefunItemStack(@Nonnull String id, @Nonnull Material type, @Nullable String name, String... lore) { super(type, name, lore); setItemId(id); } - public SlimefunItemStack(String id, Material type, Color color, String name, String... lore) { + public SlimefunItemStack(@Nonnull String id, @Nonnull Material type, @Nonnull Color color, @Nullable String name, String... lore) { super(new ItemStack(type), color, name, lore); setItemId(id); } - public SlimefunItemStack(String id, Color color, PotionEffect effect, String name, String... lore) { + public SlimefunItemStack(@Nonnull String id, @Nonnull Color color, @Nonnull PotionEffect effect, @Nullable String name, String... lore) { super(Material.POTION, im -> { if (name != null) { im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); @@ -82,30 +82,30 @@ public class SlimefunItemStack extends CustomItem { setItemId(id); } - public SlimefunItemStack(String id, ItemStack item, String name, String... lore) { + public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item, @Nullable String name, String... lore) { super(item, name, lore); setItemId(id); } - public SlimefunItemStack(String id, ItemStack item) { + public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item) { super(item); setItemId(id); } - public SlimefunItemStack(SlimefunItemStack item, int amount) { + public SlimefunItemStack(@Nonnull SlimefunItemStack item, int amount) { this(item.getItemId(), item); setAmount(amount); } - public SlimefunItemStack(String id, ItemStack item, Consumer consumer) { + public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item, @Nonnull Consumer consumer) { super(item, consumer); setItemId(id); } - public SlimefunItemStack(String id, Material type, String name, Consumer consumer) { + public SlimefunItemStack(@Nonnull String id, @Nonnull Material type, @Nullable String name, @Nonnull Consumer consumer) { super(type, meta -> { if (name != null) { meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); @@ -117,18 +117,18 @@ public class SlimefunItemStack extends CustomItem { setItemId(id); } - public SlimefunItemStack(String id, String texture, String name, String... lore) { + public SlimefunItemStack(@Nonnull String id, @Nonnull String texture, @Nullable String name, String... lore) { super(getSkull(id, texture), name, lore); this.texture = getTexture(id, texture); setItemId(id); } - public SlimefunItemStack(String id, HeadTexture head, String name, String... lore) { + public SlimefunItemStack(@Nonnull String id, @Nonnull HeadTexture head, @Nullable String name, String... lore) { this(id, head.getTexture(), name, lore); } - public SlimefunItemStack(String id, String texture, String name, Consumer consumer) { + public SlimefunItemStack(@Nonnull String id, @Nonnull String texture, @Nullable String name, @Nonnull Consumer consumer) { super(getSkull(id, texture), meta -> { if (name != null) { meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); @@ -142,7 +142,7 @@ public class SlimefunItemStack extends CustomItem { setItemId(id); } - public SlimefunItemStack(String id, String texture, Consumer consumer) { + public SlimefunItemStack(@Nonnull String id, @Nonnull String texture, @Nonnull Consumer consumer) { super(getSkull(id, texture), consumer); this.texture = getTexture(id, texture); @@ -202,11 +202,13 @@ public class SlimefunItemStack extends CustomItem { * * @return The {@link SlimefunItem} this {@link SlimefunItem} represents, casted to the given type */ - public T getItem(Class type) { + @Nullable + public T getItem(@Nonnull Class type) { SlimefunItem item = getItem(); return type.isInstance(item) ? type.cast(item) : null; } + @Nonnull public ImmutableItemMeta getImmutableMeta() { return immutableMeta; } @@ -251,20 +253,23 @@ public class SlimefunItemStack extends CustomItem { return "SlimefunItemStack (" + id + (getAmount() > 1 ? (" x " + getAmount()) : "") + ')'; } + @Nonnull public Optional getSkullTexture() { return Optional.ofNullable(texture); } + @Nullable public String getDisplayName() { if (immutableMeta == null) { // Just to be extra safe return null; } - + return immutableMeta.getDisplayName().orElse(null); } - private static ItemStack getSkull(String id, String texture) { + @Nonnull + private static ItemStack getSkull(@Nonnull String id, @Nonnull String texture) { if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) { return new ItemStack(Material.PLAYER_HEAD); } @@ -276,7 +281,7 @@ public class SlimefunItemStack extends CustomItem { private static String getTexture(@Nonnull String id, @Nonnull String texture) { Validate.notNull(id, "The id cannot be null"); Validate.notNull(texture, "The texture cannot be null"); - + if (texture.startsWith("ey")) { return texture; }