Jump to content

[1.12] AnvilRepairEvent: Keep item in left slot


IceMetalPunk

Recommended Posts

I'm using the AnvilUpdateEvent to create some custom anvil behavior when an item of mine is in the left slot of the anvil. It works perfectly! However, when the output is taken by the player, I'd like my item to remain in the left slot (with some damage, but that's the easy part). This is what I can't figure out how to do.

It seems the AnvilRepairEvent is called before the left slot is cleared, and there's no way to stop that from clearing. So how do I either prevent the left slot from clearing, or else put the item back after it's cleared to create the illusion that it remained?

Currently, I'm just giving the player the item in the AnvilRepairEvent, but it's quite annoying to do multiple anvil "crafts" if you have to keep putting it back into the anvil each time.

 

*EDIT* Update: I've managed to get it working, but it's with a clearly very hacky workaround, and I don't like how it looks at all. I'll post my code below, but can anyone come up with a less... erm, messy solution?
 

// Stupid, hacky workaround for there being no way to stop the left anvil
// slot from clearing!
public static HashMap<EntityPlayer, Pair<ContainerRepair, ItemStack>> anvilRefreshers = new HashMap<>();

@SubscribeEvent
public void onTick(PlayerTickEvent ev) {
	if (anvilRefreshers.containsKey(ev.player)) {
		Pair<ContainerRepair, ItemStack> pair = anvilRefreshers.get(ev.player);
		pair.getLeft().putStackInSlot(0, pair.getRight());
		anvilRefreshers.remove(ev.player);
	}
}

// Then, in the AnvilRepairEvent, when the proper item is in the left slot:

if (player.openContainer instanceof ContainerRepair) {
   	ItemStack give = ev.getItemInput().copy();
	EntityPlayer player = ev.getEntityPlayer();
	ContainerRepair cont = (ContainerRepair) player.openContainer;
	anvilRefreshers.put(player, Pair.of(cont, give));
}

 

I cut out some irrelevant bits, but basically, I'm use the PlayerTickEvent to delay the inventory slot setting by 1 tick, thereby replacing the item in the slot 1 tick after it's been removed. It works perfectly, but... I mean, you can tell just from that type of the anvilRefreshers map that it's ugly code. Is there a better way?

Edited by IceMetalPunk

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

Link to comment
Share on other sites

IIRC, anvil repair is called when something is changed in an input slot. There's another call stack when the anvil's output is removed from the output slot.

I suggest that you explore the anvil code to find what method handles removal. Set a breakpoint and step through. You might not be able to modify what stack(s) are left in the input slots, but you might be able to save enough data in one part of the process that you can drop an extra item that you want preserved.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

3 hours ago, jeffryfisher said:

IIRC, anvil repair is called when something is changed in an input slot. There's another call stack when the anvil's output is removed from the output slot.

I suggest that you explore the anvil code to find what method handles removal. Set a breakpoint and step through. You might not be able to modify what stack(s) are left in the input slots, but you might be able to save enough data in one part of the process that you can drop an extra item that you want preserved.

No, that's the AnvilUpdateEvent, which I'm using for other things. The AnvilRepairEvent is called when you remove items from the output slot. (More specifically, it's fired in the output slot's onTake method, exactly one line before it clears the left input slot.) As you can see from the edit in the original post, I'm making it work by storing the item stack and container in a map and then putting the item back one tick later; that method feels very "hacky" to me, though, especially since it relies on having a tick event handler running every tick, so I was hoping for a more event-based (or at least less frequently running) approach.

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

Link to comment
Share on other sites

15 hours ago, IceMetalPunk said:

No, that's the AnvilUpdateEvent, which I'm using for other things. The AnvilRepairEvent is called when you remove items

Aha, very good. My memory is almost a year rusty.

 

How would your game play work if you got rid of the hacky tick handler and simply dropped the input item as an item entity? Or, what if you returned the item to player inventory, possibly the player's off-hand if empty?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

2 minutes ago, jeffryfisher said:

Aha, very good. My memory is almost a year rusty.

 

How would your game play work if you got rid of the hacky tick handler and simply dropped the input item as an item entity? Or, what if you returned the item to player inventory, possibly the player's off-hand if empty?

Yeah, originally I was just putting it back in the player's inventory. The issue is that this item would often be used for batches of anvil "crafting", so having to keep putting it back into the anvil for each round of crafting can get annoying. (The item, a special totem, allows you to "de-craft" items in an anvil. So for instance, if I wanted to de-craft a stack of wood back into logs, I'd have to perform 16 rounds of crafting, and moving the totem back and forth between the anvil and your inventory 16 times is obnoxious.)

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

Link to comment
Share on other sites

The best I can come up with is to somehow attach a hopper to the side (input2) of the anvil and preferentially drop the token on that hopper if it's there. However, that may be even worse than the tick handler hack.

 

Hmmm... I wonder how much work it will be to rig an anvil for hopper connections (probably need to invent a custom anvil). Then you could input a hopper full of tokens plus a chest full of gold swords to be uncrafted into chests full of gold ingots  :)

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • BD303 merupakan salah satu situs slot mudah scatter paling populer dan digemari oleh kalangan slot online di tahun 2024 mainkan sekarang dengan kesempatan yang mudah menang jackpot jutaan rupiah.
  • Topics

×
×
  • Create New...

Important Information

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