Jump to content

[1.7.10] Kill a minecart without its drops


IceMetalPunk

Recommended Posts

I'm trying to kill a particular minecart, but without it dropping anything. (The reason is that I'm making a rail which destroys carts that travel over it, but with its own custom drops instead of the minecart's default drops.) I haven't been able to figure out how to do this. Whether I cal entity.setDead() or entity.killMinecart(), it always drops its own items. I've even tried setting entity.captureDrops=true before killing it, as the code seemed to suggest that would stop the drops, but it didn't.

 

How can I kill a minecart without any drops? (That includes killing container minecarts without them dropping their contents, too, by the way.)

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

I can't update to 1.8 as I'm trying to keep my mod compatible next to some popular mods that haven't updated yet. But it seems that wouldn't be what I'm looking for anyway; I need it to not drop *anything*, including the cart itself. This is true not only of container minecarts, but just regular minecarts as well, which don't even have that variable member.

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

*EDIT* Wait, no, I'm not an idiot. Maybe. For some odd reason, the onEntityCollidedWithBlock() method is getting called twice per collision, both on the server. Why is it doing that? This is the code I'm using, in a custom class that extends BlockRailPowered:

 

	@Override
public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {
	if (world.isRemote || !(entity instanceof EntityMinecart)) {
		return;
	}
	NBTTagCompound cartTags = (NBTTagCompound) entity.getEntityData().copy();
	ItemStack cartItem = ((EntityMinecart) entity).getCartItem();
	cartItem.stackTagCompound = cartTags;
	EntityItem droppedItem = new EntityItem(world, entity.posX, entity.posY, entity.posZ, cartItem);
	entity.setDead();
	world.spawnEntityInWorld(droppedItem);
	System.out.println("World is remote? "+world.isRemote);
}

 

As you can see, I'm outputting one message with the world's remote status, yet every collision, I'm getting two identical outputs, both saying the world is not remote, but both spawning item entities. What's with the doubling?

 

Previous post below...

>_< Wait, never mind. It turns out, I'm an idiot. I posted this after testing with a plain minecart with no inventory, and I thought it was dropping itself because it was dropping two minecarts, whereas the custom drops were supposed to be just one. After you said calling .setDead() on a regular minecart shouldn't drop anything, I did a bit more testing, and I realized I was making the stupid mistake of spawning my custom itemstacks on both the server and the client, which is why there were two...so the problem I thought I had didn't actually exist >_<

 

Sorry! I do still need to make sure container carts don't drop their items, though, so I'll try to make it work with reflection and if I have any trouble, I'll ask about it here. Thanks for the help!

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

Not sure why that is, but you shouldn't really care about it.

Um...it's extremely important. The code is designed to make a copy of the minecart in item form--in other words, to "break it" without its components (i.e. cart and chest, cart and hopper, etc.) or its inventory contents flying everywhere. If that code is running twice per collision, it becomes an inventory duplicator, where players can roll a minecart chest full of items over it and get twice the inventory back, then rinse and repeat. It's a game-breaking bug... I don't know why it's happening, but it definitely needs to be fixed before I can release the mod.

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

So even if you kill the Minecart on the first collision it is called again? You can easily work around that by only interacting with the Minecart if

entity.isDead

is false.

D'oh, so simple! xD

 

So now I've fixed that problem, and run into another. I'm trying to copy the cart's NBT data into the dropped ItemStack's stackTagCompound. If I understand correctly, that should mean if it breaks a container cart, it'll drop a container cart item which, when placed, will have the same contents the original cart had, yes? And yet, though I'm not getting any errors, the dropped cart is always empty when placed, even if the original cart had contents. I thought it'd be as simple as using .getEntityData().copy() on the minecart and setting the ItemStack's stackTagCompound to that, but it didn't work. So then I tried creating a new NBTTagCompound and writing the minecart's NBT data to it, then setting *that* to stackTagCompound...still nothing.

 

Here's the current code; why isn't it copying the contents properly, and how can I make it do that?

 

@Override
public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {
	if (world.isRemote || !(entity instanceof EntityMinecart)) {
		return;
	}
	if (entity.isDead) {
		return;
	}
	NBTTagCompound cartTags = new NBTTagCompound();
	entity.writeToNBT(cartTags);
	ItemStack cartItem = ((EntityMinecart) entity).getCartItem();
	cartItem.stackSize = 1;
	cartItem.stackTagCompound = cartTags;
	System.out.println(cartItem.stackTagCompound); // tags print out perfectly fine, including contents
	EntityItem droppedItem = new EntityItem(world, entity.posX, entity.posY, entity.posZ, cartItem);

	if (entity instanceof EntityMinecartContainer) {
		try {
			Field dropConts = EntityMinecartContainer.class.getDeclaredField("dropContentsWhenDead");
			dropConts.setAccessible(true);
			dropConts.setBoolean(entity, false);
		}
		catch (NoSuchFieldException e) {
			System.out.println("No such field!");
		}
		catch (SecurityException e) {
			System.out.println("Security exception!");
		}
		catch (IllegalArgumentException e) {
			System.out.println("Illegal Argument!");
		}
		catch (IllegalAccessException e) {
			System.out.println("Illegal Access Exception!");
		}
	}

	entity.setDead();
	world.spawnEntityInWorld(droppedItem);
}

 

If the tags print fine, why don't they get set when I place the cart back down? Why is the inventory lost?

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

Ah. I was under the impression that setting the item stack's stackTagCompound would cause those tags to be used on the item placed, regardless of what the item was, where applicable. Okay, I'll try doing it with event handlers, then, and hopefully I can make this work. Thanks again.

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

Ah. I was under the impression that setting the item stack's stackTagCompound would cause those tags to be used on the item placed, regardless of what the item was, where applicable.

 

Because

1) Minecraft totally has that behavior natively (hint: item stack NBT is used for hardly nothing: enchantments and nametags and that's about it).

2) That's totally how code works: reading arbitrary data tags and knowing what the fuck to do with them.

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.

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.