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

Data driven models and animations #145

Open
wants to merge 19 commits into
base: 1.20
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions build-logic/src/main/groovy/qsl.module.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ javadoc {
license {
rule rootProject.file("codeformat/COLONEL_MODIFIED_HEADER")
rule rootProject.file("codeformat/FABRIC_MODIFIED_HEADER")
rule rootProject.file("codeformat/FOUNDATION_GAMES_MODIFIED_HEADER")
OroArmor marked this conversation as resolved.
Show resolved Hide resolved
rule rootProject.file("codeformat/HEADER")

include "**/*.java"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public void entrypoints(Action<NamedDomainObjectContainer<NamedWriteOnlyList>> c
@Override
public void injectedInterface(String minecraftClass, Action<NamedWriteOnlyList> action) {
action.execute(this.injectedInterfaces.create(minecraftClass));
this.allowGenTasks();
}

@Nested
Expand Down
26 changes: 26 additions & 0 deletions codeformat/FOUNDATION_GAMES_MODIFIED_HEADER
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright 2021 FoundationGames

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Copyright ${YEAR} QuiltMC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

;;match_from: \/\*\r?\n \* Copyright 2021 FoundationGames
;;match_from: \/\/\/ F[Oo][Uu][Nn][Dd][Aa][Tt][Ii][Oo][Nn]
;;year_display: lenient_range
7 changes: 7 additions & 0 deletions library/rendering/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id("qsl.library")
}

qslLibrary {
libraryName = "rendering"
}
24 changes: 24 additions & 0 deletions library/rendering/entity_models/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
id("qsl.module")
}

qslModule {
name = "Quilt Entity Model API"
moduleName = "entity_models"
id = "quilt_entity_models"
description = "Adds data driven entity models and animations."
library = "rendering"
moduleDependencies {
core {
api("qsl_base")
impl("resource_loader")
}
}
injectedInterface("net/minecraft/class_898") {
values = ["org/quiltmc/qsl/rendering/entity_models/api/HasAnimationManager"]
}
injectedInterface("net/minecraft/class_5617\$class_5618") {
values = ["org/quiltmc/qsl/rendering/entity_models/api/HasAnimationManager"]
}
clientOnly()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.rendering.entity_models.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.qsl.resource.loader.api.reloader.SimpleResourceReloader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.client.render.animation.Animation;
import net.minecraft.client.render.entity.model.EntityModelLayer;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.profiler.Profiler;

/**
* A class that loads and holds {@link Animation}s.
*
* See {@link net.minecraft.client.render.entity.model.EntityModelLoader#getModelPart(EntityModelLayer)} for a similar usage.
*/
public class AnimationManager implements SimpleResourceReloader<AnimationManager.AnimationLoader> {
private static final Logger LOGGER = LoggerFactory.getLogger("Quilt Animation Manager");
private Map<Identifier, Animation> animations;

/**
*
* @param id The animation ID
* @return An animation if found, or null otherwise
*/
public @Nullable Animation getAnimation(Identifier id) {
return animations.get(id);
}

@Override
public CompletableFuture<AnimationLoader> load(ResourceManager manager, Profiler profiler, Executor executor) {
return CompletableFuture.supplyAsync(() -> new AnimationLoader(manager, profiler), executor);
}

@Override
public CompletableFuture<Void> apply(AnimationLoader prepared, ResourceManager manager, Profiler profiler, Executor executor) {
this.animations = prepared.getAnimations();
return CompletableFuture.runAsync(() -> {
});
}

@Override
public @NotNull Identifier getQuiltId() {
return new Identifier("quilt_entity_models", "animation_reloader");
}

public static class AnimationLoader {
private final ResourceManager manager;
private final Profiler profiler;
private final Map<Identifier, Animation> animations = new HashMap<>();

public AnimationLoader(ResourceManager manager, Profiler profiler) {
this.manager = manager;
this.profiler = profiler;
this.loadAnimations();
}

private void loadAnimations() {
profiler.push("Load Animations");
Map<Identifier, Resource> resources = manager.findResources("animations", id -> id.getPath().endsWith(".json"));
for (Map.Entry<Identifier, Resource> entry : resources.entrySet()) {
this.addAnimation(entry.getKey(), entry.getValue());
}
profiler.pop();
}

private void addAnimation(Identifier id, Resource resource) {
BufferedReader reader;
try {
reader = resource.openBufferedReader();
} catch (IOException e) {
LOGGER.error(String.format("Unable to open BufferedReader for id %s", id), e);
return;
}

JsonObject json = JsonHelper.deserialize(reader);
DataResult<Pair<Animation, JsonElement>> result = Codecs.Animations.ANIMATION.decode(JsonOps.INSTANCE, json);

if (result.error().isPresent()) {
LOGGER.error(String.format("Unable to parse animation file %s.\nReason: %s", id, result.error().get().message()));
return;
}

Identifier animationId = new Identifier(id.getNamespace(), id.getPath().substring("animations/".length()));
animations.put(animationId, result.result().get().getFirst());
}

public Map<Identifier, Animation> getAnimations() {
return animations;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.rendering.entity_models.api;

import java.util.Optional;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

import net.minecraft.client.render.animation.PartAnimation;

/**
* A class that stores a {@link BiMap} of strings to {@link PartAnimation.Interpolator}s
*/
public class AnimationUtils {
private static final BiMap<String, PartAnimation.Interpolator> INTERPOLATORS = HashBiMap.create();

static {
registerInterpolation("LINEAR", PartAnimation.Interpolators.LINEAR);
registerInterpolation("SPLINE", PartAnimation.Interpolators.SPLINE);
}

public static void registerInterpolation(String name, PartAnimation.Interpolator interpolator) {
if (INTERPOLATORS.containsKey(name)) {
throw new IllegalArgumentException(name + " already used as name");
} else if (INTERPOLATORS.containsValue(interpolator)) {
throw new IllegalArgumentException("Interpolator already assigned to " + INTERPOLATORS.inverse().get(interpolator));
}

INTERPOLATORS.put(name, interpolator);
}

public static Optional<PartAnimation.Interpolator> getInterpolatorFromName(String name) {
return Optional.ofNullable(INTERPOLATORS.get(name));
}

public static Optional<String> getNameForInterpolator(PartAnimation.Interpolator interpolator) {
return Optional.ofNullable(INTERPOLATORS.inverse().get(interpolator));
}
}
Loading