From 9d1cad62239e54bd8593bc84f666ae5a22519bc0 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Wed, 18 Mar 2020 11:56:25 -0400 Subject: [PATCH] 1.1.19 Update API Docs Allow Setting Custom Display name for Spell --- API.md | 51 ++++++++++++++++--- CHANGELOG.md | 4 ++ gradle.properties | 2 +- .../client/gui/CastingTableScreen.java | 2 +- .../sorcerycraft/command/SpellCommand.java | 7 ++- .../sorcerycraft/item/SpellItem.java | 2 +- .../sorcerycraft/spell/api/Spell.java | 25 +++++++++ .../sorcerycraft/spell/util/SpellTag.java | 15 +++--- 8 files changed, 87 insertions(+), 21 deletions(-) diff --git a/API.md b/API.md index 2034d20..6a8c42d 100644 --- a/API.md +++ b/API.md @@ -24,33 +24,65 @@ public ExampleSpell(Identifier id, int level) { super(id, level); } - + @Override public void execute(Entity target, Entity source, Entity attacker) { + // OPTIONAL // Called when ExampleSpell hits an entity - // Not required to override + // Override this if you want the spell to affect entities } - + @Override public void execute(World world, BlockHitResult hitResult) { + // OPTIONAL // Called when ExampleSpell hits a block - // Not required to override + // Override this if you want the spell to affect blocks } - + @Override public int getXPCost() { + // REQUIRED // Return the amount of levels required to make ExampleSpell in a Casting Table + switch (getLevel()) { + case 0: { + return 4; + } + case 1: { + return 8; + } + } + return -1; } - + @Override public ItemStack getItemCost() { + // REQUIRED // Return the item(s) required to make ExampleSpell in a Casting Table // Return ItemStack.EMPTY if an item is not required + switch (getLevel()) { + case 0: { + return new ItemStack(Items.SUGAR_CANE); + } + case 1: { + return new ItemStack(Items.BOOK); + } + } + return ItemStack.EMPTY; } - + @Override public int getMaxLevel() { + // REQUIRED // Return ExampleSpell's maximum level + return 2; + } + + @Override + public Text getTranslation() { + // OPTIONAL + // Return a custom display name for ExampleSpell + // Override this only if you want a custom display name + return new LiteralText("Example " + (getLevel() + 1)); } } ``` @@ -60,7 +92,7 @@ public static final Identifier EXAMPLE_SPELL = SpellRegistry.register(new Identifier("modid", "example_spell"), ExampleSpell.class); } ``` -5. Add Spell Translation +5. Add Spell Translation (skip this if you have overridden ```Spell.getTranslation()``) ```json { "spell.modid.example_spell": "Example" @@ -72,5 +104,8 @@ - ```(non-static) Spell.getID()``` - ```(static) SpellRegistry.register()``` +## Note on Spell Levels +Spell levels are 0-indexed, if you have a level 1 Example Spell, ```Spell.getLevel()``` wil return 0, and if it is level 2 ```Spell.getLevel()``` wil return 1, ```Spell.getMaxSpell()``` should be the maximum-acceptable value of ```Spell.getLevel() + 1```, so if Example Spell has levels 1-2, ```Spell.getMaxLevel()``` should return 2. + ## JavaDoc [View JavaDoc](https://javadoc.jitpack.io/com/thebrokenrail/sorcerycraft/latest/javadoc/index.html) \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 18c2d13..4c26fc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +**1.1.19** +* Update API Docs +* Allow Setting Custom Display name for Spell + **1.1.18** * Fix Crash When Using Dedicated Server diff --git a/gradle.properties b/gradle.properties index 575716c..1d64e29 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,7 +10,7 @@ org.gradle.jvmargs = -Xmx1G fabric_loader_version = 0.7.8+build.187 # Mod Properties - mod_version = 1.1.18 + mod_version = 1.1.19 maven_group = com.thebrokenrail archives_base_name = sorcerycraft diff --git a/src/main/java/com/thebrokenrail/sorcerycraft/client/gui/CastingTableScreen.java b/src/main/java/com/thebrokenrail/sorcerycraft/client/gui/CastingTableScreen.java index 456ee2f..35a0d01 100644 --- a/src/main/java/com/thebrokenrail/sorcerycraft/client/gui/CastingTableScreen.java +++ b/src/main/java/com/thebrokenrail/sorcerycraft/client/gui/CastingTableScreen.java @@ -66,7 +66,7 @@ public class CastingTableScreen extends HandledScreen button.visible = button.index < handler.getRecipes().length; if (button.visible) { Spell spell = handler.getRecipes()[button.getIndex() + indexStartOffset]; - button.setMessage(SpellTag.getTranslatedSpell(spell.getID(), spell.getLevel(), true).getString()); + button.setMessage(SpellTag.getTranslatedSpell(spell.getID(), spell.getLevel()).getString()); } } } diff --git a/src/main/java/com/thebrokenrail/sorcerycraft/command/SpellCommand.java b/src/main/java/com/thebrokenrail/sorcerycraft/command/SpellCommand.java index b6613b5..17be3fd 100644 --- a/src/main/java/com/thebrokenrail/sorcerycraft/command/SpellCommand.java +++ b/src/main/java/com/thebrokenrail/sorcerycraft/command/SpellCommand.java @@ -15,7 +15,6 @@ import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Texts; import net.minecraft.text.TranslatableText; -import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; import java.util.HashMap; @@ -33,7 +32,7 @@ public class SpellCommand { PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player"); SpellPlayerEntity spellPlayer = (SpellPlayerEntity) player; Map spellMap = spellPlayer.getDiscoveredSpells(); - ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.listing_spells", player.getDisplayName(), Texts.join(spellMap.entrySet(), spell -> SpellTag.getTranslatedSpell(spell.getKey(), spell.getValue(), true))), false); + ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.listing_spells", player.getDisplayName(), Texts.join(spellMap.entrySet(), spell -> SpellTag.getTranslatedSpell(spell.getKey(), spell.getValue()))), false); return 0; }) ) @@ -112,7 +111,7 @@ public class SpellCommand { spellMap.put(spell, level); SpellTag.setSpells(stack, spellMap); - ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.applied_spell", SpellTag.getTranslatedSpell(spell, level, true)), true); + ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.applied_spell", SpellTag.getTranslatedSpell(spell, level)), true); return 1; }) ) @@ -134,7 +133,7 @@ public class SpellCommand { Map spellMap = SpellTag.getSpells(stack); if (spellMap.containsKey(spell)) { - ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.removed_spell", SpellTag.getTranslatedSpell(spell, spellMap.get(spell), true)), true); + ctx.getSource().sendFeedback(new TranslatableText("command." + SorceryCraft.NAMESPACE + ".spell.removed_spell", SpellTag.getTranslatedSpell(spell, spellMap.get(spell))), true); spellMap.remove(spell); } SpellTag.setSpells(stack, spellMap); diff --git a/src/main/java/com/thebrokenrail/sorcerycraft/item/SpellItem.java b/src/main/java/com/thebrokenrail/sorcerycraft/item/SpellItem.java index efd56dc..b24ec8c 100644 --- a/src/main/java/com/thebrokenrail/sorcerycraft/item/SpellItem.java +++ b/src/main/java/com/thebrokenrail/sorcerycraft/item/SpellItem.java @@ -61,7 +61,7 @@ public class SpellItem extends Item { public void appendTooltip(ItemStack itemStack, World world, List tooltip, TooltipContext tooltipContext) { Map spells = SpellTag.getSpells(itemStack); for (Map.Entry entry : spells.entrySet()) { - tooltip.add(SpellTag.getTranslatedSpell(entry.getKey(), entry.getValue(), true)); + tooltip.add(SpellTag.getTranslatedSpell(entry.getKey(), entry.getValue())); } } diff --git a/src/main/java/com/thebrokenrail/sorcerycraft/spell/api/Spell.java b/src/main/java/com/thebrokenrail/sorcerycraft/spell/api/Spell.java index cc3a2e2..7f0dd17 100644 --- a/src/main/java/com/thebrokenrail/sorcerycraft/spell/api/Spell.java +++ b/src/main/java/com/thebrokenrail/sorcerycraft/spell/api/Spell.java @@ -1,7 +1,10 @@ package com.thebrokenrail.sorcerycraft.spell.api; +import com.thebrokenrail.sorcerycraft.spell.registry.SpellRegistry; import net.minecraft.entity.Entity; import net.minecraft.item.ItemStack; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.world.World; @@ -70,4 +73,26 @@ public abstract class Spell { * @return The Spell's Max Level */ public abstract int getMaxLevel(); + + /** + * Default Implementation for Spell.getTranslation() + * @param id Spell ID + * @param level Spell Level + * @return Translated Display Name + */ + public static Text getDefaultTranslation(Identifier id, int level) { + Text text = new TranslatableText("spell." + id.getNamespace() + '.' + id.getPath()); + if (level != 0 || SpellRegistry.getMaxLevel(id) != 1) { + text.append(" ").append(new TranslatableText("enchantment.level." + (level + 1))); + } + return text; + } + + /** + * Get Translated Display Name + * @return Translated Display Name + */ + public Text getTranslation() { + return getDefaultTranslation(getID(), getLevel()); + } } diff --git a/src/main/java/com/thebrokenrail/sorcerycraft/spell/util/SpellTag.java b/src/main/java/com/thebrokenrail/sorcerycraft/spell/util/SpellTag.java index c041933..282da3b 100644 --- a/src/main/java/com/thebrokenrail/sorcerycraft/spell/util/SpellTag.java +++ b/src/main/java/com/thebrokenrail/sorcerycraft/spell/util/SpellTag.java @@ -83,17 +83,20 @@ public class SpellTag { return map; } - public static Text getTranslatedSpell(Identifier id, int level, boolean allowNumber) { - Text text = new TranslatableText("spell." + id.getNamespace() + '.' + id.getPath()); - text.formatted(Formatting.GRAY); - if ((level != 0 || SpellRegistry.getMaxLevel(id) != 1) && allowNumber) { - text.append(" ").append(new TranslatableText("enchantment.level." + (level + 1))); + public static Text getTranslatedSpell(Identifier id, int level) { + Spell spell = SpellRegistry.getSpell(id, level); + Text text; + if (spell != null) { + text = spell.getTranslation(); + } else { + text = Spell.getDefaultTranslation(id, level); } + text.formatted(Formatting.GRAY); return text; } public static Text getTranslatedSpellChat(Identifier id, int level) { - return new LiteralText("[").append(SpellTag.getTranslatedSpell(id, level, true).getString()).append("]").formatted(Formatting.GREEN); + return new LiteralText("[").append(SpellTag.getTranslatedSpell(id, level).getString()).append("]").formatted(Formatting.GREEN); } public static void learnSpells(PlayerEntity player, Map itemSpells) {