Jump to content

[SOLVED] Moving a string from a TileEntity to an ItemStack


SnowyEgret

Recommended Posts

I have a field of type String on a TileEntity provided by a block. When I break and then pick up the block, I want the string on the ItemStack in the player's inventory. My intention is to use the string when I place the block again with method

 

	public void onBlockPlacedBy(World w, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {

 

I have overridden

	public void harvestBlock(World worldIn, EntityPlayer playerIn, BlockPos pos, IBlockState state, TileEntity te) {

 

which gives me the string, but I don't know how to put it on the ItemStack's NBT when the player picks the block up. Any help appreciated.

Link to comment
Share on other sites

Here is my getDops modeled on BlockFlowerPot:

 

 

@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
	List<ItemStack> itemStacks = super.getDrops(world, pos, state, fortune);
	TileEntity te = world.getTileEntity(pos);
	if (te == null) {
		System.out.println("Could not write path to tag. te=" + te);
		return itemStacks;
	}

	if (te instanceof BlockSavedTileEntity) {
		String path = ((BlockSavedTileEntity) te).getPath();
		ItemStack stack = new ItemStack(this);
		NBTTagCompound tag = stack.getTagCompound();
		((BlockSavedTileEntity) te).writeToNBT(tag);
		System.out.println("tag=" + tag);
		itemStacks.add(stack);
	} else {
		System.out.println("Could not write path to tag. TileEntity not a BlockSavedTileEntity. te=" + te);
	}

	return itemStacks;
}

 

 

 

but te is coming up null (maybe because it was broken?). Do I still need to override harvestBlock and pass string path somehow? Or is there a problem with my TileEntity?

 

 

 

package org.snowyegret.mojo.block;

import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class BlockSavedTileEntity extends TileEntity {

private String path;

public void setPath(String path) {
	this.path = path;
}

public String getPath() {
	return path;
}

@Override
public void writeToNBT(NBTTagCompound tag) {
	if (path == null) {
		System.out.println("Could not write path to tag. path=" + path);
	} else {
		tag.setString(BlockSaved.KEY_PATH, path);
		super.writeToNBT(tag);
	}
}

@Override
public void readFromNBT(NBTTagCompound tag) {
	path = tag.getString(BlockSaved.KEY_PATH);
	super.readFromNBT(tag);
}

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound tag = new NBTTagCompound();
	writeToNBT(tag);
	super.writeToNBT(tag);
	return new S35PacketUpdateTileEntity(pos, this.getBlockMetadata(), tag);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
	readFromNBT(pkt.getNbtCompound());
	super.readFromNBT(pkt.getNbtCompound());
}

}

 

 

Link to comment
Share on other sites

Is there something else I have missed? Now the block does not drop when I break it. (Same behavior when I don't override harvestBlock)

 

 

 

@Override
public boolean removedByPlayer(World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
	System.out.println("willHarvest=" + willHarvest);
	new Throwable().printStackTrace();
	if (willHarvest) {
		// Delay deletion of the block until after getDrops
		return true;
	}
	return super.removedByPlayer(world, pos, player, willHarvest);
}

@Override
public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te) {
	System.out.println("te=" + te);
	super.harvestBlock(world, player, pos, state, te);
	world.setBlockToAir(pos);
}

 

 

 

Here my console. Is it normal that removeByPlayer is called twice with different values for willHarvest?

 

 

 

[13:39:11] [server thread/INFO] [sTDOUT]: [org.snowyegret.mojo.item.spell.other.SpellSave:setText:87]: path=/tmp/Foo8418776298188150160.save
[13:39:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.block.BlockSaved:removedByPlayer:135]: willHarvest=false
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: java.lang.Throwable
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at org.snowyegret.mojo.block.BlockSaved.removedByPlayer(BlockSaved.java:136)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDestroyBlock(PlayerControllerMP.java:165)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.client.multiplayer.PlayerControllerMP.func_180511_b(PlayerControllerMP.java:252)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.client.Minecraft.clickMouse(Minecraft.java:1520)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.client.Minecraft.runTick(Minecraft.java:2127)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1088)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.client.Minecraft.run(Minecraft.java:376)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.client.main.Main.main(Main.java:117)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at java.lang.reflect.Method.invoke(Method.java:497)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)
[13:39:13] [Client thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at GradleStart.main(Unknown Source)
[13:39:13] [server thread/INFO] [sTDOUT]: [org.snowyegret.mojo.block.BlockSaved:removedByPlayer:135]: willHarvest=true
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: java.lang.Throwable
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at org.snowyegret.mojo.block.BlockSaved.removedByPlayer(BlockSaved.java:136)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.server.management.ItemInWorldManager.removeBlock(ItemInWorldManager.java:296)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.server.management.ItemInWorldManager.tryHarvestBlock(ItemInWorldManager.java:349)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.server.management.ItemInWorldManager.onBlockClicked(ItemInWorldManager.java:233)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:552)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:53)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:76)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:24)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:714)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:727)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:669)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:171)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:540)
[13:39:13] [server thread/INFO] [sTDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: 	at java.lang.Thread.run(Thread.java:745)
[13:39:13] [server thread/INFO] [sTDOUT]: [org.snowyegret.mojo.block.BlockSaved:harvestBlock:146]: te=org.snowyegret.mojo.block.BlockSavedTileEntity@3209ab1
[

 

 

 

 

Link to comment
Share on other sites

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.