From e76b5dad2a7ebb1d22b5962d2567989b4e9c1ac9 Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Thu, 4 Oct 2018 02:44:11 +0200 Subject: [PATCH 1/2] Fixed blue fire jar placing Fixed getSubItems being client only Fixed potential NPE when setting creative tabs --- .../biomesoplenty/common/init/ModBlocks.java | 16 +- .../biomesoplenty/common/init/ModItems.java | 7 +- .../common/item/ItemBOPBlock.java | 9 +- .../biomesoplenty/common/item/ItemGem.java | 5 - .../common/item/ItemJarFilled.java | 150 +++++++----------- 5 files changed, 76 insertions(+), 111 deletions(-) diff --git a/src/main/java/biomesoplenty/common/init/ModBlocks.java b/src/main/java/biomesoplenty/common/init/ModBlocks.java index 29f4d4e0f..281da42ac 100644 --- a/src/main/java/biomesoplenty/common/init/ModBlocks.java +++ b/src/main/java/biomesoplenty/common/init/ModBlocks.java @@ -88,6 +88,8 @@ import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fml.common.registry.ForgeRegistries; +import javax.annotation.Nullable; + public class ModBlocks { // TODO: use getDrops() in classes where the drops are very specific, instead of implementing all 3 of quantityDropped() getItemDropped() and damageDropped() @@ -129,9 +131,8 @@ public class ModBlocks hive = registerBlock( new BlockBOPHive(), "hive" ); honey_block = registerBlock( new BlockBOPHoney(), "honey_block" ); - blue_fire = registerBlock( new BlockBOPBlueFire(), "blue_fire" ); - blue_fire.setCreativeTab(null); - + blue_fire = registerBlock( new BlockBOPBlueFire(), "blue_fire", null); + //Material Blocks bamboo_thatching = registerBlock( (new BlockBOPGeneric(Material.WOOD, SoundType.WOOD)).setHardness(2.0F), "bamboo_thatching"); bamboo_thatching.setHarvestLevel("axe", 0); mud_brick_block = registerBlock( (new BlockBOPGeneric()).setResistance(2.0F), "mud_brick_block" ); @@ -353,11 +354,14 @@ public class ModBlocks return registerBlock(block, blockName, tab, true); } - public static Block registerBlock(Block block, String blockName, CreativeTabs tab, boolean registerItemModels) + public static Block registerBlock(Block block, String blockName, @Nullable CreativeTabs tab, boolean registerItemModels) { Preconditions.checkNotNull(block, "Cannot register a null block"); - block.setUnlocalizedName(blockName); - block.setCreativeTab(tab); + block.setUnlocalizedName(blockName); + if (tab != null) + { + block.setCreativeTab(tab); + } if (block instanceof IBOPBlock) { diff --git a/src/main/java/biomesoplenty/common/init/ModItems.java b/src/main/java/biomesoplenty/common/init/ModItems.java index 672b2b95c..bc875ef52 100644 --- a/src/main/java/biomesoplenty/common/init/ModItems.java +++ b/src/main/java/biomesoplenty/common/init/ModItems.java @@ -76,6 +76,8 @@ import net.minecraft.potion.PotionEffect; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.common.registry.ForgeRegistries; +import javax.annotation.Nullable; + public class ModItems { public static void init() @@ -146,8 +148,7 @@ public class ModItems white_dye = registerItem(new Item(), "white_dye"); black_dye = registerItem(new Item(), "black_dye"); - earth = registerItem(new Item(), "earth"); - earth.setCreativeTab(null); + earth = registerItem(new Item(), "earth", null); } public static Item registerItem(Item item, String name) @@ -155,7 +156,7 @@ public class ModItems return registerItem(item, name, CreativeTabBOP.instance); } - public static Item registerItem(Item item, String name, CreativeTabs tab) + public static Item registerItem(Item item, String name, @Nullable CreativeTabs tab) { item.setUnlocalizedName(name); if (tab != null) diff --git a/src/main/java/biomesoplenty/common/item/ItemBOPBlock.java b/src/main/java/biomesoplenty/common/item/ItemBOPBlock.java index f265724cd..2e10a4961 100644 --- a/src/main/java/biomesoplenty/common/item/ItemBOPBlock.java +++ b/src/main/java/biomesoplenty/common/item/ItemBOPBlock.java @@ -8,21 +8,15 @@ package biomesoplenty.common.item; -import java.util.List; - -import com.google.common.collect.ImmutableSet; - import biomesoplenty.common.block.IBOPBlock; import biomesoplenty.common.util.block.BlockStateUtils; +import com.google.common.collect.ImmutableSet; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; public class ItemBOPBlock extends ItemBlock { @@ -45,7 +39,6 @@ public class ItemBOPBlock extends ItemBlock // define the items which will appear in the creative tab (called by ItemBlock class) @Override - @SideOnly(Side.CLIENT) public void getSubItems(CreativeTabs tab, NonNullList subItems) { if (this.isInCreativeTab(tab)) diff --git a/src/main/java/biomesoplenty/common/item/ItemGem.java b/src/main/java/biomesoplenty/common/item/ItemGem.java index 140e764b4..10fe9c9d7 100644 --- a/src/main/java/biomesoplenty/common/item/ItemGem.java +++ b/src/main/java/biomesoplenty/common/item/ItemGem.java @@ -8,15 +8,11 @@ package biomesoplenty.common.item; -import java.util.List; - import biomesoplenty.api.enums.BOPGems; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; public class ItemGem extends Item { @@ -29,7 +25,6 @@ public class ItemGem extends Item // add all the gem types as separate items in the creative tab @Override - @SideOnly(Side.CLIENT) public void getSubItems(CreativeTabs tab, NonNullList subItems) { if (this.isInCreativeTab(tab)) diff --git a/src/main/java/biomesoplenty/common/item/ItemJarFilled.java b/src/main/java/biomesoplenty/common/item/ItemJarFilled.java index 6b24f775c..c3321fb6d 100644 --- a/src/main/java/biomesoplenty/common/item/ItemJarFilled.java +++ b/src/main/java/biomesoplenty/common/item/ItemJarFilled.java @@ -12,22 +12,17 @@ import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.api.item.BOPItems; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.stats.StatList; -import net.minecraft.util.ActionResult; -import net.minecraft.util.EnumActionResult; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; -import net.minecraft.util.IStringSerializable; -import net.minecraft.util.NonNullList; +import net.minecraft.util.*; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; -import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; + +import javax.annotation.Nonnull; +import java.util.Objects; public class ItemJarFilled extends Item { @@ -57,8 +52,7 @@ public class ItemJarFilled extends Item // add all the contents types as separate items in the creative tab @Override - @SideOnly(Side.CLIENT) - public void getSubItems(CreativeTabs tab, NonNullList subItems) + public void getSubItems(@Nonnull CreativeTabs tab, @Nonnull NonNullList subItems) { if (this.isInCreativeTab(tab)) { @@ -89,100 +83,78 @@ public class ItemJarFilled extends Item // get the correct name for this item by looking up the meta value in the JarContents enum @Override + @Nonnull public String getUnlocalizedName(ItemStack stack) { return super.getUnlocalizedName() + "_" + this.getContentsType(stack).getName(); } - - - protected Vec3d getAirPositionInFrontOfPlayer(World world, EntityPlayer player, double targetDistance) + @Override + @Nonnull + public ActionResult onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) { - float cosYaw = MathHelper.cos(-player.rotationYaw * 0.017453292F - (float)Math.PI); - float sinYaw = MathHelper.sin(-player.rotationYaw * 0.017453292F - (float)Math.PI); - float cosPitch = -MathHelper.cos(-player.rotationPitch * 0.017453292F); - float facingX = sinYaw * cosPitch; - float facingY = MathHelper.sin(-player.rotationPitch * 0.017453292F); - float facingZ = cosYaw * cosPitch; + ItemStack heldStack = player.getHeldItem(hand); + RayTraceResult raytraceresult = this.rayTrace(world, player, false); - Vec3d playerEyePosition = new Vec3d(player.posX, player.posY + (double)player.getEyeHeight(), player.posZ); - Vec3d targetPosition = playerEyePosition.addVector((double)facingX * targetDistance, (double)facingY * targetDistance, (double)facingZ * targetDistance); - - // see if there's anything in the way - RayTraceResult hit = world.rayTraceBlocks(playerEyePosition, targetPosition, true, false, false); - if (hit == null) + if (raytraceresult == null) { - return targetPosition; + return new ActionResult<>(EnumActionResult.PASS, heldStack); + } + else if (raytraceresult.typeOfHit != RayTraceResult.Type.BLOCK) + { + return new ActionResult<>(EnumActionResult.PASS, heldStack); } else { - // there's something in the way - return the point just before the collision point - double distance = playerEyePosition.distanceTo(hit.hitVec) * 0.9; - return playerEyePosition.addVector((double)facingX * distance, (double)facingY * distance, (double)facingZ * distance); - } - } - - @Override - public ActionResult onItemRightClick(World world, EntityPlayer player, EnumHand hand) - { - ItemStack stack = player.getHeldItem(hand); - if (world.isRemote) {return new ActionResult(EnumActionResult.FAIL, stack);} - switch (this.getContentsType(stack)) - { - case HONEY: default: - return new ActionResult(EnumActionResult.FAIL, stack); - } - } + BlockPos rayPos = raytraceresult.getBlockPos(); - protected ItemStack emptyJar(ItemStack stack, EntityPlayer player, ItemStack emptyJarStack) - { - if (!player.capabilities.isCreativeMode) - { - stack.setCount(stack.getCount() - 1); - } - - player.addStat(StatList.getObjectUseStats(this)); + if (!world.isBlockModifiable(player, rayPos)) + { + return new ActionResult<>(EnumActionResult.FAIL, heldStack); + } + else + { + boolean isReplaceable = world.getBlockState(rayPos).getBlock().isReplaceable(world, rayPos); + BlockPos pos = isReplaceable && raytraceresult.sideHit == EnumFacing.UP ? rayPos : rayPos.offset(raytraceresult.sideHit); - if (!player.capabilities.isCreativeMode) - { - if (!player.inventory.addItemStackToInventory(emptyJarStack)) - { - player.dropItem(emptyJarStack, false); - } - } - - return stack; - } - - - @Override - public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) - { - ItemStack stack = player.getHeldItem(hand); - if (world.isRemote) {return EnumActionResult.FAIL;} - switch (this.getContentsType(stack)) - { - case BLUE_FIRE: - pos = pos.offset(facing); - ItemStack itemstack = player.getHeldItem(hand); - - if (!player.canPlayerEdit(pos, facing, itemstack)) + if (!player.canPlayerEdit(pos, raytraceresult.sideHit, heldStack)) { - return EnumActionResult.FAIL; + return new ActionResult<>(EnumActionResult.FAIL, heldStack); + } + else if (world.getBlockState(pos).getBlock().isReplaceable(world, pos) && world.getBlockState(pos.down()).isTopSolid()) + { + world.setBlockState(pos, BOPBlocks.blue_fire.getDefaultState()); + this.emptyJar(player, hand); + return new ActionResult<>(EnumActionResult.SUCCESS, heldStack); } else { - if (world.isAirBlock(pos) && world.getBlockState(pos.down()).isTopSolid()) - { - world.setBlockState(pos, BOPBlocks.blue_fire.getDefaultState()); - this.emptyJar(stack, player, new ItemStack(BOPItems.jar_empty)); - return EnumActionResult.SUCCESS; - } - } - - // TODO: are you supposed to be able to pour out honey? How much should you get? Why don't we just use buckets? - case HONEY: default: - return EnumActionResult.SUCCESS; + return new ActionResult<>(EnumActionResult.FAIL, heldStack); + } + } + } + } + + private void emptyJar(EntityPlayer player, EnumHand hand) + { + ItemStack heldStack = player.getHeldItem(hand); + ItemStack emptyJar = new ItemStack(BOPItems.jar_empty); + if (!player.capabilities.isCreativeMode) + { + if (heldStack.isEmpty()) + { + player.setHeldItem(hand, emptyJar); + } + else if (!player.inventory.addItemStackToInventory(emptyJar)) + { + player.dropItem(emptyJar, false); + } + else if (player instanceof EntityPlayerMP) + { + ((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer); + } + heldStack.shrink(1); + player.addStat(Objects.requireNonNull(StatList.getObjectUseStats(this))); } } } From f2eb0052fe9b393c872eff4cfe4786abb1915143 Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Thu, 4 Oct 2018 02:56:25 +0200 Subject: [PATCH 2/2] Only make blue fire jars place blue fire --- .../common/item/ItemJarFilled.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main/java/biomesoplenty/common/item/ItemJarFilled.java b/src/main/java/biomesoplenty/common/item/ItemJarFilled.java index c3321fb6d..2bfccfc80 100644 --- a/src/main/java/biomesoplenty/common/item/ItemJarFilled.java +++ b/src/main/java/biomesoplenty/common/item/ItemJarFilled.java @@ -94,45 +94,48 @@ public class ItemJarFilled extends Item public ActionResult onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) { ItemStack heldStack = player.getHeldItem(hand); - RayTraceResult raytraceresult = this.rayTrace(world, player, false); - - if (raytraceresult == null) + if (getContentsType(heldStack) == JarContents.BLUE_FIRE) { - return new ActionResult<>(EnumActionResult.PASS, heldStack); - } - else if (raytraceresult.typeOfHit != RayTraceResult.Type.BLOCK) - { - return new ActionResult<>(EnumActionResult.PASS, heldStack); - } - else - { - BlockPos rayPos = raytraceresult.getBlockPos(); - - if (!world.isBlockModifiable(player, rayPos)) + RayTraceResult raytraceresult = this.rayTrace(world, player, false); + if (raytraceresult == null) { - return new ActionResult<>(EnumActionResult.FAIL, heldStack); + return new ActionResult<>(EnumActionResult.PASS, heldStack); + } + else if (raytraceresult.typeOfHit != RayTraceResult.Type.BLOCK) + { + return new ActionResult<>(EnumActionResult.PASS, heldStack); } else { - boolean isReplaceable = world.getBlockState(rayPos).getBlock().isReplaceable(world, rayPos); - BlockPos pos = isReplaceable && raytraceresult.sideHit == EnumFacing.UP ? rayPos : rayPos.offset(raytraceresult.sideHit); + BlockPos rayPos = raytraceresult.getBlockPos(); - if (!player.canPlayerEdit(pos, raytraceresult.sideHit, heldStack)) + if (!world.isBlockModifiable(player, rayPos)) { return new ActionResult<>(EnumActionResult.FAIL, heldStack); } - else if (world.getBlockState(pos).getBlock().isReplaceable(world, pos) && world.getBlockState(pos.down()).isTopSolid()) - { - world.setBlockState(pos, BOPBlocks.blue_fire.getDefaultState()); - this.emptyJar(player, hand); - return new ActionResult<>(EnumActionResult.SUCCESS, heldStack); - } else { - return new ActionResult<>(EnumActionResult.FAIL, heldStack); + boolean isReplaceable = world.getBlockState(rayPos).getBlock().isReplaceable(world, rayPos); + BlockPos pos = isReplaceable && raytraceresult.sideHit == EnumFacing.UP ? rayPos : rayPos.offset(raytraceresult.sideHit); + + if (!player.canPlayerEdit(pos, raytraceresult.sideHit, heldStack)) + { + return new ActionResult<>(EnumActionResult.FAIL, heldStack); + } + else if (world.getBlockState(pos).getBlock().isReplaceable(world, pos) && world.getBlockState(pos.down()).isTopSolid()) + { + world.setBlockState(pos, BOPBlocks.blue_fire.getDefaultState()); + this.emptyJar(player, hand); + return new ActionResult<>(EnumActionResult.SUCCESS, heldStack); + } + else + { + return new ActionResult<>(EnumActionResult.FAIL, heldStack); + } } } } + return new ActionResult<>(EnumActionResult.PASS, heldStack); } private void emptyJar(EntityPlayer player, EnumHand hand)