Skip to content

Commit

Permalink
Update resource pack api, now with a way to create typed item asset d…
Browse files Browse the repository at this point in the history
…efinitions on the fly, update to pre3
  • Loading branch information
Patbox committed Nov 27, 2024
1 parent e7a02b4 commit 7a4f7fe
Show file tree
Hide file tree
Showing 87 changed files with 1,576 additions and 339 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mkdocs-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ jobs:
git config --global user.name Docs deploy
git config --global user.email [email protected]
mike set-default latest
mike deploy --push --update-aliases 0.10.x latest
mike deploy --push --update-aliases 0.11.x latest
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ dependencies {
api project(path: ":polymer-core", configuration: "namedElements")
api project(path: ":polymer-common", configuration: "namedElements")
api project(path: ":polymer-resource-pack", configuration: "namedElements")
api project(path: ":polymer-resource-pack-extras", configuration: "namedElements")
api project(path: ":polymer-blocks", configuration: "namedElements")
api project(path: ":polymer-virtual-entity", configuration: "namedElements")

include(project(path: ":polymer-autohost"))
include(project(path: ":polymer-core"))
include(project(path: ":polymer-common"))
include(project(path: ":polymer-resource-pack"))
include(project(path: ":polymer-resource-pack-extras"))
include(project(path: ":polymer-blocks"))
include(project(path: ":polymer-virtual-entity"))
}
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ org.gradle.jvmargs=-Xmx4G
# Fabric Properties
# check these on https://fabricmc.net/use

minecraft_version=1.21.4-pre1
yarn_mappings=1.21.4-pre1+build.1
minecraft_version=1.21.4-pre3
yarn_mappings=1.21.4-pre3+build.2
loader_version=0.16.9

# Fabric API
fabric_version=0.109.1+1.21.4
fabric_version=0.110.2+1.21.4

maven_group = eu.pb4

Expand Down
1 change: 1 addition & 0 deletions polymer-autohost/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
testmodImplementation (project(path: ':polymer-blocks', configuration: 'namedElements'))
testmodImplementation (project(path: ':polymer-reg-sync-manipulator', configuration: 'namedElements'))
testmodImplementation (project(path: ':polymer-networking', configuration: 'namedElements'))
testmodImplementation (project(path: ':polymer-resource-pack-extras', configuration: 'namedElements'))

compileOnly (project(path: ':polymer-virtual-entity', configuration: 'namedElements'))
localRuntime (project(path: ':polymer-virtual-entity', configuration: 'namedElements'))
Expand Down
2 changes: 2 additions & 0 deletions polymer-blocks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies {
implementation(project(path: ':polymer-core', configuration: 'namedElements'))
implementation(project(path: ':polymer-common', configuration: 'namedElements'))
implementation(project(path: ':polymer-resource-pack', configuration: 'namedElements'))
compileOnly project(path: ":polymer-resource-pack-extras", configuration: "namedElements")

modCompileOnly "me.lucko:fabric-permissions-api:0.1-SNAPSHOT"

//modLocalRuntime "fr.catcore:server-translations-api:1.4.9+1.18.2-rc1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.pb4.blocktest;

import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.resourcepack.extras.api.ResourcePackExtras;
import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
Expand All @@ -15,7 +15,7 @@ public class TestItem extends BlockItem implements PolymerItem {
private final Identifier polymerModel;
public TestItem(Settings settings, Block block, String modelId) {
super(block, settings);
this.polymerModel = PolymerResourcePackUtils.getBridgedModelId(Identifier.of("blocktest", modelId));
this.polymerModel = ResourcePackExtras.bridgeModel(Identifier.of("blocktest", modelId));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private PolymerCommonUtils() {}

public static final SimpleEvent<ResourcePackChangeCallback> ON_RESOURCE_PACK_STATUS_CHANGE = new SimpleEvent<>();
private static Path cachedClientPath;
private final static String SAFE_CLIENT_SHA1 = "6f67d19b4467240639cb2c368ffd4b94ba889705";
private final static String SAFE_CLIENT_SHA1 = "b6959dbb500a337a3a26c0ab3ca27216750bcd9f";
private final static String SAFE_CLIENT_URL = "https://piston-data.mojang.com/v1/objects/" + SAFE_CLIENT_SHA1 + "/client.jar";
private static Path cachedClientJarRoot;

Expand Down
3 changes: 2 additions & 1 deletion polymer-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ dependencies {

compileOnly (project(path: ':polymer-resource-pack', configuration: 'namedElements'))
localRuntime (project(path: ':polymer-resource-pack', configuration: 'namedElements'))

compileOnly (project(path: ':polymer-resource-pack-extras', configuration: 'namedElements'))
localRuntime (project(path: ':polymer-resource-pack-extras', configuration: 'namedElements'))
compileOnly (project(path: ':polymer-virtual-entity', configuration: 'namedElements'))
localRuntime (project(path: ':polymer-virtual-entity', configuration: 'namedElements'))
}
Expand Down
41 changes: 30 additions & 11 deletions polymer-core/src/testmod/java/eu/pb4/polymertest/TestMod.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.pb4.polymertest;

import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.core.api.block.BlockMapper;
import eu.pb4.polymer.core.api.block.SimplePolymerBlock;
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
Expand All @@ -11,6 +12,8 @@
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.core.impl.client.InternalClientRegistry;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.resourcepack.extras.api.ResourcePackExtras;
import eu.pb4.polymer.resourcepack.extras.api.format.item.ItemAsset;
import eu.pb4.polymer.virtualentity.api.tracker.EntityTrackedData;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
Expand All @@ -34,9 +37,6 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.*;
import net.minecraft.item.consume.ApplyEffectsConsumeEffect;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.*;
import net.minecraft.potion.Potion;
import net.minecraft.recipe.RecipeSerializer;
Expand All @@ -49,24 +49,20 @@
import net.minecraft.server.network.DebugInfoSender;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.StatFormatter;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;
import net.minecraft.util.*;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.village.TradeOffer;
import net.minecraft.village.TradeOffers;
import net.minecraft.village.TradedItem;
import net.minecraft.village.VillagerProfession;
import net.minecraft.world.GameMode;
import net.minecraft.world.World;
import net.minecraft.world.biome.source.BiomeAccess;
import xyz.nucleoid.server.translations.api.LocalizationTarget;
import xyz.nucleoid.server.translations.impl.ServerTranslations;

import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
Expand Down Expand Up @@ -257,8 +253,8 @@ private static void regArmor(EquipmentSlot slot, String main, String id) {
public void onInitialize() {
//ITEM_GROUP.setIcon();
PolymerResourcePackUtils.addModAssets("apolymertest");
PolymerResourcePackUtils.addBridgedModelsFolder(Identifier.of("polymertest", "testificate"));
PolymerResourcePackUtils.addBridgedModelsFolder(Identifier.of("blocktest", "block"));
ResourcePackExtras.forDefault().addBridgedModelsFolder(Identifier.of("polymertest", "testificate"));
ResourcePackExtras.forDefault().addBridgedModelsFolder(Identifier.of("blocktest", "block"));
PolymerResourcePackUtils.getInstance().setPackDescription(Text.literal("TEST REPLACED DESCRIPTION").formatted(Formatting.GREEN));
//PolymerResourcePackUtils.markAsRequired();
//PolymerResourcePackUtils.addModAsAssetsSource("promenade");
Expand Down Expand Up @@ -500,6 +496,29 @@ public void onInitialize() {
if (PolymerImpl.IS_CLIENT) {
InternalClientRegistry.decodeState(-1);
}

new Thread(() -> {
var vanillaJar = PolymerCommonUtils.getClientJarRoot();

var itemsBase = vanillaJar.resolve("assets/minecraft/items/");

try {
Files.walk(itemsBase, 1).forEach(path -> {
try {
var asset = ItemAsset.fromJson(Files.readString(path));
//System.out.println(path + ">" + asset);
} catch (Throwable e) {
System.err.println("Error while parsing file: " + path);
e.printStackTrace();
}

});
} catch (IOException e) {
e.printStackTrace();
}


}).run();
}

public static <B, T extends B> T register(Registry<B> registry, Identifier id, T obj) {
Expand Down
37 changes: 37 additions & 0 deletions polymer-resource-pack-extras/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
archivesBaseName = "polymer-resource-pack-extras"
version = rootProject.mod_version + "+" + rootProject.minecraft_version
group = rootProject.maven_group

sourceSets {
testmod {
compileClasspath += main.compileClasspath
runtimeClasspath += main.runtimeClasspath
}
}

loom {

}

dependencies {
modCompileOnly "net.fabricmc.fabric-api:fabric-api:${rootProject.fabric_version}"

api (project(path: ':polymer-common', configuration: 'namedElements'))
api (project(path: ':polymer-resource-pack', configuration: 'namedElements'))
}

afterEvaluate {
// Disable the gen sources task on sub projects
genSourcesWithFernFlower.enabled = true
genSourcesWithCfr.enabled = false
}

processResources {
inputs.property "version", project.version
inputs.property "minecraft_version_supported", rootProject.minecraft_version_supported

filesMatching("fabric.mod.json") {
expand "version": project.version
expand "minecraft_version_supported": rootProject.minecraft_version_supported
}
}
1 change: 1 addition & 0 deletions polymer-resource-pack-extras/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project.name = 'polymer-resource-pack-extras'
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package eu.pb4.polymer.resourcepack.extras.api;

import eu.pb4.polymer.resourcepack.api.AssetPaths;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import eu.pb4.polymer.resourcepack.api.ResourcePackBuilder;
import eu.pb4.polymer.resourcepack.api.ResourcePackCreator;
import eu.pb4.polymer.resourcepack.extras.api.format.item.ItemAsset;
import eu.pb4.polymer.resourcepack.extras.api.format.item.model.BasicItemModel;
import eu.pb4.polymer.resourcepack.extras.api.format.item.tint.CustomModelDataTintSource;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.BiFunction;

/**
* Utilities allowing simple creation of resource pack
*/
public final class ResourcePackExtras {
private static final ResourcePackExtras MAIN = new ResourcePackExtras(PolymerResourcePackUtils.getInstance());
private final Map<Identifier, BiFunction<Identifier, ResourcePackBuilder, @Nullable ItemAsset>> bridgedModels = new HashMap<>();

ResourcePackExtras(ResourcePackCreator creator) {
creator.afterInitialCreationEvent.register(this::setup);
}

public static ResourcePackExtras forDefault() {
return MAIN;
}

public static ResourcePackExtras of(ResourcePackCreator creator) {
if (PolymerResourcePackUtils.getInstance() == creator) {
return MAIN;
}
return new ResourcePackExtras(creator);
}

public static ResourcePackExtras of(ResourcePackCreator creator, ResourcePackExtras source) {
if (PolymerResourcePackUtils.getInstance() == creator) {
throw new IllegalArgumentException("Passed creator matches PolymerResourcePackUtils.getInstance(), so you can't copy settings from other sources to it!");
}

var extras = new ResourcePackExtras(creator);
extras.bridgedModels.putAll(source.bridgedModels);
return extras;
}

public static Identifier bridgeModelNoItem(Identifier model) {
if (model.getPath().startsWith("item/")) {
return model.withPath(model.getPath().substring("item/".length()));
}

return bridgeModel(model);
}

public static Identifier bridgeModel(Identifier model) {
return model.withPrefixedPath("-/");
}

/**
* Adds a bridge, allowing you to access any model from selected folder as `namespace:-/modelpath`.
*
* @param modelFolderId Model folder to bridge. For example "mod:block" will bridge all models from "assets/mod/models/block"
* @return Success of addition.
*/
public boolean addBridgedModelsFolder(Identifier modelFolderId) {
return addBridgedModelsFolder(modelFolderId,
(identifier, resourcePackBuilder) -> new ItemAsset(new BasicItemModel(identifier), ItemAsset.Properties.DEFAULT)
);
}

public boolean addBridgedModelsFolderWithColor(Identifier modelFolderId) {
return addBridgedModelsFolder(modelFolderId,
(identifier, resourcePackBuilder) -> new ItemAsset(new BasicItemModel(identifier,
List.of(
new CustomModelDataTintSource(0, 0xFFFFFF),
new CustomModelDataTintSource(1, 0xFFFFFF),
new CustomModelDataTintSource(2, 0xFFFFFF),
new CustomModelDataTintSource(3, 0xFFFFFF))
), ItemAsset.Properties.DEFAULT)
);
}

public boolean addBridgedModelsFolder(Identifier modelFolderId, BiFunction<Identifier, ResourcePackBuilder, @Nullable ItemAsset> bridgeBuilder) {
this.bridgedModels.put(modelFolderId.getPath().endsWith("/") ? modelFolderId.withPath(x -> x.substring(0, x.length() - 1)) : modelFolderId, bridgeBuilder);
return true;
}

public boolean addBridgedModelsFolder(Identifier... modelFolderId) {
var b = true;
for (var model : modelFolderId) {
b &= addBridgedModelsFolder(model);
}
return b;
}

public boolean addBridgedModelsFolder(Collection<Identifier> modelFolderId) {
var b = true;
for (var model : modelFolderId) {
b &= addBridgedModelsFolder(model);
}
return b;
}

public boolean addBridgedModelsFolder(Collection<Identifier> modelFolderId, BiFunction<Identifier, ResourcePackBuilder, @Nullable ItemAsset> bridgeBuilder) {
var b = true;
for (var model : modelFolderId) {
b &= addBridgedModelsFolder(model, bridgeBuilder);
}
return b;
}


private void setup(ResourcePackBuilder builder) {
if (!this.bridgedModels.isEmpty()) {
builder.addPreFinishTask((b) -> {
b.forEachFile((path, out) -> {
if (!path.startsWith("assets/")) {
return;
}
path = path.substring("assets/".length());

for (var x : this.bridgedModels.entrySet()) {
var key = x.getKey();
var y = key.getNamespace() + "/models/" + key.getPath() + "/";
if (path.startsWith(y)) {
if (!path.endsWith(".json")) {
return;
}
path = path.substring(key.getNamespace().length() + "/models/".length(), path.length() - ".json".length());

var assetPath = AssetPaths.itemAsset(key.withPath("-/" + path));

if (b.getData(assetPath) != null) {
return;
}

var asset = x.getValue().apply(key.withPath(path), b);
if (asset != null) {
b.addData(assetPath, asset.toJson().getBytes(StandardCharsets.UTF_8));
}
return;
}
}
});
});
}
}
}
Loading

0 comments on commit 7a4f7fe

Please sign in to comment.