Skip to content

Commit

Permalink
added enchantment registry + fixed issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Globox1997 committed Nov 28, 2024
1 parent 86c98de commit f4f72e2
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 215 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
-
### Fixed:
- Fixed npe
-
- Fixed registry issue
### Changed:
-
117 changes: 65 additions & 52 deletions src/main/java/net/levelz/data/RestrictionLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,41 @@
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;
import org.apache.logging.log4j.Logger;

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<Integer> blockList = new ArrayList<>();
private List<Integer> craftingList = new ArrayList<>();
private List<Integer> entityList = new ArrayList<>();
private List<Integer> itemList = new ArrayList<>();
private List<Integer> miningList = new ArrayList<>();
private Map<String, List<Integer>> enchantmentList = new HashMap<>();
private static final List<Integer> blockList = new ArrayList<>();
private static final List<Integer> craftingList = new ArrayList<>();
private static final List<Integer> entityList = new ArrayList<>();
private static final List<Integer> itemList = new ArrayList<>();
private static final List<Integer> miningList = new ArrayList<>();
private static final List<Integer> enchantmentList = new ArrayList<>();

// private ItemStringReader itemStringReader = new ItemStringReader(BuiltinRegistries.createWrapperLookup());

@Override
public Identifier getFabricId() {
return LevelzMain.identifierOf("restriction");
return ID;
}

@Override
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -177,36 +184,42 @@ public void reload(ResourceManager manager) {
}
// enchantments
if (restrictionJsonObject.has("enchantments")) {
Optional<RegistryWrapper.Impl<Enchantment>> 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<RegistryEntry.Reference<Enchantment>> 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<Integer, Map<Integer, Integer>> 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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/levelz/init/LoaderInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/net/levelz/level/LevelManager.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
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;
import net.minecraft.nbt.NbtCompound;
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;
Expand All @@ -26,7 +28,7 @@ public class LevelManager {
public static final Map<Integer, PlayerRestriction> ENTITY_RESTRICTIONS = new HashMap<>();
public static final Map<Integer, PlayerRestriction> ITEM_RESTRICTIONS = new HashMap<>();
public static final Map<Integer, PlayerRestriction> MINING_RESTRICTIONS = new HashMap<>();
public static final Map<String, EnchantmentRestriction> ENCHANTMENT_RESTRICTIONS = new HashMap<>();
public static final Map<Integer, PlayerRestriction> ENCHANTMENT_RESTRICTIONS = new HashMap<>();
public static final Map<String, SkillBonus> BONUSES = new HashMap<>();

private final PlayerEntity playerEntity;
Expand Down Expand Up @@ -285,26 +287,24 @@ public Map<Integer, Integer> 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<Integer, Integer> entry : enchantmentRestriction.getSkillLevelRestrictions().get(level).entrySet()) {
if (this.getSkillLevel(entry.getKey()) < entry.getValue()) {
return false;
}
public boolean hasRequiredEnchantmentLevel(RegistryEntry<Enchantment> enchantment, int level) {
int enchantmentId = EnchantmentRegistry.getId(enchantment, level);
if (ENCHANTMENT_RESTRICTIONS.containsKey(enchantmentId)) {
PlayerRestriction playerRestriction = ENCHANTMENT_RESTRICTIONS.get(enchantmentId);
for (Map.Entry<Integer, Integer> entry : playerRestriction.getSkillLevelRestrictions().entrySet()) {
if (this.getSkillLevel(entry.getKey()) < entry.getValue()) {
return false;
}
}
}
return true;
}

public Map<Integer, Integer> 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<Integer, Integer> getRequiredEnchantmentLevel(RegistryEntry<Enchantment> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,8 +58,8 @@ private void hasRequiredMiningLevelMixin(Block block, CallbackInfoReturnable<Boo
}
}

@Inject(method = "hasRequiredEnchantmentLevel", at = @At("HEAD"), cancellable = true, remap = false)
private void hasRequiredEnchantmentLevelMixin(String enchantment, int level, CallbackInfoReturnable<Boolean> info) {
@Inject(method = "hasRequiredEnchantmentLevel", at = @At("HEAD"), cancellable = true)
private void hasRequiredEnchantmentLevelMixin(RegistryEntry<Enchantment> enchantment, int level, CallbackInfoReturnable<Boolean> info) {
if (this.playerEntity.getClass().getName().contains("deployer.DeployerFakePlayer") || this.playerEntity.getClass().getName().contains("core.TurtlePlayer")) {
info.setReturnValue(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class EnchantmentHelperMixin {
private static void forEachEnchantmentMixin(EnchantmentHelper.ContextAwareConsumer instance, RegistryEntry<Enchantment> enchantmentRegistryEntry, int i, EnchantmentEffectContext enchantmentEffectContext, Operation<Void> 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 {
Expand Down
Loading

0 comments on commit f4f72e2

Please sign in to comment.