Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/1.19' into 1.20.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	build.gradle
#	src/main/java/slimeknights/mantle/Mantle.java
#	src/main/java/slimeknights/mantle/client/ClientEvents.java
#	src/main/java/slimeknights/mantle/client/model/inventory/ModelItem.java
#	src/main/java/slimeknights/mantle/data/GenericDataProvider.java
#	src/main/java/slimeknights/mantle/data/loader/RegistryEntryLoader.java
#	src/main/java/slimeknights/mantle/data/loader/RegistrySetLoader.java
#	src/main/java/slimeknights/mantle/data/predicate/block/BlockPropertiesPredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/block/SetBlockPredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/block/TagBlockPredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/damage/DamageSourcePredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/entity/EntitySetPredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/entity/HasEnchantmentEntityPredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/entity/TagEntityPredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/item/ItemSetPredicate.java
#	src/main/java/slimeknights/mantle/data/predicate/item/ItemTagPredicate.java
#	src/main/java/slimeknights/mantle/fluid/texture/FluidTexture.java
#	src/main/java/slimeknights/mantle/fluid/texture/FluidTextureManager.java
#	src/main/java/slimeknights/mantle/fluid/tooltip/FluidTooltipHandler.java
#	src/main/java/slimeknights/mantle/recipe/data/FluidNameIngredient.java
#	src/main/java/slimeknights/mantle/recipe/helper/ItemOutput.java
#	src/main/java/slimeknights/mantle/recipe/ingredient/EntityIngredient.java
#	src/main/java/slimeknights/mantle/recipe/ingredient/FluidIngredient.java
#	src/main/java/slimeknights/mantle/util/JsonHelper.java
  • Loading branch information
AlphaMode committed Jun 11, 2024
2 parents e069e8a + f7af78c commit a5fafbf
Show file tree
Hide file tree
Showing 142 changed files with 6,729 additions and 1,817 deletions.
37 changes: 8 additions & 29 deletions src/main/java/slimeknights/mantle/Mantle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,18 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import slimeknights.mantle.block.entity.MantleSignBlockEntity;
import slimeknights.mantle.client.ClientEvents;
import slimeknights.mantle.command.MantleCommand;
import slimeknights.mantle.config.Config;
import slimeknights.mantle.data.predicate.block.BlockPredicate;
import slimeknights.mantle.data.predicate.block.BlockPropertiesPredicate;
import slimeknights.mantle.data.predicate.block.SetBlockPredicate;
import slimeknights.mantle.data.predicate.block.TagBlockPredicate;
import slimeknights.mantle.data.predicate.damage.DamageSourcePredicate;
import slimeknights.mantle.data.predicate.damage.SourceAttackerPredicate;
import slimeknights.mantle.data.predicate.damage.SourceMessagePredicate;
import slimeknights.mantle.data.predicate.entity.EntitySetPredicate;
import slimeknights.mantle.data.predicate.entity.HasEnchantmentEntityPredicate;
import slimeknights.mantle.data.predicate.entity.LivingEntityPredicate;
import slimeknights.mantle.data.predicate.entity.MobTypePredicate;
import slimeknights.mantle.data.predicate.entity.TagEntityPredicate;
import slimeknights.mantle.data.predicate.item.ItemPredicate;
import slimeknights.mantle.data.predicate.item.ItemSetPredicate;
import slimeknights.mantle.data.predicate.item.ItemTagPredicate;
import slimeknights.mantle.datagen.MantleFluidTagProvider;
import slimeknights.mantle.datagen.MantleFluidTooltipProvider;
import slimeknights.mantle.datagen.MantleTags;
Expand Down Expand Up @@ -87,6 +82,10 @@ public void onInitialize() {
this.registerBlockEntities();
MantleLoot.registerGlobalLootModifiers();
UseBlockCallback.EVENT.register(LecternBookItem::interactWithBlock);

if (FMLEnvironment.dist == Dist.CLIENT) {
ClientEvents.onConstruct();
}
}

private void registerCapabilities() {
Expand Down Expand Up @@ -118,29 +117,15 @@ private void registerRecipeSerializers() {
// predicates
{
// block predicates
BlockPredicate.LOADER.register(getResource("and"), BlockPredicate.AND);
BlockPredicate.LOADER.register(getResource("or"), BlockPredicate.OR);
BlockPredicate.LOADER.register(getResource("inverted"), BlockPredicate.INVERTED);
BlockPredicate.LOADER.register(getResource("any"), BlockPredicate.ANY.getLoader());
BlockPredicate.LOADER.register(getResource("requires_tool"), BlockPredicate.REQUIRES_TOOL.getLoader());
BlockPredicate.LOADER.register(getResource("set"), SetBlockPredicate.LOADER);
BlockPredicate.LOADER.register(getResource("tag"), TagBlockPredicate.LOADER);
BlockPredicate.LOADER.register(getResource("block_properties"), BlockPropertiesPredicate.LOADER);

// item predicates
ItemPredicate.LOADER.register(getResource("and"), ItemPredicate.AND);
ItemPredicate.LOADER.register(getResource("or"), ItemPredicate.OR);
ItemPredicate.LOADER.register(getResource("inverted"), ItemPredicate.INVERTED);
ItemPredicate.LOADER.register(getResource("any"), ItemPredicate.ANY.getLoader());
ItemPredicate.LOADER.register(getResource("set"), ItemSetPredicate.LOADER);
ItemPredicate.LOADER.register(getResource("tag"), ItemTagPredicate.LOADER);
// make sure the item predicate registry is loaded, nothing to register here
ItemPredicate.ANY.getLoader();

// entity predicates
LivingEntityPredicate.LOADER.register(getResource("and"), LivingEntityPredicate.AND);
LivingEntityPredicate.LOADER.register(getResource("or"), LivingEntityPredicate.OR);
LivingEntityPredicate.LOADER.register(getResource("inverted"), LivingEntityPredicate.INVERTED);
// simple
LivingEntityPredicate.LOADER.register(getResource("any"), LivingEntityPredicate.ANY.getLoader());
LivingEntityPredicate.LOADER.register(getResource("fire_immune"), LivingEntityPredicate.FIRE_IMMUNE.getLoader());
LivingEntityPredicate.LOADER.register(getResource("water_sensitive"), LivingEntityPredicate.WATER_SENSITIVE.getLoader());
LivingEntityPredicate.LOADER.register(getResource("on_fire"), LivingEntityPredicate.ON_FIRE.getLoader());
Expand All @@ -151,8 +136,6 @@ private void registerRecipeSerializers() {
LivingEntityPredicate.LOADER.register(getResource("underwater"), LivingEntityPredicate.UNDERWATER.getLoader());
LivingEntityPredicate.LOADER.register(getResource("raining_at"), LivingEntityPredicate.RAINING.getLoader());
// property
LivingEntityPredicate.LOADER.register(getResource("set"), EntitySetPredicate.LOADER);
LivingEntityPredicate.LOADER.register(getResource("tag"), TagEntityPredicate.LOADER);
LivingEntityPredicate.LOADER.register(getResource("mob_type"), MobTypePredicate.LOADER);
LivingEntityPredicate.LOADER.register(getResource("has_enchantment"), HasEnchantmentEntityPredicate.LOADER);
// register mob types
Expand All @@ -163,17 +146,14 @@ private void registerRecipeSerializers() {
MobTypePredicate.MOB_TYPES.register(new ResourceLocation("water"), MobType.WATER);

// damage predicates
DamageSourcePredicate.LOADER.register(getResource("and"), DamageSourcePredicate.AND);
DamageSourcePredicate.LOADER.register(getResource("or"), DamageSourcePredicate.OR);
DamageSourcePredicate.LOADER.register(getResource("inverted"), DamageSourcePredicate.INVERTED);
DamageSourcePredicate.LOADER.register(getResource("any"), DamageSourcePredicate.ANY.getLoader());
// vanilla properties
DamageSourcePredicate.LOADER.register(getResource("projectile"), DamageSourcePredicate.PROJECTILE.getLoader());
DamageSourcePredicate.LOADER.register(getResource("explosion"), DamageSourcePredicate.EXPLOSION.getLoader());
DamageSourcePredicate.LOADER.register(getResource("bypass_armor"), DamageSourcePredicate.BYPASS_ARMOR.getLoader());
DamageSourcePredicate.LOADER.register(getResource("damage_helmet"), DamageSourcePredicate.DAMAGE_HELMET.getLoader());
DamageSourcePredicate.LOADER.register(getResource("bypass_invulnerable"), DamageSourcePredicate.BYPASS_INVULNERABLE.getLoader());
DamageSourcePredicate.LOADER.register(getResource("bypass_magic"), DamageSourcePredicate.BYPASS_MAGIC.getLoader());
DamageSourcePredicate.LOADER.register(getResource("bypass_enchantments"), DamageSourcePredicate.BYPASS_ENCHANTMENTS.getLoader());
DamageSourcePredicate.LOADER.register(getResource("fire"), DamageSourcePredicate.FIRE.getLoader());
DamageSourcePredicate.LOADER.register(getResource("magic"), DamageSourcePredicate.MAGIC.getLoader());
DamageSourcePredicate.LOADER.register(getResource("fall"), DamageSourcePredicate.FALL.getLoader());
Expand All @@ -182,7 +162,6 @@ private void registerRecipeSerializers() {
DamageSourcePredicate.LOADER.register(getResource("melee"), DamageSourcePredicate.MELEE.getLoader());
DamageSourcePredicate.LOADER.register(getResource("message"), SourceMessagePredicate.LOADER);
DamageSourcePredicate.LOADER.register(getResource("attacker"), SourceAttackerPredicate.LOADER);

}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/slimeknights/mantle/client/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
public class ClientEvents implements ClientModInitializer {
private static final Function<OffhandCooldownTracker,Float> COOLDOWN_TRACKER = OffhandCooldownTracker::getCooldown;

/** Called on construct to initiatlize things that need early entry */
public static void onConstruct() {
FluidTextureManager.init();
}

static void registerEntityRenderers() {
BlockEntityRenderers.register(MantleRegistrations.SIGN, SignRenderer::new);
}
Expand All @@ -61,7 +66,6 @@ static void registerListeners() {
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new BookLoader());
ResourceColorManager.init();
FluidTooltipHandler.init();
FluidTextureManager.init(event);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.math.Vector3f;
import lombok.Getter;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemDisplayContext;
import org.joml.Vector3f;
import slimeknights.mantle.client.model.util.ModelHelper;
import slimeknights.mantle.util.JsonHelper;

Expand Down Expand Up @@ -81,16 +80,10 @@ public boolean isHidden() {
/** Parses a transform type from a string */
private static ItemDisplayContext parseTransformType(JsonObject json, String key) {
String name = GsonHelper.getAsString(json, key, "none");
switch (name) {
case "none": return ItemDisplayContext.NONE;
case "head": return ItemDisplayContext.HEAD;
case "gui": return ItemDisplayContext.GUI;
case "ground": return ItemDisplayContext.GROUND;
case "fixed": return ItemDisplayContext.FIXED;
case "thirdperson_righthand": return ItemDisplayContext.THIRD_PERSON_RIGHT_HAND;
case "thirdperson_lefthand": return ItemDisplayContext.THIRD_PERSON_LEFT_HAND;
case "firstperson_righthand": return ItemDisplayContext.FIRST_PERSON_RIGHT_HAND;
case "firstperson_lefthand": return ItemDisplayContext.FIRST_PERSON_LEFT_HAND;
for (TransformType type : TransformType.values()) {
if (name.equals(type.getSerializeName())) {
return type;
}
}
throw new JsonSyntaxException("Unknown transform type " + name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraftforge.client.model.geometry.IGeometryBakingContext;
import net.minecraftforge.client.model.geometry.IGeometryLoader;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.data.loadable.common.ColorLoadable;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.mantle.util.LogicHelper;

Expand Down Expand Up @@ -162,7 +163,7 @@ public boolean isUvLock(boolean defaultLock) {
* Parses the color data from JSON
*/
public static ColorData fromJson(JsonObject json) {
int color = JsonHelper.parseColor(GsonHelper.getAsString(json, "color", ""));
int color = ColorLoadable.ALPHA.getOrDefault(json, "color", -1);
int luminosity = GsonHelper.getAsInt(json, "luminosity", -1);
Boolean uvlock = null;
if (json.has("uvlock")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraftforge.client.model.geometry.UnbakedGeometryHelper;
import net.minecraftforge.client.model.pipeline.QuadBakingVertexConsumer;
import net.minecraftforge.client.model.pipeline.TransformingVertexPipeline;
import slimeknights.mantle.data.loadable.common.ColorLoadable;
import slimeknights.mantle.util.ItemLayerPixels;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.mantle.util.LogicHelper;
Expand All @@ -42,6 +43,7 @@
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.PrimitiveIterator.OfInt;
import java.util.Set;
import java.util.function.Function;

Expand Down Expand Up @@ -161,7 +163,10 @@ public static ImmutableList<BakedQuad> getQuadsForSprite(int color, int tint, Te
FaceData faceData = new FaceData(uMax, vMax);
boolean translucent = false;

for(int f = 0; f < sprite.getFrameCount(); f++) {
OfInt frames = sprite.getUniqueFrames().iterator();
boolean hasFrames = frames.hasNext();
while (frames.hasNext()) {
int f = frames.nextInt();
boolean ptu;
boolean[] ptv = new boolean[uMax];
Arrays.fill(ptv, true);
Expand Down Expand Up @@ -308,7 +313,7 @@ else if (building) {
// 3. only use the first frame
// of these, 2 would give the most accurate result. However, its also the hardest to calculate
// of the remaining methods, 3 is both more accurate and easier to calculate than 1, so I opted for that approach
if (sprite.getFrameCount() > 0) {
if (hasFrames) {
for(int v = 0; v < vMax; v++) {
for(int u = 0; u < uMax; u++) {
int alpha = sprite.getPixelRGBA(0, u, vMax - v - 1) >> 24 & 0xFF;
Expand Down Expand Up @@ -500,7 +505,7 @@ public RenderTypeGroup getRenderType(IGeometryBakingContext context, RenderTypeG
* Parses the layer data from JSON
*/
public static LayerData fromJson(JsonObject json) {
int color = JsonHelper.parseColor(GsonHelper.getAsString(json, "color", ""));
int color = ColorLoadable.ALPHA.getOrDefault(json, "color", -1);
// TODO: rename this field?
int luminosity = GsonHelper.getAsInt(json, "luminosity");
boolean noTint = GsonHelper.getAsBoolean(json, "no_tint", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.tags.TagLoader;
import net.minecraft.tags.TagManager;
import slimeknights.mantle.util.JsonHelper;

import java.io.File;
import java.nio.file.Path;
Expand All @@ -30,7 +31,6 @@
*/
public class DumpAllTagsCommand {
private static final String TAG_DUMP_PATH = "./mantle_data_dump";
private static final int EXTENSION_LENGTH = ".json".length();

/**
* Registers this sub command with the root command
Expand Down Expand Up @@ -91,8 +91,7 @@ private static int runForFolder(CommandContext<CommandSourceStack> context, Reso
String dataPackFolder = TagManager.getTagDir(key);
for (Map.Entry<ResourceLocation,List<Resource>> entry : manager.listResourceStacks(dataPackFolder, fileName -> fileName.getPath().endsWith(".json")).entrySet()) {
ResourceLocation resourcePath = entry.getKey();
String path = resourcePath.getPath();
ResourceLocation tagId = new ResourceLocation(resourcePath.getNamespace(), path.substring(dataPackFolder.length() + 1, path.length() - EXTENSION_LENGTH));
ResourceLocation tagId = JsonHelper.localize(resourcePath, dataPackFolder, ".json");
DumpTagCommand.parseTag(entry.getValue(), foundTags.computeIfAbsent(resourcePath, id -> new ArrayList<>()), tagType, tagId, resourcePath);
}

Expand Down
65 changes: 56 additions & 9 deletions src/main/java/slimeknights/mantle/data/GenericDataProvider.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package slimeknights.mantle.data;

import com.google.common.hash.Hashing;
import com.google.common.hash.HashingOutputStream;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.stream.JsonWriter;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.util.GsonHelper;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.util.JsonHelper;

import javax.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.Comparator;

/** Generic logic to convert any serializable object into JSON. */
@SuppressWarnings({"unused", "SameParameterValue"}) // API
@RequiredArgsConstructor
@Log4j2
public abstract class GenericDataProvider implements DataProvider {
protected final FabricDataOutput output;
private final PackType type;
Expand All @@ -30,14 +38,53 @@ public GenericDataProvider(FabricDataOutput output, PackType type, String folder
this(output, type, folder, JsonHelper.DEFAULT_GSON);
}

/**
* Saves the given object to JSON
* @param output Output for writing
* @param location Location relative to this data provider's root
* @param object Object to save, will be converted using this provider's GSON instance
* @param keyComparator Key comparator to use
*/
protected void saveJson(CachedOutput output, ResourceLocation location, Object object, @Nullable Comparator<String> keyComparator) {
try {
Path path = this.generator.getOutputFolder().resolve(Paths.get(type.getDirectory(), location.getNamespace(), folder, location.getPath() + ".json"));
saveStable(output, gson.toJsonTree(object), path, keyComparator);
} catch (IOException e) {
Mantle.logger.error("Couldn't create data for {}", location, e);
}
}

/**
* Saves the given object to JSON
* @param output Output for writing
* @param location Location relative to this data provider's root
* @param object Object to save, will be converted using this provider's GSON instance
*/
protected void saveJson(List<CompletableFuture<?>> futures, CachedOutput output, ResourceLocation location, Object object) {
Path path = this.output.getOutputFolder().resolve(Paths.get(type.getDirectory(), location.getNamespace(), folder, location.getPath() + ".json"));
futures.add(DataProvider.saveStable(output, gson.toJsonTree(object), path));
protected void saveJson(CachedOutput output, ResourceLocation location, Object object) {
saveJson(output, location, object, DataProvider.KEY_COMPARATOR);
}

/**
* Saves the given object to JSON using a codec
* @param output Output for writing
* @param location Location relative to this data provider's root
* @param codec Codec to save the object
* @param object Object to save, will be converted using the passed codec
*/
protected <T> void saveJson(CachedOutput output, ResourceLocation location, Codec<T> codec, T object) {
saveJson(output, location, codec.encodeStart(JsonOps.INSTANCE, object).getOrThrow(false, Mantle.logger::error));
}

/** Recreation of {@link DataProvider#saveStable(CachedOutput, JsonElement, Path)} that allows swapping tke key comparator */
@SuppressWarnings("UnstableApiUsage")
static void saveStable(CachedOutput cache, JsonElement pJson, Path pPath, @Nullable Comparator<String> keyComparator) throws IOException {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
HashingOutputStream hashingOutput = new HashingOutputStream(Hashing.sha1(), byteOutput);
JsonWriter writer = new JsonWriter(new OutputStreamWriter(hashingOutput, StandardCharsets.UTF_8));
writer.setSerializeNulls(false);
writer.setIndent(" ");
GsonHelper.writeValue(writer, pJson, keyComparator);
writer.close();
cache.writeIfNeeded(pPath, byteOutput.toByteArray(), hashingOutput.hash());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
import net.minecraft.util.GsonHelper;
import slimeknights.mantle.util.JsonHelper;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -27,7 +28,6 @@
@RequiredArgsConstructor
@Log4j2
public abstract class MergingJsonDataLoader<B> implements ResourceManagerReloadListener {
private static final int JSON_LENGTH = ".json".length();

@VisibleForTesting
protected final Gson gson;
Expand Down Expand Up @@ -57,8 +57,7 @@ public void onResourceManagerReload(ResourceManager manager) {
Map<ResourceLocation,B> map = new HashMap<>();
for (Entry<ResourceLocation,List<Resource>> entry : manager.listResourceStacks(folder, fileName -> fileName.getPath().endsWith(".json")).entrySet()) {
ResourceLocation filePath = entry.getKey();
String path = filePath.getPath();
ResourceLocation id = new ResourceLocation(filePath.getNamespace(), path.substring(folder.length() + 1, path.length() - JSON_LENGTH));
ResourceLocation id = JsonHelper.localize(filePath, folder, ".json");

for (Resource resource : entry.getValue()) {
try (Reader reader = resource.openAsReader()) {
Expand Down
Loading

0 comments on commit a5fafbf

Please sign in to comment.