Skip to content

Commit

Permalink
Implement translation keys (#965)
Browse files Browse the repository at this point in the history
* add implementation for getXTranslationKey methods

* bracket alignment

* unit test skeleton

* account for edge cases

* move item edge case handling into own method

* change check for wall hanging materials

* add tests for translatable

* delete javadoc, reformat code style, remove entity type check

* add null annotations

* change to parameterized tests

---------

Co-authored-by: Felix Naumann <felix.naumann@student.uni-luebeck.de>
Co-authored-by: Thorinwasher <ragnarlol96@gmail.com>
  • Loading branch information
3 people committed Feb 11, 2024
1 parent ca0c3cc commit 8d51fe9
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 16 deletions.
110 changes: 94 additions & 16 deletions src/main/java/be/seeseemelk/mockbukkit/MockUnsafeValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Multimap;
import io.papermc.paper.inventory.ItemRarity;
import net.kyori.adventure.key.Keyed;
import io.papermc.paper.inventory.tooltip.TooltipContext;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import net.kyori.adventure.text.Component;
Expand All @@ -14,12 +15,14 @@
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.adventure.translation.Translatable;
import org.bukkit.Color;
import org.bukkit.FeatureFlag;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.RegionAccessor;
import org.bukkit.Statistic;
import org.bukkit.Tag;
import org.bukkit.UnsafeValues;
import org.bukkit.World;
import org.bukkit.advancement.Advancement;
Expand All @@ -43,14 +46,11 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.stream.Collectors;

/**
* Mock implementation of an {@link UnsafeValues}.
Expand Down Expand Up @@ -304,31 +304,109 @@ public Entity deserializeEntity(byte[] data, World world, boolean preserveUUID)
}

@Override
public String getBlockTranslationKey(Material material)
@Nullable
public String getBlockTranslationKey(@NotNull Material material)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
if (!material.isBlock())
{
return null;
}
// edge cases: WHEAT and NETHER_WART are blocks, but still use the "item" prefix
if (material == Material.WHEAT || material == Material.NETHER_WART)
{
return formatTranslatable("item", material);
}
return formatTranslatable("block", material);
}

@Override
public String getItemTranslationKey(Material material)
@Nullable
public String getItemTranslationKey(@NotNull Material material)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
if (!material.isItem())
{
return null;
}
String edgeCaseHandledTranslationKey = handleTranslateItemEdgeCases(material);
if (edgeCaseHandledTranslationKey != null)
{
return edgeCaseHandledTranslationKey;
}
return formatTranslatable("item", material);
}

@Override
public String getTranslationKey(EntityType type)
@Nullable
public String getTranslationKey(@NotNull EntityType type)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
Preconditions.checkArgument(type.getName() != null, "Invalid name of EntityType %s for translation key", type);
return formatTranslatable("entity", type);
}

@Override
public String getTranslationKey(ItemStack itemStack)
@Nullable
public String getTranslationKey(@NotNull ItemStack itemStack)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
if (itemStack.getType().isItem())
{
Material material = itemStack.getType();
String edgeCaseHandledTranslationKey = handleTranslateItemEdgeCases(material);
if (edgeCaseHandledTranslationKey != null)
{
return edgeCaseHandledTranslationKey;
}
return formatTranslatable("item", material, true);
}
else if (itemStack.getType().isBlock())
{
return getBlockTranslationKey(itemStack.getType());
}
else
{
return null;
}
}

private String handleTranslateItemEdgeCases(Material material)
{
// edge cases: WHEAT and NETHER_WART are blocks, but still use the "item" prefix (therefore this check has to be done BEFORE the isBlock check below)
if (material == Material.WHEAT || material == Material.NETHER_WART)
{
return formatTranslatable("item", material);
}
// edge case: If a translation key from an item is requested from anything that is also a block, the block translation key is always returned
// e.g: Material#STONE is a block (but also an obtainable item in the inventory). However, the translation key is always "block.minecraft.stone".
if (material.isBlock())
{
return formatTranslatable("block", material);
}
// not an edge case
return null;
}

private <T extends Keyed & Translatable> String formatTranslatable(String prefix, T translatable, boolean fromItemStack)
{
// enforcing Translatable is not necessary, but translating only makes sense when the object is really translatable by design.
String value = translatable.key().value();
if (translatable instanceof Material material)
{
// replace wall_hanging string check with Tag check (when implemented)
if (value.contains("wall_hanging") || Tag.WALL_SIGNS.isTagged(material) || value.endsWith("wall_banner") || value.endsWith("wall_torch") || value.endsWith("wall_skull") || value.endsWith("wall_head"))
{
value = value.replace("wall_", "");
}
final Set<Material> emptyEffects = Set.of(Material.POTION, Material.SPLASH_POTION, Material.TIPPED_ARROW, Material.LINGERING_POTION);
if (fromItemStack && emptyEffects.contains(material))
{
value += ".effect.empty";
}
}
return String.format("%s.%s.%s", prefix, translatable.key().namespace(), value);
}

private <T extends Keyed & Translatable> String formatTranslatable(String prefix, T translatable)
{
return formatTranslatable(prefix, translatable, false);
}

@Override
Expand Down
98 changes: 98 additions & 0 deletions src/test/java/be/seeseemelk/mockbukkit/UnsafeValuesTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package be.seeseemelk.mockbukkit;

import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.PluginDescriptionFile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.StringReader;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -157,4 +163,96 @@ void fromLegacy_MaterialData_Null_ReturnsNull()
assertThrows(NullPointerException.class, () -> mockUnsafeValues.fromLegacy((MaterialData) null));
}

@Test
void testMaterialThatIsOnlyItemTranslationKey()
{
assertEquals("item.minecraft.saddle", Material.SADDLE.getItemTranslationKey());
assertNull(Material.SADDLE.getBlockTranslationKey());
}

@ParameterizedTest
@MethodSource("materialAndBlockTranslationKeyProvider")
void testMaterialThatIsItemAndBlockTranslationKey(String expectedBlockKey, String expectedItemKey, Material material)
{
assertEquals(expectedBlockKey, mockUnsafeValues.getBlockTranslationKey(material));
assertEquals(expectedItemKey, mockUnsafeValues.getItemTranslationKey(material));
}

static Stream<Arguments> materialAndBlockTranslationKeyProvider()
{
return Stream.of(
Arguments.of("block.minecraft.stone", "block.minecraft.stone", Material.STONE),
Arguments.of("block.minecraft.dirt", "block.minecraft.dirt", Material.DIRT),
Arguments.of("item.minecraft.wheat", "item.minecraft.wheat", Material.WHEAT),
Arguments.of("item.minecraft.nether_wart", "item.minecraft.nether_wart", Material.NETHER_WART)
);
}

@ParameterizedTest
@MethodSource("wallMaterialTranslationKeyProvider")
void testWallMaterialTranslationKey(String expectedKey, Material material)
{
assertEquals(expectedKey, material.getBlockTranslationKey());
}

static Stream<Arguments> wallMaterialTranslationKeyProvider()
{
return Stream.of(
Arguments.of("block.minecraft.acacia_sign", Material.ACACIA_SIGN),
Arguments.of("block.minecraft.acacia_sign", Material.ACACIA_WALL_SIGN),
Arguments.of("block.minecraft.acacia_hanging_sign", Material.ACACIA_HANGING_SIGN),
Arguments.of("block.minecraft.acacia_hanging_sign", Material.ACACIA_WALL_HANGING_SIGN),
Arguments.of("block.minecraft.white_banner", Material.WHITE_BANNER),
Arguments.of("block.minecraft.white_banner", Material.WHITE_WALL_BANNER),
Arguments.of("block.minecraft.torch", Material.TORCH),
Arguments.of("block.minecraft.torch", Material.WALL_TORCH),
Arguments.of("block.minecraft.skeleton_skull", Material.SKELETON_SKULL),
Arguments.of("block.minecraft.skeleton_skull", Material.SKELETON_WALL_SKULL),
Arguments.of("block.minecraft.creeper_head", Material.CREEPER_HEAD),
Arguments.of("block.minecraft.creeper_head", Material.CREEPER_WALL_HEAD)
);
}

@Test
void testEntityTranslationKey()
{
assertEquals("entity.minecraft.pig", mockUnsafeValues.getTranslationKey(EntityType.PIG));
assertThrows(IllegalArgumentException.class, () -> mockUnsafeValues.getTranslationKey(EntityType.UNKNOWN));
}

@ParameterizedTest
@MethodSource("itemStackTranslationKeyProvider")
void testItemStackTranslationKey(String expectedKey, ItemStack itemStack)
{
assertEquals(expectedKey, mockUnsafeValues.getTranslationKey(itemStack));
}

static Stream<Arguments> itemStackTranslationKeyProvider()
{
return Stream.of(
Arguments.of("item.minecraft.saddle", new ItemStack(Material.SADDLE)),
Arguments.of("block.minecraft.stone", new ItemStack(Material.STONE)),
Arguments.of("item.minecraft.wheat", new ItemStack(Material.WHEAT)),
Arguments.of("item.minecraft.nether_wart", new ItemStack(Material.NETHER_WART))
);
}

@ParameterizedTest
@MethodSource("itemStackEmptyEffectTranslationKeyProvider")
void testItemStackEmptyEffectTranslationKey(String expectedMaterialKey, Material material, String expectedItemStackKey, ItemStack itemStack)
{
assertEquals(expectedMaterialKey, material.getItemTranslationKey());
assertEquals(expectedItemStackKey, itemStack.translationKey());
}

static Stream<Arguments> itemStackEmptyEffectTranslationKeyProvider()
{
return Stream.of(
Arguments.of("item.minecraft.potion", Material.POTION, "item.minecraft.potion.effect.empty", new ItemStack(Material.POTION)),
Arguments.of("item.minecraft.splash_potion", Material.SPLASH_POTION, "item.minecraft.splash_potion.effect.empty", new ItemStack(Material.SPLASH_POTION)),
Arguments.of("item.minecraft.tipped_arrow", Material.TIPPED_ARROW, "item.minecraft.tipped_arrow.effect.empty", new ItemStack(Material.TIPPED_ARROW)),
Arguments.of("item.minecraft.lingering_potion", Material.LINGERING_POTION, "item.minecraft.lingering_potion.effect.empty", new ItemStack(Material.LINGERING_POTION))
);
}

}

0 comments on commit 8d51fe9

Please sign in to comment.