
oneofthem999
Members-
Posts
51 -
Joined
-
Last visited
Everything posted by oneofthem999
-
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
I've updated the code by initializing the myStacks field, as well as editing the writeToNBT and readFromNBT methods to use the INBTSerializable methods implemented by ItemStackHandler. Unfortunately, the block still does not hold any items when I try to add items to the block by using a hopper. Here's the RedChestTileEntity class: http://pastebin.com/EE63wT1z And the RedChestBlock class: http://pastebin.com/wR2aDZiZ And finally, the Main class: http://pastebin.com/2NdcKP9q -
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
I've updated the code by initializing the myStacks field, as well as editing the writeToNBT and readFromNBT methods to use the INBTSerializable methods implemented by ItemStackHandler. Unfortunately, the block still does not hold any items when I try to add items to the block by using a hopper. Here's the RedChestTileEntity class: http://pastebin.com/EE63wT1z And the RedChestBlock class: http://pastebin.com/wR2aDZiZ And finally, the Main class: http://pastebin.com/2NdcKP9q -
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
So I created a ItemStacksHandler field, and have overridden the ICapabilityProvider methods, but the block is still not storing any items through use of a hopper. Here's the RedChestTileEntity class: http://pastebin.com/EE63wT1z And the RedChestBlock class: http://pastebin.com/wR2aDZiZ And the Main class: http://pastebin.com/2NdcKP9q -
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
So I created a ItemStacksHandler field, and have overridden the ICapabilityProvider methods, but the block is still not storing any items through use of a hopper. Here's the RedChestTileEntity class: http://pastebin.com/EE63wT1z And the RedChestBlock class: http://pastebin.com/wR2aDZiZ And the Main class: http://pastebin.com/2NdcKP9q -
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
I'm sorry, but I don't quite understand how to do this. I understand the concept of overriding the ICapabilityProvider methods inherited by TileEntity to expose the implementation as a capability, but I don't know how to create a field storing an implementation of ItemStackHandler. -
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
I'm sorry, but I don't quite understand how to do this. I understand the concept of overriding the ICapabilityProvider methods inherited by TileEntity to expose the implementation as a capability, but I don't know how to create a field storing an implementation of ItemStackHandler. -
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
I edited the code using IItemHandler, but it's still not working. Note that I did not stop using the depreciated registerBlock, as I wanted to focus on getting this handled first before adding more classes. If there's still nothing visably wrong, then I'll start debugging line-by-line. Here's the RedChestTileEntity code: http://pastebin.com/EE63wT1z And the RedChestBlock code: http://pastebin.com/wR2aDZiZ And finally, the Main code: http://pastebin.com/2NdcKP9q -
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
I edited the code using IItemHandler, but it's still not working. Note that I did not stop using the depreciated registerBlock, as I wanted to focus on getting this handled first before adding more classes. If there's still nothing visably wrong, then I'll start debugging line-by-line. Here's the RedChestTileEntity code: http://pastebin.com/EE63wT1z And the RedChestBlock code: http://pastebin.com/wR2aDZiZ And finally, the Main code: http://pastebin.com/2NdcKP9q -
So I've been experimenting with IInventory, with the hopes of eventually making a custom size chest, and I came about with the following code. From my, limited, understanding, I thought that this would allow me to create a block that will store items. Of course, there's no GUI for it right now, but I thought that I attached hoppers to the block, and then drop items into it, those items would be stored in the block. Sadly, this is not the case. I'm basically knifing in the dark here, and I am open to suggestions. Here's the code for the RedChestBlock class: http://pastebin.com/wR2aDZiZ And the code for the RedChestTileEntity class: http://pastebin.com/EE63wT1z And finally, the code for the Main class: http://pastebin.com/2NdcKP9q Thank you for your time.
-
[1.9.4] Creating an IInventory using a TileEntity
oneofthem999 replied to oneofthem999's topic in Modder Support
So I've been experimenting with IInventory, with the hopes of eventually making a custom size chest, and I came about with the following code. From my, limited, understanding, I thought that this would allow me to create a block that will store items. Of course, there's no GUI for it right now, but I thought that I attached hoppers to the block, and then drop items into it, those items would be stored in the block. Sadly, this is not the case. I'm basically knifing in the dark here, and I am open to suggestions. Here's the code for the RedChestBlock class: http://pastebin.com/wR2aDZiZ And the code for the RedChestTileEntity class: http://pastebin.com/EE63wT1z And finally, the code for the Main class: http://pastebin.com/2NdcKP9q Thank you for your time. -
[1.9] TileEntity causes null pointer exception.
oneofthem999 replied to oneofthem999's topic in Modder Support
The block works great now. Thank you for your time. P.S. I don't know. I just never considered using an ItemStack for the variable, since I thought I wouldn't need it. -
[1.9] TileEntity causes null pointer exception.
oneofthem999 replied to oneofthem999's topic in Modder Support
The block works great now. Thank you for your time. P.S. I don't know. I just never considered using an ItemStack for the variable, since I thought I wouldn't need it. -
[1.9] TileEntity causes null pointer exception.
oneofthem999 replied to oneofthem999's topic in Modder Support
It does not crash now, but the data members of the crates are not saved when the game is exited. Any ideas on how to proceed? I thought writing and reading from the NBT would solve the problem, but I guess not... Here's the code for the CrateTileEntity: http://pastebin.com/t8ie1G67 By the way, thank you to everyone for your help so far! -
[1.9] TileEntity causes null pointer exception.
oneofthem999 replied to oneofthem999's topic in Modder Support
That makes sense, but I'm still lost at what I should replace the method body with, since I'm not sure how to check whether or not the block does indeed contain a tile entity... -
[1.9] TileEntity causes null pointer exception.
oneofthem999 replied to oneofthem999's topic in Modder Support
So I got rid of the implementation, removed the isBlockContainer, removed the removeTileEntity, registered the tile entity in the main (and set the registration name to the MODID). When the program runs, it now crashes with a null pointer exception when the Crate block is right-clicked. Here's the updated code for the CrateBlock class: http://pastebin.com/tWkDJ7yg Here's the update code for the CrateTileEntity class: http://pastebin.com/t8ie1G67 And here's the crash report: Based on the report, the error must occur because the getTileEntity(world, pos) in the CrateBlock class is returning a null. However, I am unclear as to how to improve the code. -
[1.9] TileEntity causes null pointer exception.
oneofthem999 replied to oneofthem999's topic in Modder Support
Whoops, sorry, the previous post was incomplete. Here's the crash report I keep getting: I thought about using getDescriptionPacket or onDataPacket, but I tried learning them on my own, and tutorials I've been looking at are pretty out-of-date. If anyone could help me out with understanding these methods, or point me to a good tutorial on them, I would be grateful. -
Hello everyone. So I've been experimenting with the idea of creating a crate block, which would store a unlimited amount of just one item. To do this, I would need a tile entity, since I need to store extra variables in the block. However, when I run Minecraft, it gives me a null pointer exception. Here's the code for the tile entity class: public class CrateTileEntity extends TileEntity { private ItemStack item; private int amount; /** * This method will return the item variable. */ public ItemStack getItemStack() { return item; } /** * This method will return the count variable. */ public int getAmount() { return amount; } /** * This method will be called whenever an item is added to the Crate. Items will only be added to this block through * a player's inventory (ItemStack is therefore used). * * Will return true only if a transaction takes place. */ public boolean addItem(ItemStack stack) { //If no item is selected if (stack.getItem() == null) { return false; } //If item of stack is not the correct type... if (stack.getItem() != item.getItem()) { //If item for Crate is not determined if (item == null) { //Set item and amount equal to that of ItemStack item.setItem(stack.getItem()); amount = stack.stackSize; return true; } //Else, return.false else { return false; } } //if item is selected and item is of correct type, then add to Crate else { //Adds the size of stack amount += stack.stackSize; return true; } } // Loading and saving the data to NBT, so the info is not lost when the world is reloaded. /** * This method will read the information from the NBT regarding the item and amount for the Crate. * */ @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); //ItemStack: this.item = ItemStack.loadItemStackFromNBT(compound.getCompoundTag("item")); this.amount = compound.getInteger("amount"); } /** * This method will write the information from the NBT regarding the item and amount for the Crate. * */ @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setInteger("amount", amount); //Item Stack: NBTTagCompound stack = new NBTTagCompound(); this.item.writeToNBT(stack); compound.setTag("item", stack); } } and the code for the block class: package crate; import net.minecraft.block.Block; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; import net.minecraftforge.fml.common.registry.GameRegistry; /** * This class will create a Crate block, which will be able to store an infinite amount of a selected type of item. * However, it can only store one item at a time. * * Created by Haaris Tahir-Kheli on 5/21/2016. */ public class CrateBlock extends Block implements ITileEntityProvider { //Constructor protected CrateBlock() { //Set material for block (through superclass constructor) super(Material.wood); //Set unlocalized name this.setUnlocalizedName("crateBlock"); //Set registry name and register this.setRegistryName("crateblock"); //Set up which creative tab it can be found in this.setCreativeTab(CreativeTabs.tabBlock); //Set hardness this.setHardness(8.0F); //Set blast resistance this.setResistance(5.0F); //Set brightness this.setLightLevel(0.2F); //Set harvest level this.setHarvestLevel("axe", 1); //Set block container to true this.isBlockContainer = true; } /** * This method will establish a new tile entity that will be created and associated with this block */ @Override public TileEntity createNewTileEntity(World worldIn, int meta) { return new CrateTileEntity(); } /** * This method will return the tile entity of the block that was clicked */ private CrateTileEntity getTileEntity(World world, BlockPos pos) { return (CrateTileEntity) world.getTileEntity(pos); } /** * Thie method will trigger as a clean-up for when the block is destroyed. */ @Override public void breakBlock(World world, BlockPos pos, IBlockState state) { super.breakBlock(world, pos, state); world.removeTileEntity(pos); } /** * This method will handle the passing of block events, which are useful for synchronizing data. */ @Override public boolean onBlockEventReceived(World worldIn, BlockPos pos, IBlockState state, int eventID, int eventParam) { super.onBlockEventReceived(worldIn, pos, state, eventID, eventParam); TileEntity tileentity = worldIn.getTileEntity(pos); return (tileentity == null) ? false : tileentity.receiveClientEvent(eventID, eventParam); } /** * This method will trigger whenever the block is right-clicked. */ @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { super.onBlockActivated(world, pos, state, player, hand, heldItem, side, hitX, hitY, hitZ); //Make sure event only takes place on client side if (world.isRemote) { return false; } //Check if player is holding an item if (player.getHeldItemMainhand() == null) { //Print message assessing crate player.addChatComponentMessage(new TextComponentString(TextFormatting.RED + "As you are not holding an item," + " no item will be added to the crate.")); return false; } //Create CrateTileEntity CrateTileEntity tileEntity = getTileEntity(world, pos); //Add item based on Crate tile entity if (tileEntity.addItem(heldItem)) { //Print messages assessing crate player.addChatComponentMessage(new TextComponentString(TextFormatting.AQUA + "The crate holds " + tileEntity.getItemStack().getItem().getUnlocalizedName() + ".")); player.addChatComponentMessage(new TextComponentString(TextFormatting.AQUA + "It holds " + tileEntity.getAmount() + ".")); return true; } //Else will trigger only if item is not the correct one for the crate. else { //Print message assessing crate player.addChatComponentMessage(new TextComponentString(TextFormatting.RED + "This is not a " + heldItem.getUnlocalizedName() + " crate. It is a " + tileEntity.getItemStack().getItem().getUnlocalizedName() + " crate.")); return false; } } } And finally, the main class: @Mod(modid = Main.MODID, version = Main.VERSION) public class Main { public static final String MODID = "CrateMod"; public static final String VERSION = "1.0"; public static void init() { GameRegistry.registerTileEntity(CrateTileEntity.class, "Crate tile entity"); } @EventHandler public static void init(FMLInitializationEvent event) { CrateBlock block = new CrateBlock(); } }
-
I fixed the problem actually. It had to do with by comparison method for check to see if the player landed on the Spring Block. Here's the new code: public class SpringBoard { /** * This method will launch the player up in the air based on the distance fell. Accurate to up to 50 blocks. */ @SubscribeEvent public void goSpringBoard(LivingFallEvent event) { //Check if entity is player if (event.getEntityLiving() instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.getEntityLiving(); //Check if event takes place in client-side if (!player.getEntityWorld().isRemote) { //Store position of landing block int x = player.getPosition().getX(); int y = player.getPosition().getY() - 1; int z = player.getPosition().getZ(); //Check if landing block is a spring block if (player.getEntityWorld().getBlockState(new BlockPos(x, y, z)).getBlock() instanceof Spring) { //Using following approximation for finding velocity right before player hits spring block double acceleration = 0.094; double drag = 0.0028; double velocity = Math.sqrt(2 * acceleration * event.getDistance()); velocity = velocity * (1 + drag * event.getDistance()); //Propel player upward with that velocity player.motionY = velocity; player.velocityChanged = true; event.setCanceled(true); } } } } } However, this spring board only has an upper limit of 50 blocks. After that, no matter how high I jump, I can only return to a height of 50 blocks from the spring. This is curious, since I checked the "velocity" and "motionY" (they are equal) variables right when I land from a height of 150 blocks, and they are much higher than when I jump from a height of 50. However, it always sends be back upwards 50 blocks, if I jump from a height > 50. This is fine for me, as I can always just not use the spring board for heights > 50, but it would be nice to know how I can improve my code.
-
Hello everyone. So in my attempts to learn how to build new blocks, I came up with the Spring and SpringBoard class. The idea is that when you fall on this custom block (the Spring Block), fall damage would be negated, and you will shoot up in the air the distance you fell. In other words, it would act like a slime block, but your momentum would be conserved. However, when I jump on the Spring block, neither the event is canceled, nor is the player sent to the skies. Here is the Spring class, which contains a constructor for the Spring Block. public class Spring extends Block { //Constructor public Spring() { super(Material.iron); this.setUnlocalizedName("spring"); this.setCreativeTab(CreativeTabs.tabBlock); this.setResistance(2.0F); this.setHardness(5.0F); this.setLightLevel(0.3F); } } Here's the SpringBoard class, which contains the method containing the fall event. This method should, if the player lands on a Spring Block, cancel the falling event, so the player takes no damage. It should then send the player upwards by the distance the player originally fell. public class SpringBoard { /** * This method will launch the player up in the air based on the distance fell. */ @SubscribeEvent public void goSpringBoard(LivingFallEvent event) { //Only run code on client-side if (!event.getEntity().getEntityWorld().isRemote) { return; } //Check if entity that fell is a player if (event.getEntity() instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.getEntityLiving(); //Send a message of what block player fell on. String stuff = event.getEntity().getEntityWorld().getBlockState(event.getEntityLiving().getPosition().add(0, -1, 0)).getBlock().getLocalizedName(); player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "You fell on a " + stuff)); //Check if the block that the player fell on is a Spring block if (player.worldObj.getBlockState(event.getEntityLiving().getPosition().add(0, -1, 0)).getBlock() != Main.spring) { player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "I now return.")); return; } else if (player.worldObj.getBlockState(event.getEntityLiving().getPosition().add(0, -1, 0)).getBlock() == Main.spring) { player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "This is a spring block!")); //Stop fall damage event.setCanceled(true); //Send player upward for distance of fall player.addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "" + event.getDamageMultiplier() + " " + event.getDistance())); event.getEntity().motionY = event.getDistance(); } } } } Finally, here's the code for the main file, though I'm not sure if it's needed. I am aware that the registry method I am using is depreciated and outdated, but I thought it would be acceptable for not, since I'm just trying to get a baring on how the creation of new blocks is done. @Mod(modid = Main.MODID, version = Main.VERSION) public class Main { public static final String MODID = "BlocksMods"; public static final String VERSION = "1.0"; public static Block enderBlock; public static Block spring; public static Item enderIngot; @EventHandler public static void init(FMLInitializationEvent event) { enderBlock = new EnderBlock(); enderIngot = new EnderIngot(); spring = new Spring(); GameRegistry.registerBlock(enderBlock, "enderBlock"); GameRegistry.registerItem(enderIngot, "Ender Ingot"); GameRegistry.registerBlock(spring, "sp"); //Events MinecraftForge.EVENT_BUS.register(new SpringBoard()); } } Thank you for your time.
-
So I went to that part of the code. It's supposed to record the positions of two blocks that I left-click and right-click with a wooden axe, so that I may fill the space with another block (based on block id). This class just takes care of the recording of the positions, while another command class fills the space. I didn't suspect this class to give me the error at first because the command works fine, and I can select positions for the fill region with a wooden axe just fine. But, as you pointed out, a null pointer exception is thrown for some reason whenever I right-click with no item in hand. Here's the code for the BlockFiller class. Again, note that left-clicking with no item is fine. It's just right-clicking.: public class BlockFiller { //Declare static variables. These variables will store the coordinates for the two positions for the selected region. public static List<Integer> pos1 = new ArrayList<Integer>(); public static List<Integer> pos2 = new ArrayList<Integer>(); /** * This method will allow the player to choose the positions of the selected region, thereby defining it. It will * focus on choosing the position based on a left click. */ @SubscribeEvent public void choosePositionsLeftClick(PlayerInteractEvent.LeftClickBlock event) { //Check if player has held item. if (event.getEntityPlayer().getHeldItemMainhand().getItem() == null) { return; } //Check if held item is a wooden axe. if (event.getEntityPlayer().getHeldItemMainhand().getItem() != Items.wooden_axe) { return; } //Check if player is in Creative Mode if (!event.getEntityPlayer().capabilities.isCreativeMode) { return; } //Set position 1 equal to coordinates of block interacted with using the left click //Reset pos1 pos1.clear(); //Store coordinates in pos1 pos1.add(event.getPos().getX()); pos1.add(event.getPos().getY()); pos1.add(event.getPos().getZ()); //Show chat message for notification of what spot is stored event.getEntityPlayer().addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "Position 1 set to " + event.getPos().getX() + ", " + event.getPos().getY() + ", " + event.getPos().getZ() + ".")); } /** * This method will allow the player to choose the positions of the selected region, thereby defining it. It will * focus on choosing a position based on a right click */ @SubscribeEvent public void choosePositionsRightHand(PlayerInteractEvent.RightClickBlock event) { //Check if player has held item. if (event.getEntityPlayer().getHeldItemMainhand().getItem() == null) { return; } //Check if held item in main hand is a wooden axe. if (event.getEntityPlayer().getHeldItemMainhand().getItem() != Items.wooden_axe) { return; } //Check if player is in Creative Mode if (!event.getEntityPlayer().capabilities.isCreativeMode) { return; } //Set position 2 equal to coordinates of block interacted with using the right click //Reset pos2 pos2.clear(); //Store coordinates in pos2 pos2.add(event.getPos().getX()); pos2.add(event.getPos().getY()); pos2.add(event.getPos().getZ()); //Show chat message for notification of what spot is stored event.getEntityPlayer().addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "Position 2 set to " + event.getPos().getX() + ", " + event.getPos().getY() + ", " + event.getPos().getZ() + ".")); } Also, I tried commented out the part of the code that triggered the error the first time, which was: //Check if player has held item. if (event.getEntityPlayer().getHeldItemMainhand().getItem() == null) { return; } at line 79. However, when that happened, I got the following error message: at line 85, which is the following: //Check if held item in main hand is a wooden axe. if (event.getEntityPlayer().getHeldItemMainhand().getItem() != Items.wooden_axe) { return; } Thank you for your help so far!
-
Hi everyone. So I'm trying to learn how to add and incorporate new blocks into Minecraft, and I ran into the following problem. The onBlockActivate method causes my game to crash everytime a block is right-clicked. I know it's this method that is giving me trouble, as when I removing, my game runs fine. I have no idea why this would occur, and I was hoping for some insight. Here is the code for the block class: public class EnderBlock extends Block { //Constructor public EnderBlock() { //Set material of block using inherited constructor super(Material.iron); //Set unlocalized name this.setUnlocalizedName("enderBlock"); //Set up in which creative tabe this block can be found this.setCreativeTab(CreativeTabs.tabBlock); //Set blast resistance this.setResistance(5.0F); //Set hardness this.setHardness(10.0F); //Set brightness this.setLightLevel(1.0F); } /** * This method will spawn a ender eye and a message every time the block is interacted with. * * onBlockActivated is a method that triggers when a block is right-clicked. */ @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { //Make sure method is only fully called in client-side if (!world.isRemote) { return false; } //Check to make sure hand is not empty if (player.getHeldItemMainhand() == null) { return false; } //Send message to Chat player.addChatComponentMessage(new TextComponentString(TextFormatting.DARK_RED + "You have clicked on " + "the GREAT ENDERIUM BLOCK OF GREATNESS!!!!!")); //Create ender eye and set location EntityEnderEye eye = new EntityEnderEye(world, pos.getX() + 0.5, pos.getY() + 1.5, pos.getZ() + 0.5); //Set motion of eye upward eye.motionY = 0.1; //Spawn ender eye world.spawnEntityInWorld(eye); //Return true, ending method return true; } }