Search in sources :

Example 1 with ScriptCommand

use of me.superckl.api.superscript.script.command.ScriptCommand in project BiomeTweaker by superckl.

the class ScriptObject method handleCall.

public void handleCall(final String call, final ScriptContext context, final ScriptHandler handler) throws Exception {
    String command;
    try {
        command = ScriptParser.getCommandCalled(call);
    } catch (final IllegalArgumentException e) {
        APIInfo.log.error("Failed to parse command call: " + call + " @ " + context + ". Reason: " + e.getMessage());
        return;
    }
    if (!this.validCommands.containsKey(command)) {
        APIInfo.log.error("Failed to find meaning in command " + call + " @ " + context + ". Reason: No commands found with that name.");
        return;
    }
    final ScriptCommandListing listing = this.validCommands.get(command);
    String[] args = CollectionHelper.trimAll(ScriptParser.parseArguments(call));
    args = this.modifyArguments(args, handler);
    Pair<Constructor<? extends ScriptCommand>, Object[]> pair = ScriptParser.findConstructor(listing, args, handler);
    if (pair != null) {
        pair = this.modifyConstructorPair(pair, args, handler);
        final ScriptCommand sCommand = pair.getKey().newInstance(pair.getValue());
        sCommand.setContext(context);
        sCommand.setScriptHandler(handler);
        if (listing.isPerformInst())
            sCommand.perform();
        else
            this.addCommand(sCommand);
        return;
    }
    APIInfo.log.error("Failed to parse arguments for command " + call + " @ " + context + ". Reason: No constructors found with matching arguments.");
}
Also used : Constructor(java.lang.reflect.Constructor) ScriptCommandListing(me.superckl.api.superscript.script.command.ScriptCommandListing) ScriptCommand(me.superckl.api.superscript.script.command.ScriptCommand)

Example 2 with ScriptCommand

use of me.superckl.api.superscript.script.command.ScriptCommand in project BiomeTweaker by superckl.

the class CommonProxy method setupScripts.

@Override
public void setupScripts(final ASMDataTable table) {
    // Ensure BTParamterTypes registers its defaults
    BTParameterTypes.BLOCKSTATE_BUILDER.getTypeClass();
    LogHelper.debug("Discovering @AutoRegister script commands...");
    final Map<Class<? extends ScriptObject>, Map<String, ScriptCommandListing>> listings = new HashMap<>();
    final Set<ASMData> datas = table.getAll(AutoRegister.class.getCanonicalName());
    final Set<ASMData> groupedDatas = table.getAll(AutoRegisters.class.getCanonicalName());
    final Set<ASMData> allData = new HashSet<>(datas);
    for (final ASMData data : groupedDatas) {
        final List<Map<String, Object>> anns = WarningHelper.uncheckedCast(data.getAnnotationInfo().get("value"));
        anns.forEach(l -> allData.add(new ASMData(data.getCandidate(), AutoRegister.class.getCanonicalName(), data.getClassName(), data.getObjectName(), l)));
    }
    final Set<String> examinedClasses = new HashSet<>();
    for (final ASMData data : allData) try {
        if (examinedClasses.contains(data.getClassName()))
            continue;
        final Class<?> asmClass = Class.forName(data.getClassName());
        final Class<? extends ScriptCommand> scriptClass = asmClass.asSubclass(ScriptCommand.class);
        final AutoRegister[] classAnns = scriptClass.getAnnotationsByType(AutoRegister.class);
        final Constructor<?>[] cons = scriptClass.getConstructors();
        for (final Constructor<?> con : cons) {
            if (con.isAnnotationPresent(RegisterExempt.class))
                continue;
            final AutoRegister[] methodAnns = con.getAnnotationsByType(AutoRegister.class);
            if (classAnns.length == 0 && methodAnns.length == 0)
                continue;
            AutoRegister[] annsToUse;
            if (methodAnns.length != 0)
                annsToUse = methodAnns;
            else
                annsToUse = classAnns;
            final Class<?>[] cTypes = con.getParameterTypes();
            final ParameterWrapper<?>[] pTypes = new ParameterWrapper[cTypes.length];
            for (final ParameterOverride override : con.getAnnotationsByType(ParameterOverride.class)) pTypes[override.parameterIndex()] = ParameterTypes.getExceptionWrapper(override.exceptionKey());
            for (int i = 0; i < cTypes.length; i++) {
                if (pTypes[i] != null)
                    continue;
                final ParameterType<?> type = ParameterTypes.getDefaultType(cTypes[i]);
                if (type == null)
                    throw new IllegalStateException("No parameter type found for parameter " + cTypes[i].getCanonicalName());
                pTypes[i] = type.getSimpleWrapper();
            }
            for (final AutoRegister ann : annsToUse) for (final Class<? extends ScriptObject> clazz : ann.classes()) {
                if (!listings.containsKey(clazz))
                    listings.put(clazz, new HashMap<>());
                final Map<String, ScriptCommandListing> map = listings.get(clazz);
                if (!map.containsKey(ann.name()))
                    map.put(ann.name(), new ScriptCommandListing());
                final ScriptCommandListing listing = map.get(ann.name());
                listing.addEntry(Lists.newArrayList(pTypes), WarningHelper.uncheckedCast(con));
            }
        }
        examinedClasses.add(data.getClassName());
    } catch (final Exception e) {
        LogHelper.error("Failed to auto-register a script command " + data.getAnnotationInfo().toString());
        e.printStackTrace();
    }
    for (final Entry<Class<? extends ScriptObject>, Map<String, ScriptCommandListing>> entry : listings.entrySet()) {
        LogHelper.debug("Registering " + entry.getValue().size() + " commands to " + entry.getKey().getSimpleName());
        ScriptCommandRegistry.INSTANCE.registerClassListing(entry.getKey(), entry.getValue());
    }
    ScriptHandler.registerStaticObject("Tweaker", TweakerScriptObject.class);
    try {
        ConstructorListing<ScriptObject> listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(BTParameterTypes.BASIC_BIOMES_PACKAGE.getVarArgsWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("forBiomes", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(BTParameterTypes.TYPE_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("forBiomesOfTypes", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(BTParameterTypes.ALL_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("forAllBiomes", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(BTParameterTypes.ALL_BUT_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("forAllBiomesExcept", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(BTParameterTypes.INTERSECT_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("intersectionOf", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(BTParameterTypes.SUBTRACT_BIOMES_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("subtractFrom", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(BTParameterTypes.PROPERTY_RANGE_PACKAGE.getSpecialWrapper()), BiomesScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("forBiomesWithPropertyRange", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(new ArrayList<>(), OreDecorationScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("newOreDecoration", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(new ArrayList<>(), TreesDecorationScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("newTreeDecoration", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(new ArrayList<>(), ClusterDecorationScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("newClusterDecoration", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(new ArrayList<>(), SplotchDecorationScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("newSplotchDecoration", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(Lists.newArrayList(ParameterTypes.STRING.getSimpleWrapper()), BasicBlockStateScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("forBlock", listing);
        listing = new ConstructorListing<>();
        listing.addEntry(new ArrayList<>(), BlockReplacementScriptObject.class.getDeclaredConstructor());
        ScriptParser.registerValidObjectInst("newBlockReplacement", listing);
    } catch (final Exception e2) {
        LogHelper.error("Failed to populate object listings! Some tweaks may not be applied.");
        e2.printStackTrace();
    }
}
Also used : BiomesScriptObject(me.superckl.biometweaker.script.object.BiomesScriptObject) TweakerScriptObject(me.superckl.biometweaker.script.object.TweakerScriptObject) TreesDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.TreesDecorationScriptObject) ClusterDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.ClusterDecorationScriptObject) BlockReplacementScriptObject(me.superckl.biometweaker.script.object.block.BlockReplacementScriptObject) SplotchDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.SplotchDecorationScriptObject) BasicBlockStateScriptObject(me.superckl.biometweaker.script.object.block.BasicBlockStateScriptObject) ScriptObject(me.superckl.api.superscript.script.object.ScriptObject) OreDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.OreDecorationScriptObject) HashMap(java.util.HashMap) ParameterOverride(me.superckl.api.biometweaker.script.AutoRegister.ParameterOverride) ASMData(net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData) ConstructorListing(me.superckl.api.superscript.util.ConstructorListing) AutoRegister(me.superckl.api.biometweaker.script.AutoRegister) ScriptCommandListing(me.superckl.api.superscript.script.command.ScriptCommandListing) TreesDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.TreesDecorationScriptObject) ScriptCommand(me.superckl.api.superscript.script.command.ScriptCommand) SplotchDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.SplotchDecorationScriptObject) HashSet(java.util.HashSet) ParameterType(me.superckl.api.superscript.script.ParameterType) BasicBlockStateScriptObject(me.superckl.biometweaker.script.object.block.BasicBlockStateScriptObject) Constructor(java.lang.reflect.Constructor) BiomesScriptObject(me.superckl.biometweaker.script.object.BiomesScriptObject) ClusterDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.ClusterDecorationScriptObject) AutoRegisters(me.superckl.api.biometweaker.script.AutoRegisters) RegisterExempt(me.superckl.api.biometweaker.script.AutoRegister.RegisterExempt) BlockReplacementScriptObject(me.superckl.biometweaker.script.object.block.BlockReplacementScriptObject) OreDecorationScriptObject(me.superckl.biometweaker.script.object.decoration.OreDecorationScriptObject) Map(java.util.Map) HashMap(java.util.HashMap) ParameterOverride(me.superckl.api.biometweaker.script.AutoRegister.ParameterOverride)

Aggregations

Constructor (java.lang.reflect.Constructor)2 ScriptCommand (me.superckl.api.superscript.script.command.ScriptCommand)2 ScriptCommandListing (me.superckl.api.superscript.script.command.ScriptCommandListing)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AutoRegister (me.superckl.api.biometweaker.script.AutoRegister)1 ParameterOverride (me.superckl.api.biometweaker.script.AutoRegister.ParameterOverride)1 RegisterExempt (me.superckl.api.biometweaker.script.AutoRegister.RegisterExempt)1 AutoRegisters (me.superckl.api.biometweaker.script.AutoRegisters)1 ParameterType (me.superckl.api.superscript.script.ParameterType)1 ScriptObject (me.superckl.api.superscript.script.object.ScriptObject)1 ConstructorListing (me.superckl.api.superscript.util.ConstructorListing)1 BiomesScriptObject (me.superckl.biometweaker.script.object.BiomesScriptObject)1 TweakerScriptObject (me.superckl.biometweaker.script.object.TweakerScriptObject)1 BasicBlockStateScriptObject (me.superckl.biometweaker.script.object.block.BasicBlockStateScriptObject)1 BlockReplacementScriptObject (me.superckl.biometweaker.script.object.block.BlockReplacementScriptObject)1 ClusterDecorationScriptObject (me.superckl.biometweaker.script.object.decoration.ClusterDecorationScriptObject)1 OreDecorationScriptObject (me.superckl.biometweaker.script.object.decoration.OreDecorationScriptObject)1 SplotchDecorationScriptObject (me.superckl.biometweaker.script.object.decoration.SplotchDecorationScriptObject)1