Jump to content

Recommended Posts

Posted

Hi there.

First, I wanted to make a simple tool for copying and replacing blocks, so I can easily copy a chest or something else and place copy it somewhere else.

 

This seems so easy but I have some terrible problems. It looks Java bites me when I want to make a copy of something but it's a link not a copy. When I am copying a block everything is ok but when I set another block, the TileEntity of the original block disappears and the new block also don't have a TileEntity. When I set a block to another it even doesn't set the metadata... -.-

 

Wrench of copying:

 

package jantomedes.maptools.wrenches;

import jantomedes.maptools.chainreactivity.ChainSettings;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class WrenchOfCopying extends Item{

public WrenchOfCopying(int par1) {
	super(par1);
	setMaxStackSize(1);
}

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10){
	ChainSettings.idForSetting = par3World.getBlockId(par4, par5, par6);
	ChainSettings.metadataForSetting = par3World.getBlockMetadata(par4, par5, par6);
	ChainSettings.tileEntityForSetting = par3World.getBlockTileEntity(par4, par5, par6);
	return true;
}

@SideOnly(Side.CLIENT)
public boolean isFull3D()
    {
        return true;
    }

@SideOnly(Side.CLIENT)
public void updateIcons(IconRegister par){
	this.iconIndex = par.registerIcon("maptools:wrench");
}

}

 

 

Wrench of setting:

 

package jantomedes.maptools.wrenches;

import jantomedes.maptools.chainreactivity.ChainSettings;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class WrenchOfSetting extends Item{

public WrenchOfSetting(int par1) {
	super(par1);
	setMaxStackSize(1);
}

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10){
	par3World.removeBlockTileEntity(par4, par5, par6);
	par3World.setBlock(par4, par5, par6, ChainSettings.idForSetting, ChainSettings.metadataForSetting, 3);
	TileEntity te = ChainSettings.tileEntityForSetting;
	te.xCoord = par4;
	te.yCoord = par5;
	te.zCoord = par6;
	te.worldObj = par3World;
	par3World.setBlockTileEntity(par4, par5, par6, te);
	par3World.markBlockForRenderUpdate(par4, par5, par6);
	return true;
}

@SideOnly(Side.CLIENT)
public boolean isFull3D()
    {
        return true;
    }

@SideOnly(Side.CLIENT)
public void updateIcons(IconRegister par){
	this.iconIndex = par.registerIcon("maptools:wrench");
}

}

 

 

ChainSettings:

 

package jantomedes.maptools.chainreactivity;

import net.minecraft.tileentity.TileEntity;

public class ChainSettings {
public static int textureID = 1;
public static int textureMetadata = 0;
public static boolean isSimple = true;
public static boolean isVerySimple = true;
public static EnumChainReactionType type = EnumChainReactionType.breakBlock;
public static int wait = 6;
public static boolean spreadReaction = true;
public static int idForBlock;
public static int metadataForBlock;
public static boolean shouldReactOnExp, shouldReactOnBreaking = true;
public static boolean shouldReactOnGettingPowered, shouldReactOnRightClick = false;

public static int idForSetting = 1;
public static int metadataForSetting = 0;
public static TileEntity tileEntityForSetting = null;
}

 

 

Regards. Jantomedes

Posted

another "learn java" thread?

Oh, you didn't read my post carefully. YES, this exactly a "learn java" thread.

 

Many of you already have a reflex 'if it's a Java question, we have to hate it immediately'. But I believe there are some kind people who don't have it and know the difference between a question from a noob who doesn't learn Java at all or he does it 'by modding' and a question from a person who know the basics of Java but sometimes also has some problems.

 

http://en.wikipedia.org/wiki/Reference_type

you'd have to do a deep copy.

 

Thanks for saying me exactly what I wrote in my post. Very helpful and clever.

 

That's very practical if I can ask a question on a forum and get the precise name of my problem. I also believe that there are some of these kind people who can give me a little explanation and write an example. Thanks.

 

---------------------

If I've gone too far, sorry - emotions.

Posted

You asked about "glitch", but it's not a glitch - it's a feature of programming language you're working with. If you want to know how to do a deep copy of TE, I'm not sure that it's possible in some general way (like be compatible with TEs from all mods).

 

And yes, when it's a basic Java question, you shouldn't be asking here (and I probably shoudn't have answered). I'm sorry if I'm underestimating you, but from the quesion it didn't seem that you know what a term "reference type" means.

 

I wrote it before, it's not that I don't want to help, it's just that this forum is filled with basic modding questions described in detail on wiki and with basic java questions which are here offtopic (without decent OO background it's a terrible idea to learn Java by modding). It's just that more complex problems which needs more advanced modders in order to be answered are buried. And those advanced modders just stop going on the forum, because it's all trivial for them, just repeating same basic issues over and over again.

 

PS: The question "how to clone a TE" is interesting, I'm curious about how it could be done. Maybe serialize wanted entity into NBT, modifying coordinates and recreating it on the other place?

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Posted

First off, there is a difference between asking how to program in Java, and not knowing enough to know if it's a Java issue or a Forge issue. Yes it is an essential part of Java that any Forge programmer ought to know, but he is also asking a valid question regardless of his aptitude for the language.

 

Now, on to your question:

Usually when you want to do something that is already done in the game, or another forge mod, the best thing to do is to look in the source code:

if (!this.worldObj.isRemote)
{
    Block.blocksList[par4].onBlockAdded(this.worldObj, j2, par2, k2);
}

if (Block.blocksList[par4] != null && Block.blocksList[par4].hasTileEntity(par5))
{
    tileentity = this.getChunkBlockTileEntity(par1, par2, par3);
    
    if (tileentity == null)
    {
        tileentity = Block.blocksList[par4].createTileEntity(this.worldObj, par5);
        this.worldObj.setBlockTileEntity(j2, par2, k2, tileentity);
    }

    if (tileentity != null)
    {
        tileentity.updateContainingBlockInfo();
        tileentity.blockMetadata = par5;
    }
}

These are lines 765-785 in Chunk.java (net.minecraft.world.chunk)

 

I'd suggest you use the code that this uses, seeing as how it obviously works. This means that you first check if the block you are adding has a tile entity with

hasTileEntity(int metadata)

Note that the metadata value doesn't actually matter unless it is overridden in the block. Once you've confirmed that the block has a tile entity, call

TileEntity tileentity = blockToCopy.createTileEntity(worldObj, metadata);
worldObj.setBlockTileEntity(x, y, z, tileentity);

or

worldObj.setBlockTileEntity(x, y, z, blockToCopy.createTileEntity(worldObj, metadata));

if you want to shorten it and don't need to reference the TileEntity again, which you will if you're going to copy nbt data as I'll show below.

 


 

As for copying the NBT data out of the block and into a new one, add an NBTTagCompound variable (let's call it "nbt" for the following example) to ChainSettings for storage. When you copy the block, simply call

copiedBlockTileEntity.writeToNBT(nbt)

That will set your nbt data to be passed into the pasted block (assuming that the block has a tile entity. Make sure to check for this as well.) After you create your new block and ensure that a new TileEntity is in place, use the converse nbt storage method:

pastedBlockTileEntity.readFromNBT(ChainSettings.nbt)

Make sure to check if ChainSettings.nbt is null before doing this or you could run into problems. If it's null, then you don't have any data to copy and we can assume that the copied block doesn't have any nbt data (no tile entity.) Also make sure to clear ChainSettings.nbt by setting it to null after reading it, for safety.

 

Please note that I haven't tested any of the above code, but it should work or give you good start to getting your wrenches working.

 

If I helped you, please give me some karma :P

width=336 height=83http://img836.imageshack.us/img836/1237/cooltext624963071.png[/img]

I make games, minecraft mods/plugins, and some graphic art.

 

Current Project: LoECraft - An industrial minecraft server with its own modpack and custom launcher.

Posted

First off, there is a difference between asking how to program in Java, and not knowing enough to know if it's a Java issue or a Forge issue. Yes it is an essential part of Java that any Forge programmer ought to know

It's the same thing - he wrote that it magically uses some link. If he don't know what a "reference type" (or "static" or "value type" or "method" or "field") means, he should stop trying to mod and go back to learning Java. If he don't uderstand it and just copy-paste your code, he'll be back tomorrow with similar "learn java" issue. Also it will be frustrating for him, becuase in the end he ends up with code which is impossible to maintain - copied pieces together without any knowledge why it works.

 

but he is also asking a valid question regardless of his aptitude for the language.

I did write that on the end of my post...

 

I wonder how machines from BC, IC2 or computers from RP or CC wound react on this copying. *thinking about multiblock structures* I don't think that a lot (if any) modders were considering this when writing their TE.

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Posted

First off, there is a difference between asking how to program in Java, and not knowing enough to know if it's a Java issue or a Forge issue. Yes it is an essential part of Java that any Forge programmer ought to know

It's the same thing - he wrote that it magically uses some link. If he don't know what a "reference type" (or "static" or "value type" or "method" or "field") means, he should stop trying to mod and go back to learning Java. If he don't uderstand it and just copy-paste your code, he'll be back tomorrow with similar "learn java" issue. Also it will be frustrating for him, becuase in the end he ends up with code which is impossible to maintain - copied pieces together without any knowledge why it works.

 

but he is also asking a valid question regardless of his aptitude for the language.

I did write that on the end of my post...

 

I wonder how machines from BC, IC2 or computers from RP or CC wound react on this copying. *thinking about multiblock structures* I don't think that a lot (if any) modders were considering this when writing their TE.

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Posted

You asked about "glitch", but it's not a glitch - it's a feature of programming language you're working with. If you want to know how to do a deep copy of TE, I'm not sure that it's possible in some general way (like be compatible with TEs from all mods).

 

And yes, when it's a basic Java question, you shouldn't be asking here (and I probably shoudn't have answered). I'm sorry if I'm underestimating you, but from the quesion it didn't seem that you know what a term "reference type" means.

 

I wrote it before, it's not that I don't want to help, it's just that this forum is filled with basic modding questions described in detail on wiki and with basic java questions which are here offtopic (without decent OO background it's a terrible idea to learn Java by modding). It's just that more complex problems which needs more advanced modders in order to be answered are buried. And those advanced modders just stop going on the forum, because it's all trivial for them, just repeating same basic issues over and over again.

 

PS: The question "how to clone a TE" is interesting, I'm curious about how it could be done. Maybe serialize wanted entity into NBT, modifying coordinates and recreating it on the other place?

 

I knew what a reference type is but didn't know that all not primitive types are reference types automatically. And your answer helped me - I've read on the wiki about that. I have to say it wasn't true that you didn't helped me.

 

First off, there is a difference between asking how to program in Java, and not knowing enough to know if it's a Java issue or a Forge issue. Yes it is an essential part of Java that any Forge programmer ought to know, but he is also asking a valid question regardless of his aptitude for the language.

 

Now, on to your question:

Usually when you want to do something that is already done in the game, or another forge mod, the best thing to do is to look in the source code:

if (!this.worldObj.isRemote)
{
    Block.blocksList[par4].onBlockAdded(this.worldObj, j2, par2, k2);
}

if (Block.blocksList[par4] != null && Block.blocksList[par4].hasTileEntity(par5))
{
    tileentity = this.getChunkBlockTileEntity(par1, par2, par3);
    
    if (tileentity == null)
    {
        tileentity = Block.blocksList[par4].createTileEntity(this.worldObj, par5);
        this.worldObj.setBlockTileEntity(j2, par2, k2, tileentity);
    }

    if (tileentity != null)
    {
        tileentity.updateContainingBlockInfo();
        tileentity.blockMetadata = par5;
    }
}

These are lines 765-785 in Chunk.java (net.minecraft.world.chunk)

 

I'd suggest you use the code that this uses, seeing as how it obviously works. This means that you first check if the block you are adding has a tile entity with

hasTileEntity(int metadata)

Note that the metadata value doesn't actually matter unless it is overridden in the block. Once you've confirmed that the block has a tile entity, call

TileEntity tileentity = blockToCopy.createTileEntity(worldObj, metadata);
worldObj.setBlockTileEntity(x, y, z, tileentity);

or

worldObj.setBlockTileEntity(x, y, z, blockToCopy.createTileEntity(worldObj, metadata));

if you want to shorten it and don't need to reference the TileEntity again, which you will if you're going to copy nbt data as I'll show below.

 


 

As for copying the NBT data out of the block and into a new one, add an NBTTagCompound variable (let's call it "nbt" for the following example) to ChainSettings for storage. When you copy the block, simply call

copiedBlockTileEntity.writeToNBT(nbt)

That will set your nbt data to be passed into the pasted block (assuming that the block has a tile entity. Make sure to check for this as well.) After you create your new block and ensure that a new TileEntity is in place, use the converse nbt storage method:

pastedBlockTileEntity.readFromNBT(ChainSettings.nbt)

Make sure to check if ChainSettings.nbt is null before doing this or you could run into problems. If it's null, then you don't have any data to copy and we can assume that the copied block doesn't have any nbt data (no tile entity.) Also make sure to clear ChainSettings.nbt by setting it to null after reading it, for safety.

 

Please note that I haven't tested any of the above code, but it should work or give you good start to getting your wrenches working.

 

If I helped you, please give me some karma :P

 

You didn't have to write the first part so precisely because I know how to create a new TE of good type. But thanks for the last lines with neat way of saving TE to NBT. Without that and without mnm saying it could be done like this, I would have made it with manually saving important values from TEs. Oh, or I even wouldn't because they are private.

 

And grab some karma for this NBT ;-)

 

First off, there is a difference between asking how to program in Java, and not knowing enough to know if it's a Java issue or a Forge issue. Yes it is an essential part of Java that any Forge programmer ought to know

It's the same thing - he wrote that it magically uses some link. If he don't know what a "reference type" (or "static" or "value type" or "method" or "field") means, he should stop trying to mod and go back to learning Java. If he don't uderstand it and just copy-paste your code, he'll be back tomorrow with similar "learn java" issue. Also it will be frustrating for him, becuase in the end he ends up with code which is impossible to maintain - copied pieces together without any knowledge why it works.

 

but he is also asking a valid question regardless of his aptitude for the language.

I did write that on the end of my post...

 

I wonder how machines from BC, IC2 or computers from RP or CC wound react on this copying. *thinking about multiblock structures* I don't think that a lot (if any) modders were considering this when writing their TE.

 

Mnn, you're making a little mistake. I have to say I didn't know how reference types are working but I know the basic Java. Of course I know what static/value type/method/field means. I am not somebody who thinks that learning Java by modding is a good way to do that. It isn't a way at all.

 

So why if I know Java I have such simple problem? Well, I know the basic Java. And basic means much more than just 'static' or 'field'. The point is that I have sometimes some gaps in my knowledge. The main reason is probably that I didn't understand something and forgot to get back to it. Maybe sounds dumb but that's it.

 

I don't learn Java by modding. At most just realize what I've missed or what I have to remind.

 

Anyway, this was a Java question so sorry for 'offtopicing'. And thanks fro your answers.

 

I will worry later about multiblock structures.

 

And these emotions were because of comparing me to noobs which think that they can learn Java by modding.

 

Regards. Jantomedes

 

@Down: Argh... So embarrassing...

PS. assert this.nativeLanguage != Languages.ENGLISH; ;-)

Posted

You asked about "glitch", but it's not a glitch - it's a feature of programming language you're working with. If you want to know how to do a deep copy of TE, I'm not sure that it's possible in some general way (like be compatible with TEs from all mods).

 

And yes, when it's a basic Java question, you shouldn't be asking here (and I probably shoudn't have answered). I'm sorry if I'm underestimating you, but from the quesion it didn't seem that you know what a term "reference type" means.

 

I wrote it before, it's not that I don't want to help, it's just that this forum is filled with basic modding questions described in detail on wiki and with basic java questions which are here offtopic (without decent OO background it's a terrible idea to learn Java by modding). It's just that more complex problems which needs more advanced modders in order to be answered are buried. And those advanced modders just stop going on the forum, because it's all trivial for them, just repeating same basic issues over and over again.

 

PS: The question "how to clone a TE" is interesting, I'm curious about how it could be done. Maybe serialize wanted entity into NBT, modifying coordinates and recreating it on the other place?

 

I knew what a reference type is but didn't know that all not primitive types are reference types automatically. And your answer helped me - I've read on the wiki about that. I have to say it wasn't true that you didn't helped me.

 

First off, there is a difference between asking how to program in Java, and not knowing enough to know if it's a Java issue or a Forge issue. Yes it is an essential part of Java that any Forge programmer ought to know, but he is also asking a valid question regardless of his aptitude for the language.

 

Now, on to your question:

Usually when you want to do something that is already done in the game, or another forge mod, the best thing to do is to look in the source code:

if (!this.worldObj.isRemote)
{
    Block.blocksList[par4].onBlockAdded(this.worldObj, j2, par2, k2);
}

if (Block.blocksList[par4] != null && Block.blocksList[par4].hasTileEntity(par5))
{
    tileentity = this.getChunkBlockTileEntity(par1, par2, par3);
    
    if (tileentity == null)
    {
        tileentity = Block.blocksList[par4].createTileEntity(this.worldObj, par5);
        this.worldObj.setBlockTileEntity(j2, par2, k2, tileentity);
    }

    if (tileentity != null)
    {
        tileentity.updateContainingBlockInfo();
        tileentity.blockMetadata = par5;
    }
}

These are lines 765-785 in Chunk.java (net.minecraft.world.chunk)

 

I'd suggest you use the code that this uses, seeing as how it obviously works. This means that you first check if the block you are adding has a tile entity with

hasTileEntity(int metadata)

Note that the metadata value doesn't actually matter unless it is overridden in the block. Once you've confirmed that the block has a tile entity, call

TileEntity tileentity = blockToCopy.createTileEntity(worldObj, metadata);
worldObj.setBlockTileEntity(x, y, z, tileentity);

or

worldObj.setBlockTileEntity(x, y, z, blockToCopy.createTileEntity(worldObj, metadata));

if you want to shorten it and don't need to reference the TileEntity again, which you will if you're going to copy nbt data as I'll show below.

 


 

As for copying the NBT data out of the block and into a new one, add an NBTTagCompound variable (let's call it "nbt" for the following example) to ChainSettings for storage. When you copy the block, simply call

copiedBlockTileEntity.writeToNBT(nbt)

That will set your nbt data to be passed into the pasted block (assuming that the block has a tile entity. Make sure to check for this as well.) After you create your new block and ensure that a new TileEntity is in place, use the converse nbt storage method:

pastedBlockTileEntity.readFromNBT(ChainSettings.nbt)

Make sure to check if ChainSettings.nbt is null before doing this or you could run into problems. If it's null, then you don't have any data to copy and we can assume that the copied block doesn't have any nbt data (no tile entity.) Also make sure to clear ChainSettings.nbt by setting it to null after reading it, for safety.

 

Please note that I haven't tested any of the above code, but it should work or give you good start to getting your wrenches working.

 

If I helped you, please give me some karma :P

 

You didn't have to write the first part so precisely because I know how to create a new TE of good type. But thanks for the last lines with neat way of saving TE to NBT. Without that and without mnm saying it could be done like this, I would have made it with manually saving important values from TEs. Oh, or I even wouldn't because they are private.

 

And grab some karma for this NBT ;-)

 

First off, there is a difference between asking how to program in Java, and not knowing enough to know if it's a Java issue or a Forge issue. Yes it is an essential part of Java that any Forge programmer ought to know

It's the same thing - he wrote that it magically uses some link. If he don't know what a "reference type" (or "static" or "value type" or "method" or "field") means, he should stop trying to mod and go back to learning Java. If he don't uderstand it and just copy-paste your code, he'll be back tomorrow with similar "learn java" issue. Also it will be frustrating for him, becuase in the end he ends up with code which is impossible to maintain - copied pieces together without any knowledge why it works.

 

but he is also asking a valid question regardless of his aptitude for the language.

I did write that on the end of my post...

 

I wonder how machines from BC, IC2 or computers from RP or CC wound react on this copying. *thinking about multiblock structures* I don't think that a lot (if any) modders were considering this when writing their TE.

 

Mnn, you're making a little mistake. I have to say I didn't know how reference types are working but I know the basic Java. Of course I know what static/value type/method/field means. I am not somebody who thinks that learning Java by modding is a good way to do that. It isn't a way at all.

 

So why if I know Java I have such simple problem? Well, I know the basic Java. And basic means much more than just 'static' or 'field'. The point is that I have sometimes some gaps in my knowledge. The main reason is probably that I didn't understand something and forgot to get back to it. Maybe sounds dumb but that's it.

 

I don't learn Java by modding. At most just realize what I've missed or what I have to remind.

 

Anyway, this was a Java question so sorry for 'offtopicing'. And thanks fro your answers.

 

I will worry later about multiblock structures.

 

And these emotions were because of comparing me to noobs which think that they can learn Java by modding.

 

Regards. Jantomedes

 

@Down: Argh... So embarrassing...

PS. assert this.nativeLanguage != Languages.ENGLISH; ;-)

Posted
I am not somebody who thinks that learning Java by modding is a good way to do that. It isn't a way at all.

Glad to hear this. I might have judged you too quickly, my fault. (Also parts of my posts were meant in a general way. I'm probably not very good at expressing this sort of things in English.) And looking at posts I wonder if I shoudn't have picked a better nick, a lot of people is misspelling it ;D.

 

See you around and happy modding o/.

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Posted
I am not somebody who thinks that learning Java by modding is a good way to do that. It isn't a way at all.

Glad to hear this. I might have judged you too quickly, my fault. (Also parts of my posts were meant in a general way. I'm probably not very good at expressing this sort of things in English.) And looking at posts I wonder if I shoudn't have picked a better nick, a lot of people is misspelling it ;D.

 

See you around and happy modding o/.

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

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.