Jump to content

How can I add redstone components?


WillTheGameDecoder

Recommended Posts

I've been trying to mod in a new redstone component similar to the piston.  I've searched high and low on forums and YouTube, trying to figure out how to get this to work, but I can't seem to get any proper updates or read redstone signals like I'm expecting.  In all cases, it has either has ignored all redstone signals, or it doesn't function like it should (I've tested it with onBlockActivated. it does work)  Is there something I'm missing?  Do I need a TileEntity?  Am I overriding the wrong methods?  I should also mention that I'm just setting blocks in the world to test whether code is running, and it doesn't ever seem to.

package com.willtgd.testmod.blocks;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class Teleporter extends Block {
	
	public Teleporter() {} // not important to the question at hand (i think)
	
	@Override
	public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) {
		if (world.isBlockPowered(pos.add(0, 0, 1))) {
			world.setBlockState(pos.add(1, 0, 0), Block.getBlockFromName("minecraft:stone").getDefaultState());
		}
	}
	
	@Override
	public void onNeighborChange(IBlockAccess access, BlockPos pos, BlockPos neighbor) {
		World world = Minecraft.getMinecraft().world;
		
		if (world.isBlockPowered(neighbor)) {
			world.setBlockState(neighbor, Block.getBlockFromName("minecraft:dirt").getDefaultState());
		}
	}
	
	@Override
	public void observedNeighborChange(IBlockState state, World world, BlockPos observerPos, Block changedBlock, BlockPos changePos) {
		if (world.isBlockPowered(changePos)) {
			world.setBlockState(changePos, Block.getBlockFromName("minecraft:dirt").getDefaultState());
		}
	}
	
	private void swapBlocks(World world, BlockPos pos) {
		BlockPos up = pos.add(0, 1, 0);
		BlockPos down = pos.add(0, -1, 0);
		
		IBlockState temp = world.getBlockState(up);
		
		world.setBlockState(up, world.getBlockState(down));
		world.setBlockState(down, temp);
	}
}
Link to comment
Share on other sites

1 hour ago, WillTheGameDecoder said:

swapBlocks

Well, you aren't calling this method...

 

1 hour ago, WillTheGameDecoder said:

World world = Minecraft.getMinecraft().world;

Why are you doing this? Not only are you reaching across logical sides, but you were passed a world object in the parameters. It's the IBlockAccess (you know, to access block information about the world from? Take a look at what classes implement this interface...)

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.

Link to comment
Share on other sites

42 minutes ago, Draco18s said:

Well, you aren't calling this method...

 

Why are you doing this? Not only are you reaching across logical sides, but you were passed a world object in the parameters. It's the IBlockAccess (you know, to access block information about the world from? Take a look at what classes implement this interface...)

1) I know I'm not calling the method.  I did forget to add it, yeah, but it doesn't work, even when I do add it.

2) IBlockAccess doesn't have the "isBlockPowered" method.  It only has "getStrongPower" which requires an EnumFacing as well, which I don't understand either...

Link to comment
Share on other sites

6 hours ago, WillTheGameDecoder said:

requires an EnumFacing as well, which I don't understand either...

Then test values to understand it. Or look through the code and find out what that parameter does.

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.

Link to comment
Share on other sites

9 hours ago, WillTheGameDecoder said:

IBlockAccess doesn't have the "isBlockPowered" method

Cast it.

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.

Link to comment
Share on other sites

3 minutes ago, diesieben07 said:

No. It will not always be a World.

Doing a cast -> always check that it's valid first.

I figure that this is implied knowledge for the level of programming knowledge required to be posting here.

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.

Link to comment
Share on other sites

8 minutes ago, diesieben07 said:

Not always the case either :P

I am not sure what you are getting at here. ?

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.

Link to comment
Share on other sites

16 minutes ago, diesieben07 said:

In this case you do need to do the check before casting. But saying "you always need to check before casting" is not true.

Let me clarify then:

Upcasting does not require a check (and, in some respects, the cast isn't needed at all, as the parent attributes and definition should already be available)
Downcasting does, as there may be multiple implementations or subclasses

 

Now, if there's an exception by where downcasting does not require an instanceof check, that is not specific to a given implementation (e.g. "EntityFoo is only ever extended by EntityFooBar") I'd be happy to learn about it.

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.

Link to comment
Share on other sites

56 minutes ago, WillTheGameDecoder said:

breaking

Define breaking.

58 minutes ago, WillTheGameDecoder said:

Well, that certainly helps in that aspect, but my original question still stands.

Post updated code, preferably the whole class.

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.

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

One such place is block methods getting their tile entity. A downcast to the actual tile entity implementation does not need a check there.

Ok, in general, I can agree with you, due to the way the scope works there.

 

There's still, tecnically, the possibility that it could crash, but only in cases where someone else fucked up somewhere.

There's the blockBroken method that I think is called after the block is removed that messed with a whole ton of people's code during a major update (1.10 -> 1.12?), and there's the "if someone fucks up their shouldRefresh method."

 

But you are correct in general that when things are working the way they're supposed to, no instanceof is needed. However, this is a very specific scenario that I'd argue is specific to the implementation detail of TileEntities within Minecraft and in not a general rule of programming.

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.

Link to comment
Share on other sites

1 minute ago, WillTheGameDecoder said:

Could it possibly be the fact that the method swapBlock is called twice

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.

Link to comment
Share on other sites

2 minutes ago, WillTheGameDecoder said:

No.  "onNeighborChange" doesn't get triggered at all, as far as I'm aware.  I forgot to delete that code

Does observedNeighborChange get called?

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.

Link to comment
Share on other sites

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.

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

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.