v3.2.14, packets improvements

This commit is contained in:
nulli0n 2022-12-27 15:05:08 +06:00
parent 39ec13296e
commit 6435834698
4 changed files with 50 additions and 53 deletions

View File

@ -10,12 +10,12 @@ import org.bukkit.GameMode;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import su.nexmedia.engine.utils.ItemUtil;
import su.nightexpress.excellentenchants.ExcellentEnchantsAPI; import su.nightexpress.excellentenchants.ExcellentEnchantsAPI;
import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant;
import su.nightexpress.excellentenchants.config.Config; import su.nightexpress.excellentenchants.config.Config;
import su.nightexpress.excellentenchants.manager.EnchantManager; import su.nightexpress.excellentenchants.manager.EnchantManager;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,13 +33,10 @@ public class ProtocolHook {
PacketContainer packet = event.getPacket(); PacketContainer packet = event.getPacket();
ItemStack item = packet.getItemModifier().read(0); ItemStack item = packet.getItemModifier().read(0);
boolean removeOld = event.getPlayer().getGameMode() == GameMode.CREATIVE; boolean isCreative = event.getPlayer().getGameMode() == GameMode.CREATIVE;
if (removeOld) { //else {
packet.getItemModifier().write(0, remove(item)); packet.getItemModifier().write(0, update(item, isCreative));
} //}
else {
packet.getItemModifier().write(0, update(item));
}
} }
}); });
@ -49,15 +46,11 @@ public class ProtocolHook {
PacketContainer packet = event.getPacket(); PacketContainer packet = event.getPacket();
List<ItemStack> items = packet.getItemListModifier().readSafely(0); 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++) { for (int index = 0; index < items.size(); index++) {
if (removeOld) { ItemStack item = items.get(index);
items.set(index, remove(items.get(index))); items.set(index, update(item, isCreative));
}
else {
items.set(index, update(items.get(index)));
}
} }
packet.getItemListModifier().write(0, items); packet.getItemListModifier().write(0, items);
} }
@ -67,20 +60,23 @@ public class ProtocolHook {
} }
@Nullable @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; if (item == null || item.getType().isAir()) return item;
ItemStack copy = new ItemStack(item); ItemStack copy = new ItemStack(item);
ItemMeta meta = copy.getItemMeta(); ItemMeta meta = copy.getItemMeta();
if (meta == null) return item; if (meta == null) return item;
List<String> lore = ItemUtil.getLore(copy); List<String> lore = meta.getLore() == null ? new ArrayList<>() : meta.getLore();
Map<ExcellentEnchant, Integer> enchants = EnchantManager.getItemCustomEnchants(item); Map<ExcellentEnchant, Integer> enchants = EnchantManager.getExcellentEnchantments(item);
//if (enchants.keySet().stream().anyMatch(enchant -> ItemUtil.getLoreTag(item, enchant.getId()) != null)) {
// return 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) -> { enchants.forEach((enchant, integer) -> {
lore.addAll(0, Config.formatDescription(enchant.getDescription(integer))); lore.addAll(0, Config.formatDescription(enchant.getDescription(integer)));
}); });
@ -93,23 +89,4 @@ public class ProtocolHook {
copy.setItemMeta(meta); copy.setItemMeta(meta);
return copy; 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;
}
} }

View File

@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable;
import su.nexmedia.engine.api.manager.AbstractManager; import su.nexmedia.engine.api.manager.AbstractManager;
import su.nexmedia.engine.utils.EntityUtil; import su.nexmedia.engine.utils.EntityUtil;
import su.nexmedia.engine.utils.ItemUtil; import su.nexmedia.engine.utils.ItemUtil;
import su.nexmedia.engine.utils.Pair;
import su.nexmedia.engine.utils.random.Rnd; import su.nexmedia.engine.utils.random.Rnd;
import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.ExcellentEnchants;
import su.nightexpress.excellentenchants.ExcellentEnchantsAPI; import su.nightexpress.excellentenchants.ExcellentEnchantsAPI;
@ -162,7 +163,7 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
@Deprecated @Deprecated
public static void updateItemLoreEnchants(@NotNull ItemStack item) { 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());
//ItemUtil.delLore(item, ench.getId() + "_info"); //ItemUtil.delLore(item, ench.getId() + "_info");
}); });
@ -228,7 +229,9 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
item.setItemMeta(meta); item.setItemMeta(meta);
} }
// Too expensive
@NotNull @NotNull
@Deprecated
public static Map<ExcellentEnchant, Integer> getItemCustomEnchants(@NotNull ItemStack item) { public static Map<ExcellentEnchant, Integer> getItemCustomEnchants(@NotNull ItemStack item) {
return EnchantManager.getItemEnchants(item).entrySet().stream() return EnchantManager.getItemEnchants(item).entrySet().stream()
.filter(entry -> entry.getKey() instanceof ExcellentEnchant) .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)); .collect(Collectors.toMap(k -> (T) k.getKey(), Map.Entry::getValue, (old, nev) -> nev, LinkedHashMap::new));
} }
@Deprecated
public static int getItemCustomEnchantsAmount(@NotNull ItemStack item) { public static int getItemCustomEnchantsAmount(@NotNull ItemStack item) {
return EnchantManager.getItemCustomEnchants(item).size(); return EnchantManager.getItemCustomEnchants(item).size();
} }
@ -256,6 +260,7 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
} }
@NotNull @NotNull
@Deprecated
public static Map<Enchantment, Integer> getItemEnchants(@NotNull ItemStack item) { public static Map<Enchantment, Integer> getItemEnchants(@NotNull ItemStack item) {
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
if (meta == null) return Collections.emptyMap(); 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(); return (meta instanceof EnchantmentStorageMeta meta2) ? meta2.getStoredEnchants() : meta.getEnchants();
} }
@Deprecated
public static int getItemEnchantsAmount(@NotNull ItemStack item) { public static int getItemEnchantsAmount(@NotNull ItemStack item) {
return EnchantManager.getItemEnchants(item).size(); return EnchantManager.getItemEnchants(item).size();
} }
@ -278,6 +284,16 @@ public class EnchantManager extends AbstractManager<ExcellentEnchants> {
return meta.getEnchantLevel(enchant); 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 @Nullable
public static ExcellentEnchant getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect effect) { public static ExcellentEnchant getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect effect) {
Enchantment enchantment = ExcellentEnchantsAPI.PLUGIN.getEnchantNMS().getEnchantmentByEffect(entity, effect); Enchantment enchantment = ExcellentEnchantsAPI.PLUGIN.getEnchantNMS().getEnchantmentByEffect(entity, effect);

View File

@ -16,14 +16,13 @@ import su.nightexpress.excellentenchants.manager.enchants.tool.*;
import su.nightexpress.excellentenchants.manager.enchants.weapon.*; import su.nightexpress.excellentenchants.manager.enchants.weapon.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashSet; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
public class EnchantRegister { public class EnchantRegister {
private static final ExcellentEnchants PLUGIN; 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 EnchantBlastMining BLAST_MINING;
public static final EnchantCurseOfBreaking CURSE_OF_BREAKING; public static final EnchantCurseOfBreaking CURSE_OF_BREAKING;
@ -91,7 +90,7 @@ public class EnchantRegister {
static { static {
PLUGIN = ExcellentEnchants.getPlugin(ExcellentEnchants.class); PLUGIN = ExcellentEnchants.getPlugin(ExcellentEnchants.class);
PLUGIN.getConfigManager().extractResources("/enchants/"); PLUGIN.getConfigManager().extractResources("/enchants/");
ENCHANT_LIST = new HashSet<>(); ENCHANT_REGISTRY = new HashMap<>();
// Tool enchants // Tool enchants
BLAST_MINING = init(EnchantBlastMining.class, EnchantBlastMining.ID); BLAST_MINING = init(EnchantBlastMining.class, EnchantBlastMining.ID);
@ -165,7 +164,7 @@ public class EnchantRegister {
public static void setup() { public static void setup() {
// Prevent to register enchantments during the runtime. // Prevent to register enchantments during the runtime.
if (ExcellentEnchants.isLoaded) { if (ExcellentEnchants.isLoaded) {
ENCHANT_LIST.forEach(ExcellentEnchant::loadConfig); ENCHANT_REGISTRY.values().forEach(ExcellentEnchant::loadConfig);
return; return;
} }
@ -186,7 +185,7 @@ public class EnchantRegister {
} }
Enchantment.stopAcceptingRegistrations(); Enchantment.stopAcceptingRegistrations();
PLUGIN.info("Enchants Registered: " + ENCHANT_LIST.size()); PLUGIN.info("Enchants Registered: " + ENCHANT_REGISTRY.size());
ExcellentEnchants.isLoaded = true; ExcellentEnchants.isLoaded = true;
} }
@ -199,7 +198,7 @@ public class EnchantRegister {
if (byKey == null || byName == null) return; if (byKey == null || byName == null) return;
for (ExcellentEnchant enchant : ENCHANT_LIST) { for (ExcellentEnchant enchant : ENCHANT_REGISTRY.values()) {
if (enchant instanceof ICleanable cleanable) { if (enchant instanceof ICleanable cleanable) {
cleanable.clear(); cleanable.clear();
} }
@ -208,10 +207,15 @@ public class EnchantRegister {
byName.remove(enchant.getName()); byName.remove(enchant.getName());
enchant.unregisterListeners(); enchant.unregisterListeners();
} }
ENCHANT_LIST.clear(); ENCHANT_REGISTRY.clear();
PLUGIN.info("All enchants are unregistered."); PLUGIN.info("All enchants are unregistered.");
} }
@Nullable
public static ExcellentEnchant get(@NotNull NamespacedKey key) {
return ENCHANT_REGISTRY.get(key);
}
@Nullable @Nullable
private static <T extends ExcellentEnchant> T init(@NotNull Class<T> clazz, @NotNull String id) { private static <T extends ExcellentEnchant> T init(@NotNull Class<T> clazz, @NotNull String id) {
String enchantId = id.toLowerCase(); String enchantId = id.toLowerCase();
@ -231,7 +235,7 @@ public class EnchantRegister {
if (enchant == null) return; if (enchant == null) return;
Enchantment.registerEnchantment(enchant); Enchantment.registerEnchantment(enchant);
ENCHANT_LIST.add(enchant); ENCHANT_REGISTRY.put(enchant.getKey(), enchant);
enchant.registerListeners(); enchant.registerListeners();
PLUGIN.info("Registered enchantment: " + enchant.getId()); PLUGIN.info("Registered enchantment: " + enchant.getId());
//IRegistry.a(IRegistry.ENCHANTMENT, enchant.getId(), CraftEnchantment.getRaw(enchant)); //IRegistry.a(IRegistry.ENCHANTMENT, enchant.getId(), CraftEnchantment.getRaw(enchant));

View File

@ -79,7 +79,7 @@ public class EnchantListGUI extends AbstractMenu<ExcellentEnchants> {
public boolean onPrepare(@NotNull Player player, @NotNull Inventory inventory) { public boolean onPrepare(@NotNull Player player, @NotNull Inventory inventory) {
int page = this.getPage(player); int page = this.getPage(player);
int length = this.enchantSlots.length; 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()); sorted(Comparator.comparing(ExcellentEnchant::getName)).toList());
List<List<ExcellentEnchant>> split = CollectionsUtil.split(list, length); List<List<ExcellentEnchant>> split = CollectionsUtil.split(list, length);