Skip to content

Commit

Permalink
Port fluid tooltip logic from tinkers
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Jun 6, 2022
1 parent 0edb280 commit 9a52c23
Show file tree
Hide file tree
Showing 12 changed files with 545 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/generated/resources/.cache/cache
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bd5291d8d6b5177e4c7eda679255cea77848f851 assets/mantle/mantle/fluid_tooltips/buckets.json
3da0739a22aeaf0c3134fa33730756170afae1d9 assets/mantle/mantle/fluid_tooltips/fallback.json
6d86d02a3f9975defeaf27b7d67dcb5a5d06348f data/mantle/tags/fluids/lava.json
5e80abba332ce1f1d5c6f46f0cd536ba6afdf7f3 data/mantle/tags/fluids/water.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"units": [
{
"key": "gui.mantle.fluid.kilobucket",
"needed": 1000000
},
{
"key": "gui.mantle.fluid.bucket",
"needed": 1000
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"redirect": "mantle:buckets"
}
15 changes: 15 additions & 0 deletions src/main/java/slimeknights/mantle/Mantle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.minecraft.Util;
import net.minecraft.data.DataGenerator;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand Down Expand Up @@ -114,6 +116,9 @@ private void gatherData(final GatherDataEvent event) {
if (event.includeServer()) {
generator.addProvider(new MantleFluidTagProvider(generator, event.getExistingFileHelper()));
}
if (event.includeClient()) {
generator.addProvider(new MantleFluidTooltipProvider(generator));
}
}

/**
Expand All @@ -134,4 +139,14 @@ public static ResourceLocation getResource(String name) {
public static String makeDescriptionId(String base, String name) {
return Util.makeDescriptionId(base, getResource(name));
}

/**
* Makes a translation text component for the given name
* @param base Base name, such as "block" or "gui"
* @param name Object name
* @return Translation key
*/
public static MutableComponent makeComponent(String base, String name) {
return new TranslatableComponent(makeDescriptionId(base, name));
}
}
2 changes: 2 additions & 0 deletions src/main/java/slimeknights/mantle/client/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import slimeknights.mantle.client.model.util.ColoredBlockModel;
import slimeknights.mantle.client.model.util.MantleItemLayerModel;
import slimeknights.mantle.client.model.util.ModelHelper;
import slimeknights.mantle.fluid.tooltip.FluidTooltipHandler;
import slimeknights.mantle.registration.MantleRegistrations;
import slimeknights.mantle.registration.RegistrationHelper;
import slimeknights.mantle.util.OffhandCooldownTracker;
Expand All @@ -60,6 +61,7 @@ static void registerListeners(RegisterClientReloadListenersEvent event) {
event.registerReloadListener(ModelHelper.LISTENER);
event.registerReloadListener(new BookLoader());
ResourceColorManager.init(event);
FluidTooltipHandler.init(event);
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package slimeknights.mantle.datagen;

import net.minecraft.data.DataGenerator;
import net.minecraftforge.fluids.FluidAttributes;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.fluid.tooltip.AbstractFluidTooltipProvider;
import slimeknights.mantle.fluid.tooltip.FluidTooltipHandler;

/** Mantle datagen for fluid tooltips. For mods, don't use this, use {@link AbstractFluidTooltipProvider} */
public class MantleFluidTooltipProvider extends AbstractFluidTooltipProvider {
public MantleFluidTooltipProvider(DataGenerator generator) {
super(generator, Mantle.modId);
}

@Override
protected void addFluids() {
add("buckets")
.addUnit("kilobucket", FluidAttributes.BUCKET_VOLUME * 1000)
.addUnit("bucket", FluidAttributes.BUCKET_VOLUME);
addRedirect(FluidTooltipHandler.DEFAULT_ID, id("buckets"));
}

@Override
public String getName() {
return "Mantle Fluid Tooltip Provider";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package slimeknights.mantle.fluid.tooltip;

import com.google.common.collect.ImmutableList;
import com.google.gson.JsonObject;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import net.minecraft.Util;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.HashCache;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.material.Fluid;
import slimeknights.mantle.data.GenericDataProvider;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/** Provider for fluid tooltip information */
@SuppressWarnings("unused")
public abstract class AbstractFluidTooltipProvider extends GenericDataProvider {
private final Map<ResourceLocation,ResourceLocation> redirects = new HashMap<>();;
private final Map<ResourceLocation,FluidUnitListBuilder> builders = new HashMap<>();
private final String modId;

public AbstractFluidTooltipProvider(DataGenerator generator, String modId) {
super(generator, PackType.CLIENT_RESOURCES, FluidTooltipHandler.FOLDER, FluidTooltipHandler.GSON);
this.modId = modId;
}

/** Adds all relevant fluids to the maps */
protected abstract void addFluids();

@Override
public final void run(HashCache cache) throws IOException {
addFluids();
builders.forEach((key, builder) -> saveThing(cache, key, builder.build()));
redirects.forEach((key, target) -> {
JsonObject json = new JsonObject();
json.addProperty("redirect", target.toString());
saveThing(cache, key, json);
});
}


/* Helpers */

/** Creates a ResourceLocation for the local mod */
protected ResourceLocation id(String name) {
return new ResourceLocation(modId, name);
}

/** Adds a fluid to the builder */
protected FluidUnitListBuilder add(ResourceLocation id, @Nullable TagKey<Fluid> tag) {
if (redirects.containsKey(id)) {
throw new IllegalArgumentException(id + " is already registered as a redirect");
}
FluidUnitListBuilder newBuilder = new FluidUnitListBuilder(tag);
FluidUnitListBuilder original = builders.put(id, newBuilder);
if (original != null) {
throw new IllegalArgumentException(id + " is already registered");
}
return newBuilder;
}

/** Adds a fluid to the builder */
protected FluidUnitListBuilder add(String id, TagKey<Fluid> tag) {
return add(id(id), tag);
}

/** Adds a fluid to the builder using the tag name as the ID */
protected FluidUnitListBuilder add(TagKey<Fluid> tag) {
return add(id(tag.location().getPath()), tag);
}

/** Adds a fluid to the builder with no tag */
protected FluidUnitListBuilder add(ResourceLocation id) {
return add(id, null);
}

/** Adds a fluid to the builder with no tag */
protected FluidUnitListBuilder add(String id) {
return add(id(id), null);
}

/** Adds a redirect from a named builder to a target */
protected void addRedirect(ResourceLocation id, ResourceLocation target) {
if (builders.containsKey(id)) {
throw new IllegalArgumentException(id + " is already registered as a unit list");
}
ResourceLocation original = redirects.put(id, target);
if (original != null) {
throw new IllegalArgumentException(id + " is already redirecting to " + original);
}
}

/** Builder for a unit list */
@SuppressWarnings("unused")
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
protected class FluidUnitListBuilder {
@Nullable
private final TagKey<Fluid> tag;
private final ImmutableList.Builder<FluidUnit> units = ImmutableList.builder();

/** Adds a unit with a full translation key */
public FluidUnitListBuilder addUnitRaw(String key, int amount) {
units.add(new FluidUnit(key, amount));
return this;
}

/** Adds a unit local to the current mod */
public FluidUnitListBuilder addUnit(String key, int amount) {
return addUnitRaw(Util.makeDescriptionId("gui", id("fluid." + key)), amount);
}

/** Adds a unit local to the given mod */
public FluidUnitListBuilder addUnit(String key, String domain, int amount) {
return addUnitRaw(Util.makeDescriptionId("gui", new ResourceLocation(domain, "fluid." + key)), amount);
}

/** Builds the final instance */
private FluidUnitList build() {
return new FluidUnitList(tag, units.build());
}
}
}
Loading

0 comments on commit 9a52c23

Please sign in to comment.