Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • having issues with onNeighborBlockChange
The update for 1.13 is being worked on - please be patient. (Updated 02/19/19)
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 0
gummby8

having issues with onNeighborBlockChange

Started by gummby8, September 1, 2017

2 posts in this topic

gummby8    26

gummby8

gummby8    26

  • Diamond Finder
  • gummby8
  • Members
  • 26
  • 479 posts
  • Report post
Posted September 1, 2017

I am making a block that generates a sort of force field when powered and breaks it back down when not powered

 

All the magic happens in the onNeighborBlockChange

 

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
    {
        EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
        EnumFacing enumfacingY = enumfacing.getOpposite();
        
         if( worldIn.getStrongPower(pos.offset(enumfacing), enumfacing) > 0){
        	worldIn.setBlockState(pos, state.withProperty(FACING, (EnumFacing)state.getValue(FACING)).withProperty(STATE, true), 3);
        } else {
        	worldIn.setBlockState(pos, state.withProperty(FACING, (EnumFacing)state.getValue(FACING)).withProperty(STATE, false), 3);
        }
         
         if (((Boolean)state.getValue(POWERED)).booleanValue()){
       		System.out.println("Building");
       		worldIn.setBlockState(pos.up(2), ModBlocks.force_field_blue.getDefaultState().withProperty(BlockForceFieldBlue.FACING, (EnumFacing)state.getValue(FACING)).withProperty(BlockForceFieldBlue.DAMAGE, 1), 3);
         }else{
      		System.out.println("Teardown");
      		worldIn.setBlockToAir(pos.up(2));
           	worldIn.setBlockState(pos.up(2), ModBlocks.force_field_blue.getDefaultState().withProperty(BlockForceFieldBlue.FACING, (EnumFacing)state.getValue(FACING)).withProperty(BlockForceFieldBlue.DAMAGE, 0), 3);
        }
    }

 

If there is redstone power on the "input side" it sets the powered state to true. if powered is true, it builds the forcefield. STATE 1 = on, STATE 0 = off

 

The issue that I am running into is in the console whenever I give redstone power to the input side. I see 1 building, followed by 5-6 Teardown messages. The POWERED state appears to stay true, because the output side continues to output redstone signal

 

	@Override
    public int isProvidingWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
    {
        EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);

        if (enumfacing == side && ((Boolean)state.getValue(POWERED)).booleanValue()) {
    		return 15;
    	} else {
      		return 0;
    	}
    }

 

So my thought is that it is getting neighborchanges from other blocks that are around it, which are messing up my POWERED state?.

 

How can I change this so it is only checking once, on the block on the input side to see if it should be powered or not ?

 

 

Share this post


Link to post
Share on other sites

gummby8    26

gummby8

gummby8    26

  • Diamond Finder
  • gummby8
  • Members
  • 26
  • 479 posts
  • Report post
Posted September 1, 2017

So I think I fixed this in a roundabout way.


Rather than putting the build and teardown in the onNeighborBlockChange

 

I put it in updateTick

 

/**
	 * Called when a neighboring block changes.
	 */
	public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock) {
			worldIn.scheduleUpdate(pos, this, 1);
	}
	
	 
	@Override
	public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
		System.out.println(state.getValue(POWERED));
		EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);
		EnumFacing enumfacingY = enumfacing.getOpposite();



		if (worldIn.getStrongPower(pos.offset(enumfacing), enumfacing) > 0) {
			worldIn.setBlockState(pos, state.withProperty(FACING, (EnumFacing) state.getValue(FACING)).withProperty(POWERED, true), 3);
		} else {
			worldIn.setBlockState(pos, state.withProperty(FACING, (EnumFacing) state.getValue(FACING)).withProperty(POWERED, false), 3);
		}

		if (((Boolean) state.getValue(POWERED)).booleanValue()) {
			// System.out.println("Building");
			worldIn.setBlockState(pos.up(2), ModBlocks.force_field_blue.getDefaultState().withProperty(BlockForceFieldBlue.FACING, (EnumFacing) state.getValue(FACING)).withProperty(BlockForceFieldBlue.STATE, 1), 3);
		} else {
			// System.out.println("Teardown");
			worldIn.setBlockToAir(pos.up(2));
			worldIn.setBlockState(pos.up(2), ModBlocks.force_field_blue.getDefaultState().withProperty(BlockForceFieldBlue.FACING, (EnumFacing) state.getValue(FACING)).withProperty(BlockForceFieldBlue.STATE, 0), 3);
		}
	}

 

This works, not sure if this is the best way to do it, but it is working

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0
Go To Topic Listing Modder Support

  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • SparHawke
      Error when installing forge 1.13.2-25.0.45

      By SparHawke · Posted 15 minutes ago

      I had the same issue try installing 1.13.2 - 25.0.44 then install 1.13.2 - 25.0.45. It worked just fine for me
    • Apocrypha
      Error when installing forge 1.13.2-25.0.45

      By Apocrypha · Posted 24 minutes ago

      I keep running into an error while the installer is installing, got no idea what to do
    • SparHawke
      1.13.2 - 25.0.45 Crashes Game

      By SparHawke · Posted 51 minutes ago

      Hope that's the right way to post logs  
    • diesieben07
      1.13.2 - 25.0.45 Crashes Game

      By diesieben07 · Posted 1 hour ago

      Post logs.
    • SparHawke
      1.13.2 - 25.0.45 Crashes Game

      By SparHawke · Posted 1 hour ago

      I downloaded 1.13.2 - 25.0.45 early this morning and soon as I had it installed i started the game and it immediately crashed. I then reverted back to 1.13.2 - 25.0.44 and everything worked just as it should. Could anyone enlighten me as to what the issue is . launcher_log.txt 2019-02-21-4.log.gz
  • Topics

    • Apocrypha
      1
      Error when installing forge 1.13.2-25.0.45

      By Apocrypha
      Started 24 minutes ago

    • SparHawke
      2
      1.13.2 - 25.0.45 Crashes Game

      By SparHawke
      Started 1 hour ago

    • RedBullSlurpie
      0
      Error when installing forge-1.13.2-25.0.45-installer

      By RedBullSlurpie
      Started 1 hour ago

    • kl1230
      0
      1.13 working but 1.12.2 freezes system

      By kl1230
      Started 1 hour ago

    • CJWilk
      2
      1.13.2-25.0.45 fails to install

      By CJWilk
      Started 7 hours ago

  • Who's Online (See full list)

    • elemperador
    • ItHurtsLikeHell
    • fcelon
    • diesieben07
    • HeberonYT
    • SparHawke
    • Apocrypha
    • RedBullSlurpie
    • nyuppo
    • thedarkcolour
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • having issues with onNeighborBlockChange
  • Theme
  • Contact Us

Copyright © 2017 ForgeDevelopment LLC Powered by Invision Community