Skip to content

Commit

Permalink
goh -> ObjectMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed May 18, 2024
1 parent 5c81c01 commit a886ce5
Show file tree
Hide file tree
Showing 28 changed files with 849 additions and 698 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import com.cleanroommc.groovyscript.documentation.Documentation;
import com.cleanroommc.groovyscript.documentation.linkgenerator.LinkGeneratorHooks;
import com.cleanroommc.groovyscript.event.EventHandler;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandler;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlerManager;
import com.cleanroommc.groovyscript.helper.JsonHelper;
import com.cleanroommc.groovyscript.mapper.ObjectMapper;
import com.cleanroommc.groovyscript.mapper.ObjectMapperManager;
import com.cleanroommc.groovyscript.network.CReload;
import com.cleanroommc.groovyscript.network.NetworkHandler;
import com.cleanroommc.groovyscript.network.NetworkUtils;
Expand Down Expand Up @@ -156,10 +156,10 @@ public static void initializeRunConfig(File minecraftHome) {
@ApiStatus.Internal
public static void initializeGroovyPreInit() {
// called via mixin in between construction and fml pre init
GameObjectHandlerManager.init();
ObjectMapperManager.init();
VanillaModule.initializeBinding();
ModSupport.init();
for (GameObjectHandler<?> goh : GameObjectHandlerManager.getGameObjectHandlers()) {
for (ObjectMapper<?> goh : ObjectMapperManager.getObjectMappers()) {
getSandbox().registerBinding(goh);
}
if (FMLLaunchHandler.isDeobfuscatedEnvironment()) Documentation.generate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.cleanroommc.groovyscript.api;

import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlers;
import com.cleanroommc.groovyscript.mapper.ObjectMappers;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistry;
Expand Down Expand Up @@ -35,7 +35,7 @@ public interface IGameObjectParser<T> {

static <T extends IForgeRegistryEntry<T>> IGameObjectParser<T> wrapForgeRegistry(IForgeRegistry<T> forgeRegistry) {
return (s, args) -> {
Result<ResourceLocation> rl = GameObjectHandlers.parseResourceLocation(s, args);
Result<ResourceLocation> rl = ObjectMappers.parseResourceLocation(s, args);
if (rl.hasError()) return Result.error(rl.getError());
T value = forgeRegistry.getValue(rl.getValue());
return value == null ? Result.error() : Result.some(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ default Collection<String> getAliases() {
/**
* Called before scripts are executed for the first time. Called right before {@link ModPropertyContainer#initialize()}.
* Used to initialize things like expansions with {@link com.cleanroommc.groovyscript.sandbox.expand.ExpansionHelper ExpansionHelper} and
* game object handlers with {@link com.cleanroommc.groovyscript.gameobjects.GameObjectHandlerManager GameObjectHandlerManager}.
* object mappers with {@link com.cleanroommc.groovyscript.mapper.ObjectMapperManager ObjectMapperManager}.
*
* @param container the created container for the compat mod
*/
Expand All @@ -62,6 +62,7 @@ default Collection<String> getAliases() {
/**
* Returns the override priority. Defines how this plugin should behave when another container with the same mod id exists.
* The return value should be as low as possible. Internal container always return {@link GroovyPlugin.Priority#NONE}.
*
* @return the override priority
* @see GroovyPlugin.Priority
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.cleanroommc.groovyscript.api.IGroovyContainer;
import com.cleanroommc.groovyscript.api.INamed;
import com.cleanroommc.groovyscript.api.IRegistrar;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandler;
import com.cleanroommc.groovyscript.mapper.ObjectMapper;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.NonExtendable
Expand Down Expand Up @@ -45,7 +45,7 @@ public void addPropertiesOfFields(Object o, boolean privateToo) {
}
}

public <V> GameObjectHandler.Builder<V> gameObjectHandlerBuilder(String name, Class<V> returnType) {
return new GameObjectHandler.Builder<>(name, returnType).mod(this);
public <V> ObjectMapper.Builder<V> objectMapper(String name, Class<V> returnType) {
return new ObjectMapper.Builder<>(name, returnType).mod(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.cleanroommc.groovyscript.api.Result;
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandler;
import net.minecraft.item.ItemStack;

public class Alchemistry extends ModPropertyContainer {
Expand All @@ -23,7 +22,7 @@ public class Alchemistry extends ModPropertyContainer {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("element", ItemStack.class)
container.objectMapper("element", ItemStack.class)
.parser((s, args) -> {
String parsedName = s.trim().toLowerCase().replace(" ", "_");
ChemicalCompound compound = CompoundRegistry.INSTANCE.get(parsedName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AppliedEnergistics2 extends ModPropertyContainer {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("tunnel", TunnelType.class)
container.objectMapper("tunnel", TunnelType.class)
.parser(IGameObjectParser.wrapEnum(TunnelType.class, false))
.completerOfNamed(() -> Arrays.asList(TunnelType.values()), v -> v.name().toUpperCase(Locale.ROOT))
.docOfType("P2P tunnel type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AstralSorcery extends ModPropertyContainer {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("constellation", IConstellation.class)
container.objectMapper("constellation", IConstellation.class)
.parser((s, args) -> {
for (IConstellation constellation : ConstellationRegistryAccessor.getConstellationList()) {
if (constellation.getSimpleName().equalsIgnoreCase(s)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.cleanroommc.groovyscript.api.IGameObjectParser;
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandler;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.lexicon.LexiconCategory;
import vazkii.botania.api.lexicon.LexiconEntry;
Expand Down Expand Up @@ -40,7 +39,7 @@ public static LexiconEntry getEntry(String name) {
@SuppressWarnings("Convert2MethodRef")
@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("brew", vazkii.botania.api.brew.Brew.class)
container.objectMapper("brew", vazkii.botania.api.brew.Brew.class)
.parser(IGameObjectParser.wrapStringGetter(val -> BotaniaAPI.brewMap.get(val), false))
.completerOfNames(() -> BotaniaAPI.brewMap.keySet())
.defaultValue(() -> BotaniaAPI.fallbackBrew)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.cleanroommc.groovyscript.api.IGameObjectParser;
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandler;
import org.cyclops.evilcraft.core.weather.WeatherType;

import java.util.Arrays;
Expand All @@ -17,7 +16,7 @@ public class EvilCraft extends ModPropertyContainer {
@Override
public void initialize(GroovyContainer<?> container) {
final List<String> weatherTypes = Arrays.asList("any", "clear", "rain", "lightning");
container.gameObjectHandlerBuilder("weather", WeatherType.class)
container.objectMapper("weather", WeatherType.class)
.parser(IGameObjectParser.wrapStringGetter(WeatherType::valueOf, true))
.completerOfNames(() -> weatherTypes) // elements don't have names
.defaultValue(() -> WeatherType.ANY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected static String getNormalName(String name) {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("species", AlleleBeeSpecies.class)
container.objectMapper("species", AlleleBeeSpecies.class)
.parser(Forestry::parseSpecies)
.completerOfNamed(() -> AlleleManager.alleleRegistry.getRegisteredAlleles().keySet(), s -> s.replace('.', ':')) // elements don't have names
.docOfType("allele bee species")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public class Mekanism extends ModPropertyContainer {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("gas", GasStack.class)
container.objectMapper("gas", GasStack.class)
.parser((s, args) -> {
Gas gas = GasRegistry.getGas(s);
return gas == null ? Result.error() : Result.some(new GasStack(gas, 1));
})
.completerOfNamed(GasRegistry::getRegisteredGasses, Gas::getName)
.docOfType("gas stack")
.register();
container.gameObjectHandlerBuilder("infusionType", InfuseType.class) // infusion clashes with infusion field
container.objectMapper("infusionType", InfuseType.class) // infusion clashes with infusion field
.parser(IGameObjectParser.wrapStringGetter(InfuseRegistry::get, true))
.completerOfNames(InfuseRegistry.getInfuseMap()::keySet)
.docOfType("infusion type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlers;
import com.cleanroommc.groovyscript.mapper.ObjectMappers;
import epicsquid.roots.api.Herb;
import epicsquid.roots.init.HerbRegistry;
import epicsquid.roots.modifiers.CostType;
Expand Down Expand Up @@ -41,28 +42,28 @@ public class Roots extends ModPropertyContainer {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("ritual", RitualBase.class)
container.objectMapper("ritual", RitualBase.class)
.parser(IGameObjectParser.wrapStringGetter(RitualRegistry::getRitual))
.completerOfNames(() -> RitualRegistry.ritualRegistry.keySet())
.docOfType("ritual")
.register();
container.gameObjectHandlerBuilder("herb", Herb.class)
container.objectMapper("herb", Herb.class)
.parser(IGameObjectParser.wrapStringGetter(HerbRegistry::getHerbByName))
.completerOfNames(HerbRegistry.registry::keySet)
.docOfType("herb")
.register();
container.gameObjectHandlerBuilder("cost", CostType.class)
container.objectMapper("cost", CostType.class)
.parser(IGameObjectParser.wrapEnum(CostType.class, false))
.completerOfEnum(CostType.class, false)
.docOfType("cost")
.register();
container.gameObjectHandlerBuilder("spell", SpellBase.class)
container.objectMapper("spell", SpellBase.class)
.parser(Roots::getSpell)
.completer(SpellRegistry.spellRegistry::keySet)
.defaultValueSup(() -> Result.some(FakeSpell.INSTANCE)) // crashes otherwise
.docOfType("spell")
.register();
container.gameObjectHandlerBuilder("modifier", Modifier.class)
container.objectMapper("modifier", Modifier.class)
.parser(Roots::getModifier)
.completerOfNamed(ModifierRegistry::getModifiers, v -> v.getRegistryName().toString())
.docOfType("modifier")
Expand All @@ -71,7 +72,7 @@ public void initialize(GroovyContainer<?> container) {

private static Result<SpellBase> getSpell(String s, Object... args) {
if (s.contains(":")) {
Result<ResourceLocation> rl = GameObjectHandlers.parseResourceLocation(s, args);
Result<ResourceLocation> rl = ObjectMappers.parseResourceLocation(s, args);
if (rl.hasError()) return Result.error(rl.getError());
SpellBase spell = SpellRegistry.getSpell(rl.getValue());
return spell == null ? Result.error() : Result.some(spell);
Expand All @@ -84,7 +85,7 @@ private static Result<SpellBase> getSpell(String s, Object... args) {
}

private static Result<Modifier> getModifier(String s, Object... args) {
Result<ResourceLocation> rl = GameObjectHandlers.parseResourceLocation(s, args);
Result<ResourceLocation> rl = ObjectMappers.parseResourceLocation(s, args);
if (rl.hasError()) return Result.error(rl.getError());
Modifier modifier = ModifierRegistry.get(rl.getValue());
return modifier == null ? Result.error() : Result.some(modifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public class Thaumcraft extends ModPropertyContainer {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("aspect", AspectStack.class)
container.objectMapper("aspect", AspectStack.class)
.parser(IGameObjectParser.wrapStringGetter(Thaumcraft::getAspect, AspectStack::new))
.completerOfNames(thaumcraft.api.aspects.Aspect.aspects::keySet)
.docOfType("aspect stack")
.register();
container.gameObjectHandlerBuilder("crystal", ItemStack.class)
container.objectMapper("crystal", ItemStack.class)
.parser(IGameObjectParser.wrapStringGetter(Thaumcraft::getAspect, ThaumcraftApiHelper::makeCrystal))
.completerOfNames(thaumcraft.api.aspects.Aspect.aspects::keySet)
.defaultValue(() -> ItemStack.EMPTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import cofh.thermalexpansion.util.managers.machine.CompactorManager;
import com.cleanroommc.groovyscript.api.IGameObjectParser;
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.compat.mods.ModPropertyContainer;
import com.cleanroommc.groovyscript.compat.mods.thermalexpansion.device.*;
import com.cleanroommc.groovyscript.compat.mods.thermalexpansion.dynamo.*;
import com.cleanroommc.groovyscript.compat.mods.thermalexpansion.machine.*;
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandler;
import com.cleanroommc.groovyscript.mapper.ObjectMapper;

import java.util.Arrays;
import java.util.Locale;
Expand Down Expand Up @@ -50,8 +51,8 @@ public class ThermalExpansion extends ModPropertyContainer {
public final XpCollector xpCollector = new XpCollector();

@Override
public void initialize() {
GameObjectHandler.builder("compactorMode", CompactorManager.Mode.class)
public void initialize(GroovyContainer<?> owner) {
ObjectMapper.builder("compactorMode", CompactorManager.Mode.class)
.mod("thermalexpansion")
.parser(IGameObjectParser.wrapEnum(CompactorManager.Mode.class, false))
.completerOfNamed(() -> Arrays.asList(CompactorManager.Mode.values()), v -> v.name().toUpperCase(Locale.ROOT))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public class TinkersConstruct extends ModPropertyContainer {

@Override
public void initialize(GroovyContainer<?> container) {
container.gameObjectHandlerBuilder("toolMaterial", Material.class)
container.objectMapper("toolMaterial", Material.class)
.parser(IGameObjectParser.wrapStringGetter(TinkerRegistryAccessor.getMaterials()::get))
.completerOfNames(TinkerRegistryAccessor.getMaterials()::keySet)
.docOfType("tool material")
.register();
container.gameObjectHandlerBuilder("toolTrait", ITrait.class)
container.objectMapper("toolTrait", ITrait.class)
.parser(IGameObjectParser.wrapStringGetter(TinkerRegistryAccessor.getTraits()::get))
.completerOfNamed(TinkerRegistryAccessor.getTraits()::keySet, v -> v.endsWith("_armor") ? null : v) // only suggest non armor traits
.docOfType("tool trait")
.register();
container.gameObjectHandlerBuilder("armorTrait", ITrait.class)
container.objectMapper("armorTrait", ITrait.class)
.parser(IGameObjectParser.wrapStringGetter(s -> TinkerRegistryAccessor.getTraits().get(s + "_armor")))
.completerOfNamed(TinkerRegistryAccessor.getTraits()::keySet,
v -> v.endsWith("_armor") ? v.substring(0, v.length() - 6) : null) // only suggest armor traits
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
package com.cleanroommc.groovyscript.gameobjects;

import com.cleanroommc.groovyscript.server.Completions;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Function;
import java.util.function.Supplier;

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
@FunctionalInterface
public interface Completer {

void complete(int paramIndex, Completions items);
public interface Completer extends com.cleanroommc.groovyscript.mapper.Completer {

static <V> Completer ofNamed(Supplier<Iterable<V>> values, Function<V, String> toString, int preferredParamIndex) {
return ofValues(values, v -> {
String s = toString.apply(v);
if (s != null) {
var item = new CompletionItem(toString.apply(v));
item.setKind(CompletionItemKind.Constant);
return item;
}
return null;
}, preferredParamIndex);
return (Completer) com.cleanroommc.groovyscript.mapper.Completer.ofNamed(values, toString, preferredParamIndex);
}

static <V> Completer ofValues(Supplier<Iterable<V>> values, Function<V, CompletionItem> toCompletionItem, int preferredParamIndex) {
return (paramIndex, items) -> {
if (preferredParamIndex < 0 || preferredParamIndex == paramIndex) {
items.addAll(values.get(), toCompletionItem);
}
};
}

default Completer and(Completer other) {
return (paramIndex, items) -> {
complete(paramIndex, items);
other.complete(paramIndex, items);
};
return (Completer) com.cleanroommc.groovyscript.mapper.Completer.ofValues(values, toCompletionItem, preferredParamIndex);
}
}
Loading

0 comments on commit a886ce5

Please sign in to comment.