Jump to content

[1.8] world.setBlockState not actually creating blocks


Atomos

Recommended Posts

In my mod, I set a lot of blocks. However when I use the world.setBlockState(location, blockstate) command, the block is created, but is then deleted once i reload that world. When I delete blocks, a similar thing happens. That is, I cannot walk through the generated air, and the old blocks reappear on reload. My setBlock method is as follows:

public static void setBlock(Block type,BlockPos location)
{
    World world = Minecraft.getMinecraft().theWorld;
    IBlockState blockState = type.getDefaultState(); 
	world.setBlockState(location, blockState);
}

and my deleteBlock method

public static void deleteBlock(BlockPos location)
{
	World world = Minecraft.getMinecraft().theWorld;
	world.setBlockToAir(location);
}

 

Link to comment
Share on other sites

I already know that I am running the method client side, but how am I supposed to run it server side?

Here is my onItemRightClick method:

public ItemStack onItemRightClick(ItemStack Item, World world, EntityPlayer player)
{
	double x = player.posX;
	double y = player.posY;
	double z = player.posZ;
	BlockPos playerLoc = new BlockPos(x,y,z);


	if (player.inventory.hasItem(Items.diamond)&&player.inventory.hasItem(Items.ghast_tear))
	{

		RoomData room = new RoomData();
		room.width = 10;
		room.height = 4;
		room.length = 10;
		room.isIlluminated=true;

		room.roomValues.add(0);
	//	room.roomValues.add(1);
	//	room.roomValues.add(2);
	//	room.roomValues.add(3);

		room.lightSpacing=5;
		room.center = player.getPosition();

		StoneDungeon.Generate(StoneDungeon.CreateData(room,5));

	}
	else
	{
		player.addChatComponentMessage(new ChatComponentTranslation("You do not have a catalyst or a flow source!",new Object[0]));

	}

	return Item;



}

And here is the stoneDungeon class:

package dungeonGenerator.algorithms;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentTranslation;

import java.util.*;

import net.minecraft.world.World;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.event.world.WorldEvent.*;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.*;

public class StoneDungeon {
public static int roomCount = 0;

public static List<RoomData> CreateData(RoomData base, int roomsCount)
{
	List<RoomData> output = new ArrayList<RoomData>();
	output.add(base);
	BlockPos previousPos = new BlockPos(base.center);
	List<RoomData> previousRooms = new ArrayList<RoomData>();
	List<RoomData> tempPreviousRooms = new ArrayList<RoomData>();
	previousRooms.add(base);

	for (int i = 0;i<roomsCount;i++)
	{
	for (RoomData previousRoom : previousRooms)
	{
	for (int location : previousRoom.roomValues)
	{
		RoomData room = new RoomData();
		room.height = base.height;
		room.width = base.width;
		room.length = base.length;
		room.isIlluminated = true;
		room.lightSpacing = base.lightSpacing;
		double x = previousRoom.center.getX();
		double y = previousRoom.center.getY();
		double z = previousRoom.center.getZ();
		if (location == 0)
		{
			room.center = new BlockPos(x,y,z+previousRoom.length-2);
			room.roomValues.add(1);
			room.roomValues.add(random());
			room.roomValues.add(random());
			room.roomValues.add(random());

			output.add(room);
			tempPreviousRooms.add(room);
		}
		if (location == 1)
		{
			room.center = new BlockPos(x,y,z-previousRoom.length+2);
			room.roomValues.add(0);
			room.roomValues.add(random());
			room.roomValues.add(random());
			room.roomValues.add(random());

			output.add(room);
			tempPreviousRooms.add(room);
		}
		if (location == 2)
		{
			room.center = new BlockPos(x+previousRoom.width-2,y,z);
			room.roomValues.add(3);
			room.roomValues.add(random());
			room.roomValues.add(random());
			room.roomValues.add(random());
			output.add(room);
			tempPreviousRooms.add(room);
		}
		if (location == 3)
		{
			room.center = new BlockPos(x-previousRoom.width+2,y,z);
			room.roomValues.add(2);
			room.roomValues.add(random());
			room.roomValues.add(random());
			room.roomValues.add(random());
			output.add(room);
			tempPreviousRooms.add(room);
		}
	}
	}
	previousRooms.addAll(tempPreviousRooms);
	tempPreviousRooms.clear();
	}
	Minecraft.getMinecraft().thePlayer.addChatComponentMessage(new ChatComponentTranslation("Creating "+output.size()+" Rooms"));
	return output;

}

public static void Generate(List<RoomData> rooms)
{
	//Define Variables
	for (RoomData room : rooms)
	{
	double x = room.center.getX();
	double y = room.center.getY()-1;
	double z = room.center.getZ();
	int dirUp = room.length/2;
	int dirSide = room.width/2;
	int height = room.height;
	int lightSpacing = room.lightSpacing;
	//Generate the floor
	for (int i = 0;i < dirSide;i++){
		for (int j = 0;j < dirUp;j++)
		{
			setBlock (Blocks.stonebrick,new BlockPos(x+i,y,z+j));
			setBlock (Blocks.stonebrick,new BlockPos(x-i,y,z+j));
			setBlock (Blocks.stonebrick,new BlockPos(x+i,y,z-j));
			setBlock (Blocks.stonebrick, new BlockPos(x-i,y,z-j));
			//Set illumination Blocks
			if (room.isIlluminated == true)
			{

				setBlock (Blocks.glowstone,new BlockPos(x,y,z));
				if (i%lightSpacing==0)
				{
					setBlock(Blocks.glowstone,new BlockPos(x+i,y,z));
					setBlock(Blocks.glowstone,new BlockPos(x-i,y,z));
				}
				if (j%lightSpacing==0)
				{
					setBlock(Blocks.glowstone,new BlockPos(x,y,z+j));
					setBlock(Blocks.glowstone,new BlockPos(x,y,z-j));
				}
				if (i%lightSpacing==0&&j%lightSpacing==0)
				{
					setBlock(Blocks.glowstone,new BlockPos(x+i,y,z+j));
					setBlock(Blocks.glowstone,new BlockPos(x+i,y,z-j));
					setBlock(Blocks.glowstone,new BlockPos(x-i,y,z+j));
					setBlock(Blocks.glowstone,new BlockPos(x-i,y,z-j));
				}
			}
		}
	}
	//set ceiling
	for (int i = 0;i < dirSide;i++){
		for (int j = 0;j < dirUp;j++)
		{
			setBlock (Blocks.stonebrick,new BlockPos(x+i,y+height,z+j));
			setBlock (Blocks.stonebrick,new BlockPos(x-i,y+height,z+j));
			setBlock (Blocks.stonebrick,new BlockPos(x+i,y+height,z-j));
			setBlock (Blocks.stonebrick, new BlockPos(x-i,y+height,z-j));
		}
	}
	//Break out internals
	for (int i = 0;i<dirSide;i++)
	{
		for (int j = 0;j<dirUp;j++)
		{
			for (int k = 1;k<height;k++)
			{
				deleteBlock (new BlockPos(x+i-1,y+k,z+j-1));
				deleteBlock (new BlockPos(x-i+1,y+k,z+j-1));
				deleteBlock (new BlockPos(x+i-1,y+k,z-j+1));
				deleteBlock (new BlockPos(x-i+1,y+k,z-j+1));

			}
		}
	}
	//set left & right walls
	for (int k = 0;k < height;k++)
	{
		for (int j = 0;j < dirUp;j++)
		{
			setBlock (Blocks.stonebrick, new BlockPos(x+dirSide-1,y+k,z+j));
			setBlock(Blocks.stonebrick, new BlockPos(x-dirSide+1,y+k,z+j));
			setBlock (Blocks.stonebrick, new BlockPos(x+dirSide-1,y+k,z-j));
			setBlock(Blocks.stonebrick, new BlockPos(x-dirSide+1,y+k,z-j));


		}
	}
	//set up & down walls
	for (int k = 0;k < height;k++)
	{
		for (int i = 0;i < dirUp;i++)
		{
			setBlock (Blocks.stonebrick, new BlockPos(x+i,y+k,z+dirUp-1));
			setBlock(Blocks.stonebrick, new BlockPos(x+i,y+k,z-dirUp+1));
			setBlock (Blocks.stonebrick, new BlockPos(x-i,y+k,z+dirUp-1));
			setBlock(Blocks.stonebrick, new BlockPos(x-i,y+k,z-dirUp+1));

		}
	}
	//set doors
	if (room.roomValues.contains(1)==true)
	{
		deleteBlock(new BlockPos(x,y+1,z-dirUp+1));
		deleteBlock(new BlockPos(x,y+2,z-dirUp+1));
	}
	if (room.roomValues.contains(0)==true)
	{
		deleteBlock(new BlockPos(x,y+1,z+dirUp-1));
		deleteBlock(new BlockPos(x,y+2,z+dirUp-1));
	}
	if (room.roomValues.contains(3)==true)
	{
		deleteBlock(new BlockPos(x+dirSide-1,y+1,z));
		deleteBlock(new BlockPos(x+dirSide-1,y+2,z));
	}
	if (room.roomValues.contains(2)==true)
	{
		deleteBlock(new BlockPos(x-dirSide+1,y+1,z));
		deleteBlock(new BlockPos(x-dirSide+1,y+2,z));
	}
	}
}
public static void setBlock(Block type,BlockPos location)
{
    World world = Minecraft.getMinecraft().theWorld;
    IBlockState blockState = type.getDefaultState(); 
	world.setBlockState(location, blockState);
}

public static void deleteBlock(BlockPos location)
{
	World world = Minecraft.getMinecraft().theWorld;
	world.setBlockToAir(location);
}
public static int random()
{
	Random rand = new Random();
	return rand.nextInt(4);


}
}

Link to comment
Share on other sites

My updated on right click:

public ItemStack onItemRightClick(ItemStack Item, World world, EntityPlayer player)
{
	double x = player.posX;
	double y = player.posY;
	double z = player.posZ;
	BlockPos playerLoc = new BlockPos(x,y,z);


	if (player.inventory.hasItem(Items.diamond)&&player.inventory.hasItem(Items.ghast_tear))
	{
		if (!world.isRemote)
		{
		RoomData room = new RoomData();
		room.width = 10;
		room.height = 4;
		room.length = 10;
		room.isIlluminated=true;

		room.roomValues.add(0);
	//	room.roomValues.add(1);
	//	room.roomValues.add(2);
	//	room.roomValues.add(3);

		room.lightSpacing=5;
		room.center = player.getPosition();

		StoneDungeon.Generate(StoneDungeon.CreateData(room,5));
		}

	}
	else
	{
		if (!world.isRemote)
		{
		player.addChatComponentMessage(new ChatComponentTranslation("You do not have a catalyst or a flow source!",new Object[0]));
		}
	}

	return Item;



}

and my stoneDungeon class is in an earleir reply

Link to comment
Share on other sites

dont read everything but there is a big mistake inside.

ur StoneDungeon stuff is for generating something. So it shall only run on server side. But u are calling Minecraft.getMinecraft().thePlayer.addChatComponentMessage(new ChatComponentTranslation("Creating "+output.size()+" Rooms"));

 

Never ever call Minecraft on server side, since it is not existing on server side. If u want the player u need to pass it as argument fromthe item rightclick method

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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