Jump to content

Recommended Posts

Posted (edited)

I would like to find all sign-blocks and their position in the chunk at my player position. My problem is that I dont know how to iterate through the blocks in the chunk.

 

EntityPlayer player = Minecraft.getMinecraft().player;
World world = player.getEntityWorld();
Chunk chunk  = world.getChunkFromBlockCoords(player.getPosition());

Map<BlockPos, TileEntity> myMap = chunk.getTileEntityMap();
for(double x = 0; x <= 16; x++) {
		 System.out.println(chunk.getBlockState(new BlockPos(x+player.posX, player.posY , player.posZ)));  
}  

What I dont understand is how do I get the blocks of exactly the chunk where myPlayerPosition is. I can use loops with offsets but I dont understand how the coordinations work. because for "chunk.getBlockState() I need to set a blockposition but how do I get the position of the first/last block in that actual chunk?

 

I'm looking for something like :

for(double x = 0; x <= 16; x++) {
		 System.out.println(chunk.getBlockState(new BlockPos(x+offset,blablabla)));  
}  

I know its only one dimensional but I want to get it right first before I go on for the other axis. I need to understand how I can turn this relative chunk coordinations into real worldcoordination.

 

I already found this but the topic is more about blockproperties then the iterating.

 

Edited by HenryFoster
Posted

Edit:

 

I got it:

 

@SubscribeEvent
	public void onKeyInput(InputEvent.KeyInputEvent event) 
	{
	  if(KeyBindings.myKey.isPressed())
	  {
		   System.out.println("KEY K");
	   
		   EntityPlayer player = Minecraft.getMinecraft().player;
		   World world = player.getEntityWorld();
		   Chunk chunk  = world.getChunkFromBlockCoords(player.getPosition());
	
		   int a = chunk.x*16;
		   int b = chunk.z*16;
		   System.out.println("Chunk starts at: "+ a*16 + " " + b*16);
		   for(double x = 0; x <= 15; x++) 
		   {  
			   for(double y = player.posY-2; y <= player.posY+10; y++)
			   {
				   for(double z = 0; z <= 15; z++)
				   {
					   //System.out.println(chunk.getBlockState(new BlockPos(a+x,player.posY , b+z)));  
					   BlockPos actualPosition = new BlockPos(a+x,y, b+z);
					   IBlockState state = chunk.getBlockState(actualPosition);
					   if(state.getBlock() instanceof BlockSign) 
					   {
						   for(ITextComponent i: ((TileEntitySign)world.getTileEntity(actualPosition)).signText) 
						   {
							   System.out.println(i.getUnformattedComponentText());
						   }
					   }
				   }
			   }
			   
				   
		   }
		   
	  	}
	}

I use the Chunk coordiantes a and b (yes I know bad names I will change them) and multiply them with 16 to get the start coordinates of the chunk where my player is. Later I define the position of the block "I'm looking at" with the starting position of the chunk + x/z to get the global-coordinates of that block.

Posted
20 hours ago, HenryFoster said:

multiply them

Don’t multiply, use bit shifting. The results are different (integer multiplication rounds down)

  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted

Multiplying works out the same. Its dividing that causes problems.

(You should still bitshift)

  • Like 1

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.

Posted

Theres also a performance gain with bitshifting

  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • the modpack keep crashing idk why,cause it never said anything about any mods causing it. crash log:https://drive.google.com/file/d/1iYKlUgvHUob8DjyRc3gqP_Viv_kSHO6L/view?usp=sharing mod list:https://drive.google.com/file/d/1MvMT-z9Jg2BITQ4uLshJ1uOh7q9EMBfC/view?usp=sharing but the server(anternos) works just fine
    • Hello, I am trying to make 2 recipes for a ruby. The first one is turning a block into a ruby and the other one is 9 nuggets into a ruby. But I keep on getting a error java.lang.IllegalStateException: Duplicate recipe rubymod:ruby   Any help would be great on how to fix it
    • Hello everyone, i'm new with programing Mods, and will need a lot of your help if possible,  Im trying to make a new GUI interface responsible to control the Droprate of game, it will control de loot drop and loot table for mobs and even blocks, but i try to make a simple Gui Screen, and wenever i try to use it, the game crash's with the error message in the subject, here is the code im using to:  IDE: IntelliJ Comunity - latest version Forge: 47.3.0 Minecraft version: 1.20.1 mapping_channel: parchment mapping_version=2023.09.03-1.20.1 Crash report link: https://pastebin.com/6dV8k1Fw   Code im using is:    package createchronical.droprateconfig; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import java.util.HashMap; import java.util.Map; public class ConfigScreen extends Screen { private static final ResourceLocation BACKGROUND_TEXTURE = new ResourceLocation("droprateconfig", "textures/gui/config_background.png"); // Mapa de mobs e itens com seus respectivos drop rates private final Map<String, Integer> dropRates = new HashMap<>(); public ConfigScreen() { super(Component.literal("Configurações de Drop Rate")); // Inicializa com valores de drop rate padrão dropRates.put("Zombie", 10); // Exemplo de mob dropRates.put("Creeper", 5); // Exemplo de mob dropRates.put("Iron Ore", 50); // Exemplo de item dropRates.put("Diamond", 2); // Exemplo de item } @Override protected void init() { // Cria um botão para cada mob/item e adiciona na tela int yOffset = this.height / 2 - 100; // Posicionamento inicial for (Map.Entry<String, Integer> entry : dropRates.entrySet()) { String itemName = entry.getKey(); int dropRate = entry.getValue(); // Cria um botão para cada mob/item this.addRenderableWidget(Button.builder( Component.literal(itemName + ": " + dropRate + "%"), button -> onDropRateButtonPressed(itemName) ).bounds(this.width / 2 - 100, yOffset, 200, 20).build()); yOffset += 25; // Incrementa a posição Y para o próximo botão } // Adiciona o botão de "Salvar Configurações" this.addRenderableWidget(Button.builder(Component.literal("Salvar Configurações"), button -> onSavePressed()) .bounds(this.width / 2 - 100, yOffset, 200, 20) .build()); } private void onDropRateButtonPressed(String itemName) { // Lógica para alterar o drop rate do item/mob selecionado // Aqui, vamos apenas incrementar o valor como exemplo int currentRate = dropRates.get(itemName); dropRates.put(itemName, currentRate + 5); // Aumenta o drop rate em 5% } private void onSavePressed() { // Lógica para salvar as configurações (temporariamente apenas na memória) // Vamos apenas imprimir para verificar dropRates.forEach((item, rate) -> { System.out.println("Item: " + item + " | Novo Drop Rate: " + rate + "%"); }); // Fecha a tela após salvar Screen pGuiScreen = null; assert this.minecraft != null; this.minecraft.setScreen(pGuiScreen); } @Override public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { this.renderBackground(guiGraphics); guiGraphics.blit(BACKGROUND_TEXTURE, this.width / 2 - 128, this.height / 2 - 128, 0, 0, 256, 256, 256, 256); super.render(guiGraphics, mouseX, mouseY, partialTicks); } }  
    • Also add the latest.log from /logs/
  • Topics

×
×
  • Create New...

Important Information

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