Skip to content

Commit

Permalink
1.19 scripting backport pre-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Caltinor committed Dec 4, 2024
1 parent cb57bff commit df17edf
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 98 deletions.
63 changes: 22 additions & 41 deletions src/main/java/harmonised/pmmo/api/APIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import harmonised.pmmo.config.codecs.*;
import harmonised.pmmo.core.CoreUtils;
import harmonised.pmmo.core.IDataStorage;
import net.minecraftforge.common.util.TriPredicate;
import org.apache.commons.lang3.function.TriFunction;
import org.checkerframework.checker.nullness.qual.NonNull;

import com.google.common.base.Preconditions;
Expand All @@ -25,7 +27,6 @@
import harmonised.pmmo.api.enums.ObjectType;
import harmonised.pmmo.api.enums.PerkSide;
import harmonised.pmmo.api.enums.ReqType;
import harmonised.pmmo.api.perks.Perk;
import harmonised.pmmo.config.codecs.CodecTypes.SalvageData;
import harmonised.pmmo.core.Core;
import harmonised.pmmo.util.MsLoggy;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class APIUtils {
public static int getLevel(String skill, Player player) {
Preconditions.checkNotNull(skill);
Preconditions.checkNotNull(player);
return Core.get(player.level()).getData().getPlayerSkillLevel(skill, player.getUUID());
return Core.get(player.getLevel()).getData().getPlayerSkillLevel(skill, player.getUUID());
}

/**Sets the player's current level in the skill provided
Expand All @@ -72,7 +73,7 @@ public static int getLevel(String skill, Player player) {
public static void setLevel(String skill, Player player, int level) {
Preconditions.checkNotNull(skill);
Preconditions.checkNotNull(player);
Core.get(player.level()).getData().setPlayerSkillLevel(skill, player.getUUID(), level);
Core.get(player.getLevel()).getData().setPlayerSkillLevel(skill, player.getUUID(), level);
}

/**changes the player's level in the specified skill by a specific amount.
Expand All @@ -87,7 +88,7 @@ public static void setLevel(String skill, Player player, int level) {
public static boolean addLevel(String skill, Player player, int levelChange) {
Preconditions.checkNotNull(skill);
Preconditions.checkNotNull(player);
return Core.get(player.level()).getData().changePlayerSkillLevel(skill, player.getUUID(), levelChange);
return Core.get(player.getLevel()).getData().changePlayerSkillLevel(skill, player.getUUID(), levelChange);
}

/**Gets the raw xp value associated with the specified skill and player.
Expand All @@ -99,7 +100,7 @@ public static boolean addLevel(String skill, Player player, int levelChange) {
public static long getXp(String skill, Player player) {
Preconditions.checkNotNull(skill);
Preconditions.checkNotNull(player);
return Core.get(player.level()).getData().getXpRaw(player.getUUID(), skill);
return Core.get(player.getLevel()).getData().getXpRaw(player.getUUID(), skill);
}

/**Sets the raw XP value for the player in the skill specified.
Expand All @@ -111,7 +112,7 @@ public static long getXp(String skill, Player player) {
public static void setXp(String skill, Player player, long xpRaw) {
Preconditions.checkNotNull(skill);
Preconditions.checkNotNull(player);
Core.get(player.level()).getData().setXpRaw(player.getUUID(), skill, xpRaw);
Core.get(player.getLevel()).getData().setXpRaw(player.getUUID(), skill, xpRaw);
}

/**Changes the player's current experience in the specified skill by the amount.
Expand All @@ -127,7 +128,7 @@ public static void setXp(String skill, Player player, long xpRaw) {
public static boolean addXp(String skill, Player player, long change) {
Preconditions.checkNotNull(skill);
Preconditions.checkNotNull(player);
IDataStorage data = Core.get(player.level()).getData();
IDataStorage data = Core.get(player.getLevel()).getData();
return CoreUtils.processSkillGroupXP(Map.of(skill, change)).entrySet().stream()
.allMatch(entry -> data.setXpDiff(player.getUUID(), entry.getKey(), entry.getValue()));
}
Expand All @@ -139,7 +140,7 @@ public static boolean addXp(String skill, Player player, long change) {
* @return a map of skills and raw xp
*/
public static Map<String, Long> getRawXpMap(Player player) {
return Core.get(player.level()).getData().getXpMap(player.getUUID());
return Core.get(player.getLevel()).getData().getXpMap(player.getUUID());
}

/**Returns the player's entire skill map.
Expand All @@ -148,7 +149,7 @@ public static Map<String, Long> getRawXpMap(Player player) {
* @return a map of skills and levels
*/
public static Map<String, Integer> getAllLevels(Player player) {
IDataStorage data = Core.get(player.level()).getData();
IDataStorage data = Core.get(player.getLevel()).getData();
return getRawXpMap(player).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> data.getLevelFromXP(e.getValue())));
}
Expand Down Expand Up @@ -257,7 +258,7 @@ public static Map<String, Integer> getRequirementMap(ItemStack item, ReqType typ
Preconditions.checkNotNull(item);
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(side);
return Core.get(side).getReqMap(type, item, true);
return Core.get(side).getReqMap(type, item);
}

/**Returns a skill-level map for the requirements of the block and the requirement type passed.
Expand Down Expand Up @@ -349,25 +350,6 @@ public static void registerXpAward(ObjectType oType, ResourceLocation objectID,
raw.setXpValues(type, award);
registerConfiguration(asOverride, oType, objectID, raw);
}

/**Registers a configuration for an entity or item for damage dealt
* and received xp events. <i>Note: passing other object types into
* this method will be ignored, and have no effect.</i>
*
* @param oType use only ITEM or ENTITY
* @param objectID the key for the object being configured
* @param isDealt is Dealt Damage config else if false will be received damage
* @param damageType the id or tag string for damage type
* @param award a map of skills and experience values to be awarded
* @param asOverride should this apply after datapacks as an override
*/
public static void registerDamageXpAward(ObjectType oType, ResourceLocation objectID, boolean isDealt, String damageType, Map<String, Long> award, boolean asOverride) {
if (oType == ObjectType.ENTITY || oType == ObjectType.ITEM) {
ObjectData raw = new ObjectData(asOverride);
raw.damageXpValues().put(isDealt ? EventType.DEAL_DAMAGE : EventType.RECEIVE_DAMAGE, Map.of(damageType, award));
registerConfiguration(asOverride, oType, objectID, raw);
}
}
/**registers a configuration setting for bonuses to xp gains.
*
* @param objectID the object linked to the bonus
Expand Down Expand Up @@ -449,8 +431,7 @@ public static void registerVeinData(ObjectType oType, ResourceLocation objectID,
return;
VeinData data = new VeinData(chargeCap, chargeRate, consumeAmount);
ObjectData raw = new ObjectData(asOverride, Set.of(), new HashMap<>(), new HashMap<>(), new HashMap<>(),
new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(),
new HashMap<>(), data);
new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), data);
registerConfiguration(asOverride, oType, objectID, raw);
}

Expand All @@ -463,7 +444,7 @@ public static void registerVeinData(ObjectType oType, ResourceLocation objectID,
* @param mob_modifiers a map of mob keys with a value map of attribute types and values
* @param asOverride should this apply after datapacks as an override
*/
public static void registerMobModifier(ObjectType oType, ResourceLocation locationID, Map<ResourceLocation, List<MobModifier>> mob_modifiers, boolean asOverride) {
public static void registerMobModifier(ObjectType oType, ResourceLocation locationID, Map<ResourceLocation, Map<String, Double>> mob_modifiers, boolean asOverride) {
if (oType != ObjectType.BIOME && oType != ObjectType.DIMENSION)
return;
LocationData raw = new LocationData(asOverride);
Expand Down Expand Up @@ -800,17 +781,17 @@ public static void registerListener(
*/
public static void registerPerk(
@NonNull ResourceLocation perkID,
@NonNull Perk perk,
@NonNull CompoundTag propertyDefaults,
@NonNull TriPredicate<Player, CompoundTag, Integer> customConditions,
@NonNull TriFunction<Player, CompoundTag, Integer, CompoundTag> onExecute,
@NonNull TriFunction<Player, CompoundTag, Integer, CompoundTag> onConclude,
@NonNull PerkSide side) {
switch (side) {
case SERVER -> {
Core.get(LogicalSide.SERVER).getPerkRegistry().registerPerk(perkID, perk);
Core.get(LogicalSide.CLIENT).getPerkRegistry().registerClientClone(perkID, perk);}
case CLIENT -> Core.get(LogicalSide.CLIENT).getPerkRegistry().registerPerk(perkID, perk);
case BOTH -> {
Core.get(LogicalSide.SERVER).getPerkRegistry().registerPerk(perkID, perk);
Core.get(LogicalSide.CLIENT).getPerkRegistry().registerPerk(perkID, perk);}
if (side.equals(PerkSide.SERVER) || side.equals(PerkSide.BOTH)) {
Core.get(LogicalSide.SERVER).getPerkRegistry().registerPerk(perkID, propertyDefaults, customConditions, onExecute, onConclude);
Core.get(LogicalSide.CLIENT).getPerkRegistry().registerProperties(perkID, propertyDefaults);
}
if (side.equals(PerkSide.CLIENT) || side.equals(PerkSide.BOTH))
Core.get(LogicalSide.CLIENT).getPerkRegistry().registerPerk(perkID, propertyDefaults, customConditions, onExecute, onConclude);
}

//===============UTILITY METHODS=================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public record LocationData(

public LocationData(boolean override) {
this(override, new HashSet<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(),
new ArrayList<>(), new HashMap<>(), new ArrayList<>(), new HashMap<>());
new ArrayList<>(), new HashMap<>(), new HashMap<>());
}
public LocationData() {this(
false, new HashSet<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(),
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/harmonised/pmmo/config/codecs/ObjectData.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public record ObjectData(
Map<ModifierDataType, List<LogicEntry>> nbtBonuses,
Map<ResourceLocation, SalvageData> salvage,
VeinData veinData) implements DataSource<ObjectData>{


public ObjectData(boolean override) {this(
override, new HashSet<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(),
new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), VeinData.EMPTY
);}
public ObjectData() {
this(false, new HashSet<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(),
new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), VeinData.EMPTY);
Expand Down Expand Up @@ -91,6 +95,11 @@ public void setNegativeEffects(Map<ResourceLocation, Integer> neg) {
}
@Override
public Set<String> getTagValues() {return tagValues();}

public void salvagePutAll(Map<ResourceLocation, SalvageData> collect) {
salvage().putAll(collect);
}

public static final Codec<ObjectData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.optionalFieldOf("override").forGetter(od -> Optional.of(od.override())),
Codec.STRING.listOf().optionalFieldOf("isTagFor").forGetter(od -> Optional.of(new ArrayList<>(od.tagValues))),
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/harmonised/pmmo/config/readers/CoreLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import harmonised.pmmo.config.scripting.Scripting;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import harmonised.pmmo.api.enums.ModifierDataType;
Expand Down Expand Up @@ -106,21 +104,21 @@ public void resetData() {
}

public final MergeableCodecDataManager<ObjectData, Item> ITEM_LOADER = new MergeableCodecDataManager<>(
"pmmo/items", DATA_LOGGER, ObjectData.CODEC, this::mergeLoaderData, this::printData, ObjectData::new, Registries.ITEM);
"pmmo/items", DATA_LOGGER, ObjectData.CODEC, this::mergeLoaderData, this::printData, ObjectData::new, ForgeRegistries.ITEMS);
public final MergeableCodecDataManager<ObjectData, Block> BLOCK_LOADER = new MergeableCodecDataManager<>(
"pmmo/blocks", DATA_LOGGER, ObjectData.CODEC, this::mergeLoaderData, this::printData, ObjectData::new, Registries.BLOCK);
"pmmo/blocks", DATA_LOGGER, ObjectData.CODEC, this::mergeLoaderData, this::printData, ObjectData::new, ForgeRegistries.BLOCKS);
public final MergeableCodecDataManager<ObjectData, EntityType<?>> ENTITY_LOADER = new MergeableCodecDataManager<>(
"pmmo/entities", DATA_LOGGER, ObjectData.CODEC, this::mergeLoaderData, this::printData, ObjectData::new, Registries.ENTITY_TYPE);
"pmmo/entities", DATA_LOGGER, ObjectData.CODEC, this::mergeLoaderData, this::printData, ObjectData::new, ForgeRegistries.ENTITY_TYPES);
public final MergeableCodecDataManager<LocationData, Biome> BIOME_LOADER = new MergeableCodecDataManager<>(
"pmmo/biomes", DATA_LOGGER, LocationData.CODEC, this::mergeLoaderData, this::printData, LocationData::new, Registries.BIOME);
"pmmo/biomes", DATA_LOGGER, LocationData.CODEC, this::mergeLoaderData, this::printData, LocationData::new, ForgeRegistries.BIOMES);
public final MergeableCodecDataManager<LocationData, Level> DIMENSION_LOADER = new MergeableCodecDataManager<>(
"pmmo/dimensions", DATA_LOGGER, LocationData.CODEC, this::mergeLoaderData, this::printData, LocationData::new, Registries.DIMENSION);
"pmmo/dimensions", DATA_LOGGER, LocationData.CODEC, this::mergeLoaderData, this::printData, LocationData::new, null);
public final MergeableCodecDataManager<PlayerData, Player> PLAYER_LOADER = new MergeableCodecDataManager<>(
"pmmo/players", DATA_LOGGER, PlayerData.CODEC, this::mergeLoaderData, this::printData, PlayerData::new, null);
public final MergeableCodecDataManager<EnhancementsData, Enchantment> ENCHANTMENT_LOADER = new MergeableCodecDataManager<>(
"pmmo/enchantments", DATA_LOGGER, EnhancementsData.CODEC, this::mergeLoaderData, this::printData, EnhancementsData::new, Registries.ENCHANTMENT);
"pmmo/enchantments", DATA_LOGGER, EnhancementsData.CODEC, this::mergeLoaderData, this::printData, EnhancementsData::new, ForgeRegistries.ENCHANTMENTS);
public final MergeableCodecDataManager<EnhancementsData, MobEffect> EFFECT_LOADER = new MergeableCodecDataManager<>(
"pmmo/effects", DATA_LOGGER, EnhancementsData.CODEC, this::mergeLoaderData, this::printData, EnhancementsData::new, Registries.MOB_EFFECT);
"pmmo/effects", DATA_LOGGER, EnhancementsData.CODEC, this::mergeLoaderData, this::printData, EnhancementsData::new, ForgeRegistries.MOB_EFFECTS);


private <T extends DataSource<T>> T mergeLoaderData(final List<T> raws) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Supplier;

import net.minecraft.core.RegistryAccess;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.SimplePreparableReloadListener;
Expand All @@ -16,9 +17,9 @@

public class ExecutableListener extends SimplePreparableReloadListener<Boolean> {
private final Consumer<RegistryAccess> executor;
private final RegistryAccess access;
private final Supplier<RegistryAccess> access;

public ExecutableListener(RegistryAccess access, Consumer<RegistryAccess> executor) {
public ExecutableListener(Supplier<RegistryAccess> access, Consumer<RegistryAccess> executor) {
this.access = access;
this.executor = executor;
}
Expand All @@ -28,7 +29,9 @@ public ExecutableListener(RegistryAccess access, Consumer<RegistryAccess> execut

@Override
protected void apply(Boolean pObject, ResourceManager pResourceManager, ProfilerFiller pProfiler) {
executor.accept(access);
var regAccess = access.get();
if (regAccess != null)
executor.accept(regAccess);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ of this software and associated documentation files (the "Software"), to deal

import javax.annotation.Nonnull;

import net.minecraft.core.RegistryAccess;
import org.apache.logging.log4j.Logger;

import com.google.gson.Gson;
Expand Down Expand Up @@ -273,7 +274,7 @@ protected void apply(final Map<ResourceLocation, T> processedData, final Resourc
}

@SuppressWarnings("unchecked")
public void postProcess() {
public void postProcess(RegistryAccess access) {
MsLoggy.DEBUG.log(LOG_CODE.DATA, "Begin PostProcessing for {}", folderName);
for (Map.Entry<ResourceLocation, T> dataRaw : new HashMap<>(this.data).entrySet()) {
DataSource<T> dataValue = dataRaw.getValue();
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/harmonised/pmmo/config/scripting/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import harmonised.pmmo.api.enums.ObjectType;
import harmonised.pmmo.util.MsLoggy;
import harmonised.pmmo.util.Reference;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -79,11 +78,11 @@ else if (str.endsWith(":*")) {

private static List<ResourceLocation> getMembers(boolean isTag, ResourceLocation tagID, RegistryAccess access, ObjectType type) {
return switch (type) {
case ITEM -> readRegistry(isTag, access, Registries.ITEM, tagID);
case BLOCK -> readRegistry(isTag, access, Registries.BLOCK, tagID);
case ENTITY -> readRegistry(isTag, access, Registries.ENTITY_TYPE, tagID);
case BIOME -> readRegistry(isTag, access, Registries.BIOME, tagID);
case ENCHANTMENT -> readRegistry(isTag, access, Registries.ENCHANTMENT, tagID);
case ITEM -> readRegistry(isTag, access, ForgeRegistries.ITEMS.getRegistryKey(), tagID);
case BLOCK -> readRegistry(isTag, access, ForgeRegistries.BLOCKS.getRegistryKey(), tagID);
case ENTITY -> readRegistry(isTag, access, ForgeRegistries.ENTITY_TYPES.getRegistryKey(), tagID);
case BIOME -> readRegistry(isTag, access, ForgeRegistries.BIOMES.getRegistryKey(), tagID);
case ENCHANTMENT -> readRegistry(isTag, access, ForgeRegistries.ENCHANTMENTS.getRegistryKey(), tagID);
default -> List.of();
};
}
Expand Down
Loading

0 comments on commit df17edf

Please sign in to comment.