mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-21 08:02:20 +00:00
Add message banner support for recipe pre-events
This commit is contained in:
1 parent
6a65168f4a
commit
01e9122700
4 files changed
+65
-85
No files matched your search
@@ -10,6 +10,7 @@ import noppes.npcs.EventHooks;
|
||||
import noppes.npcs.api.handler.data.IAnvilRecipe;
|
||||
import noppes.npcs.controllers.RecipeController;
|
||||
import noppes.npcs.controllers.data.RecipeAnvil;
|
||||
import noppes.npcs.scripted.event.RecipeScriptEvent;
|
||||
|
||||
public class ContainerAnvilRepair extends Container {
|
||||
// A 2-slot crafting matrix: slot 0 = damaged item, slot 1 = repair material.
|
||||
@@ -27,6 +28,7 @@ public class ContainerAnvilRepair extends Container {
|
||||
public int repairMaterialConsumed = 0;
|
||||
|
||||
private RecipeAnvil currentRecipe;
|
||||
private SlotAnvilOutput resultSlot;
|
||||
|
||||
public ContainerAnvilRepair(InventoryPlayer playerInv, World world, int x, int y, int z) {
|
||||
this.worldObj = world;
|
||||
@@ -36,7 +38,8 @@ public class ContainerAnvilRepair extends Container {
|
||||
this.player = playerInv.player;
|
||||
|
||||
// Output slot (index 0) using our custom SlotAnvilOutput.
|
||||
this.addSlotToContainer(new SlotAnvilOutput(this, anvilResult, 0, 133, 47));
|
||||
this.resultSlot = new SlotAnvilOutput(this, anvilResult, 0, 133, 47);
|
||||
this.addSlotToContainer(this.resultSlot);
|
||||
|
||||
// Input slot 0: damaged item. We restrict its stack size to 1.
|
||||
this.addSlotToContainer(new Slot(anvilMatrix, 0, 26, 47) {
|
||||
@@ -115,6 +118,7 @@ public class ContainerAnvilRepair extends Container {
|
||||
currentRecipe = matchingRecipe;
|
||||
|
||||
ItemStack output = null;
|
||||
boolean canPickup = true;
|
||||
if (matchingRecipe != null) {
|
||||
if (!matchingRecipe.availability.isAvailable(player)) {
|
||||
return;
|
||||
@@ -160,8 +164,17 @@ public class ContainerAnvilRepair extends Container {
|
||||
} else {
|
||||
repairCost = 0;
|
||||
}
|
||||
if (matchingRecipe != null) {
|
||||
ItemStack[] items = new ItemStack[]{input0, input1};
|
||||
RecipeScriptEvent.Pre pre = EventHooks.onRecipeScriptPre(player, matchingRecipe.getScriptHandler(), matchingRecipe, items);
|
||||
canPickup = !pre.isCanceled();
|
||||
output = EventHooks.onRecipeScriptPost(player, matchingRecipe.getScriptHandler(), matchingRecipe, items, output);
|
||||
}
|
||||
|
||||
anvilResult.setInventorySlotContents(0, output);
|
||||
if (this.resultSlot != null) {
|
||||
this.resultSlot.setCanPickup(canPickup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +231,7 @@ public class ContainerAnvilRepair extends Container {
|
||||
copy = stack.copy();
|
||||
if (index == 0) { // output slot
|
||||
SlotAnvilOutput resultSlot = (SlotAnvilOutput) slot;
|
||||
if (!resultSlot.preEvent(player)) {
|
||||
if (!resultSlot.canPickup()) {
|
||||
return null;
|
||||
}
|
||||
if (player.experienceTotal < this.repairCost) {
|
||||
@@ -263,7 +276,6 @@ public class ContainerAnvilRepair extends Container {
|
||||
// subtracts one from the damaged item stack.
|
||||
public class SlotAnvilOutput extends Slot {
|
||||
private final ContainerAnvilRepair container;
|
||||
private boolean preChecked = false;
|
||||
private boolean canPickup = true;
|
||||
|
||||
public SlotAnvilOutput(ContainerAnvilRepair container, IInventory inventory, int index, int x, int y) {
|
||||
@@ -271,25 +283,12 @@ public class ContainerAnvilRepair extends Container {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
public boolean preEvent(EntityPlayer player) {
|
||||
preChecked = true;
|
||||
if (!container.worldObj.isRemote) {
|
||||
RecipeAnvil recipe = container.currentRecipe;
|
||||
if (recipe != null) {
|
||||
ItemStack[] items = new ItemStack[]{
|
||||
container.anvilMatrix.getStackInRowAndColumn(0, 0),
|
||||
container.anvilMatrix.getStackInRowAndColumn(1, 0)
|
||||
};
|
||||
canPickup = !EventHooks.onRecipeScriptPre(player, recipe.getScriptHandler(), recipe, items);
|
||||
if (!canPickup) {
|
||||
container.updateRepairResult();
|
||||
preChecked = false; // reset for next attempt
|
||||
}
|
||||
} else {
|
||||
canPickup = true;
|
||||
}
|
||||
}
|
||||
return canPickup;
|
||||
public void setCanPickup(boolean value) {
|
||||
this.canPickup = value;
|
||||
}
|
||||
|
||||
public boolean canPickup() {
|
||||
return this.canPickup;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -299,31 +298,14 @@ public class ContainerAnvilRepair extends Container {
|
||||
|
||||
@Override
|
||||
public boolean canTakeStack(EntityPlayer player) {
|
||||
if (!preChecked) {
|
||||
preEvent(player);
|
||||
}
|
||||
return canPickup && player.experienceTotal >= container.repairCost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPickupFromSlot(EntityPlayer player, ItemStack stack) {
|
||||
if (!preChecked) {
|
||||
if (!preEvent(player))
|
||||
return;
|
||||
}
|
||||
if (!canPickup) {
|
||||
return;
|
||||
}
|
||||
RecipeAnvil recipe = container.currentRecipe;
|
||||
ItemStack[] items = new ItemStack[]{
|
||||
container.anvilMatrix.getStackInRowAndColumn(0, 0),
|
||||
container.anvilMatrix.getStackInRowAndColumn(1, 0)
|
||||
};
|
||||
if (!container.worldObj.isRemote && recipe != null) {
|
||||
container.updateRepairResult();
|
||||
stack = EventHooks.onRecipeScriptPost(player, recipe.getScriptHandler(), recipe, items, stack);
|
||||
}
|
||||
|
||||
if (player.experienceTotal < container.repairCost) {
|
||||
container.updateRepairResult();
|
||||
return;
|
||||
@@ -353,7 +335,6 @@ public class ContainerAnvilRepair extends Container {
|
||||
}
|
||||
container.repairCost = 0;
|
||||
container.updateRepairResult();
|
||||
preChecked = false;
|
||||
canPickup = true;
|
||||
super.onPickupFromSlot(player, stack);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user