1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +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.
fabric-api = "0.68.1+1.19.3"
fabric-loader = "0.14.11"
forge = "44.0.18"
forge = "44.1.0"
forgeSpi = "6.0.0"
mixin = "0.8.5"
parchment = "2022.11.27"

View File

@ -114,8 +114,9 @@ protected void apply(Map<ResourceLocation, JsonElement> upgrades, ResourceManage
private void loadUpgrade(Map<String, UpgradeWrapper<R, T>> current, ResourceLocation id, JsonElement json) {
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);
if (serialiser == null) throw new JsonSyntaxException("Unknown upgrade type '" + serialiserId + "'");

View File

@ -5,6 +5,7 @@
*/
package dan200.computercraft.shared.platform;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.ArgumentType;
import dan200.computercraft.api.network.wired.WiredElement;
@ -96,6 +97,16 @@ static PlatformHelper get() {
@Nullable
<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.
*

View File

@ -6,6 +6,7 @@
package dan200.computercraft;
import com.google.auto.service.AutoService;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.ArgumentType;
import dan200.computercraft.api.network.wired.WiredElement;
@ -77,6 +78,17 @@ public <K> K getRegistryObject(ResourceKey<Registry<K>> registry, ResourceLocati
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
public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) {
throw new UnsupportedOperationException("Cannot create BlockEntityType inside tests");
@ -219,10 +231,4 @@ public void onItemCrafted(ServerPlayer player, CraftingContainer container, Item
public String getInstalledVersion() {
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;
import com.google.auto.service.AutoService;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.ArgumentType;
import dan200.computercraft.api.ComputerCraftAPI;
@ -26,6 +27,7 @@
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.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.ExtendedScreenHandlerType;
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalItemTags;
@ -119,6 +121,11 @@ public <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLoc
return getRegistry(registry).get(id);
}
@Override
public boolean shouldLoadResource(JsonObject object) {
return ResourceConditions.objectMatchesConditions(object);
}
@Override
public <T extends BlockEntity> BlockEntityType<T> createBlockEntityType(BiFunction<BlockPos, BlockState, T> factory, Block block) {
return FabricBlockEntityTypeBuilder.create(factory::apply).addBlock(block).build();

View File

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