2016-06-22 23:49:47 -04:00
|
|
|
/*
|
2022-04-14 03:42:00 +00:00
|
|
|
* Copyright (c) Forge Development LLC and contributors
|
2022-02-28 13:41:23 -08:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
2016-06-22 23:49:47 -04:00
|
|
|
*/
|
|
|
|
|
|
2014-04-02 18:33:13 -07:00
|
|
|
package net.minecraftforge.event;
|
|
|
|
|
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.world.entity.player.Player;
|
2021-11-26 05:09:56 +08:00
|
|
|
import net.minecraft.world.inventory.AnvilMenu;
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2018-06-10 21:12:46 -04:00
|
|
|
import net.minecraftforge.eventbus.api.Cancelable;
|
|
|
|
|
import net.minecraftforge.eventbus.api.Event;
|
2022-06-07 15:31:26 -05:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2016-11-17 10:44:31 -08:00
|
|
|
|
2014-04-02 18:33:13 -07:00
|
|
|
/**
|
2022-06-07 15:31:26 -05:00
|
|
|
*
|
2022-08-09 21:48:46 +02:00
|
|
|
* AnvilUpdateEvent is fired whenever the input stacks (left or right) or the name in an anvil changed. <br>
|
2021-11-26 05:09:56 +08:00
|
|
|
* It is called from {@link AnvilMenu#createResult()}. <br>
|
2020-11-10 15:05:47 -05:00
|
|
|
* If the event is canceled, vanilla behavior will not run, and the output will be set to {@link ItemStack#EMPTY}. <br>
|
|
|
|
|
* If the event is not canceled, but the output is not empty, it will set the output and not run vanilla behavior. <br>
|
|
|
|
|
* if the output is empty, and the event is not canceled, vanilla behavior will execute. <br>
|
2014-04-02 18:33:13 -07:00
|
|
|
*/
|
|
|
|
|
@Cancelable
|
2025-04-19 13:18:48 +01:00
|
|
|
public final class AnvilUpdateEvent extends Event {
|
2020-11-10 15:05:47 -05:00
|
|
|
private final ItemStack left;
|
|
|
|
|
private final ItemStack right;
|
|
|
|
|
private final String name;
|
|
|
|
|
private ItemStack output;
|
2024-04-30 10:56:00 -07:00
|
|
|
private long cost;
|
2020-11-10 15:05:47 -05:00
|
|
|
private int materialCost;
|
2021-07-17 00:08:49 -05:00
|
|
|
private final Player player;
|
2020-11-10 15:05:47 -05:00
|
|
|
|
2024-04-30 10:56:00 -07:00
|
|
|
public AnvilUpdateEvent(ItemStack left, ItemStack right, String name, long cost, Player player) {
|
2014-04-02 18:33:13 -07:00
|
|
|
this.left = left;
|
|
|
|
|
this.right = right;
|
2016-12-21 15:52:30 -08:00
|
|
|
this.output = ItemStack.EMPTY;
|
2014-04-02 18:33:13 -07:00
|
|
|
this.name = name;
|
2020-11-10 15:05:47 -05:00
|
|
|
this.player = player;
|
2016-03-24 11:44:52 +03:00
|
|
|
this.setCost(cost);
|
|
|
|
|
this.setMaterialCost(0);
|
2014-04-02 18:33:13 -07:00
|
|
|
}
|
2016-03-24 11:44:52 +03:00
|
|
|
|
2020-11-10 15:05:47 -05:00
|
|
|
/**
|
|
|
|
|
* @return The item in the left input (leftmost) anvil slot.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public ItemStack getLeft() {
|
2020-11-10 15:05:47 -05:00
|
|
|
return left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return The item in the right input (center) anvil slot.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public ItemStack getRight() {
|
2020-11-10 15:05:47 -05:00
|
|
|
return right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is the name as sent by the client. It may be null if none has been sent. <br>
|
|
|
|
|
* If empty, it indicates the user wishes to clear the custom name from the item.
|
|
|
|
|
* @return The name that the output item will be set to, if applicable.
|
|
|
|
|
*/
|
|
|
|
|
@Nullable
|
2024-04-30 10:56:00 -07:00
|
|
|
public String getName() {
|
2020-11-10 15:05:47 -05:00
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is the output as determined by the event, not by the vanilla behavior between these two items. <br>
|
|
|
|
|
* If you are the first receiver of this event, it is guaranteed to be empty. <br>
|
|
|
|
|
* It will only be non-empty if changed by an event handler. <br>
|
|
|
|
|
* If this event is cancelled, this output stack is discarded.
|
|
|
|
|
* @return The item to set in the output (rightmost) anvil slot.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public ItemStack getOutput() {
|
2020-11-10 15:05:47 -05:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the output slot to a specific itemstack.
|
|
|
|
|
* @param output The stack to change the output to.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public void setOutput(ItemStack output) {
|
2020-11-10 15:05:47 -05:00
|
|
|
this.output = output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-06-07 15:31:26 -05:00
|
|
|
* This is the level cost of this anvil operation. <br>
|
2020-11-10 15:05:47 -05:00
|
|
|
* When unchanged, it is guaranteed to be left.getRepairCost() + right.getRepairCost().
|
|
|
|
|
* @return The level cost of this anvil operation.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public long getCost() {
|
2020-11-10 15:05:47 -05:00
|
|
|
return cost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the level cost of this operation. <br>
|
|
|
|
|
* The level cost does prevent the output from being available. <br>
|
|
|
|
|
* That is, a player without enough experience may not take the output.
|
|
|
|
|
* @param cost The new level cost.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public void setCost(long cost) {
|
2020-11-10 15:05:47 -05:00
|
|
|
this.cost = cost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The material cost is how many units of the right input stack are consumed.
|
|
|
|
|
* @return The material cost of this anvil operation.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public int getMaterialCost() {
|
2020-11-10 15:05:47 -05:00
|
|
|
return materialCost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets how many right inputs are consumed. <br>
|
|
|
|
|
* A material cost of zero consumes the entire stack. <br>
|
|
|
|
|
* A material cost higher than the count of the right stack
|
|
|
|
|
* consumes the entire stack. <br>
|
|
|
|
|
* The material cost does not prevent the output from being available.
|
|
|
|
|
* @param materialCost The new material cost.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public void setMaterialCost(int materialCost) {
|
2020-11-10 15:05:47 -05:00
|
|
|
this.materialCost = materialCost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return The player using this anvil container.
|
|
|
|
|
*/
|
2024-04-30 10:56:00 -07:00
|
|
|
public Player getPlayer() {
|
2020-11-10 15:05:47 -05:00
|
|
|
return this.player;
|
|
|
|
|
}
|
2014-04-02 18:33:13 -07:00
|
|
|
}
|