|
@@ -11,7 +11,6 @@ import net.minecraft.inventory.CraftResultInventory;
|
|
|
import net.minecraft.inventory.IInventory;
|
|
import net.minecraft.inventory.IInventory;
|
|
|
import net.minecraft.inventory.Inventory;
|
|
import net.minecraft.inventory.Inventory;
|
|
|
import net.minecraft.inventory.container.Container;
|
|
import net.minecraft.inventory.container.Container;
|
|
|
-import net.minecraft.inventory.container.ContainerType;
|
|
|
|
|
import net.minecraft.inventory.container.Slot;
|
|
import net.minecraft.inventory.container.Slot;
|
|
|
import net.minecraft.item.EnchantedBookItem;
|
|
import net.minecraft.item.EnchantedBookItem;
|
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.ItemStack;
|
|
@@ -23,178 +22,80 @@ import net.minecraftforge.api.distmarker.Dist;
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
import net.minecraftforge.event.AnvilUpdateEvent;
|
|
import net.minecraftforge.event.AnvilUpdateEvent;
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
|
|
-import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
import javax.annotation.Nonnull;
|
|
|
-import javax.annotation.Nullable;
|
|
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class AnvilContainerRe extends Container {
|
|
public class AnvilContainerRe extends Container {
|
|
|
- protected final CraftResultInventory field_234642_c_ = new CraftResultInventory();
|
|
|
|
|
- protected final IInventory field_234643_d_ = new Inventory(2) {
|
|
|
|
|
- /**
|
|
|
|
|
- * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think
|
|
|
|
|
- * it hasn't changed and skip it.
|
|
|
|
|
- */
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 本次合成消耗的物品合成原料个数
|
|
|
|
|
+ public int materialCost;
|
|
|
|
|
+
|
|
|
|
|
+ // 输入与输出物品槽
|
|
|
|
|
+ private final IInventory inputInventory = new Inventory(2) {
|
|
|
public void markDirty() {
|
|
public void markDirty() {
|
|
|
super.markDirty();
|
|
super.markDirty();
|
|
|
AnvilContainerRe.this.onCraftMatrixChanged(this);
|
|
AnvilContainerRe.this.onCraftMatrixChanged(this);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
- protected final IWorldPosCallable field_234644_e_;
|
|
|
|
|
- protected final PlayerEntity field_234645_f_;
|
|
|
|
|
-
|
|
|
|
|
- public AnvilContainerRe(@Nullable ContainerType<?> p_i231587_1_, int p_i231587_2_, PlayerInventory p_i231587_3_, IWorldPosCallable p_i231587_4_) {
|
|
|
|
|
- super(p_i231587_1_, p_i231587_2_);
|
|
|
|
|
- this.field_234644_e_ = p_i231587_4_;
|
|
|
|
|
- this.field_234645_f_ = p_i231587_3_.player;
|
|
|
|
|
- this.addSlot(new Slot(this.field_234643_d_, 0, 27, 47));
|
|
|
|
|
- this.addSlot(new Slot(this.field_234643_d_, 1, 76, 47));
|
|
|
|
|
- this.addSlot(new Slot(this.field_234642_c_, 2, 134, 47) {
|
|
|
|
|
- /**
|
|
|
|
|
- * Check if the stack is allowed to be placed in this slot, used for armor slots as well as furnace fuel.
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ private final CraftResultInventory outputInventory = new CraftResultInventory();
|
|
|
|
|
+
|
|
|
|
|
+ // 构造页面槽位
|
|
|
|
|
+ private void Constuct(PlayerInventory playerInventory) {
|
|
|
|
|
+ this.addSlot(new Slot(this.inputInventory, 0, 27, 47));
|
|
|
|
|
+ this.addSlot(new Slot(this.inputInventory, 1, 76, 47));
|
|
|
|
|
+ this.addSlot(new Slot(this.outputInventory, 2, 134, 47) {
|
|
|
|
|
+ // 禁止将物品放在输出格
|
|
|
public boolean isItemValid(ItemStack stack) {
|
|
public boolean isItemValid(ItemStack stack) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return whether this slot's stack can be taken from this slot.
|
|
|
|
|
- */
|
|
|
|
|
- public boolean canTakeStack(PlayerEntity playerIn) {
|
|
|
|
|
- return AnvilContainerRe.this.func_230303_b_(playerIn, this.getHasStack());
|
|
|
|
|
|
|
+ // 确认玩家能否拿走物品
|
|
|
|
|
+ public boolean canTakeStack(PlayerEntity player) {
|
|
|
|
|
+ return (player.abilities.isCreativeMode || player.experienceLevel >= maximumCost.get()) && maximumCost.get() > 0;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ // 触发玩家拿走物品的事件
|
|
|
public ItemStack onTake(PlayerEntity thePlayer, ItemStack stack) {
|
|
public ItemStack onTake(PlayerEntity thePlayer, ItemStack stack) {
|
|
|
- return AnvilContainerRe.this.func_230301_a_(thePlayer, stack);
|
|
|
|
|
|
|
+ return AnvilContainerRe.this.onTakeOutput(thePlayer, stack);
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // 增加物品栏与快捷栏槽位
|
|
|
|
|
+ int yPositionStart = 84;
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
for (int j = 0; j < 9; ++j) {
|
|
for (int j = 0; j < 9; ++j) {
|
|
|
- this.addSlot(new Slot(p_i231587_3_, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
|
|
|
|
|
|
|
+ this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, yPositionStart + i * 18));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
for (int k = 0; k < 9; ++k) {
|
|
for (int k = 0; k < 9; ++k) {
|
|
|
- this.addSlot(new Slot(p_i231587_3_, k, 8 + k * 18, 152)); // 142
|
|
|
|
|
|
|
+ this.addSlot(new Slot(playerInventory, k, 8 + k * 18, 58 + yPositionStart));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public AnvilContainerRe(int id, PlayerInventory playerInventory) {
|
|
|
|
|
- this(id, playerInventory, IWorldPosCallable.DUMMY);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public AnvilContainerRe(int id, PlayerInventory playerInventory, IWorldPosCallable worldPosCallable) {
|
|
|
|
|
- this(RegMain.containerAnvil.get(), id, playerInventory, worldPosCallable);
|
|
|
|
|
- this.trackInt(this.maximumCost);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Callback for when the crafting matrix is changed.
|
|
|
|
|
- */
|
|
|
|
|
- public void onCraftMatrixChanged(IInventory inventoryIn) {
|
|
|
|
|
- super.onCraftMatrixChanged(inventoryIn);
|
|
|
|
|
- if (inventoryIn == this.field_234643_d_) {
|
|
|
|
|
- this.updateRepairOutput();
|
|
|
|
|
|
|
+ // 在玩家从输出格拿走物品时触发:铁砧损坏、减少经验等
|
|
|
|
|
+ private ItemStack onTakeOutput(PlayerEntity player, ItemStack itemStack) {
|
|
|
|
|
+ if (!player.abilities.isCreativeMode) {
|
|
|
|
|
+ player.addExperienceLevel(-this.maximumCost.get());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Called when the container is closed.
|
|
|
|
|
- */
|
|
|
|
|
- public void onContainerClosed(PlayerEntity playerIn) {
|
|
|
|
|
- super.onContainerClosed(playerIn);
|
|
|
|
|
- this.field_234644_e_.consume((p_234647_2_, p_234647_3_) -> this.clearContainer(playerIn, p_234647_2_, this.field_234643_d_));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Determines whether supplied player can use this container
|
|
|
|
|
- */
|
|
|
|
|
- public boolean canInteractWith(PlayerEntity playerIn) {
|
|
|
|
|
- return this.field_234644_e_.applyOrElse((p_234646_2_, p_234646_3_) -> this.func_230302_a_(p_234646_2_.getBlockState(p_234646_3_)) && playerIn.getDistanceSq((double) p_234646_3_.getX() + 0.5D, (double) p_234646_3_.getY() + 0.5D, (double) p_234646_3_.getZ() + 0.5D) <= 64.0D, true);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ float breakChance = net.minecraftforge.common.ForgeHooks.onAnvilRepair(player, itemStack, AnvilContainerRe.this.inputInventory.getStackInSlot(0), AnvilContainerRe.this.inputInventory.getStackInSlot(1));
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Handle when the stack in slot {@code index} is shift-clicked. Normally this moves the stack between the player
|
|
|
|
|
- * inventory and the other inventory(s).
|
|
|
|
|
- */
|
|
|
|
|
- public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
|
|
|
|
|
- ItemStack itemstack = ItemStack.EMPTY;
|
|
|
|
|
- Slot slot = this.inventorySlots.get(index);
|
|
|
|
|
- if (slot != null && slot.getHasStack()) {
|
|
|
|
|
- ItemStack itemstack1 = slot.getStack();
|
|
|
|
|
- itemstack = itemstack1.copy();
|
|
|
|
|
- if (index == 2) {
|
|
|
|
|
- if (!this.mergeItemStack(itemstack1, 3, 39, true)) {
|
|
|
|
|
- return ItemStack.EMPTY;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- slot.onSlotChange(itemstack1, itemstack);
|
|
|
|
|
- } else if (index != 0 && index != 1) {
|
|
|
|
|
- if (index < 39) {
|
|
|
|
|
- if (!this.mergeItemStack(itemstack1, 0, 2, false)) {
|
|
|
|
|
- return ItemStack.EMPTY;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } else if (!this.mergeItemStack(itemstack1, 3, 39, false)) {
|
|
|
|
|
- return ItemStack.EMPTY;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (itemstack1.isEmpty()) {
|
|
|
|
|
- slot.putStack(ItemStack.EMPTY);
|
|
|
|
|
- } else {
|
|
|
|
|
- slot.onSlotChanged();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (itemstack1.getCount() == itemstack.getCount()) {
|
|
|
|
|
- return ItemStack.EMPTY;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- slot.onTake(playerIn, itemstack1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return itemstack;
|
|
|
|
|
- }
|
|
|
|
|
- private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
|
- public int materialCost;
|
|
|
|
|
- private final IntReferenceHolder maximumCost = IntReferenceHolder.single();
|
|
|
|
|
-
|
|
|
|
|
- protected boolean func_230302_a_(BlockState p_230302_1_) {
|
|
|
|
|
- return p_230302_1_.isIn(BlockTags.ANVIL);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected boolean func_230303_b_(PlayerEntity p_230303_1_, boolean p_230303_2_) {
|
|
|
|
|
- return (p_230303_1_.abilities.isCreativeMode || p_230303_1_.experienceLevel >= this.maximumCost.get()) && this.maximumCost.get() > 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected ItemStack func_230301_a_(PlayerEntity p_230301_1_, ItemStack p_230301_2_) {
|
|
|
|
|
- if (!p_230301_1_.abilities.isCreativeMode) {
|
|
|
|
|
- p_230301_1_.addExperienceLevel(-this.maximumCost.get());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- float breakChance = net.minecraftforge.common.ForgeHooks.onAnvilRepair(p_230301_1_, p_230301_2_, AnvilContainerRe.this.field_234643_d_.getStackInSlot(0), AnvilContainerRe.this.field_234643_d_.getStackInSlot(1));
|
|
|
|
|
-
|
|
|
|
|
- this.field_234643_d_.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
|
|
|
|
+ this.inputInventory.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
if (this.materialCost > 0) {
|
|
if (this.materialCost > 0) {
|
|
|
- ItemStack itemstack = this.field_234643_d_.getStackInSlot(1);
|
|
|
|
|
|
|
+ ItemStack itemstack = this.inputInventory.getStackInSlot(1);
|
|
|
if (!itemstack.isEmpty() && itemstack.getCount() > this.materialCost) {
|
|
if (!itemstack.isEmpty() && itemstack.getCount() > this.materialCost) {
|
|
|
itemstack.shrink(this.materialCost);
|
|
itemstack.shrink(this.materialCost);
|
|
|
- this.field_234643_d_.setInventorySlotContents(1, itemstack);
|
|
|
|
|
|
|
+ this.inputInventory.setInventorySlotContents(1, itemstack);
|
|
|
} else {
|
|
} else {
|
|
|
- this.field_234643_d_.setInventorySlotContents(1, ItemStack.EMPTY);
|
|
|
|
|
|
|
+ this.inputInventory.setInventorySlotContents(1, ItemStack.EMPTY);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- this.field_234643_d_.setInventorySlotContents(1, ItemStack.EMPTY);
|
|
|
|
|
|
|
+ this.inputInventory.setInventorySlotContents(1, ItemStack.EMPTY);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
this.maximumCost.set(0);
|
|
this.maximumCost.set(0);
|
|
|
- this.field_234644_e_.consume((p_234633_1_, p_234633_2_) -> {
|
|
|
|
|
|
|
+ this.worldPosCallable.consume((p_234633_1_, p_234633_2_) -> {
|
|
|
BlockState blockstate = p_234633_1_.getBlockState(p_234633_2_);
|
|
BlockState blockstate = p_234633_1_.getBlockState(p_234633_2_);
|
|
|
- if (!p_230301_1_.abilities.isCreativeMode && blockstate.isIn(BlockTags.ANVIL) && p_230301_1_.getRNG().nextFloat() < breakChance) {
|
|
|
|
|
|
|
+ if (!player.abilities.isCreativeMode && blockstate.isIn(BlockTags.ANVIL) && player.getRNG().nextFloat() < breakChance) {
|
|
|
BlockState blockstate1 = AnvilBlock.damage(blockstate);
|
|
BlockState blockstate1 = AnvilBlock.damage(blockstate);
|
|
|
if (blockstate1 == null) {
|
|
if (blockstate1 == null) {
|
|
|
p_234633_1_.removeBlock(p_234633_2_, false);
|
|
p_234633_1_.removeBlock(p_234633_2_, false);
|
|
@@ -208,37 +109,35 @@ public class AnvilContainerRe extends Container {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
});
|
|
});
|
|
|
- return p_230301_2_;
|
|
|
|
|
|
|
+ return itemStack;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * called when the Anvil Input Slot changes, calculates the new result and puts it in the output slot
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ // 当输入物品改变时更新输出
|
|
|
public void updateRepairOutput() {
|
|
public void updateRepairOutput() {
|
|
|
- ItemStack itemstack = this.field_234643_d_.getStackInSlot(0);
|
|
|
|
|
|
|
+ ItemStack itemstack = this.inputInventory.getStackInSlot(0);
|
|
|
this.maximumCost.set(1);
|
|
this.maximumCost.set(1);
|
|
|
- int i = 0;
|
|
|
|
|
|
|
+ int cost = 0;
|
|
|
int j = 0;
|
|
int j = 0;
|
|
|
int k = 0;
|
|
int k = 0;
|
|
|
if (itemstack.isEmpty()) {
|
|
if (itemstack.isEmpty()) {
|
|
|
- this.field_234642_c_.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
|
|
|
|
+ this.outputInventory.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
this.maximumCost.set(0);
|
|
this.maximumCost.set(0);
|
|
|
} else {
|
|
} else {
|
|
|
ItemStack itemstack1 = itemstack.copy();
|
|
ItemStack itemstack1 = itemstack.copy();
|
|
|
- ItemStack itemstack2 = this.field_234643_d_.getStackInSlot(1);
|
|
|
|
|
|
|
+ ItemStack itemstack2 = this.inputInventory.getStackInSlot(1);
|
|
|
Map<Enchantment, Integer> map = EnchantmentHelper.getEnchantments(itemstack1);
|
|
Map<Enchantment, Integer> map = EnchantmentHelper.getEnchantments(itemstack1);
|
|
|
j = j + itemstack.getRepairCost() + (itemstack2.isEmpty() ? 0 : itemstack2.getRepairCost());
|
|
j = j + itemstack.getRepairCost() + (itemstack2.isEmpty() ? 0 : itemstack2.getRepairCost());
|
|
|
this.materialCost = 0;
|
|
this.materialCost = 0;
|
|
|
boolean flag = false;
|
|
boolean flag = false;
|
|
|
|
|
|
|
|
if (!itemstack2.isEmpty()) {
|
|
if (!itemstack2.isEmpty()) {
|
|
|
- if (!onAnvilChange(this, itemstack, itemstack2, field_234642_c_, itemstack.getDisplayName().getString(), j))
|
|
|
|
|
|
|
+ if (!onAnvilChange(this, itemstack, itemstack2, outputInventory, itemstack.getDisplayName().getString(), j))
|
|
|
return;
|
|
return;
|
|
|
flag = itemstack2.getItem() == Items.ENCHANTED_BOOK && !EnchantedBookItem.getEnchantments(itemstack2).isEmpty();
|
|
flag = itemstack2.getItem() == Items.ENCHANTED_BOOK && !EnchantedBookItem.getEnchantments(itemstack2).isEmpty();
|
|
|
if (itemstack1.isDamageable() && itemstack1.getItem().getIsRepairable(itemstack, itemstack2)) {
|
|
if (itemstack1.isDamageable() && itemstack1.getItem().getIsRepairable(itemstack, itemstack2)) {
|
|
|
int l2 = Math.min(itemstack1.getDamage(), itemstack1.getMaxDamage() / 4);
|
|
int l2 = Math.min(itemstack1.getDamage(), itemstack1.getMaxDamage() / 4);
|
|
|
if (l2 <= 0) {
|
|
if (l2 <= 0) {
|
|
|
- this.field_234642_c_.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
|
|
|
|
+ this.outputInventory.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
this.maximumCost.set(0);
|
|
this.maximumCost.set(0);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -247,14 +146,14 @@ public class AnvilContainerRe extends Container {
|
|
|
for (i3 = 0; l2 > 0 && i3 < itemstack2.getCount(); ++i3) {
|
|
for (i3 = 0; l2 > 0 && i3 < itemstack2.getCount(); ++i3) {
|
|
|
int j3 = itemstack1.getDamage() - l2;
|
|
int j3 = itemstack1.getDamage() - l2;
|
|
|
itemstack1.setDamage(j3);
|
|
itemstack1.setDamage(j3);
|
|
|
- ++i;
|
|
|
|
|
|
|
+ ++cost;
|
|
|
l2 = Math.min(itemstack1.getDamage(), itemstack1.getMaxDamage() / 4);
|
|
l2 = Math.min(itemstack1.getDamage(), itemstack1.getMaxDamage() / 4);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
this.materialCost = i3;
|
|
this.materialCost = i3;
|
|
|
} else {
|
|
} else {
|
|
|
if (!flag && (itemstack1.getItem() != itemstack2.getItem() || !itemstack1.isDamageable())) {
|
|
if (!flag && (itemstack1.getItem() != itemstack2.getItem() || !itemstack1.isDamageable())) {
|
|
|
- this.field_234642_c_.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
|
|
|
|
+ this.outputInventory.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
this.maximumCost.set(0);
|
|
this.maximumCost.set(0);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -271,7 +170,7 @@ public class AnvilContainerRe extends Container {
|
|
|
|
|
|
|
|
if (l1 < itemstack1.getDamage()) {
|
|
if (l1 < itemstack1.getDamage()) {
|
|
|
itemstack1.setDamage(l1);
|
|
itemstack1.setDamage(l1);
|
|
|
- i += 2;
|
|
|
|
|
|
|
+ cost += 2;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -285,14 +184,14 @@ public class AnvilContainerRe extends Container {
|
|
|
int j2 = map1.get(enchantment1);
|
|
int j2 = map1.get(enchantment1);
|
|
|
j2 = i2 == j2 ? j2 + 1 : Math.max(j2, i2);
|
|
j2 = i2 == j2 ? j2 + 1 : Math.max(j2, i2);
|
|
|
boolean flag1 = enchantment1.canApply(itemstack);
|
|
boolean flag1 = enchantment1.canApply(itemstack);
|
|
|
- if (this.field_234645_f_.abilities.isCreativeMode || itemstack.getItem() == Items.ENCHANTED_BOOK) {
|
|
|
|
|
|
|
+ if (this.player.abilities.isCreativeMode || itemstack.getItem() == Items.ENCHANTED_BOOK) {
|
|
|
flag1 = true;
|
|
flag1 = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for (Enchantment enchantment : map.keySet()) {
|
|
for (Enchantment enchantment : map.keySet()) {
|
|
|
if (enchantment != enchantment1 && !enchantment1.isCompatibleWith(enchantment)) {
|
|
if (enchantment != enchantment1 && !enchantment1.isCompatibleWith(enchantment)) {
|
|
|
flag1 = false;
|
|
flag1 = false;
|
|
|
- ++i;
|
|
|
|
|
|
|
+ ++cost;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -324,16 +223,16 @@ public class AnvilContainerRe extends Container {
|
|
|
k3 = Math.max(1, k3 / 2);
|
|
k3 = Math.max(1, k3 / 2);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- i += k3 * j2;
|
|
|
|
|
|
|
+ cost += k3 * j2;
|
|
|
if (itemstack.getCount() > 1) {
|
|
if (itemstack.getCount() > 1) {
|
|
|
- i = 40;
|
|
|
|
|
|
|
+ cost = 100;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (flag3 && !flag2) {
|
|
if (flag3 && !flag2) {
|
|
|
- this.field_234642_c_.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
|
|
|
|
+ this.outputInventory.setInventorySlotContents(0, ItemStack.EMPTY);
|
|
|
this.maximumCost.set(0);
|
|
this.maximumCost.set(0);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -342,12 +241,12 @@ public class AnvilContainerRe extends Container {
|
|
|
|
|
|
|
|
if (flag && !itemstack1.isBookEnchantable(itemstack2)) itemstack1 = ItemStack.EMPTY;
|
|
if (flag && !itemstack1.isBookEnchantable(itemstack2)) itemstack1 = ItemStack.EMPTY;
|
|
|
|
|
|
|
|
- this.maximumCost.set(j + i);
|
|
|
|
|
- if (i <= 0) {
|
|
|
|
|
|
|
+ this.maximumCost.set(j + cost);
|
|
|
|
|
+ if (cost <= 0) {
|
|
|
itemstack1 = ItemStack.EMPTY;
|
|
itemstack1 = ItemStack.EMPTY;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (this.maximumCost.get() >= 40 && !this.field_234645_f_.abilities.isCreativeMode) {
|
|
|
|
|
|
|
+ if (this.maximumCost.get() >= 100 && !this.player.abilities.isCreativeMode) {
|
|
|
itemstack1 = ItemStack.EMPTY;
|
|
itemstack1 = ItemStack.EMPTY;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -357,42 +256,116 @@ public class AnvilContainerRe extends Container {
|
|
|
k2 = itemstack2.getRepairCost();
|
|
k2 = itemstack2.getRepairCost();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- k2 = getNewRepairCost(k2);
|
|
|
|
|
|
|
+ k2 = k2 * 2 + 1; // new repair cost
|
|
|
|
|
|
|
|
itemstack1.setRepairCost(k2);
|
|
itemstack1.setRepairCost(k2);
|
|
|
EnchantmentHelper.setEnchantments(map, itemstack1);
|
|
EnchantmentHelper.setEnchantments(map, itemstack1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.field_234642_c_.setInventorySlotContents(0, itemstack1);
|
|
|
|
|
|
|
+ this.outputInventory.setInventorySlotContents(0, itemstack1);
|
|
|
this.detectAndSendChanges();
|
|
this.detectAndSendChanges();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 从 ForgeHooks 中复制
|
|
|
|
|
|
|
+ // 关于 maximumCost 的 I/O
|
|
|
|
|
+ private final IntReferenceHolder maximumCost = IntReferenceHolder.single();
|
|
|
|
|
+ @OnlyIn(Dist.CLIENT) public int getMaximumCost() {
|
|
|
|
|
+ return this.maximumCost.get();
|
|
|
|
|
+ }
|
|
|
|
|
+ public void setMaximumCost(int value) {
|
|
|
|
|
+ this.maximumCost.set(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ * --------------------------------------------------------------
|
|
|
|
|
+ * 以下方法直接使用原本的代码,不需要进行修改
|
|
|
|
|
+ * --------------------------------------------------------------
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+ // 构造函数
|
|
|
|
|
+ private final IWorldPosCallable worldPosCallable;
|
|
|
|
|
+ private final PlayerEntity player;
|
|
|
|
|
+ public AnvilContainerRe(int id, PlayerInventory playerInventory) {
|
|
|
|
|
+ this(id, playerInventory, IWorldPosCallable.DUMMY);
|
|
|
|
|
+ }
|
|
|
|
|
+ public AnvilContainerRe(int id, PlayerInventory playerInventory, IWorldPosCallable worldPosCallable) {
|
|
|
|
|
+ super(RegMain.containerAnvil.get(), id);
|
|
|
|
|
+ this.worldPosCallable = worldPosCallable;
|
|
|
|
|
+ this.player = playerInventory.player;
|
|
|
|
|
+ Constuct(playerInventory);
|
|
|
|
|
+ this.trackInt(this.maximumCost);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 接口: 当输入物品改变时触发更新
|
|
|
|
|
+ public void onCraftMatrixChanged(IInventory inventoryIn) {
|
|
|
|
|
+ super.onCraftMatrixChanged(inventoryIn);
|
|
|
|
|
+ if (inventoryIn == this.inputInventory) {
|
|
|
|
|
+ this.updateRepairOutput();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 接口: 决定玩家是否可以使用该方块
|
|
|
|
|
+ public boolean canInteractWith(PlayerEntity playerIn) {
|
|
|
|
|
+ return this.worldPosCallable.applyOrElse((world, blockPos) -> world.getBlockState(blockPos).isIn(BlockTags.ANVIL) && playerIn.getDistanceSq((double) blockPos.getX() + 0.5D, (double) blockPos.getY() + 0.5D, (double) blockPos.getZ() + 0.5D) <= 64.0D, true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 接口: 当玩家使用 Shift+左键 快速转移物品时触发,需要尝试转移物品
|
|
|
|
|
+ public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
|
|
|
|
|
+ ItemStack itemstack = ItemStack.EMPTY;
|
|
|
|
|
+ Slot slot = this.inventorySlots.get(index);
|
|
|
|
|
+ if (slot != null && slot.getHasStack()) {
|
|
|
|
|
+ ItemStack itemstack1 = slot.getStack();
|
|
|
|
|
+ itemstack = itemstack1.copy();
|
|
|
|
|
+ if (index == 2) {
|
|
|
|
|
+ if (!this.mergeItemStack(itemstack1, 3, 39, true)) {
|
|
|
|
|
+ return ItemStack.EMPTY;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ slot.onSlotChange(itemstack1, itemstack);
|
|
|
|
|
+ } else if (index != 0 && index != 1) {
|
|
|
|
|
+ if (index < 39) {
|
|
|
|
|
+ if (!this.mergeItemStack(itemstack1, 0, 2, false)) {
|
|
|
|
|
+ return ItemStack.EMPTY;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (!this.mergeItemStack(itemstack1, 3, 39, false)) {
|
|
|
|
|
+ return ItemStack.EMPTY;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (itemstack1.isEmpty()) {
|
|
|
|
|
+ slot.putStack(ItemStack.EMPTY);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ slot.onSlotChanged();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (itemstack1.getCount() == itemstack.getCount()) {
|
|
|
|
|
+ return ItemStack.EMPTY;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ slot.onTake(playerIn, itemstack1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return itemstack;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 接口: 当页面关闭时触发,尝试返还物品
|
|
|
|
|
+ public void onContainerClosed(PlayerEntity playerIn) {
|
|
|
|
|
+ super.onContainerClosed(playerIn);
|
|
|
|
|
+ this.worldPosCallable.consume((p_234647_2_, p_234647_3_) -> this.clearContainer(playerIn, p_234647_2_, this.inputInventory));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 铁砧输入改变时 Forge 触发的钩子事件(从 ForgeHooks.java 中复制)
|
|
|
public static boolean onAnvilChange(AnvilContainerRe container, @Nonnull ItemStack left, @Nonnull ItemStack right, IInventory outputSlot, String name, int baseCost) {
|
|
public static boolean onAnvilChange(AnvilContainerRe container, @Nonnull ItemStack left, @Nonnull ItemStack right, IInventory outputSlot, String name, int baseCost) {
|
|
|
AnvilUpdateEvent e = new AnvilUpdateEvent(left, right, name, baseCost);
|
|
AnvilUpdateEvent e = new AnvilUpdateEvent(left, right, name, baseCost);
|
|
|
if (MinecraftForge.EVENT_BUS.post(e)) return false;
|
|
if (MinecraftForge.EVENT_BUS.post(e)) return false;
|
|
|
if (e.getOutput().isEmpty()) return true;
|
|
if (e.getOutput().isEmpty()) return true;
|
|
|
-
|
|
|
|
|
outputSlot.setInventorySlotContents(0, e.getOutput());
|
|
outputSlot.setInventorySlotContents(0, e.getOutput());
|
|
|
container.setMaximumCost(e.getCost());
|
|
container.setMaximumCost(e.getCost());
|
|
|
container.materialCost = e.getMaterialCost();
|
|
container.materialCost = e.getMaterialCost();
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static int getNewRepairCost(int oldRepairCost) {
|
|
|
|
|
- return oldRepairCost * 2 + 1;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Get's the maximum xp cost
|
|
|
|
|
- */
|
|
|
|
|
- @OnlyIn(Dist.CLIENT)
|
|
|
|
|
- public int getMaximumCost() {
|
|
|
|
|
- return this.maximumCost.get();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void setMaximumCost(int value) {
|
|
|
|
|
- this.maximumCost.set(value);
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|