package electroblob.wizardry; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import org.apache.commons.lang3.RandomStringUtils; import electroblob.wizardry.packet.PacketGlyphData; import electroblob.wizardry.packet.WizardryPacketHandler; import electroblob.wizardry.spell.Spell; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.world.World; import net.minecraft.world.WorldSavedData; import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; /** Class responsible for generating and storing the randomised spell names and descriptions for each world, which are * displayed as glyphs using the SGA font renderer. * @since Wizardry 1.1 */ @Mod.EventBusSubscriber public class SpellGlyphData extends WorldSavedData { public static final String NAME = Wizardry.MODID + "_glyphData"; public Map randomNames = new HashMap(Spell.getTotalSpellCount()); public Map randomDescriptions = new HashMap(Spell.getTotalSpellCount()); // Required constructors public SpellGlyphData() { this(NAME); } public SpellGlyphData(String name){ super(name); } /** Generates random names and descriptions for any spells which don't already have them. */ public void generateGlyphNames(World world){ for(Spell spell : Spell.getSpells(Spell.allSpells)){ if(!randomNames.containsKey(spell)) randomNames.put(spell, generateRandomName(world.rand)); } for(Spell spell : Spell.getSpells(Spell.allSpells)){ if(!randomDescriptions.containsKey(spell)) randomDescriptions.put(spell, generateRandomDescription(world.rand)); } this.markDirty(); } private String generateRandomName(Random random){ String name = ""; for(int i=0; i names = new ArrayList(); List descriptions = new ArrayList(); for(Spell spell : Spell.getSpells(Spell.allSpells)){ names.add(this.randomNames.get(spell)); descriptions.add(this.randomDescriptions.get(spell)); } PacketGlyphData.Message msg = new PacketGlyphData.Message(names, descriptions); WizardryPacketHandler.net.sendTo(msg, player); Wizardry.logger.info("Synchronising spell glyph data for " + player.getName()); } /** Helper method to retrieve the random glyph name for the given spell from the map stored in the given world. */ public static String getGlyphName(Spell spell, World world){ Map names = SpellGlyphData.get(world).randomNames; return names == null ? "" : names.get(spell); } /** Helper method to retrieve the random glyph description for the given spell from the map stored in the given world. */ public static String getGlyphDescription(Spell spell, World world){ Map descriptions = SpellGlyphData.get(world).randomDescriptions; return descriptions == null ? "" : descriptions.get(spell); } @Override public void readFromNBT(NBTTagCompound nbt){ this.randomNames = new HashMap(); this.randomDescriptions = new HashMap(); NBTTagList tagList = nbt.getTagList("spellGlyphData", NBT.TAG_COMPOUND); for(int i=0; i