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
3 changes: 2 additions & 1 deletion examples/postInit/actuallyadditions.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

if (!isLoaded('actuallyadditions')) return
// MODS_LOADED: actuallyadditions

println 'mod \'actuallyadditions\' detected, running script'


Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/advancedmortars.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('advancedmortars')) return
// MODS_LOADED: advancedmortars
println 'mod \'advancedmortars\' detected, running script'

// Mortar
Expand Down
10 changes: 8 additions & 2 deletions examples/postInit/appliedenergistics2.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@

if (!isLoaded('appliedenergistics2')) return
/*
NO_RUN
NO_RELOAD
MODS_LOADED: appliedenergistics2
SIDE: client
*/

// MODS_LOADED: appliedenergistics2
println 'mod \'appliedenergistics2\' detected, running script'

// Can be access via either `appliedenergistics2` or `ae2`
Expand Down
4 changes: 2 additions & 2 deletions examples/postInit/astral.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import net.minecraft.util.math.MathHelper

if (!isLoaded('astralsorcery')) return
// MODS_LOADED: astralsorcery
println 'mod \'astralsorcery\' detected, running script'

import net.minecraft.util.math.MathHelper

// Constellation bracket handler:
// Major (Bright) Constellations:
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/avaritia.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('avaritia')) return
// MODS_LOADED: avaritia
println 'mod \'avaritia\' detected, running script'

// extreme crafting
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/bloodmagic.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('bloodmagic')) return
// MODS_LOADED: bloodmagic
println 'mod \'bloodmagic\' detected, running script'


Expand Down
6 changes: 3 additions & 3 deletions examples/postInit/botania.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import net.minecraft.potion.PotionEffect
import net.minecraft.util.text.TextFormatting

if (!isLoaded('botania')) return
// MODS_LOADED: botania
println 'mod \'botania\' detected, running script'

import net.minecraft.potion.PotionEffect
import net.minecraft.util.text.TextFormatting

// Bracket Handlers
// Brew:
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/chisel.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('chisel')) return
// MODS_LOADED: chisel
println 'mod \'chisel\' detected, running script'

// Carving
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/compactmachines.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('compactmachines3')) return
// MODS_LOADED: compactmachines3
println 'mod \'compactmachines3\' detected, running script'

// Miniaturization:
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/draconicevolution.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('draconicevolution')) return
// MODS_LOADED: draconicevolution
println 'mod \'draconicevolution\' detected, running script'

// Fusion:
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/enderio.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('enderio')) return
// MODS_LOADED: enderio
println 'mod \'enderio\' detected, running script'

// Alloy Smelter (Alloying):
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/evilcraft.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('evilcraft')) return
// MODS_LOADED: evilcraft
println 'mod \'evilcraft\' detected, running script'

// Weather Bracket Handler
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/extendedcrafting.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('extendedcrafting')) return
// MODS_LOADED: extendedcrafting
println 'mod \'extendedcrafting\' detected, running script'

// Combination Crafting (Combination):
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/forestry.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('forestry')) return
// MODS_LOADED: forestry
println 'mod \'forestry\' detected, running script'

// Species Bracket Handler
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/immersiveengineering.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('immersiveengineering')) return
// MODS_LOADED: immersiveengineering
println 'mod \'immersiveengineering\' detected, running script'

// Alloy Kiln:
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/inspirations.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('inspirations')) return
// MODS_LOADED: inspirations
println 'mod \'inspirations\' detected, running script'

// Cauldron:
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/integrateddynamics.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('integrateddynamics')) return
// MODS_LOADED: integrateddynamics
println 'mod \'integrateddynamics\' detected, running script'

// Drying Basin and Mechanical Drying Basin:
Expand Down
5 changes: 1 addition & 4 deletions examples/postInit/loottables.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

if (isReloading()) {
println 'Cancelled running script loot'
return
}
// NO_RELOAD

def pyramidLootTable = loot.getTable(resource('minecraft:chests/stronghold_library'))

Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/mekanism.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('mekanism')) return
// MODS_LOADED: mekanism
println 'mod \'mekanism\' detected, running script'

// Bracket Handlers
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/roots.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('roots')) return
// MODS_LOADED: roots
println 'mod \'roots\' detected, running script'

// Bracket Handlers
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/thaumcraft.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('thaumcraft')) return
// MODS_LOADED: thaumcraft
println 'mod \'thaumcraft\' detected, running script'

mods.thaumcraft.Crucible.removeByOutput(item('minecraft:gunpowder'))
Expand Down
2 changes: 1 addition & 1 deletion examples/postInit/thermal.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

if (!isLoaded('thermalexpansion')) return
// MODS_LOADED: thermalexpansion
println 'mod \'thermalexpansion\' detected, running script'

mods.te.Pulverizer.recipeBuilder()
Expand Down
13 changes: 6 additions & 7 deletions examples/postInit/woot.groovy
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

if (!isLoaded('woot')) return
// MODS_LOADED: woot
println 'mod \'woot\' detected, running script'

//import ipsis.woot.configuration.EnumConfigKey

//import ipsis.woot.util.WootMobName
import ipsis.woot.configuration.EnumConfigKey
import ipsis.woot.util.WootMobName

// Note:
// Drops, Spawning, Policy, and Mob Config can also be controlled via .json config file
Expand Down Expand Up @@ -50,7 +49,7 @@ mods.woot.drops.recipeBuilder()
.size(5, 10, 20, 50)
.register()

//mods.woot.drops.removeByEntity(new WootMobName('minecraft:ender_dragon'))
mods.woot.drops.removeByEntity(new WootMobName('minecraft:ender_dragon'))
mods.woot.drops.removeByEntity(entity('minecraft:ender_dragon'))
mods.woot.drops.removeByEntity('minecraft:ender_dragon')
mods.woot.drops.removeByEntity('minecraft:ender_dragon', '') // NBT tag
Expand All @@ -72,8 +71,8 @@ mods.woot.spawning.recipeBuilder()
.register()


//mods.woot.spawning.remove(new WootMobName('minecraft:ender_dragon'))
//mods.woot.spawning.removeByEntity(new WootMobName('minecraft:ender_dragon'))
mods.woot.spawning.remove(new WootMobName('minecraft:ender_dragon'))
mods.woot.spawning.removeByEntity(new WootMobName('minecraft:ender_dragon'))
mods.woot.spawning.removeByEntity(entity('minecraft:ender_dragon'))
mods.woot.spawning.removeByEntity('minecraft:ender_dragon')
mods.woot.spawning.removeByEntity('minecraft:ender_dragon', '') // NBT tag
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/sandbox/Preprocessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.cleanroommc.groovyscript.sandbox;

import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.registry.ReloadableRegistryManager;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Loader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class Preprocessor {

public static boolean validatePreprocessors(File file) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
boolean isComment = false;
String line;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.isEmpty()) continue;
if (line.startsWith("/*")) {
isComment = true;
line = line.substring(2).trim();
if (line.isEmpty()) continue;
}
if (line.startsWith("//")) {
line = line.substring(2).trim();
if (line.isEmpty()) continue;
} else if (!isComment) {
return true;
}
if (isComment && line.endsWith("*/")) {
isComment = false;
}

if (!processPreprocessor(line)) {
return false;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}

private static boolean processPreprocessor(String line) {
if (line.startsWith("NO_RUN")) {
return false;
}
if (line.startsWith("DEBUG_ONLY")) {
return GroovyScript.getRunConfig().isDebug();
}
if (line.startsWith("NO_RELOAD")) {
return !ReloadableRegistryManager.isFirstLoad();
}
if (line.startsWith("MODS_LOADED")) {
return checkModsLoaded(getArguments(line));
}
if (line.startsWith("SIDE")) {
return checkSide(getArguments(line));
}
return true;
}

private static boolean checkModsLoaded(String[] mods) {
for (String mod : mods) {
if (!Loader.isModLoaded(mod)) {
return false;
}
}
return true;
}

private static boolean checkSide(String[] sides) {
if (sides.length != 1) {
GroovyLog.get().error("Side preprocessor should have exactly one argument, but found {}", Arrays.asList(sides));
return true;
}
String side = sides[0].toUpperCase();
if (side.equals("CLIENT")) {
return FMLCommonHandler.instance().getSide().isClient();
}
if (side.equals("SERVER")) {
return FMLCommonHandler.instance().getSide().isServer();
}
GroovyLog.get().error("Side processor argument must be CLIENT or SERVER (lower case is allowed too)");
return true;
}

private static String[] getArguments(String line) {
String[] parts = line.split(":", 2);
if (parts.length < 2) {
return new String[0];
}
String[] args = parts[1].split(",");
for (int i = 0; i < args.length; i++) {
args[i] = args[i].trim();
}
return args;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ private Collection<File> getSortedFilesOf(Collection<String> paths) {
try (Stream<Path> stream = Files.walk(rootFile.toPath())) {
stream.filter(path1 -> isGroovyFile(path1.toString()))
.map(Path::toFile)
.filter(Preprocessor::validatePreprocessors)
.sorted(Comparator.comparing(File::getPath))
.forEach(file -> {
if (files.containsKey(file)) {
Expand Down