Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isEnabled to IScriptReloadable, clarifying some NPE errors #127

Merged
merged 4 commits into from
Feb 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.jetbrains.annotations.ApiStatus;

import java.util.Collection;

public interface IScriptReloadable extends INamed {

@GroovyBlacklist
Expand All @@ -13,4 +11,10 @@ public interface IScriptReloadable extends INamed {
@GroovyBlacklist
@ApiStatus.OverrideOnly
void afterScriptLoad();

@GroovyBlacklist
default boolean isEnabled() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.cleanroommc.groovyscript.compat.mods;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.IDynamicGroovyProperty;
import com.cleanroommc.groovyscript.api.IScriptReloadable;
import com.cleanroommc.groovyscript.api.IVirtualizedRegistrar;
import com.cleanroommc.groovyscript.api.*;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -32,7 +29,16 @@ public Collection<IScriptReloadable> getRegistries() {

@Override
public @Nullable Object getProperty(String name) {
return registries.get(name);
IScriptReloadable registry = registries.get(name);
if (registry == null) {
GroovyLog.get().error("Attempted to access registry {}, but could not find a registry with that name", name);
return null;
}
if (!registry.isEnabled()) {
GroovyLog.get().error("Attempted to access registry {}, but that registry was disabled", registry.getName());
return null;
}
return registry;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.minecraftforge.fluids.FluidStack;
import org.cyclops.cyclopscore.recipe.custom.api.IRecipe;
import org.cyclops.cyclopscore.recipe.custom.component.IngredientRecipeComponent;
import org.cyclops.evilcraft.Configs;
import org.cyclops.evilcraft.block.BloodInfuserConfig;
import org.cyclops.evilcraft.core.recipe.custom.DurationXpRecipeProperties;
import org.cyclops.evilcraft.core.recipe.custom.IngredientFluidStackAndTierRecipeComponent;
import org.jetbrains.annotations.Nullable;
Expand All @@ -23,6 +25,11 @@ public BloodInfuser() {
super();
}

@Override
public boolean isEnabled() {
return Configs.isEnabled(BloodInfuserConfig.class);
}

@RecipeBuilderDescription(example = {
@Example(".input(item('minecraft:clay')).output(item('minecraft:clay')).fluidInput(fluid('evilcraftblood') * 1000).tier(3).duration(100).xp(10000)"),
@Example(".input(item('minecraft:gold_ingot')).output(item('minecraft:clay')).fluidInput(100000)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
import net.minecraft.item.ItemStack;
import org.cyclops.cyclopscore.recipe.custom.api.IRecipe;
import org.cyclops.evilcraft.Configs;
import org.cyclops.evilcraft.block.EnvironmentalAccumulatorConfig;
import org.cyclops.evilcraft.core.recipe.custom.EnvironmentalAccumulatorRecipeComponent;
import org.cyclops.evilcraft.core.recipe.custom.EnvironmentalAccumulatorRecipeProperties;
Expand All @@ -23,6 +24,11 @@ public EnvironmentalAccumulator() {
super();
}

@Override
public boolean isEnabled() {
return Configs.isEnabled(EnvironmentalAccumulatorConfig.class);
}

@RecipeBuilderDescription(example = {
@Example(".input(item('minecraft:clay')).output(item('minecraft:clay') * 2).inputWeather(weather('clear')).outputWeather(weather('rain')).processingspeed(1).cooldowntime(1000).duration(10)"),
@Example(".input(item('minecraft:gold_ingot')).output(item('minecraft:diamond')).inputWeather(weather('rain')).outputWeather(weather('lightning')).speed(10).cooldown(1)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import forestry.api.genetics.AlleleManager;
import forestry.apiculture.genetics.alleles.AlleleBeeSpecies;
import forestry.modules.ForestryModuleUids;
import org.jetbrains.annotations.Nullable;

public class Forestry extends ModPropertyContainer {

Expand Down Expand Up @@ -39,18 +38,6 @@ public Forestry() {
addRegistry(beeMutations);
}

@Override
public void initialize() {
GameObjectHandlerManager.registerGameObjectHandler("forestry", "species", Forestry::parseSpecies);
}

@Override
public @Nullable Object getProperty(String name) {
Object registry = super.getProperty(name);
if (registry instanceof ForestryRegistry<?> && !((ForestryRegistry<?>) registry).isEnabled()) return null;
return registry;
}

public static Result<AlleleBeeSpecies> parseSpecies(String mainArg, Object... args) {
if (!ForestryAPI.moduleManager.isModuleEnabled("forestry", ForestryModuleUids.APICULTURE)) {
return Result.error("Can't get bee species while apiculture is disabled.");
Expand All @@ -71,4 +58,9 @@ protected static String getNormalName(String name) {
String capital = name.substring(0, 1).toUpperCase() + name.substring(1);
return "species" + capital;
}

@Override
public void initialize() {
GameObjectHandlerManager.registerGameObjectHandler("forestry", "species", Forestry::parseSpecies);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import org.cyclops.cyclopscore.recipe.custom.api.IRecipe;
import org.cyclops.cyclopscore.recipe.custom.component.DurationRecipeProperties;
import org.cyclops.cyclopscore.recipe.custom.component.IngredientAndFluidStackRecipeComponent;
import org.cyclops.integrateddynamics.Configs;
import org.cyclops.integrateddynamics.block.BlockDryingBasin;
import org.cyclops.integrateddynamics.block.BlockDryingBasinConfig;
import org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin;
import org.jetbrains.annotations.Nullable;

@RegistryDescription
Expand All @@ -20,6 +24,11 @@ public DryingBasin() {
super();
}

@Override
public boolean isEnabled() {
return Configs.isEnabled(BlockDryingBasinConfig.class);
}

@RecipeBuilderDescription(example = {
@Example(".input(item('minecraft:gold_ingot')).output(item('minecraft:clay')).fluidInput(fluid('water') * 500).fluidOutput(fluid('lava') * 2000).mechanical().duration(5)"),
@Example(".output(item('minecraft:clay')).fluidInput(fluid('water') * 2000)")
Expand All @@ -30,8 +39,8 @@ public RecipeBuilder recipeBuilder() {

@Override
public void onReload() {
removeScripted().forEach(org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes()::remove);
restoreFromBackup().forEach(org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes()::add);
removeScripted().forEach(BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes()::remove);
restoreFromBackup().forEach(BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes()::add);
}

public void add(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties> recipe) {
Expand All @@ -41,19 +50,19 @@ public void add(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFlu
public void add(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties> recipe, boolean add) {
if (recipe == null) return;
addScripted(recipe);
if (add) org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().add(recipe);
if (add) BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().add(recipe);
}

public boolean remove(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties> recipe) {
if (recipe == null) return false;
addBackup(recipe);
org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().remove(recipe);
BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().remove(recipe);
return true;
}

@MethodDescription(description = "groovyscript.wiki.removeByInput")
public boolean removeByInput(ItemStack input) {
return org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
return BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
if (r.getInput().getIngredient().test(input)) {
addBackup(r);
return true;
Expand All @@ -64,7 +73,7 @@ public boolean removeByInput(ItemStack input) {

@MethodDescription(description = "groovyscript.wiki.removeByOutput")
public boolean removeByOutput(ItemStack input) {
return org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
return BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
if (r.getOutput().getIngredient().test(input)) {
addBackup(r);
return true;
Expand All @@ -75,13 +84,13 @@ public boolean removeByOutput(ItemStack input) {

@MethodDescription(description = "groovyscript.wiki.removeAll", priority = 2000, example = @Example(commented = true))
public void removeAll() {
org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().forEach(this::addBackup);
org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().clear();
BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().forEach(this::addBackup);
BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes().clear();
}

@MethodDescription(description = "groovyscript.wiki.streamRecipes", type = MethodDescription.Type.QUERY)
public SimpleObjectStream<IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties>> streamRecipes() {
return new SimpleObjectStream<>(org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes())
return new SimpleObjectStream<>(BlockDryingBasin.getInstance().getRecipeRegistry().allRecipes())
.setRemover(this::remove);
}

Expand Down Expand Up @@ -137,6 +146,8 @@ public void validate(GroovyLog.Msg msg) {
msg.add(output.isEmpty() && fluidOutput.isEmpty(), "either output or fluidOutput must have an entry, yet both were empty");
msg.add(duration < 0, "duration must be a non negative integer, yet it was {}", duration);
msg.add(!basic && !mechanical, "either basic or mechanical must be true");
msg.add(basic && !ModSupport.INTEGRATED_DYNAMICS.get().dryingBasin.isEnabled(), "basic is enabled, yet the Drying Basin is disabled via config");
msg.add(mechanical && !ModSupport.INTEGRATED_DYNAMICS.get().mechanicalDryingBasin.isEnabled(), "mechanic is enabled, yet the Mechanical Drying Basin is disabled via config");
}

@Override
Expand All @@ -148,15 +159,15 @@ public void validate(GroovyLog.Msg msg) {

if (basic) {
ModSupport.INTEGRATED_DYNAMICS.get().dryingBasin.add(
org.cyclops.integrateddynamics.block.BlockDryingBasin.getInstance().getRecipeRegistry().registerRecipe(
BlockDryingBasin.getInstance().getRecipeRegistry().registerRecipe(
new IngredientAndFluidStackRecipeComponent(itemInput, true, fluidInput.getOrEmpty(0)),
new IngredientAndFluidStackRecipeComponent(output.get(0), fluidOutput.getOrEmpty(0)),
new DurationRecipeProperties(duration)
), false);
}
if (mechanical) {
ModSupport.INTEGRATED_DYNAMICS.get().mechanicalDryingBasin.add(
org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().registerRecipe(
BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().registerRecipe(
new IngredientAndFluidStackRecipeComponent(itemInput, true, fluidInput.getOrEmpty(0)),
new IngredientAndFluidStackRecipeComponent(output.getOrEmpty(0), fluidOutput.getOrEmpty(0)),
new DurationRecipeProperties(duration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.cyclops.cyclopscore.recipe.custom.api.IRecipe;
import org.cyclops.cyclopscore.recipe.custom.component.DurationRecipeProperties;
import org.cyclops.cyclopscore.recipe.custom.component.IngredientAndFluidStackRecipeComponent;
import org.cyclops.integrateddynamics.Configs;
import org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin;
import org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasinConfig;

@RegistryDescription
public class MechanicalDryingBasin extends VirtualizedRegistry<IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties>> {
Expand All @@ -15,15 +18,20 @@ public MechanicalDryingBasin() {
super();
}

@Override
public boolean isEnabled() {
return Configs.isEnabled(BlockMechanicalDryingBasinConfig.class);
}

@RecipeBuilderDescription(example = @Example(".input(item('minecraft:diamond')).fluidInput(fluid('water') * 50).fluidOutput(fluid('lava') * 20000).duration(300)"), requirement = @Property(property = "mechanical", defaultValue = "true"))
public DryingBasin.RecipeBuilder recipeBuilder() {
return new DryingBasin.RecipeBuilder().mechanical();
}

@Override
public void onReload() {
removeScripted().forEach(org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes()::remove);
restoreFromBackup().forEach(org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes()::add);
removeScripted().forEach(BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes()::remove);
restoreFromBackup().forEach(BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes()::add);
}

public void add(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties> recipe) {
Expand All @@ -33,19 +41,19 @@ public void add(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFlu
public void add(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties> recipe, boolean add) {
if (recipe == null) return;
addScripted(recipe);
if (add) org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().add(recipe);
if (add) BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().add(recipe);
}

public boolean remove(IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties> recipe) {
if (recipe == null) return false;
addBackup(recipe);
org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().remove(recipe);
BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().remove(recipe);
return true;
}

@MethodDescription(description = "groovyscript.wiki.removeByInput")
public boolean removeByInput(ItemStack input) {
return org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
return BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
if (r.getInput().getIngredient().test(input)) {
addBackup(r);
return true;
Expand All @@ -56,7 +64,7 @@ public boolean removeByInput(ItemStack input) {

@MethodDescription(description = "groovyscript.wiki.removeByOutput")
public boolean removeByOutput(ItemStack input) {
return org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
return BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().removeIf(r -> {
if (r.getOutput().getIngredient().test(input)) {
addBackup(r);
return true;
Expand All @@ -67,13 +75,13 @@ public boolean removeByOutput(ItemStack input) {

@MethodDescription(description = "groovyscript.wiki.removeAll", priority = 2000, example = @Example(commented = true))
public void removeAll() {
org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().forEach(this::addBackup);
org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().clear();
BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().forEach(this::addBackup);
BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes().clear();
}

@MethodDescription(description = "groovyscript.wiki.streamRecipes", type = MethodDescription.Type.QUERY)
public SimpleObjectStream<IRecipe<IngredientAndFluidStackRecipeComponent, IngredientAndFluidStackRecipeComponent, DurationRecipeProperties>> streamRecipes() {
return new SimpleObjectStream<>(org.cyclops.integrateddynamics.block.BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes())
return new SimpleObjectStream<>(BlockMechanicalDryingBasin.getInstance().getRecipeRegistry().allRecipes())
.setRemover(this::remove);
}
}
Loading