From f4f72e229c703f4f298c13c4230e63e99901fec7 Mon Sep 17 00:00:00 2001 From: Globox1997 Date: Thu, 28 Nov 2024 16:49:52 +0100 Subject: [PATCH] added enchantment registry + fixed issue --- CHANGELOG.md | 2 +- .../net/levelz/data/RestrictionLoader.java | 117 ++++++++++-------- src/main/java/net/levelz/init/LoaderInit.java | 2 +- .../java/net/levelz/level/LevelManager.java | 32 ++--- .../mixin/compat/LevelManagerCompatMixin.java | 6 +- .../mixin/misc/EnchantmentHelperMixin.java | 2 +- .../misc/EnchantmentScreenHandlerMixin.java | 4 +- .../mixin/misc/ProcessItemFactoryMixin.java | 2 +- .../net/levelz/network/LevelClientPacket.java | 3 +- .../network/packet/RestrictionPacket.java | 74 +---------- .../levelz/registry/EnchantmentRegistry.java | 67 ++++++++++ .../net/levelz/registry/EnchantmentZ.java | 23 ++++ .../net/levelz/screen/SkillInfoScreen.java | 56 +++------ .../levelz/screen/SkillRestrictionScreen.java | 4 +- .../net/levelz/screen/widget/LineWidget.java | 60 +++++---- .../java/net/levelz/util/PacketHelper.java | 2 +- 16 files changed, 241 insertions(+), 215 deletions(-) create mode 100644 src/main/java/net/levelz/registry/EnchantmentRegistry.java create mode 100644 src/main/java/net/levelz/registry/EnchantmentZ.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 24ac40a..6cc5049 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,6 @@ - ### Fixed: - Fixed npe -- +- Fixed registry issue ### Changed: - \ No newline at end of file diff --git a/src/main/java/net/levelz/data/RestrictionLoader.java b/src/main/java/net/levelz/data/RestrictionLoader.java index 17806bd..c9af954 100644 --- a/src/main/java/net/levelz/data/RestrictionLoader.java +++ b/src/main/java/net/levelz/data/RestrictionLoader.java @@ -7,12 +7,11 @@ import net.levelz.LevelzMain; import net.levelz.init.ConfigInit; import net.levelz.level.LevelManager; -import net.levelz.level.restriction.PlayerRestriction; import net.levelz.level.Skill; -import net.levelz.level.restriction.EnchantmentRestriction; -import net.minecraft.enchantment.Enchantment; -import net.minecraft.registry.*; -import net.minecraft.registry.entry.RegistryEntry; +import net.levelz.level.restriction.PlayerRestriction; +import net.levelz.registry.EnchantmentRegistry; +import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryWrapper; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; import org.apache.logging.log4j.LogManager; @@ -20,22 +19,29 @@ import java.io.InputStream; import java.io.InputStreamReader; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; -public class RestrictionLoader implements SimpleSynchronousResourceReloadListener { +public record RestrictionLoader(RegistryWrapper.WrapperLookup wrapperLookup) implements SimpleSynchronousResourceReloadListener { + + public static final Identifier ID = LevelzMain.identifierOf("restriction"); private static final Logger LOGGER = LogManager.getLogger("LevelZ"); - private List blockList = new ArrayList<>(); - private List craftingList = new ArrayList<>(); - private List entityList = new ArrayList<>(); - private List itemList = new ArrayList<>(); - private List miningList = new ArrayList<>(); - private Map> enchantmentList = new HashMap<>(); + private static final List blockList = new ArrayList<>(); + private static final List craftingList = new ArrayList<>(); + private static final List entityList = new ArrayList<>(); + private static final List itemList = new ArrayList<>(); + private static final List miningList = new ArrayList<>(); + private static final List enchantmentList = new ArrayList<>(); + +// private ItemStringReader itemStringReader = new ItemStringReader(BuiltinRegistries.createWrapperLookup()); @Override public Identifier getFabricId() { - return LevelzMain.identifierOf("restriction"); + return ID; } @Override @@ -51,6 +57,7 @@ public void reload(ResourceManager manager) { if (!ConfigInit.CONFIG.restrictions) { return; } + EnchantmentRegistry.updateEnchantments(this.wrapperLookup()); manager.findResources("restriction", id -> id.getPath().endsWith(".json")).forEach((id, resourceRef) -> { try { @@ -87,11 +94,11 @@ public void reload(ResourceManager manager) { if (Registries.BLOCK.containsId(blockIdentifier)) { int blockRawId = Registries.BLOCK.getRawId(Registries.BLOCK.get(blockIdentifier)); - if (this.blockList.contains(blockRawId)) { + if (blockList.contains(blockRawId)) { continue; } if (replace) { - this.blockList.add(blockRawId); + blockList.add(blockRawId); } LevelManager.BLOCK_RESTRICTIONS.put(blockRawId, new PlayerRestriction(blockRawId, skillLevelRestrictions)); } else { @@ -106,11 +113,11 @@ public void reload(ResourceManager manager) { if (Registries.ITEM.containsId(craftingIdentifier)) { int craftingRawId = Registries.ITEM.getRawId(Registries.ITEM.get(craftingIdentifier)); - if (this.craftingList.contains(craftingRawId)) { + if (craftingList.contains(craftingRawId)) { continue; } if (replace) { - this.craftingList.add(craftingRawId); + craftingList.add(craftingRawId); } LevelManager.CRAFTING_RESTRICTIONS.put(craftingRawId, new PlayerRestriction(craftingRawId, skillLevelRestrictions)); } else { @@ -125,11 +132,11 @@ public void reload(ResourceManager manager) { if (Registries.ENTITY_TYPE.containsId(entityIdentifier)) { int entityRawId = Registries.ENTITY_TYPE.getRawId(Registries.ENTITY_TYPE.get(entityIdentifier)); - if (this.entityList.contains(entityRawId)) { + if (entityList.contains(entityRawId)) { continue; } if (replace) { - this.entityList.add(entityRawId); + entityList.add(entityRawId); } LevelManager.ENTITY_RESTRICTIONS.put(entityRawId, new PlayerRestriction(entityRawId, skillLevelRestrictions)); } else { @@ -144,11 +151,11 @@ public void reload(ResourceManager manager) { if (Registries.ITEM.containsId(itemIdentifier)) { int itemRawId = Registries.ITEM.getRawId(Registries.ITEM.get(itemIdentifier)); - if (this.itemList.contains(itemRawId)) { + if (itemList.contains(itemRawId)) { continue; } if (replace) { - this.itemList.add(itemRawId); + itemList.add(itemRawId); } LevelManager.ITEM_RESTRICTIONS.put(itemRawId, new PlayerRestriction(itemRawId, skillLevelRestrictions)); } else { @@ -163,11 +170,11 @@ public void reload(ResourceManager manager) { if (Registries.BLOCK.containsId(miningIdentifier)) { int miningRawId = Registries.BLOCK.getRawId(Registries.BLOCK.get(miningIdentifier)); - if (this.miningList.contains(miningRawId)) { + if (miningList.contains(miningRawId)) { continue; } if (replace) { - this.miningList.add(miningRawId); + miningList.add(miningRawId); } LevelManager.MINING_RESTRICTIONS.put(miningRawId, new PlayerRestriction(miningRawId, skillLevelRestrictions)); } else { @@ -177,36 +184,42 @@ public void reload(ResourceManager manager) { } // enchantments if (restrictionJsonObject.has("enchantments")) { - Optional> wrapper = BuiltinRegistries.createWrapperLookup().getOptionalWrapper(RegistryKeys.ENCHANTMENT); - if (wrapper.isPresent()) { - JsonObject enchantmentObject = restrictionJsonObject.getAsJsonObject("enchantments"); - for (String enchantment : enchantmentObject.keySet()) { - Identifier enchantmentIdentifier = Identifier.of(enchantment); - Optional> enchantmentReference = wrapper.get().getOptional(RegistryKey.of(RegistryKeys.ENCHANTMENT, enchantmentIdentifier)); - if (enchantmentReference.isPresent()) { - int level = enchantmentObject.get(enchantment).getAsInt(); - if (this.enchantmentList.containsKey(enchantment) && this.enchantmentList.get(enchantment).contains(level)) { - continue; - } - if (replace) { - if (this.enchantmentList.containsKey(enchantment)) { - this.enchantmentList.get(enchantment).add(level); - } else { - this.enchantmentList.put(enchantment, new ArrayList<>(level)); - } - } - - if (LevelManager.ENCHANTMENT_RESTRICTIONS.containsKey(enchantmentReference.get().getIdAsString())) { - LevelManager.ENCHANTMENT_RESTRICTIONS.get(enchantmentReference.get().getIdAsString()).getSkillLevelRestrictions().put(level, skillLevelRestrictions); - } else { - Map> map = new HashMap<>(); - map.put(enchantmentObject.get(enchantment).getAsInt(), skillLevelRestrictions); - LevelManager.ENCHANTMENT_RESTRICTIONS.put(enchantmentReference.get().getIdAsString(), - new EnchantmentRestriction(enchantmentReference.get(), map)); - } + JsonObject enchantmentObject = restrictionJsonObject.getAsJsonObject("enchantments"); + for (String enchantment : enchantmentObject.keySet()) { + Identifier enchantmentIdentifier = Identifier.of(enchantment); + int level = enchantmentObject.get(enchantment).getAsInt(); + if (EnchantmentRegistry.containsId(enchantmentIdentifier, level)) { + int enchantmentRawId = EnchantmentRegistry.getId(enchantmentIdentifier, level); + if (enchantmentList.contains(enchantmentRawId)) { + continue; + } + if (replace) { + enchantmentList.add(enchantmentRawId); + } + LevelManager.ENCHANTMENT_RESTRICTIONS.put(enchantmentRawId, new PlayerRestriction(enchantmentRawId, skillLevelRestrictions)); + } else { + LOGGER.warn("Restriction {} contains an unrecognized enchantment id called {}.", mapKey, enchantmentIdentifier); + } + } + } + // Todo: Test + if (restrictionJsonObject.has("components")) { +// System.out.println(this.itemStringReader.consume(new StringReader("potion[potion_contents={potion:\"fire_resistance\"}]"))); +// System.out.println(this.itemStringReader.consume(new StringReader("potion[potion_contents={potion:\"fire_resistance\"}]")).components()); +// System.out.println(this.itemStringReader.consume(new StringReader("potion[potion_contents={potion:\"fire_resistance\"}]")).item().value()); +// Registries.ENCHANTMENT. + + JsonObject componentObject = restrictionJsonObject.getAsJsonObject("components"); + for (String component : componentObject.keySet()) { + Identifier itemIdentifier = Identifier.of(component); + if (Registries.ITEM.containsId(itemIdentifier)) { + if (Registries.DATA_COMPONENT_TYPE.containsId(Identifier.of(componentObject.get(component).getAsString()))) { + int itemRawId = Registries.ITEM.getRawId(Registries.ITEM.get(itemIdentifier)); } else { - LOGGER.warn("Restriction {} contains an unrecognized enchantment id called {}.", mapKey, enchantmentIdentifier); + LOGGER.warn("Restriction {} contains an unrecognized component called {}.", mapKey, componentObject.get(component).getAsString()); } + } else { + LOGGER.warn("Restriction {} contains an unrecognized item id at component called {}.", mapKey, itemIdentifier); } } } diff --git a/src/main/java/net/levelz/init/LoaderInit.java b/src/main/java/net/levelz/init/LoaderInit.java index 6789dee..4da9f27 100644 --- a/src/main/java/net/levelz/init/LoaderInit.java +++ b/src/main/java/net/levelz/init/LoaderInit.java @@ -16,7 +16,7 @@ public class LoaderInit { public static void init() { ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new SkillLoader()); - ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new RestrictionLoader()); + ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(RestrictionLoader.ID, RestrictionLoader::new); ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, serverResourceManager, success) -> { if (success) { for (int i = 0; i < server.getPlayerManager().getPlayerList().size(); i++) { diff --git a/src/main/java/net/levelz/level/LevelManager.java b/src/main/java/net/levelz/level/LevelManager.java index 5400000..56c8067 100644 --- a/src/main/java/net/levelz/level/LevelManager.java +++ b/src/main/java/net/levelz/level/LevelManager.java @@ -1,11 +1,12 @@ package net.levelz.level; import net.levelz.init.ConfigInit; -import net.levelz.level.restriction.EnchantmentRestriction; import net.levelz.level.restriction.PlayerRestriction; +import net.levelz.registry.EnchantmentRegistry; import net.levelz.util.LevelHelper; import net.levelz.util.PacketHelper; import net.minecraft.block.Block; +import net.minecraft.enchantment.Enchantment; import net.minecraft.entity.EntityType; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; @@ -13,6 +14,7 @@ import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtList; import net.minecraft.registry.Registries; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.network.ServerPlayerEntity; import java.util.HashMap; @@ -26,7 +28,7 @@ public class LevelManager { public static final Map ENTITY_RESTRICTIONS = new HashMap<>(); public static final Map ITEM_RESTRICTIONS = new HashMap<>(); public static final Map MINING_RESTRICTIONS = new HashMap<>(); - public static final Map ENCHANTMENT_RESTRICTIONS = new HashMap<>(); + public static final Map ENCHANTMENT_RESTRICTIONS = new HashMap<>(); public static final Map BONUSES = new HashMap<>(); private final PlayerEntity playerEntity; @@ -285,26 +287,24 @@ public Map getRequiredMiningLevel(Block block) { } // enchantment - public boolean hasRequiredEnchantmentLevel(String enchantment, int level) { - if (ENCHANTMENT_RESTRICTIONS.containsKey(enchantment)) { - EnchantmentRestriction enchantmentRestriction = ENCHANTMENT_RESTRICTIONS.get(enchantment); - if (enchantmentRestriction.getSkillLevelRestrictions().containsKey(level)) { - for (Map.Entry entry : enchantmentRestriction.getSkillLevelRestrictions().get(level).entrySet()) { - if (this.getSkillLevel(entry.getKey()) < entry.getValue()) { - return false; - } + public boolean hasRequiredEnchantmentLevel(RegistryEntry enchantment, int level) { + int enchantmentId = EnchantmentRegistry.getId(enchantment, level); + if (ENCHANTMENT_RESTRICTIONS.containsKey(enchantmentId)) { + PlayerRestriction playerRestriction = ENCHANTMENT_RESTRICTIONS.get(enchantmentId); + for (Map.Entry entry : playerRestriction.getSkillLevelRestrictions().entrySet()) { + if (this.getSkillLevel(entry.getKey()) < entry.getValue()) { + return false; } } } return true; } - public Map getRequiredEnchantmentLevel(String enchantment, int level) { - if (ENCHANTMENT_RESTRICTIONS.containsKey(enchantment)) { - EnchantmentRestriction enchantmentRestriction = ENCHANTMENT_RESTRICTIONS.get(enchantment); - if (enchantmentRestriction.getSkillLevelRestrictions().containsKey(level)) { - return enchantmentRestriction.getSkillLevelRestrictions().get(level); - } + public Map getRequiredEnchantmentLevel(RegistryEntry enchantment, int level) { + int enchantmentId = EnchantmentRegistry.getId(enchantment, level); + if (ENCHANTMENT_RESTRICTIONS.containsKey(enchantmentId)) { + PlayerRestriction playerRestriction = ENCHANTMENT_RESTRICTIONS.get(enchantmentId); + return playerRestriction.getSkillLevelRestrictions(); } return Map.of(0, 0); } diff --git a/src/main/java/net/levelz/mixin/compat/LevelManagerCompatMixin.java b/src/main/java/net/levelz/mixin/compat/LevelManagerCompatMixin.java index c8216e4..79fadbf 100644 --- a/src/main/java/net/levelz/mixin/compat/LevelManagerCompatMixin.java +++ b/src/main/java/net/levelz/mixin/compat/LevelManagerCompatMixin.java @@ -2,9 +2,11 @@ import net.levelz.level.LevelManager; import net.minecraft.block.Block; +import net.minecraft.enchantment.Enchantment; import net.minecraft.entity.EntityType; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; +import net.minecraft.registry.entry.RegistryEntry; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -56,8 +58,8 @@ private void hasRequiredMiningLevelMixin(Block block, CallbackInfoReturnable info) { + @Inject(method = "hasRequiredEnchantmentLevel", at = @At("HEAD"), cancellable = true) + private void hasRequiredEnchantmentLevelMixin(RegistryEntry enchantment, int level, CallbackInfoReturnable info) { if (this.playerEntity.getClass().getName().contains("deployer.DeployerFakePlayer") || this.playerEntity.getClass().getName().contains("core.TurtlePlayer")) { info.setReturnValue(true); } diff --git a/src/main/java/net/levelz/mixin/misc/EnchantmentHelperMixin.java b/src/main/java/net/levelz/mixin/misc/EnchantmentHelperMixin.java index 150ecf7..9494ae3 100644 --- a/src/main/java/net/levelz/mixin/misc/EnchantmentHelperMixin.java +++ b/src/main/java/net/levelz/mixin/misc/EnchantmentHelperMixin.java @@ -25,7 +25,7 @@ public class EnchantmentHelperMixin { private static void forEachEnchantmentMixin(EnchantmentHelper.ContextAwareConsumer instance, RegistryEntry enchantmentRegistryEntry, int i, EnchantmentEffectContext enchantmentEffectContext, Operation original) { if (enchantmentEffectContext.owner() != null && enchantmentEffectContext.owner() instanceof PlayerEntity playerEntity) { LevelManager levelManager = ((LevelManagerAccess) playerEntity).getLevelManager(); - if (levelManager.hasRequiredEnchantmentLevel(enchantmentRegistryEntry.getIdAsString(), i)) { + if (levelManager.hasRequiredEnchantmentLevel(enchantmentRegistryEntry, i)) { original.call(instance, enchantmentRegistryEntry, i, enchantmentEffectContext); } } else { diff --git a/src/main/java/net/levelz/mixin/misc/EnchantmentScreenHandlerMixin.java b/src/main/java/net/levelz/mixin/misc/EnchantmentScreenHandlerMixin.java index e57af8d..67b408e 100644 --- a/src/main/java/net/levelz/mixin/misc/EnchantmentScreenHandlerMixin.java +++ b/src/main/java/net/levelz/mixin/misc/EnchantmentScreenHandlerMixin.java @@ -59,7 +59,7 @@ private List generateEnchantmentsMixin(List list = new ArrayList<>(); for (EnchantmentLevelEntry enchantmentLevelEntry : original) { - if (levelManager.hasRequiredEnchantmentLevel(enchantmentLevelEntry.enchantment.getIdAsString(), enchantmentLevelEntry.level)) { + if (levelManager.hasRequiredEnchantmentLevel(enchantmentLevelEntry.enchantment, enchantmentLevelEntry.level)) { list.add(enchantmentLevelEntry); } } @@ -70,7 +70,7 @@ private List generateEnchantmentsMixin(List enchantmentRng = EnchantmentHelper.generateEnchantments(this.playerEntity.getRandom(), stack, level, ((RegistryEntryList.Named) optional.get()).stream()); for (EnchantmentLevelEntry enchantmentLevelEntry : enchantmentRng) { - if (levelManager.hasRequiredEnchantmentLevel(enchantmentLevelEntry.enchantment.getIdAsString(), enchantmentLevelEntry.level)) { + if (levelManager.hasRequiredEnchantmentLevel(enchantmentLevelEntry.enchantment, enchantmentLevelEntry.level)) { list.add(enchantmentLevelEntry); break; } diff --git a/src/main/java/net/levelz/mixin/misc/ProcessItemFactoryMixin.java b/src/main/java/net/levelz/mixin/misc/ProcessItemFactoryMixin.java index eacd90c..b944080 100644 --- a/src/main/java/net/levelz/mixin/misc/ProcessItemFactoryMixin.java +++ b/src/main/java/net/levelz/mixin/misc/ProcessItemFactoryMixin.java @@ -40,7 +40,7 @@ private static void method_59950Mixin(ItemStack itemStack, World world, Entity e boolean hasAllRequiredLevels = true; Map, Integer> enchantments = new HashMap<>(); for (Object2IntMap.Entry> entry : itemEnchantmentsComponent.getEnchantmentEntries()) { - if (levelManager.hasRequiredEnchantmentLevel(entry.getKey().getIdAsString(), entry.getIntValue())) { + if (levelManager.hasRequiredEnchantmentLevel(entry.getKey(), entry.getIntValue())) { enchantments.put(entry.getKey(), entry.getIntValue()); } else { hasAllRequiredLevels = false; diff --git a/src/main/java/net/levelz/network/LevelClientPacket.java b/src/main/java/net/levelz/network/LevelClientPacket.java index bcb59a9..dc1e6ac 100644 --- a/src/main/java/net/levelz/network/LevelClientPacket.java +++ b/src/main/java/net/levelz/network/LevelClientPacket.java @@ -4,7 +4,6 @@ import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.levelz.access.LevelManagerAccess; -import net.levelz.entity.LevelExperienceOrbEntity; import net.levelz.level.LevelManager; import net.levelz.level.PlayerSkill; import net.levelz.level.Skill; @@ -76,7 +75,7 @@ public static void init() { RestrictionPacket.RestrictionRecord entityRestrictions = payload.entityRestrictions(); RestrictionPacket.RestrictionRecord itemRestrictions = payload.itemRestrictions(); RestrictionPacket.RestrictionRecord miningRestrictions = payload.miningRestrictions(); - RestrictionPacket.EnchantmentRestrictionRecord enchantmentRestrictions = payload.enchantmentRestrictions(); + RestrictionPacket.RestrictionRecord enchantmentRestrictions = payload.enchantmentRestrictions(); context.client().execute(() -> { LevelManager.BLOCK_RESTRICTIONS.clear(); diff --git a/src/main/java/net/levelz/network/packet/RestrictionPacket.java b/src/main/java/net/levelz/network/packet/RestrictionPacket.java index 98cdfb2..492d697 100644 --- a/src/main/java/net/levelz/network/packet/RestrictionPacket.java +++ b/src/main/java/net/levelz/network/packet/RestrictionPacket.java @@ -2,22 +2,18 @@ import net.levelz.LevelzMain; import net.levelz.level.restriction.PlayerRestriction; -import net.levelz.level.restriction.EnchantmentRestriction; -import net.minecraft.enchantment.Enchantment; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.CustomPayload; -import net.minecraft.registry.BuiltinRegistries; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.RegistryKeys; -import net.minecraft.registry.RegistryWrapper; -import net.minecraft.util.Identifier; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public record RestrictionPacket(RestrictionRecord blockRestrictions, RestrictionRecord craftingRestrictions, RestrictionRecord entityRestrictions, - RestrictionRecord itemRestrictions, RestrictionRecord miningRestrictions, EnchantmentRestrictionRecord enchantmentRestrictions) implements CustomPayload { + RestrictionRecord itemRestrictions, RestrictionRecord miningRestrictions, RestrictionRecord enchantmentRestrictions) implements CustomPayload { public static final CustomPayload.Id PACKET_ID = new CustomPayload.Id<>(LevelzMain.identifierOf("restriction_packet")); @@ -28,7 +24,7 @@ public record RestrictionPacket(RestrictionRecord blockRestrictions, Restriction value.itemRestrictions.write(buf); value.miningRestrictions.write(buf); value.enchantmentRestrictions.write(buf); - }, buf -> new RestrictionPacket(RestrictionRecord.read(buf), RestrictionRecord.read(buf), RestrictionRecord.read(buf), RestrictionRecord.read(buf), RestrictionRecord.read(buf), EnchantmentRestrictionRecord.read(buf))); + }, buf -> new RestrictionPacket(RestrictionRecord.read(buf), RestrictionRecord.read(buf), RestrictionRecord.read(buf), RestrictionRecord.read(buf), RestrictionRecord.read(buf), RestrictionRecord.read(buf))); public record RestrictionRecord(List ids, List restrictions) { @@ -73,64 +69,6 @@ public static RestrictionRecord read(PacketByteBuf buf) { } - public record EnchantmentRestrictionRecord(List ids, List restrictions) { - - public void write(PacketByteBuf buf) { - buf.writeInt(ids().size()); - for (String id : ids) { - buf.writeString(id); - } - buf.writeInt(restrictions().size()); - for (int i = 0; i < restrictions().size(); i++) { - EnchantmentRestriction enchantmentRestriction = restrictions().get(i); - buf.writeString(enchantmentRestriction.getEnchantment().getIdAsString()); - buf.writeInt(enchantmentRestriction.getSkillLevelRestrictions().size()); - for (Map.Entry> entry : enchantmentRestriction.getSkillLevelRestrictions().entrySet()) { - buf.writeInt(entry.getKey()); - buf.writeInt(entry.getValue().size()); - for (Map.Entry enchantmentEntry : entry.getValue().entrySet()) { - buf.writeInt(enchantmentEntry.getKey()); - buf.writeInt(enchantmentEntry.getValue()); - } - } - } - } - - public static EnchantmentRestrictionRecord read(PacketByteBuf buf) { - Optional> wrapper = BuiltinRegistries.createWrapperLookup().getOptionalWrapper(RegistryKeys.ENCHANTMENT); - List ids = new ArrayList<>(); - List enchantmentRestrictions = new ArrayList<>(); - if (wrapper.isPresent()) { - int idSize = buf.readInt(); - for (int i = 0; i < idSize; i++) { - ids.add(buf.readString()); - } - int size = buf.readInt(); - for (int i = 0; i < size; i++) { - String id = buf.readString(); - - int enchantmentSize = buf.readInt(); - Map> skillLevelRestrictions = new HashMap<>(); - - for (int u = 0; u < enchantmentSize; u++) { - int enchantmentLevel = buf.readInt(); - int restrictions = buf.readInt(); - Map enchantmentRestriction = new HashMap<>(); - for (int o = 0; o < restrictions; o++) { - int skillId = buf.readInt(); - int skillLevel = buf.readInt(); - enchantmentRestriction.put(skillId, skillLevel); - } - skillLevelRestrictions.put(enchantmentLevel,enchantmentRestriction); - } - enchantmentRestrictions.add(new EnchantmentRestriction(wrapper.get().getOptional(RegistryKey.of(RegistryKeys.ENCHANTMENT, Identifier.of(id))).get(), skillLevelRestrictions)); - } - } - return new EnchantmentRestrictionRecord(ids, enchantmentRestrictions); - } - - } - @Override public Id getId() { return PACKET_ID; diff --git a/src/main/java/net/levelz/registry/EnchantmentRegistry.java b/src/main/java/net/levelz/registry/EnchantmentRegistry.java new file mode 100644 index 0000000..b64fa69 --- /dev/null +++ b/src/main/java/net/levelz/registry/EnchantmentRegistry.java @@ -0,0 +1,67 @@ +package net.levelz.registry; + +import net.minecraft.enchantment.Enchantment; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.RegistryWrapper; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.util.Identifier; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class EnchantmentRegistry { + + public static final Map ENCHANTMENTS = new HashMap<>(); + public static final Map INDEX_ENCHANTMENTS = new HashMap<>(); + + public static boolean containsId(RegistryEntry enchantment, int level) { + return containsId(enchantment.toString(), level); + } + + public static boolean containsId(Identifier identifier, int level) { + return containsId(identifier.toString(), level); + } + + public static boolean containsId(String enchantment, int level) { + return INDEX_ENCHANTMENTS.containsKey(enchantment + level); + } + + public static EnchantmentZ getEnchantmentZ(int key) { + return ENCHANTMENTS.get(key); + } + + public static int getId(RegistryEntry enchantment, int level) { + return getId(enchantment.getIdAsString(), level); + } + + public static int getId(Identifier identifier, int level) { + return getId(identifier.toString(), level); + } + + public static int getId(String enchantment, int level) { + return getId(enchantment + level); + } + + private static int getId(String enchantment) { + if (INDEX_ENCHANTMENTS.containsKey(enchantment)) { + return INDEX_ENCHANTMENTS.get(enchantment); + } + return -1; + } + + public static void updateEnchantments(RegistryWrapper.WrapperLookup wrapperLookup) { + ENCHANTMENTS.clear(); + INDEX_ENCHANTMENTS.clear(); + Optional> wrapper = wrapperLookup.getOptionalWrapper(RegistryKeys.ENCHANTMENT); + for (RegistryWrapper.Impl enchantmentImpl : wrapper.stream().toList()) { + for (RegistryEntry.Reference enchantment : enchantmentImpl.streamEntries().toList()) { + for (int i = 1; i <= enchantment.value().getMaxLevel(); i++) { + INDEX_ENCHANTMENTS.put(enchantment.getIdAsString() + i, ENCHANTMENTS.size()); + ENCHANTMENTS.put(ENCHANTMENTS.size(), new EnchantmentZ(enchantment, i)); + } + } + } + } + +} diff --git a/src/main/java/net/levelz/registry/EnchantmentZ.java b/src/main/java/net/levelz/registry/EnchantmentZ.java new file mode 100644 index 0000000..f7207e6 --- /dev/null +++ b/src/main/java/net/levelz/registry/EnchantmentZ.java @@ -0,0 +1,23 @@ +package net.levelz.registry; + +import net.minecraft.enchantment.Enchantment; +import net.minecraft.registry.entry.RegistryEntry; + +public class EnchantmentZ { + + private final RegistryEntry entry; + private final int level; + + public EnchantmentZ(RegistryEntry entry, int level) { + this.entry = entry; + this.level = level; + } + + public RegistryEntry getEntry() { + return entry; + } + + public int getLevel() { + return level; + } +} diff --git a/src/main/java/net/levelz/screen/SkillInfoScreen.java b/src/main/java/net/levelz/screen/SkillInfoScreen.java index 8adef24..f32b964 100644 --- a/src/main/java/net/levelz/screen/SkillInfoScreen.java +++ b/src/main/java/net/levelz/screen/SkillInfoScreen.java @@ -6,20 +6,21 @@ import net.levelz.init.ConfigInit; import net.levelz.init.KeyInit; import net.levelz.level.LevelManager; -import net.levelz.level.restriction.PlayerRestriction; import net.levelz.level.Skill; import net.levelz.level.SkillBonus; -import net.levelz.level.restriction.EnchantmentRestriction; +import net.levelz.level.restriction.PlayerRestriction; import net.levelz.screen.widget.LineWidget; import net.libz.api.Tab; import net.libz.util.DrawTabHelper; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; -import net.minecraft.enchantment.EnchantmentLevelEntry; import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; @Environment(EnvType.CLIENT) public class SkillInfoScreen extends Screen implements Tab { @@ -58,10 +59,10 @@ protected void init() { if (skillExtraText.getString().equals(skillExtra)) { break; } - this.lines.add(new LineWidget(this.client, skillExtraText, null, null, 0)); + this.lines.add(new LineWidget(this.client, skillExtraText, null, 0)); } if (!this.lines.isEmpty()) { - this.lines.addFirst(new LineWidget(this.client, Text.translatable("skill.levelz.info"), null, null, 0)); + this.lines.addFirst(new LineWidget(this.client, Text.translatable("skill.levelz.info"), null, 0)); } int skillInfoLines = this.lines.size(); for (String bonusKey : SkillBonus.BONUS_KEYS) { @@ -78,18 +79,19 @@ protected void init() { if (bonusInfoText.getString().startsWith("---")) { break; } - this.lines.add(new LineWidget(this.client, bonusInfoText, null, null, 0)); + this.lines.add(new LineWidget(this.client, bonusInfoText, null, 0)); } } } } if (this.lines.size() > skillInfoLines) { - this.lines.add(skillInfoLines, new LineWidget(this.client, Text.translatable("bonus.levelz.info"), null, null, 0)); + this.lines.add(skillInfoLines, new LineWidget(this.client, Text.translatable("bonus.levelz.info"), null, 0)); } addRestrictionLines(LevelManager.ITEM_RESTRICTIONS, Text.translatable("restriction.levelz.item_usage"), 0); addRestrictionLines(LevelManager.BLOCK_RESTRICTIONS, Text.translatable("restriction.levelz.block_usage"), 1); addRestrictionLines(LevelManager.ENTITY_RESTRICTIONS, Text.translatable("restriction.levelz.entity_usage"), 2); + addRestrictionLines(LevelManager.ENCHANTMENT_RESTRICTIONS, Text.translatable("restriction.levelz.enchantments"), 3); } private void addRestrictionLines(Map levelRestrictions, Text restrictionText, int code) { @@ -113,40 +115,12 @@ private void addRestrictionLines(Map levelRestrictio } } - Map enchantments = null; - if (code == 0) { - - int enchantmentCode = -1; - for (EnchantmentRestriction enchantmentRestrictions : LevelManager.ENCHANTMENT_RESTRICTIONS.values()) { - if (enchantments == null) { - enchantments = new HashMap<>(); - } - // Enchantment lvl, Skill Id, Lvl - for (Map.Entry> enchantmentLevelRestriction : enchantmentRestrictions.getSkillLevelRestrictions().entrySet()) { - // Skill Id, Lvl - for (Map.Entry enchantmentRestriction : enchantmentLevelRestriction.getValue().entrySet()) { - if (enchantmentRestriction.getKey() == this.skill.getId()) { - if (map.containsKey(enchantmentRestriction.getValue())) { - map.get(enchantmentRestriction.getValue()).put(enchantmentCode, new PlayerRestriction(enchantmentCode, enchantmentLevelRestriction.getValue())); - } else { - Map newMap = new TreeMap<>(); - newMap.put(enchantmentCode, new PlayerRestriction(enchantmentCode, enchantmentLevelRestriction.getValue())); - map.put(enchantmentRestriction.getValue(), newMap); - } - enchantments.put(enchantmentCode, new EnchantmentLevelEntry(enchantmentRestrictions.getEnchantment(), enchantmentLevelRestriction.getKey())); - enchantmentCode--; - break; - } - } - } - } - } if (!map.isEmpty()) { - this.lines.add(new LineWidget(this.client, restrictionText, null, null, 0)); + this.lines.add(new LineWidget(this.client, restrictionText, null, 0)); } for (Map.Entry> restrictions : map.entrySet()) { - this.lines.add(new LineWidget(this.client, Text.translatable("text.levelz.gui.short_level", restrictions.getKey()), null, null, 0)); + this.lines.add(new LineWidget(this.client, Text.translatable("text.levelz.gui.short_level", restrictions.getKey()), null, 0)); if (restrictions.getValue().size() > 10) { Map newMap = new TreeMap<>(); @@ -156,16 +130,16 @@ private void addRestrictionLines(Map levelRestrictio newMap.put(specificRestriction.getKey(), specificRestriction.getValue()); count++; if (count == restrictions.getValue().size() - 1) { - this.lines.add(new LineWidget(this.client, null, newMap, enchantments, code)); + this.lines.add(new LineWidget(this.client, null, newMap, code)); break; } if (count % 9 == 0) { - this.lines.add(new LineWidget(this.client, null, new TreeMap<>(newMap), enchantments, code)); + this.lines.add(new LineWidget(this.client, null, new TreeMap<>(newMap), code)); newMap.clear(); } } } else { - this.lines.add(new LineWidget(this.client, null, restrictions.getValue(), enchantments, code)); + this.lines.add(new LineWidget(this.client, null, restrictions.getValue(), code)); } } } diff --git a/src/main/java/net/levelz/screen/SkillRestrictionScreen.java b/src/main/java/net/levelz/screen/SkillRestrictionScreen.java index aa58fa9..cbf79aa 100644 --- a/src/main/java/net/levelz/screen/SkillRestrictionScreen.java +++ b/src/main/java/net/levelz/screen/SkillRestrictionScreen.java @@ -76,11 +76,11 @@ protected void init() { newMap.put(entry.getKey(), entry.getValue()); count++; if (count == this.restrictions.size() - 1) { - this.lines.add(new LineWidget(this.client, null, newMap, null, code)); + this.lines.add(new LineWidget(this.client, null, newMap, code)); break; } if (count != 0 && count % 9 == 0) { - this.lines.add(new LineWidget(this.client, null, new LinkedHashMap<>(newMap), null, code)); + this.lines.add(new LineWidget(this.client, null, new LinkedHashMap<>(newMap), code)); newMap.clear(); } diff --git a/src/main/java/net/levelz/screen/widget/LineWidget.java b/src/main/java/net/levelz/screen/widget/LineWidget.java index 1547495..7c4da5a 100644 --- a/src/main/java/net/levelz/screen/widget/LineWidget.java +++ b/src/main/java/net/levelz/screen/widget/LineWidget.java @@ -6,11 +6,16 @@ import net.levelz.level.LevelManager; import net.levelz.level.restriction.PlayerRestriction; import net.levelz.mixin.entity.VehicleEntityAccessor; +import net.levelz.registry.EnchantmentRegistry; +import net.levelz.registry.EnchantmentZ; import net.levelz.screen.LevelScreen; import net.minecraft.block.Block; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.ItemEnchantmentsComponent; import net.minecraft.enchantment.Enchantment; +import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnchantmentLevelEntry; import net.minecraft.entity.EntityType; import net.minecraft.entity.vehicle.VehicleEntity; @@ -19,6 +24,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.SpawnEggItem; import net.minecraft.registry.Registries; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import org.jetbrains.annotations.Nullable; @@ -34,26 +40,23 @@ public class LineWidget { private final Text text; @Nullable private final Map restrictions; - @Nullable - private final Map enchantments; private final int code; - private Map entityStack; - private Map entityImages; + private Map customStacks; + private Map customImages; /** - * @param code 0 = item, 1 = block, 2 = entity + * @param code 0 = item, 1 = block, 2 = entity, 3 = enchantment */ - public LineWidget(MinecraftClient client, @Nullable Text text, @Nullable Map restrictions, @Nullable Map enchantments, int code) { + public LineWidget(MinecraftClient client, @Nullable Text text, @Nullable Map restrictions, int code) { this.client = client; this.text = text; this.restrictions = restrictions; - this.enchantments = enchantments; this.code = code; if (this.code == 2) { - this.entityStack = new HashMap<>(); - this.entityImages = new HashMap<>(); + this.customStacks = new HashMap<>(); + this.customImages = new HashMap<>(); for (Integer id : this.restrictions.keySet()) { EntityType entityType = Registries.ENTITY_TYPE.get(id); boolean imageExists = false; @@ -63,15 +66,21 @@ public LineWidget(MinecraftClient client, @Nullable Text text, @Nullable Map(); + for (Integer id : this.restrictions.keySet()) { + EnchantmentZ enchantmentZ = EnchantmentRegistry.getEnchantmentZ(id); + this.customStacks.put(id, EnchantedBookItem.forEnchantment(new EnchantmentLevelEntry(enchantmentZ.getEntry(), enchantmentZ.getLevel()))); + } } } @@ -85,26 +94,27 @@ public void render(DrawContext drawContext, int x, int y, int mouseX, int mouseY Text tooltipTitle; drawContext.drawTexture(LevelScreen.ICON_TEXTURE, x + separator - 1, y - 1, 0, 148, 18, 18); if (this.code == 0) { - if (entry.getKey() < 0 && this.enchantments != null && this.enchantments.containsKey(entry.getKey())) { - tooltipTitle = Enchantment.getName(this.enchantments.get(entry.getKey()).enchantment, this.enchantments.get(entry.getKey()).level); - drawContext.drawItem(EnchantedBookItem.forEnchantment(this.enchantments.get(entry.getKey())), x + separator, y); - } else { - Item item = Registries.ITEM.get(entry.getKey()); - tooltipTitle = item.getName(); - drawContext.drawItem(Registries.ITEM.get(entry.getKey()).getDefaultStack(), x + separator, y); - } + Item item = Registries.ITEM.get(entry.getKey()); + tooltipTitle = item.getName(); + drawContext.drawItem(Registries.ITEM.get(entry.getKey()).getDefaultStack(), x + separator, y); } else if (this.code == 1) { Block block = Registries.BLOCK.get(entry.getKey()); tooltipTitle = block.getName(); drawContext.drawItem(block.asItem().getDefaultStack(), x + separator, y); - } else {// if (this.code == 2) + } else if (this.code == 2) { EntityType entityType = Registries.ENTITY_TYPE.get(entry.getKey()); tooltipTitle = entityType.getName(); - if (this.entityStack.containsKey(entry.getKey())) { - drawContext.drawItem(this.entityStack.get(entry.getKey()), x + separator, y); + if (this.customStacks.containsKey(entry.getKey())) { + drawContext.drawItem(this.customStacks.get(entry.getKey()), x + separator, y); } else { - drawContext.drawTexture(this.entityImages.get(entry.getKey()), x + separator, y, 0, 0, 16, 16); + drawContext.drawTexture(this.customImages.get(entry.getKey()), x + separator, y, 0, 0, 16, 16); } + } else {// if (this.code == 3) { + ItemStack stack = this.customStacks.get(entry.getKey()); + RegistryEntry enchantment = EnchantmentHelper.getEnchantments(stack).getEnchantments().stream().findFirst().get(); + int level = stack.getOrDefault(DataComponentTypes.STORED_ENCHANTMENTS, ItemEnchantmentsComponent.DEFAULT).getLevel(enchantment); + tooltipTitle = Enchantment.getName(enchantment, level); + drawContext.drawItem(stack, x + separator, y); } if (!showTooltip && LevelScreen.isPointWithinBounds(x + separator, y, 16, 16, mouseX, mouseY)) { List tooltip = new ArrayList<>(); diff --git a/src/main/java/net/levelz/util/PacketHelper.java b/src/main/java/net/levelz/util/PacketHelper.java index 9bb59a6..66bd36c 100644 --- a/src/main/java/net/levelz/util/PacketHelper.java +++ b/src/main/java/net/levelz/util/PacketHelper.java @@ -64,6 +64,6 @@ public static void updateRestrictions(ServerPlayerEntity serverPlayerEntity) { ServerPlayNetworking.send(serverPlayerEntity, new RestrictionPacket(new RestrictionPacket.RestrictionRecord(LevelManager.BLOCK_RESTRICTIONS.keySet().stream().toList(), LevelManager.BLOCK_RESTRICTIONS.values().stream().toList()), new RestrictionPacket.RestrictionRecord(LevelManager.CRAFTING_RESTRICTIONS.keySet().stream().toList(), LevelManager.CRAFTING_RESTRICTIONS.values().stream().toList()), new RestrictionPacket.RestrictionRecord(LevelManager.ENTITY_RESTRICTIONS.keySet().stream().toList(), LevelManager.ENTITY_RESTRICTIONS.values().stream().toList()), new RestrictionPacket.RestrictionRecord(LevelManager.ITEM_RESTRICTIONS.keySet().stream().toList(), LevelManager.ITEM_RESTRICTIONS.values().stream().toList()), new RestrictionPacket.RestrictionRecord(LevelManager.MINING_RESTRICTIONS.keySet().stream().toList(), LevelManager.MINING_RESTRICTIONS.values().stream().toList()), - new RestrictionPacket.EnchantmentRestrictionRecord(LevelManager.ENCHANTMENT_RESTRICTIONS.keySet().stream().toList(), LevelManager.ENCHANTMENT_RESTRICTIONS.values().stream().toList()))); + new RestrictionPacket.RestrictionRecord(LevelManager.ENCHANTMENT_RESTRICTIONS.keySet().stream().toList(), LevelManager.ENCHANTMENT_RESTRICTIONS.values().stream().toList()))); } }