Posted September 17, 20187 yr I have a block that shoots an arrow every time it is activated by right click whilst holding cobblestone. The arrows it spawn, however, bounce off of any entity it hits. Any idea what might cause this, and possible fixes? Thanks! Code: public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float side, float hitX, float hitY) { if(playerIn.getHeldItemMainhand().getItem().equals(Item.getItemFromBlock(Blocks.COBBLESTONE))) { if(facing == ((EnumFacing)state.getValue(FACING))) { return true; } if(rechargeTimer == 0) { if(worldIn.isRemote) { EntityTippedArrow entityarrow; if(((EnumFacing)state.getValue(FACING)).getIndex() ==2){ entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+0.5, ((double)pos.getY()+0.9), (double)pos.getZ()-0.1); }else if(((EnumFacing)state.getValue(FACING)).getIndex() ==3) { entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+0.5, ((double)pos.getY()+0.9), (double)pos.getZ()+1.1); }else if(((EnumFacing)state.getValue(FACING)).getIndex() ==4) { entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()-0.1, ((double)pos.getY()+0.9), (double)pos.getZ()+0.5); }else { entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+1.1, ((double)pos.getY()+0.9), (double)pos.getZ()+0.5); } entityarrow.setPotionEffect(new ItemStack(Items.ARROW)); float f = -MathHelper.sin(playerIn.rotationYaw * 0.017453292F) * MathHelper.cos(playerIn.rotationPitch * 0.017453292F); float f1 = -MathHelper.sin(playerIn.rotationPitch * 0.017453292F); float f2 = MathHelper.cos(playerIn.rotationYaw * 0.017453292F) * MathHelper.cos(playerIn.rotationPitch * 0.017453292F); entityarrow.setIsCritical(true); entityarrow.setDamage(30.0); entityarrow.setKnockbackStrength(1); entityarrow.shoot((double)f, (double)f1+0.05, (double)f2, 3.0F, 1.0F); worldIn.spawnEntity(entityarrow); this.rechargeTimer =1; } if(!playerIn.capabilities.isCreativeMode) { playerIn.getActiveItemStack().shrink(1); } worldIn.playSound(playerIn, pos, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.NEUTRAL, 0.5F, 0.4F / (0.2F + 0.8F)); } worldIn.scheduleUpdate(pos, this, 10); } return true; } Edited September 17, 20187 yr by Lumby
September 17, 20187 yr 13 minutes ago, Lumby said: if(worldIn.isRemote) { This would be your issue. You are only spawning your arrow on the client. You need to do the opposite. Also you can't do this 13 minutes ago, Lumby said: if(rechargeTimer == 0) { Blocks are singletons thus every block in the world that shares this class shares all the variables of that class. You must use a TileEntity if you want to store the data per-block.
September 17, 20187 yr Author Thank you so much for your help! Sorry I'm still new to modding so don't really have a clear idea which things should be on server or client. 1 hour ago, V0idWa1k3r said: Blocks are singletons thus every block in the world that shares this class shares all the variables of that class. You must use a TileEntity if you want to store the data per-block. Regarding this, I've made some adjustments to my block class and created a new TileEntity to store the cooldown. Essentially, that cooldown is to prevent spamming the shots and give the block a steady rate of fire. At first it worked fine, but occasionally when I tried to load the world it gives an exception and crashes: Quote A TileEntity type com.crumbletheundead.blocks.machines.Trebuchet.TileEntityTrebuchet has throw an exception trying to write state. It will not persist. Report this to the mod author java.lang.RuntimeException: class com.crumbletheundead.blocks.machines.Trebuchet.TileEntityTrebuchet is missing a mapping! This is a bug! Any ideas what this might be? This is my updated block class: public class TrebuchetBlock extends BlockBase implements ITileEntityProvider{ public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public TrebuchetBlock(String name, Material material) { super(name, material); setSoundType(SoundType.STONE); setHardness(18.0F); setResistance(18.0F); setHarvestLevel("pickaxe",3); setCreativeTab(CreativeTabs.REDSTONE); } public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand){ if (!worldIn.isRemote){ if(worldIn.getTileEntity(pos) instanceof TileEntityTrebuchet) { ((TileEntityTrebuchet)worldIn.getTileEntity(pos)).setCooldown(0); } } } public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float side, float hitX, float hitY){ if(playerIn.getHeldItemMainhand().getItem().equals(Item.getItemFromBlock(Blocks.COBBLESTONE))) { if(facing == ((EnumFacing)state.getValue(FACING))) { return true; } if((worldIn.getTileEntity(pos)) instanceof TileEntityTrebuchet) { if(((TileEntityTrebuchet)worldIn.getTileEntity(pos)).getCooldown()==0) { if(!worldIn.isRemote) { EntityTippedArrow entityarrow; if(((EnumFacing)state.getValue(FACING)).getIndex() ==2){ entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+0.5, ((double)pos.getY()+0.9), (double)pos.getZ()-0.2); }else if(((EnumFacing)state.getValue(FACING)).getIndex() ==3) { entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+0.5, ((double)pos.getY()+0.9), (double)pos.getZ()+1.2); }else if(((EnumFacing)state.getValue(FACING)).getIndex() ==4) { entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()-0.2, ((double)pos.getY()+0.9), (double)pos.getZ()+0.5); }else{ entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+1.2, ((double)pos.getY()+0.9), (double)pos.getZ()+0.5); } entityarrow.setPotionEffect(new ItemStack(Items.ARROW)); float f = -MathHelper.sin(playerIn.rotationYaw * 0.017453292F) * MathHelper.cos(playerIn.rotationPitch * 0.017453292F); float f1 = -MathHelper.sin(playerIn.rotationPitch * 0.017453292F); float f2 = MathHelper.cos(playerIn.rotationYaw * 0.017453292F) * MathHelper.cos(playerIn.rotationPitch * 0.017453292F); entityarrow.setIsCritical(true); entityarrow.setDamage(30.0); entityarrow.setKnockbackStrength(1); entityarrow.shoot((double)f, (double)f1+0.05, (double)f2, 3.0F, 1.0F); worldIn.spawnEntity(entityarrow); ((TileEntityTrebuchet)worldIn.getTileEntity(pos)).setCooldown(1); } if(!playerIn.capabilities.isCreativeMode) { playerIn.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).shrink(1); } worldIn.playSound(playerIn, pos, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.NEUTRAL, 0.5F, 0.4F / (0.2F + 0.8F)); } } worldIn.scheduleUpdate(pos, this, 60); } return true; } protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {FACING}); } public IBlockState withRotation(IBlockState state, Rotation rot) { return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING))); } public IBlockState withMirror(IBlockState state, Mirror mirrorIn) { return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING))); } public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()); } public int getMetaFromState(IBlockState state) { int i = 0; i = i | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex(); return i; } public IBlockState getStateFromMeta(int meta) { return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)); } public TileEntity createNewTileEntity(World worldIn, int meta) { TileEntityTrebuchet tileEntityTrebuchet = new TileEntityTrebuchet(); return tileEntityTrebuchet; } @Override public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { if(stack.hasDisplayName()) { TileEntity tileentity = worldIn.getTileEntity(pos); if(tileentity instanceof TileEntityTrebuchet){ ((TileEntityTrebuchet)tileentity).setCustomName(stack.getDisplayName()); ((TileEntityTrebuchet)tileentity).setCooldown(0); } } } } And TileEntity Class: public class TileEntityTrebuchet extends TileEntity{ private String customName; public int cooldown; public boolean hasCustomName() { return this.customName != null && !this.customName.isEmpty(); } public void setCustomName(String customName) { this.customName = customName; } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); if (compound.hasKey("cooldown")) { cooldown = compound.getInteger("cooldown"); } if(compound.hasKey("CustomName", 8)) { this.customName = compound.getString("CustomName"); } } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setInteger("cooldown", cooldown); if(compound.hasKey("CustomName", 8)) { compound.setString("CustomName", this.customName); } return compound; } public int getCooldown() { return this.cooldown; } public void setCooldown(int cd) { this.cooldown= cd; } public boolean isUsableByPlayer(EntityPlayer player) { return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D; } } Edited September 17, 20187 yr by Lumby
September 18, 20187 yr Author Thanks for the suggestion! I've registered the tileEntity and added the necessary methods and annotations as you mentioned. The world no longer crashes, but no tileEntities are created every time I place a block. Any idea what might cause this? Block class: public class TrebuchetBlock extends BlockBase { public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL); public TrebuchetBlock(String name, Material material) { super(name, material); setSoundType(SoundType.STONE); setHardness(18.0F); setResistance(18.0F); setHarvestLevel("pickaxe",3); setCreativeTab(CreativeTabs.REDSTONE); } public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand){ if (!worldIn.isRemote){ if(worldIn.getTileEntity(pos) instanceof TileEntityTrebuchet) { ((TileEntityTrebuchet)worldIn.getTileEntity(pos)).setCooldown(0); } } } public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float side, float hitX, float hitY){ if(playerIn.getHeldItemMainhand().getItem().equals(Item.getItemFromBlock(Blocks.COBBLESTONE))) { if((worldIn.getTileEntity(pos)) instanceof TileEntityTrebuchet) { worldIn.playSound(playerIn, pos, SoundEvents.BLOCK_CHEST_OPEN, SoundCategory.BLOCKS, 0.5F, 0.4F / (0.2F + 0.8F)); if(((TileEntityTrebuchet)worldIn.getTileEntity(pos)).getCooldown()<=0) { if(!worldIn.isRemote) { EntityTippedArrow entityarrow; if(((EnumFacing)state.getValue(FACING)).getIndex() ==2){ entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+0.5, ((double)pos.getY()+0.9), (double)pos.getZ()-0.2); }else if(((EnumFacing)state.getValue(FACING)).getIndex() ==3) { entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+0.5, ((double)pos.getY()+0.9), (double)pos.getZ()+1.2); }else if(((EnumFacing)state.getValue(FACING)).getIndex() ==4) { entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()-0.2, ((double)pos.getY()+0.9), (double)pos.getZ()+0.5); }else{ entityarrow = new EntityTippedArrow(worldIn, (double)pos.getX()+1.2, ((double)pos.getY()+0.9), (double)pos.getZ()+0.5); } entityarrow.setPotionEffect(new ItemStack(Items.ARROW)); float f = -MathHelper.sin(playerIn.rotationYaw * 0.017453292F) * MathHelper.cos(playerIn.rotationPitch * 0.017453292F); float f1 = -MathHelper.sin(playerIn.rotationPitch * 0.017453292F); float f2 = MathHelper.cos(playerIn.rotationYaw * 0.017453292F) * MathHelper.cos(playerIn.rotationPitch * 0.017453292F); entityarrow.setIsCritical(true); entityarrow.setDamage(30.0); entityarrow.setKnockbackStrength(1); entityarrow.shoot((double)f, (double)f1+0.05, (double)f2, 3.0F, 1.0F); worldIn.spawnEntity(entityarrow); ((TileEntityTrebuchet)worldIn.getTileEntity(pos)).setCooldown(1); } if(!playerIn.capabilities.isCreativeMode) { playerIn.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).shrink(1); } worldIn.playSound(playerIn, pos, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.NEUTRAL, 0.5F, 0.4F / (0.2F + 0.8F)); } } worldIn.scheduleUpdate(pos, this, 60); } return true; } protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {FACING}); } public IBlockState withRotation(IBlockState state, Rotation rot) { return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING))); } public IBlockState withMirror(IBlockState state, Mirror mirrorIn) { return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING))); } public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()); } public int getMetaFromState(IBlockState state) { int i = 0; i = i | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex(); return i; } public IBlockState getStateFromMeta(int meta) { return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)); } @Override public TileEntity createTileEntity(World world, IBlockState state) { TileEntityTrebuchet tileEntityTrebuchet = new TileEntityTrebuchet(); return tileEntityTrebuchet; } @Override public boolean hasTileEntity() { return true; } @Override public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { if(stack.hasDisplayName()) { TileEntity tileentity = worldIn.getTileEntity(pos); if(tileentity instanceof TileEntityTrebuchet){ ((TileEntityTrebuchet)tileentity).setCustomName(stack.getDisplayName()); ((TileEntityTrebuchet)tileentity).setCooldown(0); } } } } TileEntity class: public class TileEntityTrebuchet extends TileEntity{ private String customName; private int cooldown; public TileEntityTrebuchet() { this.cooldown = 0; } public boolean hasCustomName() { return this.customName != null && !this.customName.isEmpty(); } public void setCustomName(String customName) { this.customName = customName; } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); if (compound.hasKey("cooldown")) { cooldown = compound.getInteger("cooldown"); } if(compound.hasKey("CustomName", 8)) { this.customName = compound.getString("CustomName"); } } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); if(compound.hasKey("cooldown")) { compound.setInteger("cooldown", cooldown); } if(compound.hasKey("CustomName", 8)) { compound.setString("CustomName", this.customName); } return compound; } public int getCooldown() { return this.cooldown; } public void setCooldown(int cd) { this.cooldown= cd; } public void shrinkCooldown() { this.cooldown--; } } This is my preInit() method: @EventHandler() public static void PreInit(FMLPreInitializationEvent event) { RegistryHandler.preInitRegistries(); } preInitRegistries() in my RegistryHandler class: public static void preInitRegistries() { TileEntityHandler.registerTileEntities(); Main.network = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MOD_ID); RegistryHandler.registerPackets(Main.network); } TileEntityHandler class: public class TileEntityHandler { public static void registerTileEntities() { GameRegistry.registerTileEntity(TileEntityTrebuchet.class, new ResourceLocation(Reference.MOD_ID + ":trebuchet")); } } Edited September 18, 20187 yr by Lumby
September 18, 20187 yr @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setInteger("cooldown", cooldown); if(compound.hasKey("CustomName", 8)) { compound.setString("CustomName", this.customName); } return compound; } This 16 hours ago, Lumby said: if(compound.hasKey("CustomName", 8)) will never be true. This public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand){ if (!worldIn.isRemote){ if(worldIn.getTileEntity(pos) instanceof TileEntityTrebuchet) { ((TileEntityTrebuchet)worldIn.getTileEntity(pos)).setCooldown(0); } } } doesn't do what you think it does. It fires each tick, sure... for one random block in all of loaded chunks. Implement ITickable in your TileEntity and do the stuff with the cooldown there. What makes you think no tile entity is created? Have you placed a breakpoing in any of your methods that get the tile entity and inspected the TE local?
September 18, 20187 yr Author 6 minutes ago, V0idWa1k3r said: Have you placed a breakpoing in any of your methods that get the tile entity and inspected the TE local? Sorry I don't quite get what this means, I'm assuming you're asking if I put any tests? 17 minutes ago, Lumby said: if((worldIn.getTileEntity(pos)) instanceof TileEntityTrebuchet) { I tested that the cobblestone if statement fired by having a SoundEvent fire right after it, and it did fire. The above if statement comes right after that cobblestone if statement. I then put the same SoundEvent to fire right after the above if statement, but it never did. I then went into MCEdit to check for TileEntities, and none showed up (other modded tileEntities of mine, however, did show up in MCEdit). Edited September 18, 20187 yr by Lumby
September 18, 20187 yr I don't see anything obviously wrong with your TE and Block(apart from what me and Diesieben mentioned). Use the debugger to find out what's going on. Alternatively if you could setup a github repository of your mod I could debug it locally.
September 18, 20187 yr Author Got it, I'll keep at it for another while before bothering you guys again with the repository. Nonetheless, thank you all very much for the effort.
September 18, 20187 yr 1 hour ago, Lumby said: Got it, I'll keep at it for another while bswefore bothering you guys again with the repository. Nonetheless, thank you all very much for the effort. 2 hours ago, Lumby said: public boolean hasTileEntity() { This is why your TE is not appearing, this method needs to have a IBlockState parameter. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
September 18, 20187 yr 11 minutes ago, Animefan8888 said: This is why your TE is not appearing, this method needs to have a IBlockState parameter. And this is why you should always @Override Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
September 18, 20187 yr 4 minutes ago, Draco18s said: And this is why you should always @Override In the code the OP posted he has one though 2 hours ago, Lumby said: @Override public boolean hasTileEntity() { return true; } He is just using a deprecated version. Which is his issue as far as I can tell.
September 18, 20187 yr 53 minutes ago, V0idWa1k3r said: In the code the OP posted he has one though He is just using a deprecated version. Which is his issue as far as I can tell. *Digs into what the non-state method signature is. Ah, it does look like that, and not part of ITileEntityProvider.* My mistake Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
September 18, 20187 yr Author 1 hour ago, Animefan8888 said: This is why your TE is not appearing, this method needs to have a IBlockState parameter. 1 hour ago, V0idWa1k3r said: He is just using a deprecated version. Which is his issue as far as I can tell. That worked like a charm, thanks for the help! Edited September 18, 20187 yr by Lumby
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.