Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/preInit/mekanism.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import net.minecraftforge.client.event.TextureStitchEvent
if (!isLoaded('mekanism')) return
println 'mod \'mekanism\' detected, running script'

eventManager.listen(TextureStitchEvent.Pre) { event ->
event.getMap().registerSprite(resource('placeholdername:blocks/mekanism_infusion_texture'))
eventManager.listen { TextureStitchEvent.Pre event ->
event.getMap().registerSprite(resource('groovyscriptdev:blocks/mekanism_infusion_texture'))
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public static String asGroovyCode(ChemicalElement element, boolean colored) {
return GroovyScriptCodeConverter.formatGenericHandler("element", element.getName(), colored);
}

public static String itemStackToElementGroovyCode(ItemStack itemStack, boolean colored) {
for (var x : ElementRegistry.INSTANCE.getAllElements()) {
if (ItemStack.areItemsEqual(x.toItemStack(1), itemStack)) {
return asGroovyCode(x, colored);
}
}
for (var x : CompoundRegistry.INSTANCE.compounds()) {
if (ItemStack.areItemsEqual(x.toItemStack(1), itemStack)) {
return asGroovyCode(x, colored);
}
}
return null;
}

@Override
public void initialize(GroovyContainer<?> container) {
container.objectMapperBuilder("element", ItemStack.class)
Expand All @@ -49,6 +63,7 @@ public void initialize(GroovyContainer<?> container) {
.completerOfNamed(CompoundRegistry.INSTANCE::compounds, ChemicalCompound::getName)
.completerOfNamed(ElementRegistry.INSTANCE::getAllElements, ChemicalElement::getName)
.docOfType("chemical element or compound as item stack")
.toGroovyCode(x -> itemStackToElementGroovyCode(x, false))
.register();

InfoParserRegistry.addInfoParser(InfoParserElement.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void initialize(GroovyContainer<?> container) {
.completerOfNamed(() -> Arrays.asList(TunnelType.values()), v -> v.name().toUpperCase(Locale.ROOT))
.defaultValue(() -> TunnelType.ME)
.docOfType("P2P tunnel type")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();

InfoParserRegistry.addInfoParser(InfoParserTunnel.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void initialize(GroovyContainer<?> container) {
})
.completerOfNamed(ConstellationRegistryAccessor::getConstellationList, IConstellation::getSimpleName)
.docOfType("constellation")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();
ExpansionHelper.mixinClass(ItemStack.class, CrystalItemStackExpansion.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void initialize(GroovyContainer<?> container) {
.completerOfNames(() -> BotaniaAPI.brewMap.keySet())
.defaultValue(() -> BotaniaAPI.fallbackBrew)
.docOfType("brew")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();

InfoParserRegistry.addInfoParser(InfoParserBrew.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public static String asGroovyCode(String weatherType, boolean colored) {
return GroovyScriptCodeConverter.formatGenericHandler("weather", weatherType, colored);
}

public static String asGroovyCode(WeatherType weatherType, boolean colored) {
return asGroovyCode(weatherType.toString(), colored);
}

@Override
public void initialize(GroovyContainer<?> container) {
final List<String> weatherTypes = Arrays.asList("any", "clear", "rain", "lightning");
Expand All @@ -27,6 +31,7 @@ public void initialize(GroovyContainer<?> container) {
.completerOfNames(() -> weatherTypes) // elements don't have names
.defaultValue(() -> WeatherType.ANY)
.docOfType("weather type")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();

InfoParserRegistry.addInfoParser(InfoParserWeather.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ public void initialize(GroovyContainer<?> container) {
})
.completerOfNamed(GasRegistry::getRegisteredGasses, Gas::getName)
.docOfType("gas stack")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();
container.objectMapperBuilder("infusionType", InfuseType.class)
.parser(IObjectParser.wrapStringGetter(InfuseRegistry::get, true))
.completerOfNames(InfuseRegistry.getInfuseMap()::keySet)
.docOfType("infusion type")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();

InfoParserRegistry.addInfoParser(InfoParserGas.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,32 @@ public void initialize(GroovyContainer<?> container) {
.parser(IObjectParser.wrapStringGetter(RitualRegistry::getRitual))
.completerOfNames(() -> RitualRegistry.ritualRegistry.keySet())
.docOfType("ritual")
.toGroovyCode(x -> GroovyScriptCodeConverter.formatGenericHandler("ritual", x.getName(), false))
.register();
container.objectMapperBuilder("herb", Herb.class)
.parser(IObjectParser.wrapStringGetter(HerbRegistry::getHerbByName))
.completerOfNames(HerbRegistry.registry::keySet)
.docOfType("herb")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();
container.objectMapperBuilder("cost", CostType.class)
.parser(IObjectParser.wrapEnum(CostType.class, false))
.completerOfEnum(CostType.class, false)
.docOfType("cost")
.toGroovyCode(x -> GroovyScriptCodeConverter.formatGenericHandler("cost", x.name(), false))
.register();
container.objectMapperBuilder("spell", SpellBase.class)
.parser(Roots::getSpell)
.completer(SpellRegistry.spellRegistry::keySet)
.defaultValueSup(() -> Result.some(FakeSpell.INSTANCE)) // crashes otherwise
.docOfType("spell")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();
container.objectMapperBuilder("modifier", Modifier.class)
.parser(Roots::getModifier)
.completerOfNamed(ModifierRegistry::getModifiers, v -> v.getRegistryName().toString())
.docOfType("modifier")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();

InfoParserRegistry.addInfoParser(InfoParserHerb.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import net.minecraft.item.ItemStack;
import thaumcraft.api.ThaumcraftApiHelper;
import thaumcraft.api.aspects.AspectList;
import thaumcraft.api.aspects.IEssentiaContainerItem;
import thaumcraft.api.items.ItemsTC;

import java.util.Collection;

Expand All @@ -42,18 +44,26 @@ public static String asGroovyCode(AspectStack aspectStack, boolean colored) {
return asGroovyCode(aspectStack.getAspect(), colored) + GroovyScriptCodeConverter.formatMultiple(aspectStack.getAmount(), colored);
}

public static String itemStackCrystalToAspectGroovyCode(ItemStack itemStack, boolean colored) {
if (itemStack.getItem() != ItemsTC.crystalEssence) return null;
var aspects = ((IEssentiaContainerItem) ItemsTC.crystalEssence).getAspects(itemStack);
return aspects.size() > 0 ? asGroovyCode(aspects.getAspects()[0], colored) : null;
}

@Override
public void initialize(GroovyContainer<?> container) {
container.objectMapperBuilder("aspect", AspectStack.class)
.parser(IObjectParser.wrapStringGetter(Thaumcraft::getAspect, AspectStack::new))
.completerOfNames(thaumcraft.api.aspects.Aspect.aspects::keySet)
.docOfType("aspect stack")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();
container.objectMapperBuilder("crystal", ItemStack.class)
.parser(IObjectParser.wrapStringGetter(Thaumcraft::getAspect, ThaumcraftApiHelper::makeCrystal))
.completerOfNames(thaumcraft.api.aspects.Aspect.aspects::keySet)
.defaultValue(() -> ItemStack.EMPTY)
.docOfType("aspect crystal as item stack")
.toGroovyCode(x -> itemStackCrystalToAspectGroovyCode(x, false))
.register();
ExpansionHelper.mixinClass(ItemStack.class, AspectItemStackExpansion.class);
ExpansionHelper.mixinClass(ItemStack.class, WarpItemStackExpansion.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
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 java.util.Arrays;
import java.util.Locale;
import com.cleanroommc.groovyscript.helper.ingredient.GroovyScriptCodeConverter;

public class ThermalExpansion extends GroovyPropertyContainer {

Expand Down Expand Up @@ -53,8 +51,9 @@ public class ThermalExpansion extends GroovyPropertyContainer {
public void initialize(GroovyContainer<?> container) {
container.objectMapperBuilder("compactorMode", CompactorManager.Mode.class)
.parser(IObjectParser.wrapEnum(CompactorManager.Mode.class, false))
.completerOfNamed(() -> Arrays.asList(CompactorManager.Mode.values()), v -> v.name().toUpperCase(Locale.ROOT))
.completerOfEnum(CompactorManager.Mode.class, false)
.defaultValue(() -> CompactorManager.Mode.ALL)
.toGroovyCode(x -> GroovyScriptCodeConverter.formatGenericHandler("compactorMode", x.name(), false))
.register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.cleanroommc.groovyscript.compat.mods.tinkersconstruct.material.ToolMaterialBuilder;
import com.cleanroommc.groovyscript.compat.mods.tinkersconstruct.material.traits.TraitRegistryEvent;
import com.cleanroommc.groovyscript.core.mixin.tconstruct.TinkerRegistryAccessor;
import com.cleanroommc.groovyscript.helper.ingredient.GroovyScriptCodeConverter;
import net.minecraftforge.common.MinecraftForge;
import slimeknights.tconstruct.library.materials.Material;
import slimeknights.tconstruct.library.traits.ITrait;
Expand All @@ -23,24 +24,43 @@ public class TinkersConstruct extends GroovyPropertyContainer {
public final CastingBasin castingBasin = new CastingBasin();
public final Materials materials = new Materials();

public static String asGroovyCode(Material material, boolean colored) {
return GroovyScriptCodeConverter.formatGenericHandler("toolMaterial", material.identifier, colored);
}

public static String asGroovyCode(String handler, ITrait trait, boolean colored) {
return GroovyScriptCodeConverter.formatGenericHandler(handler, trait.getIdentifier(), colored);
}

public static String asGroovyCodeTool(ITrait trait, boolean colored) {
return asGroovyCode("toolTrait", trait, colored);
}

public static String asGroovyCodeArmor(ITrait trait, boolean colored) {
return asGroovyCode("armorTrait", trait, colored);
}

@Override
public void initialize(GroovyContainer<?> container) {
container.objectMapperBuilder("toolMaterial", Material.class)
.parser(IObjectParser.wrapStringGetter(TinkerRegistryAccessor.getMaterials()::get))
.completerOfNames(TinkerRegistryAccessor.getMaterials()::keySet)
.docOfType("tool material")
.toGroovyCode(x -> asGroovyCode(x, false))
.register();
container.objectMapperBuilder("toolTrait", ITrait.class)
.parser(IObjectParser.wrapStringGetter(TinkerRegistryAccessor.getTraits()::get))
.completerOfNamed(TinkerRegistryAccessor.getTraits()::keySet, v -> v.endsWith("_armor") ? null : v) // only suggest non armor traits
.docOfType("tool trait")
.toGroovyCode(x -> asGroovyCodeTool(x, false))
.register();
container.objectMapperBuilder("armorTrait", ITrait.class)
.parser(IObjectParser.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
.docOfType("armor trait")
.toGroovyCode(x -> asGroovyCodeArmor(x, false))
.register();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.world.DimensionType;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.fluids.FluidStack;
Expand Down Expand Up @@ -190,6 +191,10 @@ public static String asGroovyCode(Potion potion, boolean colored) {
return formatResourceLocation("potion", Potion.REGISTRY.getNameForObject(potion), colored);
}

public static String asGroovyCode(SoundEvent sound, boolean colored) {
return formatForgeRegistryImpl("sound", sound, colored);
}

public static String asGroovyCode(PotionEffect potionEffect, boolean colored) {
StringBuilder builder = new StringBuilder();
List<String> list = Lists.newArrayList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.lang.reflect.Modifier;
import java.util.*;

public abstract class AbstractObjectMapper<T> extends Closure<T> implements INamed, IDocumented, IObjectParser<T>, TextureBinder<T> {
public abstract class AbstractObjectMapper<T> extends Closure<T> implements INamed, IDocumented, IObjectParser<T>, IObjectMapperInverter<T>, TextureBinder<T> {

private final String name;
private final GroovyContainer<?> mod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cleanroommc.groovyscript.api.Result;
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.helper.ingredient.GroovyScriptCodeConverter;
import com.cleanroommc.groovyscript.server.CompletionParams;
import com.cleanroommc.groovyscript.server.Completions;
import net.minecraft.block.Block;
Expand Down Expand Up @@ -60,6 +61,11 @@ public void provideCompletion(int index, CompletionParams params, Completions it
}
}

@Override
public String getGroovyCode(IBlockState value) {
return GroovyScriptCodeConverter.asGroovyCode(value, false);
}

@Override
public void bindTexture(IBlockState iBlockState) {
ItemStack itemStack = new ItemStack(iBlockState.getBlock(), 1, iBlockState.getBlock().getMetaFromState(iBlockState));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.cleanroommc.groovyscript.mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* A function to create a string from an object, with the string reprenting a full Object Mapper.
* This is used for {@link com.cleanroommc.groovyscript.mapper.ObjectMapper object mappers}.
* <p>
* A few examples of what this outputs would be
* {@code item('minecraft:clay')}, {@code biome('minecraft:desert')}, or {@code blockstate('minecraft:log', 'axis=z', 'variant=oak')}.
* <p>
* For Object Mappers with only one creation method,
* the return value should exactly correspond to the Object Mapper that creates it:
* <br>
* {@code item % item('minecraft:clay') == "item('minecraft:clay')"}.
* <p>
* Some Object Mappers may have multiple alternative creation methods,
* in which case the return value can be any one of those alternatives,
* but must still create the same object:
* <br>
* {@code blockstate % blockstate('minecraft:log:0') == "blockstate('minecraft:log', 'axis=y', 'variant=oak')"}
* and
* <br>
* {@code blockstate % blockstate('minecraft:log', 'axis=y', 'variant=oak') == "blockstate('minecraft:log', 'axis=y', 'variant=oak')"}
*
* @param <T> the type of the objects being inverted
*/
@FunctionalInterface
public interface IObjectMapperInverter<T> {

/**
* @param value the object being converted
* @return a String directly corresponding to the code that would create {@param value}
*/
String getGroovyCode(T value);

/**
* Using Groovy's Operator Overloading, this is %.
*/
default String mod(T value) {
return getGroovyCode(value);
}

/**
* Converts any number of objects into a collection of their strings
*/
@SuppressWarnings("unchecked")
default Collection<String> mod(T... values) {
List<String> list = new ArrayList<>();
for (T value : values) {
list.add(getGroovyCode(value));
}
return list;
}

/**
* Converts any number of objects into a collection of their strings
*/
default Collection<String> mod(Iterable<T> values) {
List<String> list = new ArrayList<>();
for (T value : values) {
list.add(getGroovyCode(value));
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cleanroommc.groovyscript.api.Result;
import com.cleanroommc.groovyscript.compat.mods.GroovyContainer;
import com.cleanroommc.groovyscript.helper.ingredient.GroovyScriptCodeConverter;
import com.cleanroommc.groovyscript.server.CompletionParams;
import com.cleanroommc.groovyscript.server.Completions;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -39,6 +40,11 @@ public void provideCompletion(int index, CompletionParams params, Completions it
if (index == 0) items.addAllOfRegistry(ForgeRegistries.ITEMS);
}

@Override
public String getGroovyCode(ItemStack value) {
return GroovyScriptCodeConverter.asGroovyCode(value, false, false);
}

@Override
public void bindTexture(ItemStack item) {
GlStateManager.enableDepth();
Expand Down
Loading