Jump to content

[1.12.2] [Solved] Set Minecraft Vanilla block to my own custom block


Toastyman23

Recommended Posts

I'm making a mod that adds a broom to Minecraft so I can learn how things work. I basically just want certain blocks to get dirty over time, and a right click from my broom will set them back to normal.

 

The way I want to do this is by setting every plank block in a certain radius(maybe 50 blocks) to my own custom block, which will just be the plank but dirtier. Then I'll make it so if the block is activated with the broom in hand, it'll set the block back to the default plank texture. What I haven't figured out is how exactly you set blocks to other blocks. Here's what I've tried so far.

 

package toastyman231.broommodandmore.util.handlers;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
//import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import toastyman231.broommodandmore.init.ModBlocks;

@EventBusSubscriber
public class TickHandler extends World
{
	EntityPlayer player;
	BlockPos pos = new BlockPos(player.posX, player.posY, player.posZ);
	Block blk = ModBlocks.TEST_BLOCK;
	BlockPos pos1 = new BlockPos(pos.getX()+2, pos.getY(), pos.getZ());
	IBlockState state = blk.getDefaultState();
	
	protected TickHandler(ISaveHandler saveHandlerIn, WorldInfo info, WorldProvider providerIn, Profiler profilerIn,
			boolean client) 
	{
		super(saveHandlerIn, info, providerIn, profilerIn, client);
	}
	
	@SubscribeEvent
	 public void onWorldTick(TickEvent.WorldTickEvent event)
	 {
		pos = new BlockPos(player.posX, player.posY, player.posZ);
		
		destroyBlock(pos, true);
		setBlockState(pos1, state);
	 }

	@Override
	protected IChunkProvider createChunkProvider() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected boolean isChunkLoaded(int x, int z, boolean allowEmpty) {
		// TODO Auto-generated method stub
		return false;
	}
}

 

So far this doesn't do anything, and I don't know if it's because I set up the tick event wrong or if setBlockState just isn't how you replace blocks or what. What exactly am I doing wrong here, and how do I get every block of a certain type within 50 blocks of the player and set them to another block?

Edited by Toastyman23
Link to comment
Share on other sites

2 hours ago, Toastyman23 said:

I'm extending World so I can use setBlockState easier.

That's not how that works.

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

Okay so I've been working on the mod for a little bit and I've hit another wall. I did what diesieben07 said and switched to PlayerTickEvent and checked TickEvent.Phase, however it still does nothing upon running. Here's the new code:

package toastyman231.broommodandmore.util.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import toastyman231.broommodandmore.init.ModBlocks;

@EventBusSubscriber
public class TickHandler extends World
{
	Block blk = ModBlocks.TEST_BLOCK;
	IBlockState state = blk.getDefaultState();
	
	protected TickHandler(ISaveHandler saveHandlerIn, WorldInfo info, WorldProvider providerIn, Profiler profilerIn,
			boolean client) 
	{
		super(saveHandlerIn, info, providerIn, profilerIn, client);
	}
	
	public void ReplaceBlocks(BlockPos loc1, BlockPos loc2) 
	{
		BlockPos position = (BlockPos) BlockPos.getAllInBox(loc1, loc2);
		List<BlockPos> poses = new ArrayList<BlockPos>();
		poses.add(position);
		
		int index = 0;
		for(BlockPos blockPos : poses)
		{
			if(Minecraft.getMinecraft().world.getBlockState(poses.get(index)) == Blocks.GRASS.getBlockState())
			{
				setBlockState((BlockPos) poses.get(index), state);
                index++;
			}
		}
	}

	@SubscribeEvent
	 public void onPlayerTick(TickEvent.PlayerTickEvent event) /*throws InterruptedException*/
	 {
		if(event.phase == TickEvent.Phase.START)
		{
			EntityPlayer player = event.player;
			
			//TimeUnit.SECONDS.sleep(30);
			BlockPos pos = new BlockPos(player.posX, player.posY, player.posZ);
			BlockPos pos1 = new BlockPos(pos.getX()-24, pos.getY()-24, pos.getZ()-24);
			BlockPos pos2 = new BlockPos(pos.getX()+25, pos.getY()+25, pos.getZ()+25);
			
			ReplaceBlocks(pos1, pos2);
		}
	 }

	@Override
	protected IChunkProvider createChunkProvider() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected boolean isChunkLoaded(int x, int z, boolean allowEmpty) {
		// TODO Auto-generated method stub
		return false;
	}
}

 

What did I do wrong this time folks?

Link to comment
Share on other sites

34 minutes ago, Toastyman23 said:

TickHandler extends World

You're still extending World. Of course shit don't work.

34 minutes ago, Toastyman23 said:

(Minecraft.getMinecraft().world.getBlockState(poses.get(index))

Why are you getting the client world? Use the world given to you by the event (doesn't have one, get the event's player's world).

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

10 hours ago, Draco18s said:

You're still extending World. Of course shit don't work.

How do I do it without extending World then? I tried just setting a reference but it kept trying to tell me that TickHandler didn't have a function called setBlockState. I did it once before I made this thread too, without any issues, but now it doesn't work.

Link to comment
Share on other sites

2 minutes ago, Toastyman23 said:

How do I do it without extending World then? I tried just setting a reference but it kept trying to tell me that TickHandler didn't have a function called setBlockState. I did it once before I made this thread too, without any issues, but now it doesn't work.

setBlockState is a method of world, it is also public so therefore you can call it from an instance of World. Which all Entities have a public instance. Also how well do you know Java?

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, Animefan8888 said:

setBlockState is a method of world, it is also public so therefore you can call it from an instance of World. Which all Entities have a public instance. Also how well do you know Java?

I took a class in Java but I'm more experienced with C# and Unity.

 

Turns out I was just being stupid and was using the wrong type of reference. I have it error free without extending World now, but it still doesn't work.

package toastyman231.broommodandmore.util.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import toastyman231.broommodandmore.init.ModBlocks;

@EventBusSubscriber
public abstract class TickHandler
{
	World worldRef;
	Block blk = ModBlocks.TEST_BLOCK;
	IBlockState state = blk.getDefaultState();
	
	public void ReplaceBlocks(EntityPlayer play, BlockPos loc1, BlockPos loc2) 
	{
		BlockPos position = (BlockPos) BlockPos.getAllInBox(loc1, loc2);
		List<BlockPos> poses = new ArrayList<BlockPos>();
		poses.add(position);
		
		int index = 0;
		for(BlockPos blockPos : poses)
		{
			if(play.world.getBlockState(poses.get(index)) == Blocks.GRASS.getBlockState())
			{
				worldRef.setBlockState(poses.get(index), state);
                index++;
			}
		}
	}

	@SubscribeEvent
	 public void onPlayerTick(TickEvent.PlayerTickEvent event) /*throws InterruptedException*/
	 {
		if(event.phase == TickEvent.Phase.START)
		{
			EntityPlayer player = event.player;
			
			//TimeUnit.SECONDS.sleep(30);
			BlockPos pos = new BlockPos(player.posX, player.posY, player.posZ);
			BlockPos pos1 = new BlockPos(pos.getX()-24, pos.getY()-24, pos.getZ()-24);
			BlockPos pos2 = new BlockPos(pos.getX()+25, pos.getY()+25, pos.getZ()+25);
			
			ReplaceBlocks(player, pos1, pos2);
		}
	 }
}

 

Link to comment
Share on other sites

1 minute ago, Toastyman23 said:

Turns out I was just being stupid and was using the wrong type of reference. I have it error free without extending World now, but it still doesn't work.

You never set your world variable to anything, and you expected that to work?

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

5 minutes ago, Animefan8888 said:

You never set your world variable to anything, and you expected that to work?

I figured it would inherit what it needed from World, but I guess not. What am I supposed to set it to? I tried World worldRef = new World(); but it just says it can't instantiate the type World because World is an abstract class. What exactly am I supposed to be doing here?

Link to comment
Share on other sites

2 minutes ago, Toastyman23 said:

I figured it would inherit what it needed from World, but I guess not. What am I supposed to set it to? I tried World worldRef = new World(); but it just says it can't instantiate the type World because World is an abstract class. What exactly am I supposed to be doing here?

 

11 hours ago, Draco18s said:

Use the world given to you by the event (doesn't have one, get the event's player's world).

 

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

Just now, Toastyman23 said:

Where do I get the world given to me by the event? I didn't code Minecraft, I don't know how it handles this stuff.

The PlayerTickEvent has a reference to the EntityPlayer as you know. All Entities including the player have a reference to the world they are in.

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, Animefan8888 said:

The PlayerTickEvent has a reference to the EntityPlayer as you know. All Entities including the player have a reference to the world they are in.

So then the player I'm passing in should work, right? I removed the reference to World and changed the setBlockState line to use the world from event.player. However it still doesn't work.

package toastyman231.broommodandmore.util.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import toastyman231.broommodandmore.init.ModBlocks;

@EventBusSubscriber
public class TickHandler
{
	Block blk = ModBlocks.TEST_BLOCK;
	IBlockState state = blk.getDefaultState();
	
	public void ReplaceBlocks(EntityPlayer play, BlockPos loc1, BlockPos loc2) 
	{
		BlockPos position = (BlockPos) BlockPos.getAllInBox(loc1, loc2);
		List<BlockPos> poses = new ArrayList<BlockPos>();
		poses.add(position);
		
		int index = 0;
		for(BlockPos blockPos : poses)
		{
			if(play.world.getBlockState(poses.get(index)) == Blocks.GRASS.getBlockState())
			{
				play.world.setBlockState(poses.get(index), state);
                index++;
			}
		}
	}

	@SubscribeEvent
	 public void onPlayerTick(TickEvent.PlayerTickEvent event) /*throws InterruptedException*/
	 {
		if(event.phase == TickEvent.Phase.START)
		{
			EntityPlayer player = event.player;
			
			//TimeUnit.SECONDS.sleep(30);
			BlockPos pos = new BlockPos(player.posX, player.posY, player.posZ);
			BlockPos pos1 = new BlockPos(pos.getX()-24, pos.getY()-24, pos.getZ()-24);
			BlockPos pos2 = new BlockPos(pos.getX()+25, pos.getY()+25, pos.getZ()+25);
			
			ReplaceBlocks(player, pos1, pos2);
		}
	 }
}

 

Link to comment
Share on other sites

1 minute ago, Toastyman23 said:

I go into the world and nothing happens. It's just really laggy and no blocks change.

for(BlockPos blockPos : poses)

Do you know what this line does?

4 minutes ago, Toastyman23 said:

BlockPos position = (BlockPos) BlockPos.getAllInBox(loc1, loc2);

Why are you casting an Iterable to BlockPos, this shoudn't even compile.

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

Just now, Animefan8888 said:

for(BlockPos blockPos : poses)

Do you know what this line does?

That cycles through every BlockPos stored in the list of BlockPoses poses.

 

1 minute ago, Animefan8888 said:

Why are you casting an Iterable to BlockPos, this shoudn't even compile.

Idk it threw me an error and said doing that would fix it.

Link to comment
Share on other sites

Just now, Toastyman23 said:

Idk it threw me an error and said doing that would fix it.

Use the Iterable returned by the method instead of the list you have created.

1 minute ago, Toastyman23 said:

That cycles through every BlockPos stored in the list of BlockPoses poses.

Why do you have the index variable just use the one created in the loop that is already a BlockPos.

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, Animefan8888 said:

Use the Iterable returned by the method instead of the list you have created.

How exactly would I go about doing that?

3 minutes ago, Animefan8888 said:

Why do you have the index variable just use the one created in the loop that is already a BlockPos.

Ok I fixed that. I didn't realize list.getIndexOf() was a thing.

Link to comment
Share on other sites

1 minute ago, Toastyman23 said:

Ok I fixed that. I didn't realize list.getIndexOf() was a thing.

I don't believe this is what I told you to do, for (BlockPos blockPos <--- This is a variable : poses)

2 minutes ago, Toastyman23 said:

How exactly would I go about doing that?

Iterable<BlockPos> poses = BlockPos.getAllInBox

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

7 minutes ago, Animefan8888 said:

I don't believe this is what I told you to do, for (BlockPos blockPos <--- This is a variable : poses)

Iterable<BlockPos> poses = BlockPos.getAllInBox

Ok I think I made the right changes, but it still doesn't work.

package toastyman231.broommodandmore.util.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import toastyman231.broommodandmore.init.ModBlocks;

@EventBusSubscriber
public class TickHandler
{
	Block blk = ModBlocks.TEST_BLOCK;
	IBlockState state = blk.getDefaultState();
	
	public void ReplaceBlocks(EntityPlayer play, BlockPos loc1, BlockPos loc2) 
	{
		Iterable<BlockPos> poses = BlockPos.getAllInBox(loc1, loc2);
		
		for(BlockPos blockPos : poses)
		{
			if(play.world.getBlockState(blockPos) == Blocks.GRASS.getBlockState())
			{
				play.world.setBlockState(blockPos, state);
			}
		}
	}

	@SubscribeEvent
	 public void onPlayerTick(TickEvent.PlayerTickEvent event) /*throws InterruptedException*/
	 {
		if(event.phase == TickEvent.Phase.START)
		{
			EntityPlayer player = event.player;
			
			//TimeUnit.SECONDS.sleep(30);
			BlockPos pos = new BlockPos(player.posX, player.posY, player.posZ);
			BlockPos pos1 = new BlockPos(pos.getX()-24, pos.getY()-24, pos.getZ()-24);
			BlockPos pos2 = new BlockPos(pos.getX()+25, pos.getY()+25, pos.getZ()+25);
			
			ReplaceBlocks(player, pos1, pos2);
		}
	 }
}

 

Link to comment
Share on other sites

2 minutes ago, Toastyman23 said:

Ok I think I made the right changes, but it still doesn't work.

You need to make your onPlayerTick method static.

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

3 minutes ago, Animefan8888 said:

You need to make your onPlayerTick method static.

I made everything static and it still doesn't work.

package toastyman231.broommodandmore.util.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import toastyman231.broommodandmore.init.ModBlocks;

@EventBusSubscriber
public class TickHandler
{
	static Block blk = ModBlocks.TEST_BLOCK;
	static IBlockState state = blk.getDefaultState();
	
	public static void ReplaceBlocks(EntityPlayer play, BlockPos loc1, BlockPos loc2) 
	{
		Iterable<BlockPos> poses = BlockPos.getAllInBox(loc1, loc2);
		
		for(BlockPos blockPos : poses)
		{
			if(play.world.getBlockState(blockPos) == Blocks.GRASS.getBlockState())
			{
				play.world.setBlockState(blockPos, state);
			}
		}
	}

	@SubscribeEvent
	 public static void onPlayerTick(TickEvent.PlayerTickEvent event) /*throws InterruptedException*/
	 {
		if(event.phase == TickEvent.Phase.START)
		{
			EntityPlayer player = event.player;
			
			//TimeUnit.SECONDS.sleep(30);
			BlockPos pos = new BlockPos(player.posX, player.posY, player.posZ);
			BlockPos pos1 = new BlockPos(pos.getX()-24, pos.getY()-24, pos.getZ()-24);
			BlockPos pos2 = new BlockPos(pos.getX()+25, pos.getY()+25, pos.getZ()+25);
			
			ReplaceBlocks(player, pos1, pos2);
		}
	 }
}

 

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.