Add Player context to AnvilUpdateEvent (#7442)
This commit is contained in:
parent
15bac0eeaa
commit
f3b53001c9
3 changed files with 134 additions and 30 deletions
|
@ -26,7 +26,7 @@
|
|||
+
|
||||
if (!itemstack2.func_190926_b()) {
|
||||
- boolean flag = itemstack2.func_77973_b() == Items.field_151134_bR && !EnchantedBookItem.func_92110_g(itemstack2).isEmpty();
|
||||
+ if (!net.minecraftforge.common.ForgeHooks.onAnvilChange(this, itemstack, itemstack2, field_234642_c_, field_82857_m, j)) return;
|
||||
+ if (!net.minecraftforge.common.ForgeHooks.onAnvilChange(this, itemstack, itemstack2, field_234642_c_, field_82857_m, j, this.field_234645_f_)) return;
|
||||
+ flag = itemstack2.func_77973_b() == Items.field_151134_bR && !EnchantedBookItem.func_92110_g(itemstack2).isEmpty();
|
||||
if (itemstack1.func_77984_f() && itemstack1.func_77973_b().func_82789_a(itemstack, itemstack2)) {
|
||||
int l2 = Math.min(itemstack1.func_77952_i(), itemstack1.func_77958_k() / 4);
|
||||
|
|
|
@ -672,9 +672,15 @@ public class ForgeHooks
|
|||
return ret;
|
||||
}
|
||||
|
||||
@Deprecated // TODO: Remove 1.17 - Use player-contextual version below.
|
||||
public static boolean onAnvilChange(RepairContainer container, @Nonnull ItemStack left, @Nonnull ItemStack right, IInventory outputSlot, String name, int baseCost)
|
||||
{
|
||||
AnvilUpdateEvent e = new AnvilUpdateEvent(left, right, name, baseCost);
|
||||
return onAnvilChange(container, left, right, outputSlot, name, baseCost, null);
|
||||
}
|
||||
|
||||
public static boolean onAnvilChange(RepairContainer container, @Nonnull ItemStack left, @Nonnull ItemStack right, IInventory outputSlot, String name, int baseCost, PlayerEntity player)
|
||||
{
|
||||
AnvilUpdateEvent e = new AnvilUpdateEvent(left, right, name, baseCost, player);
|
||||
if (MinecraftForge.EVENT_BUS.post(e)) return false;
|
||||
if (e.getOutput().isEmpty()) return true;
|
||||
|
||||
|
|
|
@ -19,52 +19,150 @@
|
|||
|
||||
package net.minecraftforge.event;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.container.RepairContainer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
*
|
||||
* AnvilUpdateEvent is fired when a player places items in both the left and right slots of a anvil.
|
||||
* If the event is canceled, vanilla behavior will not run, and the output will be set to null.
|
||||
* If the event is not canceled, but the output is not null, it will set the output and not run vanilla behavior.
|
||||
* if the output is null, and the event is not canceled, vanilla behavior will execute.
|
||||
* AnvilUpdateEvent is fired when the inputs (either input stack, or the name) to an anvil are changed. <br>
|
||||
* It is called from {@link RepairContainer#updateRepairOutput}. <br>
|
||||
* 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>
|
||||
*/
|
||||
@Cancelable
|
||||
public class AnvilUpdateEvent extends Event
|
||||
{
|
||||
@Nonnull
|
||||
private final ItemStack left; // The left side of the input
|
||||
@Nonnull
|
||||
private final ItemStack right; // The right side of the input
|
||||
private final String name; // The name to set the item, if the user specified one.
|
||||
@Nonnull
|
||||
private ItemStack output; // Set this to set the output stack
|
||||
private int cost; // The base cost, set this to change it if output != null
|
||||
private int materialCost; // The number of items from the right slot to be consumed during the repair. Leave as 0 to consume the entire stack.
|
||||
|
||||
public AnvilUpdateEvent(@Nonnull ItemStack left, @Nonnull ItemStack right, String name, int cost)
|
||||
private final ItemStack left;
|
||||
private final ItemStack right;
|
||||
private final String name;
|
||||
private ItemStack output;
|
||||
private int cost;
|
||||
private int materialCost;
|
||||
@Nullable // TODO: Remove 1.17 - Nullable only in the instance that a mod uses the deprecated ctor.
|
||||
private final PlayerEntity player;
|
||||
|
||||
@Deprecated //TODO: Remove 1.17 - Use Player-contextual constructor below.
|
||||
public AnvilUpdateEvent(ItemStack left, ItemStack right, String name, int cost)
|
||||
{
|
||||
this(left, right, name, cost, null);
|
||||
}
|
||||
|
||||
public AnvilUpdateEvent(ItemStack left, ItemStack right, String name, int cost, PlayerEntity player)
|
||||
{
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.output = ItemStack.EMPTY;
|
||||
this.name = name;
|
||||
this.player = player;
|
||||
this.setCost(cost);
|
||||
this.setMaterialCost(0);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public ItemStack getLeft() { return left; }
|
||||
@Nonnull
|
||||
public ItemStack getRight() { return right; }
|
||||
public String getName() { return name; }
|
||||
@Nonnull
|
||||
public ItemStack getOutput() { return output; }
|
||||
public void setOutput(@Nonnull ItemStack output) { this.output = output; }
|
||||
public int getCost() { return cost; }
|
||||
public void setCost(int cost) { this.cost = cost; }
|
||||
public int getMaterialCost() { return materialCost; }
|
||||
public void setMaterialCost(int materialCost) { this.materialCost = materialCost; }
|
||||
/**
|
||||
* @return The item in the left input (leftmost) anvil slot.
|
||||
*/
|
||||
public ItemStack getLeft()
|
||||
{
|
||||
return left;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The item in the right input (center) anvil slot.
|
||||
*/
|
||||
public ItemStack getRight()
|
||||
{
|
||||
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
|
||||
public String getName()
|
||||
{
|
||||
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.
|
||||
*/
|
||||
public ItemStack getOutput()
|
||||
{
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the output slot to a specific itemstack.
|
||||
* @param output The stack to change the output to.
|
||||
*/
|
||||
public void setOutput(ItemStack output)
|
||||
{
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the level cost of this anvil operation. <br>
|
||||
* When unchanged, it is guaranteed to be left.getRepairCost() + right.getRepairCost().
|
||||
* @return The level cost of this anvil operation.
|
||||
*/
|
||||
public int getCost()
|
||||
{
|
||||
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.
|
||||
*/
|
||||
public void setCost(int cost)
|
||||
{
|
||||
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.
|
||||
*/
|
||||
public int getMaterialCost()
|
||||
{
|
||||
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.
|
||||
*/
|
||||
public void setMaterialCost(int materialCost)
|
||||
{
|
||||
this.materialCost = materialCost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nullable in the case someone is using the deprecated constructor.
|
||||
* @return The player using this anvil container.
|
||||
*/
|
||||
@Nullable // TODO: Remove 1.17
|
||||
public PlayerEntity getPlayer()
|
||||
{
|
||||
return this.player;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue