From b8b41aa5846dae5a9bbf4fee82020f1d9e2b2a48 Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 12 Aug 2024 17:08:54 -0400 Subject: Initial commit --- .../java/fyresmodjam/worldgen/FyresWorldData.java | 480 +++++++++++++++++++++ 1 file changed, 480 insertions(+) create mode 100644 src/main/java/fyresmodjam/worldgen/FyresWorldData.java (limited to 'src/main/java/fyresmodjam/worldgen') diff --git a/src/main/java/fyresmodjam/worldgen/FyresWorldData.java b/src/main/java/fyresmodjam/worldgen/FyresWorldData.java new file mode 100644 index 0000000..caf0b1f --- /dev/null +++ b/src/main/java/fyresmodjam/worldgen/FyresWorldData.java @@ -0,0 +1,480 @@ +package fyresmodjam.worldgen; + +import java.awt.Color; +import java.util.HashMap; + +import fyresmodjam.ModjamMod; +import net.minecraft.entity.boss.EntityDragon; +import net.minecraft.entity.boss.EntityWither; +import net.minecraft.entity.monster.EntityGhast; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.potion.Potion; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.World; +import net.minecraft.world.storage.MapStorage; +import net.minecraft.world.storage.WorldSavedData; +import net.minecraftforge.fml.common.FMLCommonHandler; + +public class FyresWorldData extends WorldSavedData { + + /* + * @formatter:off + */ + public static String[] validDisadvantages = { + "Tougher Mobs", + "Weak", + "Explosive Traps", + "Increased Mob Spawn", + "Neverending Rain", + "Neverending Night", + "Permadeath", + "Trigger-happy", + "Diabolic", + "Organized Foes" + }; + + public static String[] disadvantageDescriptions = { + "Hostile enemies takes 25% less damage", + "-25% melee damage", + "Traps also trigger explosions when set off", + "+33% hostile mob spawn rate", + "Constantly rains", + "Constant night", + "Items dropped upon death are permanently lost", + "Mobs have a chance to explode on death", + "All mobs have a chance to recieve a random blessing", + "Mobs spawn with armor and weapons, increasing with level" + }; + + public static String[] validTasks = { + "Kill", + "Burn" + }; + + /* + * @formatter:on + */ + + public static String key = "FyresWorldData"; + + public int[] potionValues = null; + public int[] potionDurations = null; + + public int[][] mushroomColors = null; + + public String currentDisadvantage = null; + + public String currentTask = null; + public int currentTaskID = -1; + public int currentTaskAmount = 0; + public int progress = 0; + public int tasksCompleted = 0; + public int rewardLevels = -1; + + public boolean enderDragonKilled = false; + + // TODO pull these into a class instead of using parallel arrays + @SuppressWarnings("rawtypes") + public static Class[] validMobs = { + EntityDragon.class, EntityGhast.class, + EntityWither.class + }; + public static String[] validMobNames = { + "Ender Dragon", "Ghast", "Wither" + }; + public static int[][] mobNumbers = { + new int[] { + 1, 1 + }, new int[] { + 5, 15 + }, new int[] { + 1, 1 + } + }; + + public HashMap blessingByPlayer = new HashMap(); + public HashMap potionKnowledgeByPlayer = new HashMap(); + + public HashMap killStatsByPlayer = new HashMap(); + public HashMap weaponStatsByPlayer = new HashMap(); + public HashMap craftingStatsByPlayer = new HashMap(); + + public static ItemStack[] validItems = { + new ItemStack(Blocks.DIAMOND_BLOCK), + new ItemStack(Blocks.GOLD_BLOCK), + new ItemStack(Blocks.EMERALD_BLOCK), + new ItemStack(Blocks.LAPIS_BLOCK), + new ItemStack(Items.DIAMOND), + new ItemStack(Items.EMERALD), + new ItemStack(Items.GOLD_INGOT), + new ItemStack(Items.NETHER_STAR), + new ItemStack(Items.GHAST_TEAR) + }; + + public FyresWorldData() { + super(key); + } + + public FyresWorldData(String key) { + super(key); + } + + public static FyresWorldData forWorld(World world) { + + MapStorage storage = world.getPerWorldStorage(); + FyresWorldData result = (FyresWorldData) storage + .getOrLoadData(FyresWorldData.class, key); + + if (result == null) { + result = new FyresWorldData(); + storage.setData(key, result); + result.checkWorldData(); + } + + return result; + } + + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + if (nbttagcompound.hasKey("values")) { + potionValues = nbttagcompound + .getIntArray("values"); + } + if (nbttagcompound.hasKey("durations")) { + potionDurations = nbttagcompound + .getIntArray("durations"); + } + if (nbttagcompound.hasKey("currentDisadvantage")) { + currentDisadvantage = nbttagcompound + .getString("currentDisadvantage"); + } + + if (nbttagcompound.hasKey("currentTask")) { + currentTask = nbttagcompound + .getString("currentTask"); + } + if (nbttagcompound.hasKey("currentTaskID")) { + currentTaskID = nbttagcompound + .getInteger("currentTaskID"); + } + if (nbttagcompound.hasKey("currentTaskAmount")) { + currentTaskAmount = nbttagcompound + .getInteger("currentTaskAmount"); + } + if (nbttagcompound.hasKey("progress")) { + progress = nbttagcompound.getInteger("progress"); + } + if (nbttagcompound.hasKey("tasksCompleted")) { + tasksCompleted = nbttagcompound + .getInteger("tasksCompleted"); + } + + if (nbttagcompound.hasKey("enderDragonKilled")) { + enderDragonKilled = nbttagcompound + .getBoolean("enderDragonKilled"); + } + + if (nbttagcompound.hasKey("rewardLevels")) { + rewardLevels = nbttagcompound + .getInteger("rewardLevels"); + } + + mushroomColors = new int[13][]; + for (int i = 0; i < 13; i++) { + if (nbttagcompound.hasKey( + "mushroomColors_" + (i + 1))) { + mushroomColors[i] = nbttagcompound + .getIntArray("mushroomColors_" + + (i + 1)); + } + } + + if (nbttagcompound.hasKey("TempPlayerStats")) { + NBTTagCompound tempStats = nbttagcompound + .getCompoundTag("TempPlayerStats"); + + // TODO is this what we should be doing? + for (String key : tempStats.getKeySet()) { + if (key == null || !(tempStats.getTag(key) instanceof NBTTagCompound)) { + continue; + } + NBTTagCompound player = (NBTTagCompound) tempStats.getTag(key); + + blessingByPlayer.put( + player.getString("Name"), + player.getString( + "Blessing")); + potionKnowledgeByPlayer.put( + player.getString("Name"), + player.getIntArray( + "PotionKnowledge")); + if (player.hasKey("KillStats")) { + killStatsByPlayer.put(player + .getString("Name"), + player.getCompoundTag( + "KillStats")); + } + if (player.hasKey("WeaponStats")) { + weaponStatsByPlayer.put(player + .getString("Name"), + player.getCompoundTag( + "WeaponStats")); + } + if (player.hasKey("CraftingStats")) { + craftingStatsByPlayer.put(player + .getString("Name"), + player.getCompoundTag( + "CraftingStats")); + } + } + } + + checkWorldData(); + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) { + checkWorldData(); + + nbttagcompound.setIntArray("values", potionValues); + nbttagcompound.setIntArray("durations", potionDurations); + nbttagcompound.setString("currentDisadvantage", + currentDisadvantage); + + nbttagcompound.setString("currentTask", currentTask); + nbttagcompound.setInteger("currentTaskID", currentTaskID); + nbttagcompound.setInteger("currentTaskAmount", + currentTaskAmount); + nbttagcompound.setInteger("progress", progress); + nbttagcompound.setInteger("tasksCompleted", + tasksCompleted); + + nbttagcompound.setBoolean("enderDragonKilled", + enderDragonKilled); + + nbttagcompound.setInteger("rewardLevels", rewardLevels); + + for (int i = 0; i < 13; i++) { + nbttagcompound.setIntArray( + "mushroomColors_" + (i + 1), + mushroomColors[i]); + } + + if (!blessingByPlayer.isEmpty()) { + NBTTagCompound tempPlayerStats = new NBTTagCompound(); + + for (String s : blessingByPlayer.keySet()) { + if (s == null) { + continue; + } + + NBTTagCompound player = new NBTTagCompound(); + + player.setString("Name", s); + player.setString("Blessing", + blessingByPlayer.get(s)); + player.setIntArray("PotionKnowledge", + potionKnowledgeByPlayer + .get(s)); + if (killStatsByPlayer.containsKey(s)) { + player.setTag("KillStats", + killStatsByPlayer + .get(s)); + } + if (weaponStatsByPlayer.containsKey(s)) { + player.setTag("WeaponStats", + weaponStatsByPlayer + .get(s)); + } + if (craftingStatsByPlayer.containsKey(s)) { + player.setTag("CraftingStats", + craftingStatsByPlayer + .get(s)); + } + + tempPlayerStats.setTag(s, player); + } + + nbttagcompound.setTag("TempPlayerStats", + tempPlayerStats); + } + + return nbttagcompound; + } + + private void checkWorldData() { + if (potionValues == null) { + // TODO fix this + potionValues = new int[] { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + /* + * for (int i = 0; i < 12; i++) { int randPotionID + * = ModjamMod.r.nextInt( + * Potion.REGISTRY.getKeys().size()); + * + * boolean stop = false; for(ResourceLocation rloc + * : Potion.REGISTRY.getKeys()) { Potion pot = + * Potion.REGISTRY.getObject(rloc); + * + * if(pot == null) continue; + * + * stop = true; randPotionID = ModjamMod.r.nextInt( + * Potion.REGISTRY.getKeys().size()); + * + * for (int i3 = 0; i3 < 12; i3++) { if + * (potionValues[i3] == randPotionID) { stop = + * false; break; } } } + * + * potionValues[i] = randPotionID; } + */ + } else { + /* + * for (int i = 0; i < 12; i++) { if + * (Potion.potionTypes[potionValues[i]] == null) { + * int randPotionID = ModjamMod.r + * .nextInt(Potion.potionTypes.length); + * + * while (Potion.potionTypes[randPotionID] == null) + * { randPotionID = ModjamMod.r + * .nextInt(Potion.potionTypes.length); } + * + * potionValues[i] = randPotionID; } } + */ + } + + if (potionDurations == null) { + potionDurations = new int[12]; + } + + for (int i = 0; i < 12; i++) { + if (potionDurations[i] != 0) { + continue; + } + potionDurations[i] = 5 + ModjamMod.r.nextInt(26); + } + + if (mushroomColors == null) { + mushroomColors = new int[13][2]; + + for (int i = 0; i < 13; i++) { + mushroomColors[i][0] = Color.HSBtoRGB( + ModjamMod.r.nextFloat(), + ModjamMod.r.nextFloat(), + ModjamMod.r.nextFloat()); + mushroomColors[i][1] = Color.HSBtoRGB( + ModjamMod.r.nextFloat(), + ModjamMod.r.nextFloat(), + ModjamMod.r.nextFloat()); + } + } + + boolean changeDisadvantage = true; + + if (currentDisadvantage != null) { + boolean valid = false; + + for (String s : validDisadvantages) { + if (s.equals(currentDisadvantage)) { + valid = true; + break; + } + } + + changeDisadvantage = !valid && !currentDisadvantage + .equals("None"); + } + + if (changeDisadvantage) { + currentDisadvantage = validDisadvantages[ModjamMod.r + .nextInt(validDisadvantages.length)]; + + MinecraftServer server = FMLCommonHandler + .instance() + .getMinecraftServerInstance(); + + while (server != null && server.isHardcore() + && currentDisadvantage.equals( + "Permadeath")) { + currentDisadvantage = validDisadvantages[ModjamMod.r + .nextInt(validDisadvantages.length)]; + } + } + + if (currentTask == null) { + giveNewTask(); + } else { + boolean changeTask = true; + + for (String s : validTasks) { + if (s.equals(currentTask)) { + changeTask = false; + break; + } + } + + if (changeTask || (currentTask != null + && currentTask.equals("Kill") + && currentTaskID == 0 + && enderDragonKilled)) { + giveNewTask(); + } else { + if (currentTask.equals("Kill")) { + currentTaskID %= validMobs.length; + } + } + } + + if (rewardLevels == -1) { + rewardLevels = 5 + ModjamMod.r.nextInt(6); + } + } + + public void giveNewTask() { + progress = 0; + + currentTask = validTasks[ModjamMod.r + .nextInt(validTasks.length)]; + + if (currentTask.equals("Kill")) { + currentTaskID = !enderDragonKilled + ? ModjamMod.r.nextInt( + validMobs.length) + : 1 + ModjamMod.r.nextInt( + validMobs.length - 1); + currentTaskAmount = mobNumbers[currentTaskID][0] + + ModjamMod.r.nextInt( + mobNumbers[currentTaskID][1]); + } else if (currentTask.equals("Burn")) { + currentTaskID = ModjamMod.r + .nextInt(validItems.length); + + if (validItems[currentTaskID] + .getItem() == Items.NETHER_STAR) { + currentTaskAmount = 1; + } else { + currentTaskAmount = 5 + + ModjamMod.r.nextInt(28); + } + + if (validItems[currentTaskID] + .getItem() instanceof ItemBlock) { + currentTaskAmount /= 4; + } + } + + rewardLevels = 5 + ModjamMod.r.nextInt(6); + + markDirty(); + } + + public String getDisadvantage() { + return ModjamMod.disableDisadvantages ? "None" + : currentDisadvantage; + } +} -- cgit v1.2.3