Skip to content

Commit

Permalink
update to 1.21 and reclassify as a GAMELIBRARY
Browse files Browse the repository at this point in the history
- Explicitly set FMLModType to GAMELIBRARY (behavior prior to
  neoforged/FancyModLoader#96) so that the lib is available in the GAME
  module layer to have access to Minecraft. (reversion from previous
  changes to classify as a LIBRARY, which seems less useful here)
- Update ComponentUtil to use Component#translatableWithFallback
  due to the removal of I18nExtension.

Signed-off-by: srs-bsns <[email protected]>
  • Loading branch information
srs-bsns committed Jul 11, 2024
1 parent a39922a commit b8197dc
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 239 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ tasks.withType(Jar).configureEach {
'Implementation-Title' : "${base.archivesName.get()}",
'Implementation-Version' : "${project.version}",
'Build-Jdk' : "${System.properties['java.vendor']} ${System.properties['java.vm.version']}",
'Build-Timestamp' : java.time.OffsetDateTime.now() as String
'Build-Timestamp' : java.time.OffsetDateTime.now() as String,
"FMLModType" : "GAMELIBRARY"
])
}

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group = de.srsco
lib_name = srsLib
mc_version = 1.20.5
neo_version = 20.5.5-beta
mc_version = 1.21
neo_version = 21.0.78-beta
91 changes: 48 additions & 43 deletions src/main/java/de/srsco/srslib/util/ComponentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,102 +30,107 @@
package de.srsco.srslib.util;


import java.util.Arrays;
import java.util.function.BiFunction;

import javax.annotation.Nullable;

import org.slf4j.Logger;

import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;

import net.neoforged.neoforge.common.I18nExtension;

@SuppressWarnings({"unused", "WeakerAccess"})
public final class ComponentUtil
{
private static final Logger LOGGER = Util.getLogger(ComponentUtil.class);
private static final String EMPTY_STRING = "";
private static final Object[] EMPTY_ARGS = new Object[0];

private ComponentUtil() {}


/* Text & Component Helpers */

/**
* <h3>A BiFunction to generate an error message for a missing langkey.</h3>
* (If the langkey was not found in the lang file and a fallback was not provided.)
* Logs an error as well as returns a Component for in-game feedback.
* <h3>Creates a translatable Component for a langkey.</h3>
*
* @param key A langkey
* @return A translatable Component.
*
* @since 0.1.0, MC 1.19.1, 2022.08.08
*/
public static final BiFunction<CharSequence, String, Component> INVALID_LANGKEY = (key, objs) -> {
LOGGER.debug("Bad langkey: {}, Using Objects: {}", key, objs);
return Component.literal("Bad langkey: " + key + ", Using Objects: " + objs);
};
public static Component getTranslation(final String key)
{
return getTranslation(key, null, null, EMPTY_ARGS);
}

/**
* <h3>Creates a translatable Component for a langkey.</h3>
* Uses {@link I18nExtension}
* <h3>Creates a translatable Component for a langkey or uses a fallback string if the langkey does not exist.</h3>
*
* @param key A langkey
* @return A translatable Component, or a literal Component if the langkey does not exist.
* @param key A langkey
* @param fallback An optional fallback string.
* @return A translatable Component.
*
* @since 0.1.0, MC 1.19.1, 2022.08.08
* @since 4.0.0, MC 1.21, 2024.07.10
*/
public static Component getTranslation(final String key)
public static Component getTranslation(final String key, @Nullable final String fallback)
{
return getTranslation(key, CommonComponents.EMPTY);
return getTranslation(key, fallback, null, EMPTY_ARGS);
}

/**
* <h3>Creates a translatable Component for a langkey.</h3>
* Uses {@link I18nExtension}
*
* @param key A langkey
* @param objs Objects to be used in a formatted text string.
* @return A translatable Component, or a literal Component if the langkey does not exist.
* @param objs Objects to be used in a formatted text string
* @return A translatable Component, or a literal Component if the langkey does not exist
*
* @since 0.1.0, MC 1.19.1, 2022.08.08
*/
public static Component getTranslation(final String key, final Object... objs)
{
return getTranslation(key, CommonComponents.EMPTY, null, objs);
return getTranslation(key, null, null, objs);
}

/**
* <h3>Creates a translatable Component for a langkey using a possibly supplied Component.</h3>
* Uses {@link I18nExtension}
* <h3>Creates a translatable Component for a langkey or uses a fallback string if the langkey does not exist.</h3>
*
* @param key A langkey
* @param fallback An optional fallback string
* @param objs Objects to be used in a formatted text string
* @return A translatable Component, or a literal Component if the langkey does not exist
*
* @since 4.0.0, MC 1.21, 2024.07.10
*/
public static Component getTranslation(final String key, @Nullable final String fallback, final Object... objs)
{
return getTranslation(key, fallback, null, objs);
}

/**
* <h3>Creates a translatable Component for a langkey using an optional supplied Component.</h3>
*
* @param component A possible Component to be used for translation (i.e. a {@link Player} display name).
* @param key A langkey
* @param objs Objects to be used in a formatted text string.
* @return A translatable Component, or a literal Component if the langkey does not exist.
* @param component A possible Component to be used for translation (i.e. a {@link Player} display name)
* @param objs Objects to be used in a formatted text string
* @return A translatable Component, or a literal Component if the langkey does not exist
*
* @since 0.1.0, MC 1.19.1, 2022.08.08
*/
public static Component getTranslation(final String key, @Nullable final Component component, final Object... objs)
{
return getTranslation(key, component, null, objs);
return getTranslation(key, null, component, objs);
}

/**
* <h3>Creates a translatable Component for a langkey using a possible supplied Component or uses a fallback string if the langkey does not exist.</h3>
* Uses {@link I18nExtension}
* <h3>Creates a translatable Component for a langkey using an optional supplied Component or uses a fallback string if the langkey does not exist.</h3>
*
* @param component A possible Component to be used for translation (i.e. a {@link Player} display name).
* @param key A langkey
* @param objs Objects to be used in a formatted text string.
* @return A translatable Component, or a literal Component if the langkey does not exist.
* @param fallback An optional fallback string
* @param component A possible Component to be used for translation (i.e. a {@link Player} display name)
* @param objs Objects to be used in a formatted text string
* @return A translatable Component
*
* @since 0.1.0, MC 1.19.1, 2022.08.08
* @since 4.0.0, MC 1.21, 2024.07.10
*/
public static Component getTranslation(final String key, @Nullable final Component component, @Nullable final Component fallback, final Object... objs)
public static Component getTranslation(final String key, @Nullable final String fallback, @Nullable final Component component, final Object... objs)
{
return I18nExtension.getPattern(key, () -> EMPTY_STRING).equals(EMPTY_STRING)
? fallback != null ? fallback : INVALID_LANGKEY.apply(key, Arrays.toString(objs))
: component != null ? Component.translatable(key, component, objs) : Component.translatable(key, objs);
final var fb = (fallback != null && fallback.isBlank()) ? null : fallback;
return component == null ? Component.translatableWithFallback(key, fb, objs) : Component.translatableWithFallback(key, fb, component, objs);
}
}
Loading

0 comments on commit b8197dc

Please sign in to comment.