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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://drive.google.com/file/d/1M0EG-c5yfRd08DSnE5HVQNCu2v0YtA7A/view?usp=sharing  
    • so im not sure if your still watching this TileEntity but once we loaded a FTB modpack we wanted to run on the server it no longer seems to run. The console opens like normal but we've been sitting here for like a little over an hour and the server screen has not yet opened. Should we just keep waiting at this point?
    • I have now easily fixed the duplication error present, I was just not looking.   I have been working for the past half hour to try and fix another error present, this time with the Creative Mode Tab.   I have changed some things around to get where I am currently. (ModFoods to ModDrinks*) and it cannot find the symbol ".get" at the end of the code. *The custom class you recommended pOutput.accept(ModDrinks.ORANGE_JUICE.get()); I think the point I am at currently is the closest I have to how it should be but because I am not as experienced with java I would not know.  I have also removed ORANGE_JUICE and LEMON_JUICE from the ModFoods class, to avoid confliction. I do hope all this can be fully resolved soon.  
    • [SOLVED]  public class RenderGUIHandler { @SubscribeEvent public void renderGUI(RenderGameOverlayEvent.Text event){ Client.hud.draw(); } } As I was playing around a little with the code, i found out about an option to change The RenderGameOverlayEvent.Post to RenderGameOverlayEvent.Text
    • public class HUD { public Minecraft mc = Minecraft.getMinecraft(); public static class ModuleComparator implements Comparator<Module>{ @Override public int compare(Module o1, Module o2) { if (Minecraft.getMinecraft().fontRendererObj.getStringWidth(o1.name) > Minecraft.getMinecraft().fontRendererObj.getStringWidth(o2.name)){ return -1; } if (Minecraft.getMinecraft().fontRendererObj.getStringWidth(o1.name) < Minecraft.getMinecraft().fontRendererObj.getStringWidth(o2.name)){ return 1; } return 0; } } public void draw(){ ScaledResolution sr = new ScaledResolution(mc); FontRenderer fr = mc.fontRendererObj; Collections.sort(Client.modules, new ModuleComparator()); GlStateManager.pushMatrix(); GlStateManager.translate(4,4,0); GlStateManager.scale(1.5,1.5,1); GlStateManager.translate(-4, -4, 0); fr.drawString("Skyline", 10, 10, -1); GlStateManager.popMatrix(); int count = 0; for (Module m : Client.modules){ if (!m.toggled || m.name.equals("TabGUI")) continue; int offset = count* (fr.FONT_HEIGHT + 6); GL11.glTranslated(0.0f, 0.0f, -1.0f); Gui.drawRect(sr.getScaledWidth() - fr.getStringWidth(m.name) - 10, offset, sr.getScaledWidth() - fr.getStringWidth(m.name) - 8, 6 + fr.FONT_HEIGHT + offset, -1); Gui.drawRect(sr.getScaledWidth() - fr.getStringWidth(m.name) - 8, offset, sr.getScaledWidth(), 6 + fr.FONT_HEIGHT + offset, 0x90000000); fr.drawString(m.name, sr.getScaledWidth() - fr.getStringWidth(m.name) - 4, offset + 4, -1); count++; } Client.onEvent(new EventRenderGUI()); } } I have just recently stumbled upon this Problem, where the HudRenderer renders the wrong section of the textures and therefore completely destroys the Minecraft Armour and Hunger Bar. I am currently thinking if it is an issue with the DrawRect changing something it shouldn't. But I couldn't find anything about it. (To keep things Clean, the function is Called from another file) public class RenderGUIHandler { @SubscribeEvent public void renderGUI(RenderGameOverlayEvent.Post event){ Client.hud.draw(); } } Any help would be greatly appreciated  
  • Topics

×
×
  • Create New...

Important Information

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