This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
SorceryCraft/src/main/java/com/thebrokenrail/sorcerycraft/spell/util/SpellHelper.java

136 lines
4.7 KiB
Java

package com.thebrokenrail.sorcerycraft.spell.util;
import com.thebrokenrail.sorcerycraft.SorceryCraft;
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
import com.thebrokenrail.sorcerycraft.spell.api.registry.SpellRegistry;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.Map;
public class SpellHelper {
public static final String SPELL_TAG = "Spells";
public static void setSpells(ItemStack itemStack, Map<Identifier, Integer> map) {
CompoundTag tag;
if (itemStack.hasTag()) {
tag = itemStack.getTag();
} else {
tag = new CompoundTag();
tag.put(SPELL_TAG, new ListTag());
}
assert tag != null;
tag.put(SPELL_TAG, createSpellsTag(map));
itemStack.setTag(tag);
}
public static ListTag createSpellsTag(Map<Identifier, Integer> map) {
ListTag spells = new ListTag();
for (Map.Entry<Identifier, Integer> entry : map.entrySet()) {
CompoundTag spell = new CompoundTag();
spell.putString("id", entry.getKey().toString());
spell.putInt("level", entry.getValue());
spells.add(spell);
}
return spells;
}
public static Map<Identifier, Integer> getSpells(ItemStack itemStack) {
return getSpells(itemStack.getTag());
}
public static Map<Identifier, Integer> getSpells(CompoundTag tag) {
if (tag == null) {
tag = new CompoundTag();
tag.put(SPELL_TAG, new ListTag());
}
Tag spellsTag = tag.get(SPELL_TAG);
ListTag spells;
if (spellsTag instanceof ListTag) {
spells = (ListTag) spellsTag;
} else {
spells = new ListTag();
}
Map<Identifier, Integer> map = new HashMap<>();
for (int i = 0; i < spells.size(); i++) {
CompoundTag spell = spells.getCompound(i);
Identifier id = new Identifier(spell.getString("id"));
int level = spell.getInt("level");
if (map.get(id) == null || map.get(id) < level) {
map.put(id, level);
}
}
return map;
}
public static Text getTranslatedSpell(Identifier id, int level) {
Spell spell = SpellRegistry.getSpell(id, level);
MutableText 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(SpellHelper.getTranslatedSpell(id, level).getString()).append("]").formatted(Formatting.GREEN);
}
public static void learnSpells(PlayerEntity player, Map<Identifier, Integer> itemSpells) {
SpellPlayerEntity spellPlayer = (SpellPlayerEntity) player;
World world = player.getEntityWorld();
Map<Identifier, Integer> playerSpells = spellPlayer.getDiscoveredSpells();
boolean changed = false;
for (Map.Entry<Identifier, Integer> entry : itemSpells.entrySet()) {
Spell spell = SpellRegistry.getSpell(entry);
if (spell != null) {
if (spell.getLevel() >= spell.getMaxLevel()) {
spell = SpellRegistry.getSpell(entry.getKey(), spell.getMaxLevel() - 1);
}
assert spell != null;
if (!playerSpells.containsKey(spell.getID()) || playerSpells.get(spell.getID()) < spell.getLevel()) {
changed = true;
playerSpells.put(spell.getID(), spell.getLevel());
assert world.getServer() != null;
Text text = getTranslatedSpellChat(spell.getID(), spell.getLevel());
world.getServer().getPlayerManager().sendToAll(new TranslatableText("chat." + SorceryCraft.NAMESPACE + ".discovered_spell", player.getDisplayName(), text));
}
}
}
if (changed) {
SorceryCraft.playSpellSound(player);
spellPlayer.setDiscoveredSpells(playerSpells);
SorceryCraft.DISCOVER_ALL_SPELLS_CRITERION.trigger((ServerPlayerEntity) player);
}
}
}