Jump to content

Recommended Posts

Posted

Hi,

 

I just started working on my very first mod. Its the second Java project I'm working on, thus I do have some basic knowledge about the language itself.

 

The mod is very similar to CPWs IronChest. In fact his code was among the many I looked at the one I learned most from. He is using a single block for all of his chests. So am I. He however is using separate TileEntity classes for every kind of chest. I guess he decided to do it this way because the chunk system calls a block's TileEntity's default constructor and does thus not supply any information about the block (metadata).

 

This is the code of interest:

 

public TileEntityAdvancedChest()
{
// problem area
}

public TileEntityAdvancedChest(ChestType type)
{
this.type = type;
this.size = type.rows * type.columns;
this.inv = new ItemStack[this.size];
}

 

Using the block's metadata to create the desired TileEntity is easy as long as the right constructor is being called (the second one). But is there some way to get the block's metadata whenever the default constructor is being called? I don't like the solution of having a lot of different classes flying around. One idea I've had was creating classes during runtime, based on the different chest types (using a class template), but I don't have a clue how to achieve something like this in Java. The search for enum based generic classes didn't help to much, because they were using the enumerator class, whilst I'd like to somehow use the values.

 

The registry would look like this:

for (AdvancedChestType type : AdvancedChestType.values())
{
type.registerName(advancedChestBlock);
GameRegistry.registerTileEntity(TileEntityAdvancedChest<type>.class, "tileEntity" + type.realName);
}

 

Maybe someone around here knows a neat way for this to work out.

 

Cyclonit :)

Posted

You probably can call worldObj.getBlockMetadata(X, Y, Z) in your constructor to get the block's metadata. With it you can call your second constructor with the type parameter.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

I'd need the block's coordinates for that and I cannot use "this" in the constructor to get something.

 

Yes, you can:

this.xCoord

this.yCoord

this.zCoord

and call your constructor:

this(type)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Writing it this way:

 

public TileEntityAdvancedChest()
{
	int meta = this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);

	this(AdvancedChestType.values()[meta]);
}

 

Isn't possible because the constructor call must be the first line in the default constructor.

 

public TileEntityAdvancedChest()
{
	this(AdvancedChestType.values()[this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord]);
}

 

Doesn't work because you cannot use "this" (or super) while calling a constructor for the class itself.

Posted

Writing it this way:

 

public TileEntityAdvancedChest()
{
	int meta = this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord);

	this(AdvancedChestType.values()[meta]);
}

 

Isn't possible because the constructor call must be the first line in the default constructor.

 

public TileEntityAdvancedChest()
{
	this(AdvancedChestType.values()[this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord]);
}

 

Doesn't work because you cannot use "this" (or super) while calling a constructor for the class itself.

 

Oh yeah, sorry. My bad. There is a way... Can I see your Block class?

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Of course:

 

package cyclonit.advancedChests;

import java.util.List;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import cyclonit.advancedChests.core.AdvancedChestType;
import cyclonit.advancedChests.core.Constants;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.World;

public class BlockAdvancedChest extends Block {

private AdvancedChestType type;

public BlockAdvancedChest(int blockID, AdvancedChestType type) {
	super(blockID, Material.iron);

	this.type = type;

	this.setCreativeTab(CreativeTabs.tabDecorations);
	this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
}

@Override
public void getSubBlocks(int blockID, CreativeTabs creativeTabs, List itemList)
{
	for (AdvancedChestType type : AdvancedChestType.values())
	{
		itemList.add(new ItemStack(blockID, 1, type.ordinal()));
	}
}


/*
 *  Textures
 *  
 *  We are using a single block-ID to store several blocks. Thus we
 *  must provide the block's texture based on the metadata instead
 *  of using just the ID.
 */
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconRegister)
{
	for (AdvancedChestType type : AdvancedChestType.values())
	{
		type.registerIcons(iconRegister);
	}
}

@SideOnly(Side.CLIENT)
@Override
public Icon getBlockTextureFromSideAndMetadata(int side, int meta)
{
	return AdvancedChestType.values()[meta].getTexture(side);
}



/*
 * TileEntity
 * 
 * Using a single block-ID for several blocks requires using the
 * metadata for deciding what kind of a TileEntity we need. We
 * therefore use createTileEntity(World, int) instead of the usual
 * createNewTileEntity(World).
 */
@Override
public boolean hasTileEntity(int meta)
{
	return meta < AdvancedChestType.values().length;
}

@Override
public TileEntity createTileEntity(World world, int meta) {
	return new TileEntityAdvancedChest(AdvancedChestType.values()[meta]);
}


/*
 * Container
 */
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int a, float b, float c, float d) 
{
	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
	if (tileEntity == null || player.isSneaking()) 
	{
		return false;
	}
	// open GUI 0
	player.openGui(AdvancedChests.instance, 0, world, x, y, z);
	return true;
}

}

Posted

I'm doing that already. The metadata is being resolved to a type and then passed on to the TE constructor.

 

The issue is this:

2013-05-04 09:45:51 [iNFO] [sTDERR] java.lang.InstantiationException: cyclonit.advancedChests.TileEntityAdvancedChest
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at java.lang.Class.newInstance0(Unknown Source)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at java.lang.Class.newInstance(Unknown Source)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.tileentity.TileEntity.createAndLoadEntity(TileEntity.java:137)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.world.chunk.storage.AnvilChunkLoader.readChunkFromNBT(AnvilChunkLoader.java:432)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.world.chunk.storage.AnvilChunkLoader.checkedReadChunkFromNBT(AnvilChunkLoader.java:103)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadChunk(AnvilChunkLoader.java:83)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.world.gen.ChunkProviderServer.safeLoadChunk(ChunkProviderServer.java:182)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:118)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.server.MinecraftServer.initialWorldChunkLoad(MinecraftServer.java:281)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.server.integrated.IntegratedServer.loadAllWorlds(IntegratedServer.java:88)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.server.integrated.IntegratedServer.startServer(IntegratedServer.java:105)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:429)
2013-05-04 09:45:51 [iNFO] [sTDERR] 	at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16)

 

The function TileEntity.createAndLoadEntity calls (TileEntity)oclass.newInstance(); and thus not my constructor but the default one.

Posted

What do you mean by that? The problem is, that the default constructor is being called during chunk loading. It does however not know (yet) what kind of a chest it is supposed to create (it would need the block's metadata).

 

Here is my TE:

package cyclonit.advancedChests;

import cyclonit.advancedChests.core.AdvancedChestType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityAdvancedChest extends TileEntity implements IInventory {

private AdvancedChestType type;

private ItemStack[] inv;
private int size;


/*
 * Constructors
 * 
 * We need the default constructor to bypass the first instantiation
 * by TileEntity.createAndLoadEntity().
 */	
public TileEntityAdvancedChest(AdvancedChestType type) 
{
	this.type = type;
	this.size = type.rows * type.columns;
	this.inv = new ItemStack[this.size];
}


public AdvancedChestType getType()
{
	return this.type;
}

@Override
public int getSizeInventory()
{
	return this.size;
}

@Override
public ItemStack getStackInSlot(int i) 
{
	return this.inv[i];
}

@Override
public String getInvName() {
	return "container." + this.type.realName;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}



@Override
public ItemStack decrStackSize(int slot, int amount) 
{
	if (this.inv[slot] != null)
	{
		ItemStack stack;

		if (this.inv[slot].stackSize <= amount)
		{
			stack = this.inv[slot];
			this.inv[slot] = null;
		}
		else
		{
			stack = this.inv[slot].splitStack(amount);
		}

		this.onInventoryChanged();
		return stack;
	}
	else
	{
		return null;
	}
}

@Override
public ItemStack getStackInSlotOnClosing(int i) 
{
	// If there is something in the selected slot, drop it.
	if (this.inv[i] != null)
	{
		ItemStack itemstack = this.inv[i];
		this.inv[i] = null;
		return itemstack;
	}
	else
	{
		return null;
	}
}

@Override
public void setInventorySlotContents(int i, ItemStack itemStack) {
	this.inv[i] = itemStack;

	if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit())
	{
		itemStack.stackSize = this.getInventoryStackLimit();
	}

	this.onInventoryChanged();		
}



@Override
public boolean isInvNameLocalized() {
	return false;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityPlayer) {
	return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
}

@Override
public boolean isStackValidForSlot(int i, ItemStack itemstack) {
	return true;
}



@Override
public void openChest() {

}

@Override
public void closeChest() {

}


/*
 * NBT
 */
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
	super.readFromNBT(tagCompound);

	NBTTagList tagList = tagCompound.getTagList("Inventory");
	for (int i = 0; i < tagList.tagCount(); i++) {
		NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
		byte slot = tag.getByte("Slot");
		if (slot >= 0 && slot < inv.length) {
			inv[slot] = ItemStack.loadItemStackFromNBT(tag);
		}
	}
}

@Override
public void writeToNBT(NBTTagCompound tagCompound) {
	super.writeToNBT(tagCompound);

	NBTTagList itemList = new NBTTagList();
	for (int i = 0; i < inv.length; i++) {
		ItemStack stack = inv[i];
		if (stack != null) {
			NBTTagCompound tag = new NBTTagCompound();
			tag.setByte("Slot", (byte) i);
			stack.writeToNBT(tag);
			itemList.appendTag(tag);
		}
	}
	tagCompound.setTag("Inventory", itemList);
}


}

Posted

Thank you both for your input :)

 

In readFromNBT accessing the block's metadata directly is impossible because neither this.getBlockMetadata() nor this.getWorldObj() are available. But working around this is fairly straight forward: writeToNBT can access the block's metadata. I thus store the block's metadata alongside the chest's inventory and load it in readFromNBT.

 

Here is my full solution:

package cyclonit.advancedChests;

import cyclonit.advancedChests.core.AdvancedChestType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityAdvancedChest extends TileEntity implements IInventory {

private AdvancedChestType type;

private ItemStack[] inv;
private int size;


/*
 * Constructors
 * 
 * We need the default constructor to bypass the first instantiation
 * by TileEntity.createAndLoadEntity().
 */	
public TileEntityAdvancedChest()
{
	super();
}

public TileEntityAdvancedChest(AdvancedChestType type) 
{
	this.type = type;
	this.size = type.rows * type.columns;
	this.inv = new ItemStack[this.size];
}


public AdvancedChestType getType()
{
	return this.type;
}

@Override
public int getSizeInventory()
{
	return this.size;
}

@Override
public ItemStack getStackInSlot(int i) 
{
	return this.inv[i];
}

@Override
public String getInvName() {
	return "container." + this.type.realName;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}



@Override
public ItemStack decrStackSize(int slot, int amount) 
{
	if (this.inv[slot] != null)
	{
		ItemStack stack;

		if (this.inv[slot].stackSize <= amount)
		{
			stack = this.inv[slot];
			this.inv[slot] = null;
		}
		else
		{
			stack = this.inv[slot].splitStack(amount);
		}

		this.onInventoryChanged();
		return stack;
	}
	else
	{
		return null;
	}
}

@Override
public ItemStack getStackInSlotOnClosing(int i) 
{
	// If there is something in the selected slot, drop it.
	if (this.inv[i] != null)
	{
		ItemStack itemstack = this.inv[i];
		this.inv[i] = null;
		return itemstack;
	}
	else
	{
		return null;
	}
}

@Override
public void setInventorySlotContents(int i, ItemStack itemStack) {
	this.inv[i] = itemStack;

	if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit())
	{
		itemStack.stackSize = this.getInventoryStackLimit();
	}

	this.onInventoryChanged();		
}



@Override
public boolean isInvNameLocalized() {
	return false;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityPlayer) {
	return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
}

@Override
public boolean isStackValidForSlot(int i, ItemStack itemstack) {
	return true;
}



@Override
public void openChest() {

}

@Override
public void closeChest() {

}


/*
 * NBT
 * 
 * In addition to storing the chest's inventory, we use the NBT
 * to store the chest's metadata. We can thus always load the
 * correct type without having to use different classes for every
 * kind of chest.
 */
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
	super.readFromNBT(tagCompound);

	// Load the type
	int meta = tagCompound.getInteger("Metadata");
	this.type = AdvancedChestType.values()[meta];
	this.size = this.type.rows * this.type.columns;
	this.inv = new ItemStack[this.size];

	// Load the inventory
	NBTTagList tagList = tagCompound.getTagList("Inventory");
	for (int i = 0; i < tagList.tagCount(); i++) {
		NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
		byte slot = tag.getByte("Slot");
		if (slot >= 0 && slot < inv.length) {
			inv[slot] = ItemStack.loadItemStackFromNBT(tag);
		}
	}
}

@Override
public void writeToNBT(NBTTagCompound tagCompound) {
	super.writeToNBT(tagCompound);

	// Save the inventory
	NBTTagList itemList = new NBTTagList();
	for (int i = 0; i < inv.length; i++) {
		ItemStack stack = inv[i];
		if (stack != null) {
			NBTTagCompound tag = new NBTTagCompound();
			tag.setByte("Slot", (byte) i);
			stack.writeToNBT(tag);
			itemList.appendTag(tag);
		}
	}
	tagCompound.setTag("Inventory", itemList);

	// Save the metadata
	tagCompound.setInteger("Metadata", this.getBlockMetadata());
}


}

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

    • Hi all,  I have the following issue: I'd like to parse some json using gson and rely on the (somewhat new) java record types. This is supported in gson 2.10+. Gson is already a dependency used by minecraft, however it's 2.8.x for 1.19.2 which I'm targeting at the moment. My idea was to include the newer version of the library in my mod and then shadow it so it gets used inside the scope of my mod instead of the older 2.8. This works fine for building the jar: If I decompile my mod.jar, I can see that it's correctly using the shadowed classes. However obviously when using the runClient intellj config, the shadowing doesn't get invoked. Is there any way of invoking shadow when using runClient, or am I on the wrong track and there's a better way of doing this entirely? Thanks in advance!
    • Yep I did upgrade just because it showed me a new version available.  I'll redownload the mod list and make sure anything works.  Thanks!
    • The latest log was taken down by pastebin for some reason. Did you try removing the mods you added? The mods you updated, was there a specific reason you updated, or just because? It's possible the updates introduced incompatibilitie, or even need a newer build of forge. If you didn't need the updates for a specific reason, you could also try downgrading those mods.
    • Please read the FAQ, and post logs as described there. https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/
    • I am using forge 1.20.1 (version 47.3.0). My pc has an RTX 4080 super and an i9 14900 KF, I am on the latest Nvidia graphics driver, latest windows 10 software, I have java 23, forge 1.12.2 works and so does all vanilla versions but for some reason no version of forge 1.20.1 works and instead the game just crashes with the error code "-1." I have no mods in my mods fodler, I have deleted my options.txt and forge.cfg files in case my settings were causing a crash and have tried removing my forge version from the installations folder and reinstalling but no matter what I still crash with the same code and my log doesn't tell me anything: 18:34:53.924 game 2025-02-06 18:34:53,914 main WARN Advanced terminal features are not available in this environment 18:34:54.023 game [18:34:54] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, mrmirchi, --version, 1.20.1-forge-47.3.0, --gameDir, C:\Users\aryam\AppData\Roaming\.minecraft, --assetsDir, C:\Users\aryam\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 2db00ea8d678420a8956109a85d90e9d, --accessToken, ????????, --clientId, ZWI3NThkNzMtNmNlZS00MGI5LTgyZTgtYmZkNzcwMTM5MGMx, --xuid, 2535436222989555, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\aryam\AppData\Roaming\.minecraft\quickPlay\java\1738838092785.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.3.0, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] 18:34:54.027 game [18:34:54] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 18:34:54.132 game [18:34:54] [main/INFO] [ne.mi.fm.lo.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow 18:34:54.191 game [18:34:54] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6 18:34:54.303 game [18:34:54] [main/INFO] [EARLYDISPLAY/]: Requested GL version 4.6 got version 4.6 18:34:54.367 monitor Process Monitor Process crashed with exit code -1     screenshot of log: https://drive.google.com/file/d/1WdkH88H865XErvmIqAKjlg7yrmj8EYy7/view?usp=sharing
  • Topics

×
×
  • Create New...

Important Information

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