1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-19 05:32:55 +00:00

Support resource conditions in upgrade JSON

This commit is contained in:
Jonathan Coates 2023-01-02 15:56:01 +00:00
parent af15030fa4
commit bdecb88cca
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 40 additions and 8 deletions

View File

@ -4,7 +4,7 @@
# MC version is specified in gradle.properties, as we need that in settings.gradle. # MC version is specified in gradle.properties, as we need that in settings.gradle.
fabric-api = "0.68.1+1.19.3" fabric-api = "0.68.1+1.19.3"
fabric-loader = "0.14.11" fabric-loader = "0.14.11"
forge = "44.0.18" forge = "44.1.0"
forgeSpi = "6.0.0" forgeSpi = "6.0.0"
mixin = "0.8.5" mixin = "0.8.5"
parchment = "2022.11.27" parchment = "2022.11.27"

View File

@ -114,8 +114,9 @@ public class UpgradeManager<R extends UpgradeSerialiser<? extends T>, T extends
private void loadUpgrade(Map<String, UpgradeWrapper<R, T>> current, ResourceLocation id, JsonElement json) { private void loadUpgrade(Map<String, UpgradeWrapper<R, T>> current, ResourceLocation id, JsonElement json) {
var root = GsonHelper.convertToJsonObject(json, "top element"); var root = GsonHelper.convertToJsonObject(json, "top element");
var serialiserId = new ResourceLocation(GsonHelper.getAsString(root, "type")); if (!PlatformHelper.get().shouldLoadResource(root)) return;
var serialiserId = new ResourceLocation(GsonHelper.getAsString(root, "type"));
var serialiser = PlatformHelper.get().tryGetRegistryObject(registry, serialiserId); var serialiser = PlatformHelper.get().tryGetRegistryObject(registry, serialiserId);
if (serialiser == null) throw new JsonSyntaxException("Unknown upgrade type '" + serialiserId + "'"); if (serialiser == null) throw new JsonSyntaxException("Unknown upgrade type '" + serialiserId + "'");

View File

@ -5,6 +5,7 @@
*/ */
package dan200.computercraft.shared.platform; package dan200.computercraft.shared.platform;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.arguments.ArgumentType;
import dan200.computercraft.api.network.wired.WiredElement; import dan200.computercraft.api.network.wired.WiredElement;
@ -96,6 +97,16 @@ public interface PlatformHelper extends dan200.computercraft.impl.PlatformHelper
@Nullable @Nullable
<T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id); <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id);
/**
* Determine if this resource should be loaded, based on platform-specific loot conditions.
* <p>
* This should only be called from the {@code apply} stage of a reload listener.
*
* @param object The root JSON object of this resource.
* @return If this resource should be loaded.
*/
boolean shouldLoadResource(JsonObject object);
/** /**
* Create a new block entity type which serves a particular block. * Create a new block entity type which serves a particular block.
* *

View File

@ -6,6 +6,7 @@
package dan200.computercraft; package dan200.computercraft;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.arguments.ArgumentType;
import dan200.computercraft.api.network.wired.WiredElement; import dan200.computercraft.api.network.wired.WiredElement;
@ -77,6 +78,17 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
throw new UnsupportedOperationException("Cannot query registry inside tests"); throw new UnsupportedOperationException("Cannot query registry inside tests");
} }
@Nullable
@Override
public <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
throw new UnsupportedOperationException("Cannot query registries");
}
@Override
public boolean shouldLoadResource(JsonObject object) {
throw new UnsupportedOperationException("Cannot use loot conditions");
}
@Override @Override
public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) { public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) {
throw new UnsupportedOperationException("Cannot create BlockEntityType inside tests"); throw new UnsupportedOperationException("Cannot create BlockEntityType inside tests");
@ -219,10 +231,4 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
public String getInstalledVersion() { public String getInstalledVersion() {
return "1.0"; return "1.0";
} }
@Nullable
@Override
public <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) {
throw new UnsupportedOperationException("Cannot query registries");
}
} }

View File

@ -6,6 +6,7 @@
package dan200.computercraft.shared.platform; package dan200.computercraft.shared.platform;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.arguments.ArgumentType;
import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.ComputerCraftAPI;
@ -26,6 +27,7 @@ import net.fabricmc.fabric.api.lookup.v1.block.BlockApiCache;
import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup; import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.api.registry.FuelRegistry; import net.fabricmc.fabric.api.registry.FuelRegistry;
import net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType; import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalItemTags; import net.fabricmc.fabric.api.tag.convention.v1.ConventionalItemTags;
@ -119,6 +121,11 @@ public class PlatformHelperImpl implements PlatformHelper {
return getRegistry(registry).get(id); return getRegistry(registry).get(id);
} }
@Override
public boolean shouldLoadResource(JsonObject object) {
return ResourceConditions.objectMatchesConditions(object);
}
@Override @Override
public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) { public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) {
return FabricBlockEntityTypeBuilder.create(factory::apply).addBlock(block).build(); return FabricBlockEntityTypeBuilder.create(factory::apply).addBlock(block).build();

View File

@ -6,6 +6,7 @@
package dan200.computercraft.shared.platform; package dan200.computercraft.shared.platform;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.arguments.ArgumentType;
import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.ComputerCraftAPI;
@ -57,6 +58,7 @@ import net.minecraftforge.common.Tags;
import net.minecraftforge.common.ToolActions; import net.minecraftforge.common.ToolActions;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.crafting.conditions.ICondition;
import net.minecraftforge.common.extensions.IForgeMenuType; import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.common.util.NonNullConsumer; import net.minecraftforge.common.util.NonNullConsumer;
import net.minecraftforge.event.ForgeEventFactory; import net.minecraftforge.event.ForgeEventFactory;
@ -109,6 +111,11 @@ public class PlatformHelperImpl implements PlatformHelper {
return RegistryManager.ACTIVE.getRegistry(registry).getValue(id); return RegistryManager.ACTIVE.getRegistry(registry).getValue(id);
} }
@Override
public boolean shouldLoadResource(JsonObject object) {
return ICondition.shouldRegisterEntry(object);
}
@Override @Override
public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) { public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) {
return new BlockEntityType<>(factory::apply, Set.of(block), null); return new BlockEntityType<>(factory::apply, Set.of(block), null);