# rounding floats to nearest 0.2

## Recommended Posts

I'm using this code to determine temperature

```if(isPowered())
temperature += 1.0F;
temperature -= getCoolant(worldObj, xCoord, yCoord - 1, zCoord);
temperature = ((float) Math.ceil(temperature * 5) / 5.0F);
if(temperature < 0.0F)
temperature = 0.0F;```

and here's the getCoolant() method

```public float getCoolant(World world, int x, int y, int z)
{
float coolant = 0.0F;
for(int i = -1; i < 2; i++)
{
for(int j = -1; j < 2; j++)
{
int block = world.getBlockId(x + i, y, z + j);
if(block == Block.waterMoving.blockID || block == Block.waterStill.blockID)
{
coolant += 0.2F;
}
else if(block == Block.lavaMoving.blockID || block == Block.lavaStill.blockID)
{
coolant -= 0.2F;
}
else if(Block.blocksList[block] != null && Block.blocksList[block] instanceof BlockFluidBase)
{
BlockFluidBase liquid = (BlockFluidBase) Block.blocksList[block];
Fluid fluid = liquid.getFluid();
if(fluid != null)
{
if((float) fluid.getTemperature(world, x + i, y, z + j) < getTemperature())
coolant += 0.2F;
else
coolant -= 0.2F;
}
}
}
}
return coolant;
}```

but in the gui it shows up as x.6 then x.2 then x.0 then x.6 then x.2 then x.0 then... so I was wondering if anyone knows how I could get this to work better correctly? currently 2/9 spots are obstructed if that helps. also, the liquid being used is water, so the BlockFluidBase part of the method doesn't matter.

##### Share on other sites

If this is simply a Java question, this would be a much better fit for StackOverflow. In fact, I'll bet you anything it's already been answered there!

I like to make mods, just like you. Here's one worth checking out

##### Share on other sites

funny thing, I was looking on the internet for such a solution and didn't find a working one. :-( I'll try again. but let me know if you come up with anything.

##### Share on other sites

I figured out a solution on my own.

##### Share on other sites

I figured out a solution on my own.

Would you mind sharing the concept behind your solution? I can't be bothered to work it out from scratch

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there

##### Share on other sites

well logicly its roughly

float x = wtv;

round = ((int)((wtv+0.1)*5))/5f;

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

##### Share on other sites

what he said. his way is more efficient than what I had.

##### Share on other sites

you need flow control if the temperature is negative though :\ like i said that "roughly" the formula

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

##### Share on other sites

I do that later in the method.

##### Share on other sites

or dont have negative temperature (degree kelvin ?)

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

##### Share on other sites

that's basically what I do, if it's less than 0 I set it to 0

##### Share on other sites

problem solved !

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Yep!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

• ### Posts

• I am getting an error message that my mods and my server are not on the same minecraft version when they clearly are. My server and my mods are on 1.20.1 and the error says that the server is 1.20 even though it's on forge 47.2.30.
• .Omg, I'm so freaking blind I didn't notice that, it works now, thank you so much @Xoroshio
• On the one off the last lines of code you have put && boots.isEmpty() Instead of && !boots.isEmpty() is that a typo?
• I'm trying to apply effects to the emerald armor but it doesn't work, I read that the onArmorTick() method doesn't work anymore, but I tried with inventoryTick, onInventoryTick and onArmorTick but they don't apply the effect either, I don't know if I'm writing it wrong, so I need help to fix it. This is the code in question: public class ModArmorItem extends ArmorItem{ private static final Map<ArmorMaterial, MobEffectInstance> MATERIAL_TO_EFFECT_MAP = (new ImmutableMap.Builder<ArmorMaterial, MobEffectInstance>()) .put(ModArmorMaterials.EMERALD, new MobEffectInstance( MobEffects.HERO_OF_THE_VILLAGE, 200, 1, false, false, true)) .build(); public ModArmorItem(ArmorMaterial pMaterial, Type pType, Properties pProperties) { super(pMaterial, pType, pProperties); } @Override public void onArmorTick(ItemStack stack, Level world, Player player){ if (!world.isClientSide()) { if (hasFullSuitOfArmorOn(player)) { evaluateArmorEffects(player); } } } private void evaluateArmorEffects(Player player) { for (Map.Entry<ArmorMaterial,MobEffectInstance> entry : MATERIAL_TO_EFFECT_MAP.entrySet()){ ArmorMaterial mapArmorMaterial = entry.getKey(); MobEffectInstance mapStatusEffect = entry.getValue(); if (hasCorrectArmorOn(mapArmorMaterial, player)) { addStatusEffectForMaterial(player, mapArmorMaterial, mapStatusEffect); } } } private void addStatusEffectForMaterial(Player player, ArmorMaterial mapArmorMaterial, MobEffectInstance mapStatusEffect) { boolean hasPlayerEffect = player.hasEffect(mapStatusEffect.getEffect()); if (hasCorrectArmorOn(mapArmorMaterial, player) && !hasPlayerEffect) { player.addEffect(new MobEffectInstance(mapStatusEffect)); } } private boolean hasCorrectArmorOn(ArmorMaterial material, Player player) { for (ItemStack armorStack : player.getInventory().armor){ if (!(armorStack.getItem() instanceof ArmorItem)) { return false; } } ArmorItem helmet = ((ArmorItem)player.getInventory().getArmor(3).getItem()); ArmorItem brestplace = ((ArmorItem)player.getInventory().getArmor(2).getItem()); ArmorItem leggins = ((ArmorItem)player.getInventory().getArmor(1).getItem()); ArmorItem boots = ((ArmorItem)player.getInventory().getArmor(0).getItem()); return helmet.getMaterial() == material && brestplace.getMaterial() == material && leggins.getMaterial() == material && boots.getMaterial() == material; } private boolean hasFullSuitOfArmorOn(Player player){ ItemStack helmet = player.getInventory().getArmor(3); ItemStack breastplace = player.getInventory().getArmor(2); ItemStack leggins = player.getInventory().getArmor(1); ItemStack boots = player.getInventory().getArmor(0); return !helmet.isEmpty() && !breastplace.isEmpty() && !leggins.isEmpty() && boots.isEmpty(); } } And here is the armor   public static final RegistryObject<Item> EMERALD_HELMET = ITEMS.register( "emerald_helmet", () -> new ModArmorItem(ModArmorMaterials.EMERALD, ArmorItem.Type.HELMET, new Item.Properties()) ); public static final RegistryObject<Item> EMERALD_CHESTPLATE = ITEMS.register( "emerald_chestplate", () -> new ModArmorItem(ModArmorMaterials.EMERALD, ArmorItem.Type.CHESTPLATE, new Item.Properties()) ); public static final RegistryObject<Item> EMERALD_LEGGINS = ITEMS.register( "emerald_leggins", () -> new ModArmorItem(ModArmorMaterials.EMERALD, ArmorItem.Type.LEGGINGS, new Item.Properties()) ); public static final RegistryObject<Item> EMERALD_BOOTS = ITEMS.register( "emerald_boots", () -> new ModArmorItem(ModArmorMaterials.EMERALD, ArmorItem.Type.BOOTS, new Item.Properties()) ); I hope you can help me. Thanks.
• so ive been having this issue for a little bit, and have honestly no clue on how to fix it. I'm using minecraft 1.20.1 for clarification.  Here is the latest log: https://pastebin.com/neMuxELv

• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...