Merge pull request #1111 from GirafiStudios/biome_finder

Fixed biome finder not working on servers, Closes #1109
This commit is contained in:
Adubbz 2017-10-05 10:20:23 +11:00 committed by GitHub
commit 33c595f486

View file

@ -23,12 +23,13 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.translation.I18n;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.biome.Biome; import net.minecraft.world.biome.Biome;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.text.WordUtils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List; import java.util.List;
@ -41,7 +42,7 @@ public class ItemBiomeFinder extends Item
{ {
@Override @Override
@SideOnly(Side.CLIENT) @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;} if (entity == null) {return 0.00F;}
@ -73,12 +74,12 @@ public class ItemBiomeFinder extends Item
} }
} }
public float getFlashingFrame(EntityLivingBase entity) private float getFlashingFrame(EntityLivingBase entity)
{ {
return (entity.ticksExisted % 2 == 0 ? 0.10F : 0.11F); return (entity.ticksExisted % 2 == 0 ? 0.10F : 0.11F);
} }
public float getFrameForPositionRelativeToPlayer(EntityLivingBase entity, int biomePosX, int biomePosZ) private float getFrameForPositionRelativeToPlayer(EntityLivingBase entity, int biomePosX, int biomePosZ)
{ {
double xDiff = (double)biomePosX - entity.posX; double xDiff = (double)biomePosX - entity.posX;
double zDiff = (double)biomePosZ - entity.posZ; double zDiff = (double)biomePosZ - entity.posZ;
@ -95,23 +96,26 @@ public class ItemBiomeFinder extends Item
@Override @Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) @Nonnull
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand)
{ {
ItemStack stack = player.getHeldItem(hand); ItemStack stack = player.getHeldItem(hand);
if (!stack.hasTagCompound()) {stack.setTagCompound(new NBTTagCompound());} if (!stack.hasTagCompound()) {stack.setTagCompound(new NBTTagCompound());}
NBTTagCompound nbt = stack.getTagCompound(); NBTTagCompound nbt = stack.getTagCompound();
if (nbt == null) return new ActionResult<>(EnumActionResult.PASS, stack);
if (nbt.getBoolean("found")) if (nbt.getBoolean("found"))
{ {
return new ActionResult<ItemStack>(EnumActionResult.FAIL, stack); return new ActionResult<>(EnumActionResult.FAIL, stack);
} }
if (nbt.hasKey("searchStarted") && (world.getWorldTime() - nbt.getLong("searchStarted") < 100)) if (nbt.hasKey("searchStarted") && (world.getWorldTime() - nbt.getLong("searchStarted") < 100))
{ {
return new ActionResult<ItemStack>(EnumActionResult.FAIL, stack); return new ActionResult<>(EnumActionResult.FAIL, stack);
} }
if (!nbt.hasKey("biomeIDToFind")) if (!nbt.hasKey("biomeIDToFind"))
{ {
return new ActionResult<ItemStack>(EnumActionResult.FAIL, stack); return new ActionResult<>(EnumActionResult.FAIL, stack);
} }
Biome biomeToFind = Biome.getBiome(nbt.getInteger("biomeIDToFind")); // returns ocean if biomeIDToFind is out of bounds Biome biomeToFind = Biome.getBiome(nbt.getInteger("biomeIDToFind")); // returns ocean if biomeIDToFind is out of bounds
@ -122,12 +126,14 @@ public class ItemBiomeFinder extends Item
if (world.isRemote) if (world.isRemote)
{ {
// client functionality stops here // client functionality stops here
return new ActionResult<ItemStack>(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 // 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 // search for biomeToFind, maximum distance 5000 blocks
BlockPos pos = BiomeUtils.spiralOutwardsLookingForBiome(world, biomeToFind, player.posX, player.posZ); BlockPos pos = BiomeUtils.spiralOutwardsLookingForBiome(world, biomeToFind, player.posX, player.posZ);
@ -135,32 +141,31 @@ public class ItemBiomeFinder extends Item
if (pos == null) if (pos == null)
{ {
// server notifies player that search was unsuccessful // 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 // write not found tag
writeNBTNotFound(nbt); writeNBTNotFound(nbt);
} }
else else
{ {
// server notifies player that search was successful // 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 // write found tag
writeNBTFound(nbt, pos); 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 // 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<ItemStack>(EnumActionResult.PASS, stack.copy()); return new ActionResult<>(EnumActionResult.PASS, stack.copy());
}
return new ActionResult<>(EnumActionResult.PASS, stack);
} }
} private static void sendChatMessage(EntityPlayer player, String msg, Object format, TextFormatting color)
public static void sendChatMessage(EntityPlayer player, String msg, TextFormatting color)
{ {
TextComponentTranslation chatComponent = new TextComponentTranslation(msg); TextComponentTranslation chatComponent = new TextComponentTranslation(msg, format);
chatComponent.getStyle().setColor(color); chatComponent.getStyle().setColor(color);
player.sendMessage(chatComponent); 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.setLong("searchStarted", world.getWorldTime());
@ -168,7 +173,7 @@ public class ItemBiomeFinder extends Item
nbt.removeTag("posZ"); 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.removeTag("searchStarted");
@ -176,7 +181,7 @@ public class ItemBiomeFinder extends Item
nbt.setInteger("posZ", pos.getZ()); 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("searchStarted");
@ -190,12 +195,13 @@ public class ItemBiomeFinder extends Item
{ {
if (!itemStack.hasTagCompound()) {return;} if (!itemStack.hasTagCompound()) {return;}
NBTTagCompound nbt = itemStack.getTagCompound(); 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 Biome biomeToFind = Biome.getBiome(nbt.getInteger("biomeIDToFind")); // returns ocean if biomeIDToFind is out of bounds
if (biomeToFind != null)
{
tooltip.add(biomeToFind.getBiomeName()); tooltip.add(biomeToFind.getBiomeName());
} }
} }
}
} }