From ea575db71c9f30d50d2b9cb9d3d3ce543ae4f65b Mon Sep 17 00:00:00 2001 From: Adubbz Date: Mon, 18 Jan 2016 21:01:37 +1100 Subject: [PATCH] Fixed achievement saving and a crash due to positioning --- .../handler/AchievementEventHandler.java | 108 +++++++++--------- .../common/init/ModAchievements.java | 6 +- 2 files changed, 59 insertions(+), 55 deletions(-) diff --git a/src/main/java/biomesoplenty/common/handler/AchievementEventHandler.java b/src/main/java/biomesoplenty/common/handler/AchievementEventHandler.java index 0b4c63c60..52ba5d98f 100644 --- a/src/main/java/biomesoplenty/common/handler/AchievementEventHandler.java +++ b/src/main/java/biomesoplenty/common/handler/AchievementEventHandler.java @@ -36,91 +36,95 @@ import com.google.common.collect.Sets; public class AchievementEventHandler { private static final Set BOP_BIOMES_TO_EXPLORE = Sets.union(BOPBiomes.REG_INSTANCE.getPresentBiomes(), BiomeGenBase.explorationBiomesList); - - @SubscribeEvent - public void onItemPickup(PlayerEvent.ItemPickupEvent event) - { - Item item = event.pickedUp.getEntityItem().getItem(); - Block block = Block.getBlockFromItem(item); - EntityPlayer player = event.player; - - if (block != null && block instanceof BlockBOPLog) - { - event.player.addStat(AchievementList.mineWood, 1); - } - - //Flower Child Achievement - if (block != null && block instanceof BlockBOPFlower) - { - player.addStat(BOPAchievements.obtain_flowers, 1); - } - - //Berry Good Achievement + + @SubscribeEvent + public void onItemPickup(PlayerEvent.ItemPickupEvent event) + { + //Only add achievements on the server + if (event.player.worldObj.isRemote) + return; + + Item item = event.pickedUp.getEntityItem().getItem(); + Block block = Block.getBlockFromItem(item); + EntityPlayer player = event.player; + + if (block != null && block instanceof BlockBOPLog) + { + player.triggerAchievement(AchievementList.mineWood); + } + + //Flower Child Achievement + if (block != null && block instanceof BlockBOPFlower) + { + player.triggerAchievement(BOPAchievements.obtain_flowers); + } + + //Berry Good Achievement if (item != null && item == BOPItems.berries) { - player.addStat(BOPAchievements.obtain_berry, 1); + player.triggerAchievement(BOPAchievements.obtain_berry); } - + //Totally Coral Achievement if (block != null && block == BOPBlocks.coral) { - player.addStat(BOPAchievements.obtain_coral, 1); + player.triggerAchievement(BOPAchievements.obtain_coral); } - + //Rather Thorny Achievement if (block != null && block == BlockBOPPlant.paging.getBlock(BOPPlants.THORN)) { - player.addStat(BOPAchievements.obtain_thorn, 1); + player.triggerAchievement(BOPAchievements.obtain_thorn); } - + //Pick Your Poison Achievement if (block != null && block == BlockBOPPlant.paging.getBlock(BOPPlants.POISONIVY)) { - player.addStat(BOPAchievements.obtain_poison_ivy, 1); + player.triggerAchievement(BOPAchievements.obtain_poison_ivy); } - - //Stalk Market Achievement + + //Stalk Market Achievement if (item != null && item == BOPItems.turnip) { - player.addStat(BOPAchievements.obtain_turnip, 1); + player.triggerAchievement(BOPAchievements.obtain_turnip); } - + //Honeycomb Crunch Achievement if (item != null && item == BOPItems.honeycomb) { - player.addStat(BOPAchievements.obtain_honeycomb, 1); + player.triggerAchievement(BOPAchievements.obtain_honeycomb); } - + //Don't Breathe This Achievement if (item != null && item == BOPItems.pixie_dust) { - player.addStat(BOPAchievements.obtain_pixie_dust, 1); + player.triggerAchievement(BOPAchievements.obtain_pixie_dust); } - + //Far Out Achievement if (item != null && item == BOPItems.crystal_shard) { - player.addStat(BOPAchievements.obtain_celestial_crystal, 1); + player.triggerAchievement(BOPAchievements.obtain_celestial_crystal); } - } - - @SubscribeEvent - public void onPlayerUpdate(LivingUpdateEvent event) - { - if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) - { - EntityPlayerMP player = (EntityPlayerMP)event.entity; - - //Check every five seconds if the player has entered a new biome, if they haven't already gotten the achievement + } + + @SubscribeEvent + public void onPlayerUpdate(LivingUpdateEvent event) + { + if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) + { + EntityPlayerMP player = (EntityPlayerMP)event.entity; + + //Check every five seconds if the player has entered a new biome, if they haven't already gotten the achievement if (player.ticksExisted % 20 * 5 == 0 && !player.getStatFile().hasAchievementUnlocked(BOPAchievements.explore_all_biomes)) { this.updateBiomesExplored(player); } - } - } - - public void updateBiomesExplored(EntityPlayerMP player) - { + } + } + + public void updateBiomesExplored(EntityPlayerMP player) + { BiomeGenBase currentBiome = player.worldObj.getBiomeGenForCoords(new BlockPos(MathHelper.floor_double(player.posX), 0, MathHelper.floor_double(player.posZ))); String biomeName = currentBiome.biomeName; //Get a list of the current explored biomes @@ -169,5 +173,5 @@ public class AchievementEventHandler player.triggerAchievement(BOPAchievements.explore_all_biomes); } } - } + } } diff --git a/src/main/java/biomesoplenty/common/init/ModAchievements.java b/src/main/java/biomesoplenty/common/init/ModAchievements.java index e0bda32e0..28c4fd689 100644 --- a/src/main/java/biomesoplenty/common/init/ModAchievements.java +++ b/src/main/java/biomesoplenty/common/init/ModAchievements.java @@ -24,7 +24,6 @@ import net.minecraft.stats.Achievement; import net.minecraftforge.common.AchievementPage; import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.api.item.BOPItems; -import biomesoplenty.common.block.BlockBOPGrass; import biomesoplenty.common.block.BlockBOPPlant; import biomesoplenty.common.enums.BOPPlants; @@ -50,13 +49,14 @@ public class ModAchievements obtain_honeycomb = addAchievement("achievement.obtain_honeycomb", "obtain_honeycomb", 3, -3, new ItemStack(BOPItems.honeycomb), obtain_coral); obtain_pixie_dust = addAchievement("achievement.obtain_pixie_dust", "obtain_pixie_dust", 5, -4, new ItemStack(BOPItems.pixie_dust), obtain_honeycomb); obtain_celestial_crystal = addAchievement("achievement.obtain_celestial_crystal", "obtain_celestial_crystal", 7, -1, new ItemStack(BOPItems.crystal_shard), obtain_pixie_dust); - craft_ornamental_artifact = addAchievement("achievement.craft_ornamental_artifact", "craft_ornamental_artifact", 3, -8, new ItemStack(BOPItems.gem), obtain_pixie_dust); - explore_all_biomes = addAchievement("achievement.explore_all_biomes", "explore_all_biomes", 0, -8, new ItemStack(BOPItems.earth), craft_ornamental_artifact).setSpecial(); + craft_ornamental_artifact = addAchievement("achievement.craft_ornamental_artifact", "craft_ornamental_artifact", 3, -6, new ItemStack(BOPItems.gem), obtain_pixie_dust); + explore_all_biomes = addAchievement("achievement.explore_all_biomes", "explore_all_biomes", 0, -7, new ItemStack(BOPItems.earth), craft_ornamental_artifact).setSpecial(); } private static Achievement addAchievement(String unlocalizedName, String identifier, int column, int row, ItemStack iconStack, Achievement parent) { Achievement achievement = new Achievement(unlocalizedName, identifier, column, row, iconStack, parent); + achievement.registerStat(); achievementPage.getAchievements().add(achievement); return achievement; }