1.1.19
All checks were successful
SorceryCraft/pipeline/head This commit looks good

Update API Docs
Allow Setting Custom Display name for Spell
This commit is contained in:
TheBrokenRail 2020-03-18 11:56:25 -04:00
parent 373f0ad8d3
commit 9d1cad6223
8 changed files with 87 additions and 21 deletions

51
API.md
View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -66,7 +66,7 @@ public class CastingTableScreen extends HandledScreen<CastingTableScreenHandler>
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());
}
}
}

View File

@ -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<Identifier, Integer> 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<Identifier, Integer> 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);

View File

@ -61,7 +61,7 @@ public class SpellItem extends Item {
public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) {
Map<Identifier, Integer> spells = SpellTag.getSpells(itemStack);
for (Map.Entry<Identifier, Integer> entry : spells.entrySet()) {
tooltip.add(SpellTag.getTranslatedSpell(entry.getKey(), entry.getValue(), true));
tooltip.add(SpellTag.getTranslatedSpell(entry.getKey(), entry.getValue()));
}
}

View File

@ -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());
}
}

View File

@ -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<Identifier, Integer> itemSpells) {