From 00456bd22420805873176e96880a3a96518cd700 Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Wed, 4 Oct 2017 19:48:18 +0200 Subject: [PATCH 1/2] Fixed biome finder not working on servers Additional null check, to hopefully prevent issues in the future Some minor cleanup --- .../common/item/ItemBiomeFinder.java | 76 ++++++++++--------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java b/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java index 963cb4308..0ce0f241f 100644 --- a/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java +++ b/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java @@ -23,12 +23,13 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextFormatting; -import net.minecraft.util.text.translation.I18n; import net.minecraft.world.World; import net.minecraft.world.biome.Biome; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import org.apache.commons.lang3.text.WordUtils; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.List; @@ -41,7 +42,7 @@ public class ItemBiomeFinder extends Item { @Override @SideOnly(Side.CLIENT) - public float apply(ItemStack stack, World world, EntityLivingBase entity) + public float apply(@Nonnull ItemStack stack, World world, EntityLivingBase entity) { if (entity == null) {return 0.00F;} @@ -73,12 +74,12 @@ public class ItemBiomeFinder extends Item } } - public float getFlashingFrame(EntityLivingBase entity) + float getFlashingFrame(EntityLivingBase entity) { return (entity.ticksExisted % 2 == 0 ? 0.10F : 0.11F); } - public float getFrameForPositionRelativeToPlayer(EntityLivingBase entity, int biomePosX, int biomePosZ) + float getFrameForPositionRelativeToPlayer(EntityLivingBase entity, int biomePosX, int biomePosZ) { double xDiff = (double)biomePosX - entity.posX; double zDiff = (double)biomePosZ - entity.posZ; @@ -95,90 +96,94 @@ public class ItemBiomeFinder extends Item @Override - public ActionResult onItemRightClick(World world, EntityPlayer player, EnumHand hand) + @Nonnull + public ActionResult onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) { ItemStack stack = player.getHeldItem(hand); if (!stack.hasTagCompound()) {stack.setTagCompound(new NBTTagCompound());} NBTTagCompound nbt = stack.getTagCompound(); - + + if (nbt == null) return new ActionResult<>(EnumActionResult.PASS, stack); + if (nbt.getBoolean("found")) { - return new ActionResult(EnumActionResult.FAIL, stack); + return new ActionResult<>(EnumActionResult.FAIL, stack); } if (nbt.hasKey("searchStarted") && (world.getWorldTime() - nbt.getLong("searchStarted") < 100)) { - return new ActionResult(EnumActionResult.FAIL, stack); + return new ActionResult<>(EnumActionResult.FAIL, stack); } if (!nbt.hasKey("biomeIDToFind")) { - return new ActionResult(EnumActionResult.FAIL, stack); + return new ActionResult<>(EnumActionResult.FAIL, stack); } Biome biomeToFind = Biome.getBiome(nbt.getInteger("biomeIDToFind")); // returns ocean if biomeIDToFind is out of bounds - + // both client and server set the 'searching' tag - client to update the rendering, server so it can't be used again too quickly writeNBTSearching(nbt, world); - + // carry out the search on the server, not the client if (world.isRemote) { // client functionality stops here - return new ActionResult(EnumActionResult.PASS, stack); + return new ActionResult<>(EnumActionResult.PASS, stack); } - else - { + else if (biomeToFind != null && biomeToFind.getRegistryName() != null) + { + String biomeName = WordUtils.capitalize(biomeToFind.getRegistryName().getResourcePath()); + // server notifies player that search is starting - sendChatMessage(player, I18n.translateToLocalFormatted("biome_finder.searching",biomeToFind.getBiomeName()), TextFormatting.DARK_PURPLE); - + sendChatMessage(player, "biome_finder.searching", biomeName, TextFormatting.DARK_PURPLE); + // search for biomeToFind, maximum distance 5000 blocks BlockPos pos = BiomeUtils.spiralOutwardsLookingForBiome(world, biomeToFind, player.posX, player.posZ); - + if (pos == null) { // server notifies player that search was unsuccessful - sendChatMessage(player, I18n.translateToLocalFormatted("biome_finder.not_found",biomeToFind.getBiomeName()), TextFormatting.RED); + sendChatMessage(player, "biome_finder.not_found", biomeName, TextFormatting.RED); // write not found tag writeNBTNotFound(nbt); } else { // server notifies player that search was successful - sendChatMessage(player, I18n.translateToLocalFormatted("biome_finder.found",biomeToFind.getBiomeName()), TextFormatting.GREEN); + sendChatMessage(player, "biome_finder.found", biomeName, TextFormatting.GREEN); // write found tag writeNBTFound(nbt, pos); } // It is necessary for the server to return a copy of the stack to make sure that the client successfully replaces it in the inventory - return new ActionResult(EnumActionResult.PASS, stack.copy()); - + return new ActionResult<>(EnumActionResult.PASS, stack.copy()); } - + return new ActionResult<>(EnumActionResult.PASS, stack); } - - public static void sendChatMessage(EntityPlayer player, String msg, TextFormatting color) + + private static void sendChatMessage(EntityPlayer player, String msg, Object format, TextFormatting color) { - TextComponentTranslation chatComponent = new TextComponentTranslation(msg); + TextComponentTranslation chatComponent = new TextComponentTranslation(msg, format); chatComponent.getStyle().setColor(color); player.sendMessage(chatComponent); } - public static void writeNBTSearching(NBTTagCompound nbt, World world) + private static void writeNBTSearching(NBTTagCompound nbt, World world) { - nbt.setBoolean("found",false); + nbt.setBoolean("found", false); nbt.setLong("searchStarted", world.getWorldTime()); nbt.removeTag("posX"); nbt.removeTag("posZ"); } - public static void writeNBTFound(NBTTagCompound nbt, BlockPos pos) + private static void writeNBTFound(NBTTagCompound nbt, BlockPos pos) { - nbt.setBoolean("found",true); + nbt.setBoolean("found", true); nbt.removeTag("searchStarted"); nbt.setInteger("posX", pos.getX()); nbt.setInteger("posZ", pos.getZ()); } - public static void writeNBTNotFound(NBTTagCompound nbt) + private static void writeNBTNotFound(NBTTagCompound nbt) { - nbt.setBoolean("found",false); + nbt.setBoolean("found", false); nbt.removeTag("searchStarted"); nbt.removeTag("posX"); nbt.removeTag("posZ"); @@ -190,12 +195,13 @@ public class ItemBiomeFinder extends Item { if (!itemStack.hasTagCompound()) {return;} NBTTagCompound nbt = itemStack.getTagCompound(); - if (nbt.hasKey("biomeIDToFind")) + if (nbt != null && nbt.hasKey("biomeIDToFind")) { Biome biomeToFind = Biome.getBiome(nbt.getInteger("biomeIDToFind")); // returns ocean if biomeIDToFind is out of bounds - tooltip.add(biomeToFind.getBiomeName()); + if (biomeToFind != null) + { + tooltip.add(biomeToFind.getBiomeName()); + } } } - - } \ No newline at end of file From 478130c26ad54541b053d26a9feb151af00e89df Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Thu, 5 Oct 2017 01:19:23 +0200 Subject: [PATCH 2/2] Specified methods access levels, rather than having them package-private --- src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java b/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java index 0ce0f241f..83f6da475 100644 --- a/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java +++ b/src/main/java/biomesoplenty/common/item/ItemBiomeFinder.java @@ -74,12 +74,12 @@ public class ItemBiomeFinder extends Item } } - float getFlashingFrame(EntityLivingBase entity) + private float getFlashingFrame(EntityLivingBase entity) { return (entity.ticksExisted % 2 == 0 ? 0.10F : 0.11F); } - float getFrameForPositionRelativeToPlayer(EntityLivingBase entity, int biomePosX, int biomePosZ) + private float getFrameForPositionRelativeToPlayer(EntityLivingBase entity, int biomePosX, int biomePosZ) { double xDiff = (double)biomePosX - entity.posX; double zDiff = (double)biomePosZ - entity.posZ;