Jump to content

Recommended Posts

Posted

Ok, here is my question, is it possible to have the texture of a custom modeled block change based on an event, like if it's right clicked with an item... I have an idea on how I could do it, but I don't know where to place the code.

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

Well I was planning on making my custom block to change it's texture when it is rightclicked with a certain item, like a stick makes it's texture be texture 2, while on default it would be texture 1. I would assume I could use either a switch to set texture based on metadata of the block, but I don't know how to impliment it.

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

Well, all you really have to do is think logical. Let us analyse what you want:

- change texture

- right click

- change metadata

- stick

 

The block class has a convenient right click method which we can simply override

 

    @Override
    public boolean onBlockActivated(World parWorld, int parXCoordinate, ...)
    {	
return false;
    }

 

The metadata as well can easily be set in this method by calling:

 

parWorld.setBlockMetadataWithNotify(parXCoordinate, parYCoordinate, ...);

 

If it is just two texture you can toggle inbetween them by putting an if statement as a parameter: ParWorld.getBlockMetaData(x, y, z) == 0 ? 1 : 0

 

Selecting what to hold can also be achieved here, simply use the player parameter

 

player.getHeldItem().getItem().equals(Items.stick)

 

And rendering, well, you allready know a switch and then it is a simple matter of finding the correct method, as there are several that do just about the same things so I won't post any and leave it up to you, have a look in the Block.class and simply get the metadata and return the correct texture.

 

Not that hard is it? All you have to do is think logical, have a look in the base classes what is allready there, what you can use and you're good to go.

Posted

Ok, so in my attempt to see if I could get it to start to work, I now get a null point exception when I right click it, here is the crash:

 

[13:08:03] [server thread/ERROR] [FML]: A TileEntity type com.AutonMatrix.tileentity.TileEntityAIBeacon has throw an exception trying to write state. It will not persist. Report this to the mod author
java.lang.RuntimeException: class com.AutonMatrix.tileentity.TileEntityAIBeacon is missing a mapping! This is a bug!
at net.minecraft.tileentity.TileEntity.writeToNBT(TileEntity.java:100) ~[TileEntity.class:?]
at com.AutonMatrix.tileentity.TileEntityAIBeacon.writeToNBT(TileEntityAIBeacon.java:24) ~[TileEntityAIBeacon.class:?]
at net.minecraft.world.chunk.storage.AnvilChunkLoader.writeChunkToNBT(AnvilChunkLoader.java:323) [AnvilChunkLoader.class:?]
at net.minecraft.world.chunk.storage.AnvilChunkLoader.saveChunk(AnvilChunkLoader.java:132) [AnvilChunkLoader.class:?]
at net.minecraft.world.gen.ChunkProviderServer.safeSaveChunk(ChunkProviderServer.java:229) [ChunkProviderServer.class:?]
at net.minecraft.world.gen.ChunkProviderServer.saveChunks(ChunkProviderServer.java:281) [ChunkProviderServer.class:?]
at net.minecraft.world.WorldServer.saveAllChunks(WorldServer.java:875) [WorldServer.class:?]
at net.minecraft.server.MinecraftServer.saveAllWorlds(MinecraftServer.java:370) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.stopServer(MinecraftServer.java:403) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.stopServer(IntegratedServer.java:255) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:531) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:742) [MinecraftServer$2.class:?]
[13:08:03] [server thread/INFO]: Saving chunks for level 'simple'/Nether
[13:08:03] [server thread/INFO]: Saving chunks for level 'simple'/The End
[13:08:03] [server thread/INFO] [FML]: Unloading dimension 0
[13:08:03] [server thread/INFO] [FML]: Unloading dimension -1
[13:08:03] [server thread/INFO] [FML]: Unloading dimension 1
[13:08:04] [Client thread/FATAL]: Unreported exception thrown!
java.lang.NullPointerException
at com.AutonMatrix.block.AM_BlockBeacon.onBlockActivated(AM_BlockBeacon.java:46) ~[AM_BlockBeacon.class:?]
at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerRightClick(PlayerControllerMP.java:380) ~[PlayerControllerMP.class:?]
at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1497) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.runTick(Minecraft.java:2010) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:995) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:910) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:112) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_65]
at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_65]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134) [launchwrapper-1.9.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.9.jar:?]
---- Minecraft Crash Report ----
// My bad.

Time: 8/22/14 1:08 PM
Description: Unexpected error

java.lang.NullPointerException: Unexpected error
at com.AutonMatrix.block.AM_BlockBeacon.onBlockActivated(AM_BlockBeacon.java:46)
at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerRightClick(PlayerControllerMP.java:380)
at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1497)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:2010)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:995)
at net.minecraft.client.Minecraft.run(Minecraft.java:910)
at net.minecraft.client.main.Main.main(Main.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Stacktrace:
at com.AutonMatrix.block.AM_BlockBeacon.onBlockActivated(AM_BlockBeacon.java:46)
at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerRightClick(PlayerControllerMP.java:380)
at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1497)

-- Affected level --
Details:
Level name: MpServer
All players: 1 total; [EntityClientPlayerMP['Player865'/0, l='MpServer', x=850.32, y=2.62, z=-89.52]]
Chunk stats: MultiplayerChunkCache: 270, 270
Level seed: 0
Level generator: ID 01 - flat, ver 0. Features enabled: false
Level generator options: 
Level spawn location: World: (881,4,-102), Chunk: (at 1,0,10 in 55,-7; contains blocks 880,0,-112 to 895,255,-97), Region: (1,-1; contains chunks 32,-32 to 63,-1, blocks 512,0,-512 to 1023,255,-1)
Level time: 77856 game time, 4337 day time
Level dimension: 0
Level storage version: 0x00000 - Unknown?
Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
Forced entities: 1 total; [EntityClientPlayerMP['Player865'/0, l='MpServer', x=850.32, y=2.62, z=-89.52]]
Retry entities: 0 total; []
Server brand: fml,forge
Server type: Integrated singleplayer server
Stacktrace:
at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:412)
at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2522)
at net.minecraft.client.Minecraft.run(Minecraft.java:939)
at net.minecraft.client.main.Main.main(Main.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

 

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

To be specific it seems to happen only with an empty hand, so how do I fix that, or make it detect if right clicked with an empty hand?

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

You have at least a null pointer exception on right click.

Did you actually check if the player is holding something before you're trying to check that illusive item?

Minecraft doesn't check before it returns a method like a sensibly written program would do,

instead it just throws the nulls everywhere and hopes you catch them.

 

And then you have something about nbt which I do not wish to adress since I simply do not know what that is about?

Nbt not being able to find its mapping, how?

It would be nice if someone else could clarify that or otherwise I would definitely need to see the code about that.

Posted

Well, I have fixed that issue, now I have discovered something with my testing, you see I was making my block print a certain phrase when it's right clicked with a stick, and noticed it printed it two times, how would I resolve this, for future reference?

 

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

Ok and now I can't seem to understand how to get the texture to change correctly, I seem to be confusing myself :(

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

To see if I can get help... here are the render file and the block file.

 

package com.AutonMatrix.renderer;

import org.lwjgl.opengl.GL11;

import com.AutonMatrix.block.AM_BlockBeacon;
import com.AutonMatrix.lib.Strings;
import com.AutonMatrix.model.Modelaibeacon;

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;

public class RenderAIBeacon extends TileEntitySpecialRenderer {

private static final ResourceLocation texture = new ResourceLocation(Strings.MODID + ":" + "textures/model/beaco.png");
private static ResourceLocation texturea = AM_BlockBeacon.texturee;
private Modelaibeacon model;

public RenderAIBeacon(){
	this.model = new Modelaibeacon();
}

@Override
public void renderTileEntityAt(TileEntity tileentity, double x, double y, double z, float f) {
	GL11.glPushMatrix();
		GL11.glTranslatef((float)x + 0.5F, (float)y + 1.5F, (float)z + 0.5F);
		GL11.glRotatef(180, 0F, 0F, 1F);

		this.bindTexture(texturea);

		GL11.glPushMatrix();
			this.model.renderModel(0.0625F);
		GL11.glPopMatrix();
	GL11.glPopMatrix();

}

}

package com.AutonMatrix.block;

import com.AutonMatrix.creativeTabs.AM_CreativeTabs;
import com.AutonMatrix.lib.Strings;
import com.AutonMatrix.tileentity.TileEntityAIBeacon;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

public class AM_BlockBeacon extends BlockContainer {

public static ResourceLocation texturee = new ResourceLocation(Strings.MODID + ":" + "textures/model/beaco.png");

public AM_BlockBeacon(String name) {
	super(Material.circuits);
	this.setBlockName(name);
	this.setCreativeTab(AM_CreativeTabs.tabBlock);
	setBlockBounds(6f/16f, 0.0F, 6f/16f, 10f/16f , 0.875f , 10f/16f);

}

public int getRenderType(){
	return -1;
}

public boolean isOpaqueCube(){
	return false;
}

public boolean renderAsNormalBlock(){
	return false;
}

@Override
public TileEntity createNewTileEntity(World var1, int var2) {
	return new TileEntityAIBeacon();
}

@Override
public int onBlockPlaced(World world, int x, int y, int z, int side, float hitx, float hity, float hitz, int metadata)
    {
	texturee = new ResourceLocation(Strings.MODID + ":" + "textures/model/beaco.png");
	return metadata;

    }

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_){
	if(entityplayer.getHeldItem() != null ){
		if(entityplayer.getHeldItem().getItem().equals(Items.stick)){
			System.out.println("Item is a stick");
			texturee = new ResourceLocation(Strings.MODID + ":" + "textures/model/beac2.png");
			return true;
		}
		else{
			System.out.println("Item isn't a stick");
			texturee = new ResourceLocation(Strings.MODID + ":" + "textures/model/beac1.png");
			return true;
		}
	}
	else{
		System.out.println("No Item in hand");
		return true;
	}		


}

}

 

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

I don't want to be annoying but try to find something out yourself, I too had to just look at the error logs and look up how things are done in minecraft. E.g. I've only been modding, like what? 2 weeks? Which also applies to java since I come from c++/vb.net and only got into that to mod minecraft.

If you want to get further than this try to be a bit more proactive in finding your problems.

 

Well, I have fixed that issue, now I have discovered something with my testing, you see I was making my block print a certain phrase when it's right clicked with a stick, and noticed it printed it two times, how would I resolve this, for future reference?

 

I have noticed that the right click function, no matter where it is applied to in minecraft, triggers twice. I haven't bothered looking for a solution since if does that it could be an intended feature, and well, it's so easy to fix in the function itself that it really isn't bothersome at all. It might just be however that the basecode doesn't distinguish between a key-down and a key-up so two keysignals are sent.

In any case, don't worry about it.

 

Ok and now I can't seem to understand how to get the texture to change correctly, I seem to be confusing myself

 

You are changing a value in your tileentity class that isn't passed through to your tileentityrender class and secondly, you're trying to load textures in the middle of the runtime of your block, not really ideal if I may say so.

A simple solution: Make a resourcelocation for every texture in your RenderAIBeacon class.

In your Tileentity class(which doesn't seem to be included)create a new byte variable.

If you want to load texture 1, set that var to 1, if you want to load texture 2 set it to 2 etc.

In your render class again, cast your tileentity to your tileentityclass and simple go and get that value with a switch, and if it is 2 bind texture two, if it is 1 bind texture one etc.

Thirdly, the block class is static, e.g. it persists for all blocks that are made of that type, if you would somehow succeed in changing its texture there it would change it for every single other block of that class in your world.

Posted

Hmm, this does make sense.. Kinda ironic, I get high 90's in college level java programming classes, yet minecraft modding makes little sense to me

Member of Aerotech Networks, a Multi-Gaming server.

Also Currently porting the "Rise of the Automatons" minecraft mod

Posted

It triggers once only. Once for client and once for server so it may seem like it triggers twice. When printing debugging lines to console you should always check the side as well (for example if you have World reference, check is World.isRemote returns true or false).

 

To archive desired effect you should decide do you want to use metadata or nbt to store data for texture. If it is just the texture metadata is easiest way to do so. So you store that data in right-click method and in renderer you retrive that data by getting it from block (metadata for example, TileEntity.getWorldObj().getBlockMetadata(.... ) and then check what it is and set correct texture.

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.