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

Some performance improvements

This commit is contained in:
TheBusyBiscuit 2020-06-26 16:27:16 +02:00
parent 6ffa5ab9a7
commit 8d0c816ed2
4 changed files with 25 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package io.github.thebusybiscuit.slimefun4.core.services;
import java.util.Optional;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -65,7 +66,7 @@ public class CustomItemDataService implements PersistentDataService, Keyed {
* @return An {@link Optional} describing the result
*/
public Optional<String> getItemData(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
if (item == null || item.getType() == Material.AIR || !item.hasItemMeta()) {
return Optional.empty();
}

View File

@ -33,7 +33,14 @@ public class SolarHelmet extends SlimefunItem {
addItemSetting(charge);
}
public void chargeItems(Player p) {
/**
* This method recharges the equipment of the given {@link Player} by the configured
* factor of this {@link SolarHelmet}.
*
* @param p
* The {@link Player} wearing this {@link SolarHelmet}
*/
public void rechargeItems(Player p) {
recharge(p.getInventory().getHelmet());
recharge(p.getInventory().getChestplate());
recharge(p.getInventory().getLeggings());

View File

@ -92,10 +92,17 @@ public class ArmorTask implements Runnable {
}
private void checkForSolarHelmet(Player p) {
SlimefunItem item = SlimefunItem.getByItem(p.getInventory().getHelmet());
ItemStack helmet = p.getInventory().getHelmet();
if (SlimefunPlugin.getRegistry().isBackwardsCompatible() && !SlimefunUtils.isItemSimilar(helmet, SlimefunItems.SOLAR_HELMET, true, false)) {
// Performance saver for slow backwards-compatible versions of Slimefun
return;
}
SlimefunItem item = SlimefunItem.getByItem(helmet);
if (item instanceof SolarHelmet && Slimefun.hasUnlocked(p, item, true)) {
((SolarHelmet) item).chargeItems(p);
((SolarHelmet) item).rechargeItems(p);
}
}

View File

@ -805,19 +805,17 @@ public class SlimefunItem implements Placeable {
return getByID(((SlimefunItemStack) item).getItemId());
}
// This wrapper improves the heavy ItemStack#getItemMeta() call by caching it.
ItemStackWrapper wrapper = new ItemStackWrapper(item);
if (item.hasItemMeta()) {
Optional<String> itemID = SlimefunPlugin.getItemDataService().getItemData(wrapper);
Optional<String> itemID = SlimefunPlugin.getItemDataService().getItemData(item);
if (itemID.isPresent()) {
return getByID(itemID.get());
}
}
// Backwards compatibility
if (SlimefunPlugin.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_14) || SlimefunPlugin.getRegistry().isBackwardsCompatible()) {
// This wrapper improves the heavy ItemStack#getItemMeta() call by caching it.
ItemStackWrapper wrapper = new ItemStackWrapper(item);
// Quite expensive performance-wise
// But necessary for supporting legacy items
for (SlimefunItem sfi : SlimefunPlugin.getRegistry().getAllSlimefunItems()) {