v3.2.14, packets improvements
This commit is contained in:
parent
39ec13296e
commit
6435834698
@ -10,12 +10,12 @@ import org.bukkit.GameMode;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import su.nexmedia.engine.utils.ItemUtil;
|
||||
import su.nightexpress.excellentenchants.ExcellentEnchantsAPI;
|
||||
import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant;
|
||||
import su.nightexpress.excellentenchants.config.Config;
|
||||
import su.nightexpress.excellentenchants.manager.EnchantManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -33,13 +33,10 @@ public class ProtocolHook {
|
||||
PacketContainer packet = event.getPacket();
|
||||
|
||||
ItemStack item = packet.getItemModifier().read(0);
|
||||
boolean removeOld = event.getPlayer().getGameMode() == GameMode.CREATIVE;
|
||||
if (removeOld) {
|
||||
packet.getItemModifier().write(0, remove(item));
|
||||
}
|
||||
else {
|
||||
packet.getItemModifier().write(0, update(item));
|
||||
}
|
||||
boolean isCreative = event.getPlayer().getGameMode() == GameMode.CREATIVE;
|
||||
//else {
|
||||
packet.getItemModifier().write(0, update(item, isCreative));
|
||||
//}
|
||||
}
|
||||
});
|
||||
|
||||
@ -49,15 +46,11 @@ public class ProtocolHook {
|
||||
PacketContainer packet = event.getPacket();
|
||||
|
||||
List<ItemStack> items = packet.getItemListModifier().readSafely(0);
|
||||
boolean removeOld = event.getPlayer().getGameMode() == GameMode.CREATIVE;
|
||||
boolean isCreative = event.getPlayer().getGameMode() == GameMode.CREATIVE;
|
||||
|
||||
for (int index = 0; index < items.size(); index++) {
|
||||
if (removeOld) {
|
||||
items.set(index, remove(items.get(index)));
|
||||
}
|
||||
else {
|
||||
items.set(index, update(items.get(index)));
|
||||
}
|
||||
ItemStack item = items.get(index);
|
||||
items.set(index, update(item, isCreative));
|
||||
}
|
||||
packet.getItemListModifier().write(0, items);
|
||||
}
|
||||
@ -67,20 +60,23 @@ public class ProtocolHook {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ItemStack update(@Nullable ItemStack item) {
|
||||
private static ItemStack update(@Nullable ItemStack item, boolean isCreative) {
|
||||
if (item == null || item.getType().isAir()) return item;
|
||||
|
||||
ItemStack copy = new ItemStack(item);
|
||||
ItemMeta meta = copy.getItemMeta();
|
||||
if (meta == null) return item;
|
||||
|
||||
List<String> lore = ItemUtil.getLore(copy);
|
||||
Map<ExcellentEnchant, Integer> enchants = EnchantManager.getItemCustomEnchants(item);
|
||||
//if (enchants.keySet().stream().anyMatch(enchant -> ItemUtil.getLoreTag(item, enchant.getId()) != null)) {
|
||||
// return item;
|
||||
//}
|
||||
List<String> lore = meta.getLore() == null ? new ArrayList<>() : meta.getLore();
|
||||
Map<ExcellentEnchant, Integer> enchants = EnchantManager.getExcellentEnchantments(item);
|
||||
|
||||
if (Config.ENCHANTMENTS_DESCRIPTION_ENABLED) {
|
||||
enchants.keySet().forEach(enchant -> lore.removeIf(line -> line.contains(enchant.getDisplayName())));
|
||||
if (isCreative) {
|
||||
enchants.forEach((enchant, integer) -> {
|
||||
lore.removeAll(Config.formatDescription(enchant.getDescription(integer)));
|
||||
});
|
||||
}
|
||||
if (Config.ENCHANTMENTS_DESCRIPTION_ENABLED && !isCreative) {
|
||||
enchants.forEach((enchant, integer) -> {
|
||||
lore.addAll(0, Config.formatDescription(enchant.getDescription(integer)));
|
||||
});
|
||||
@ -93,23 +89,4 @@ public class ProtocolHook {
|
||||
copy.setItemMeta(meta);
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ItemStack remove(@Nullable ItemStack item) {
|
||||
if (item == null || item.getType().isAir()) return item;
|
||||
|
||||
List<String> from = ItemUtil.getLore(item);
|
||||
Map<ExcellentEnchant, Integer> enchants = EnchantManager.getItemCustomEnchants(item);
|
||||
enchants.forEach((enchant, integer) -> {
|
||||
from.removeIf(line -> line.equalsIgnoreCase(enchant.getNameFormatted(integer)));
|
||||
from.removeAll(Config.formatDescription(enchant.getDescription(integer)));
|
||||
});
|
||||
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return item;
|
||||
|
||||
meta.setLore(from);
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import su.nexmedia.engine.api.manager.AbstractManager;
|
||||
import su.nexmedia.engine.utils.EntityUtil;
|
||||
import su.nexmedia.engine.utils.ItemUtil;
|
||||
import su.nexmedia.engine.utils.Pair;
|
||||
import su.nexmedia.engine.utils.random.Rnd;
|
||||
import su.nightexpress.excellentenchants.ExcellentEnchants;
|
||||
import su.nightexpress.excellentenchants.ExcellentEnchantsAPI;
|
||||
@ -162,7 +163,7 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
|
||||
|
||||
@Deprecated
|
||||
public static void updateItemLoreEnchants(@NotNull ItemStack item) {
|
||||
EnchantRegister.ENCHANT_LIST.forEach(ench -> {
|
||||
EnchantRegister.ENCHANT_REGISTRY.values().forEach(ench -> {
|
||||
//ItemUtil.delLore(item, ench.getId());
|
||||
//ItemUtil.delLore(item, ench.getId() + "_info");
|
||||
});
|
||||
@ -228,7 +229,9 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
|
||||
// Too expensive
|
||||
@NotNull
|
||||
@Deprecated
|
||||
public static Map<ExcellentEnchant, Integer> getItemCustomEnchants(@NotNull ItemStack item) {
|
||||
return EnchantManager.getItemEnchants(item).entrySet().stream()
|
||||
.filter(entry -> entry.getKey() instanceof ExcellentEnchant)
|
||||
@ -246,6 +249,7 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
|
||||
.collect(Collectors.toMap(k -> (T) k.getKey(), Map.Entry::getValue, (old, nev) -> nev, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static int getItemCustomEnchantsAmount(@NotNull ItemStack item) {
|
||||
return EnchantManager.getItemCustomEnchants(item).size();
|
||||
}
|
||||
@ -256,6 +260,7 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Deprecated
|
||||
public static Map<Enchantment, Integer> getItemEnchants(@NotNull ItemStack item) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return Collections.emptyMap();
|
||||
@ -263,6 +268,7 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
|
||||
return (meta instanceof EnchantmentStorageMeta meta2) ? meta2.getStoredEnchants() : meta.getEnchants();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static int getItemEnchantsAmount(@NotNull ItemStack item) {
|
||||
return EnchantManager.getItemEnchants(item).size();
|
||||
}
|
||||
@ -278,6 +284,16 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
|
||||
return meta.getEnchantLevel(enchant);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Map<ExcellentEnchant, Integer> getExcellentEnchantments(@NotNull ItemStack item) {
|
||||
return EnchantManager.getItemEnchants(item).entrySet().stream()
|
||||
.map(entry -> {
|
||||
ExcellentEnchant ex = EnchantRegister.get(entry.getKey().getKey());
|
||||
return ex == null ? null : Pair.of(ex, entry.getValue());
|
||||
}).filter(Objects::nonNull)
|
||||
.collect(Collectors.toMap(Pair::getFirst, Pair::getSecond, (old, nev) -> nev, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ExcellentEnchant getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect effect) {
|
||||
Enchantment enchantment = ExcellentEnchantsAPI.PLUGIN.getEnchantNMS().getEnchantmentByEffect(entity, effect);
|
||||
|
@ -16,14 +16,13 @@ import su.nightexpress.excellentenchants.manager.enchants.tool.*;
|
||||
import su.nightexpress.excellentenchants.manager.enchants.weapon.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class EnchantRegister {
|
||||
|
||||
private static final ExcellentEnchants PLUGIN;
|
||||
public static final Set<ExcellentEnchant> ENCHANT_LIST;
|
||||
public static final Map<NamespacedKey, ExcellentEnchant> ENCHANT_REGISTRY;
|
||||
|
||||
public static final EnchantBlastMining BLAST_MINING;
|
||||
public static final EnchantCurseOfBreaking CURSE_OF_BREAKING;
|
||||
@ -91,7 +90,7 @@ public class EnchantRegister {
|
||||
static {
|
||||
PLUGIN = ExcellentEnchants.getPlugin(ExcellentEnchants.class);
|
||||
PLUGIN.getConfigManager().extractResources("/enchants/");
|
||||
ENCHANT_LIST = new HashSet<>();
|
||||
ENCHANT_REGISTRY = new HashMap<>();
|
||||
|
||||
// Tool enchants
|
||||
BLAST_MINING = init(EnchantBlastMining.class, EnchantBlastMining.ID);
|
||||
@ -165,7 +164,7 @@ public class EnchantRegister {
|
||||
public static void setup() {
|
||||
// Prevent to register enchantments during the runtime.
|
||||
if (ExcellentEnchants.isLoaded) {
|
||||
ENCHANT_LIST.forEach(ExcellentEnchant::loadConfig);
|
||||
ENCHANT_REGISTRY.values().forEach(ExcellentEnchant::loadConfig);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -186,7 +185,7 @@ public class EnchantRegister {
|
||||
}
|
||||
|
||||
Enchantment.stopAcceptingRegistrations();
|
||||
PLUGIN.info("Enchants Registered: " + ENCHANT_LIST.size());
|
||||
PLUGIN.info("Enchants Registered: " + ENCHANT_REGISTRY.size());
|
||||
ExcellentEnchants.isLoaded = true;
|
||||
}
|
||||
|
||||
@ -199,7 +198,7 @@ public class EnchantRegister {
|
||||
|
||||
if (byKey == null || byName == null) return;
|
||||
|
||||
for (ExcellentEnchant enchant : ENCHANT_LIST) {
|
||||
for (ExcellentEnchant enchant : ENCHANT_REGISTRY.values()) {
|
||||
if (enchant instanceof ICleanable cleanable) {
|
||||
cleanable.clear();
|
||||
}
|
||||
@ -208,10 +207,15 @@ public class EnchantRegister {
|
||||
byName.remove(enchant.getName());
|
||||
enchant.unregisterListeners();
|
||||
}
|
||||
ENCHANT_LIST.clear();
|
||||
ENCHANT_REGISTRY.clear();
|
||||
PLUGIN.info("All enchants are unregistered.");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ExcellentEnchant get(@NotNull NamespacedKey key) {
|
||||
return ENCHANT_REGISTRY.get(key);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static <T extends ExcellentEnchant> T init(@NotNull Class<T> clazz, @NotNull String id) {
|
||||
String enchantId = id.toLowerCase();
|
||||
@ -231,7 +235,7 @@ public class EnchantRegister {
|
||||
if (enchant == null) return;
|
||||
|
||||
Enchantment.registerEnchantment(enchant);
|
||||
ENCHANT_LIST.add(enchant);
|
||||
ENCHANT_REGISTRY.put(enchant.getKey(), enchant);
|
||||
enchant.registerListeners();
|
||||
PLUGIN.info("Registered enchantment: " + enchant.getId());
|
||||
//IRegistry.a(IRegistry.ENCHANTMENT, enchant.getId(), CraftEnchantment.getRaw(enchant));
|
||||
|
@ -79,7 +79,7 @@ public class EnchantListGUI extends AbstractMenu<ExcellentEnchants> {
|
||||
public boolean onPrepare(@NotNull Player player, @NotNull Inventory inventory) {
|
||||
int page = this.getPage(player);
|
||||
int length = this.enchantSlots.length;
|
||||
List<ExcellentEnchant> list = new ArrayList<>(EnchantRegister.ENCHANT_LIST.stream().
|
||||
List<ExcellentEnchant> list = new ArrayList<>(EnchantRegister.ENCHANT_REGISTRY.values().stream().
|
||||
sorted(Comparator.comparing(ExcellentEnchant::getName)).toList());
|
||||
List<List<ExcellentEnchant>> split = CollectionsUtil.split(list, length);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user