diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerAnvil.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerAnvil.patch index 57377bd991..7cf494102d 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerAnvil.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerAnvil.patch @@ -5,7 +5,7 @@ import org.slf4j.Logger; +// CraftBukkit start -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftAnvilView; +// CraftBukkit end + public class ContainerAnvil extends ContainerAnvilAbstract { @@ -18,7 +18,7 @@ + // CraftBukkit start + public static final int DEFAULT_DENIED_COST = -1; + public int maximumRepairCost = 40; -+ private CraftInventoryView bukkitEntity; ++ private CraftAnvilView bukkitEntity; + // CraftBukkit end public ContainerAnvil(int i, PlayerInventory playerinventory) { @@ -105,21 +105,22 @@ } } -@@ -323,4 +333,18 @@ +@@ -323,4 +333,19 @@ public int getCost() { return this.cost.get(); } + + // CraftBukkit start + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftAnvilView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + -+ org.bukkit.craftbukkit.inventory.CraftInventory inventory = new org.bukkit.craftbukkit.inventory.CraftInventoryAnvil( -+ access.getLocation(), this.inputSlots, this.resultSlots, this); -+ bukkitEntity = new CraftInventoryView(this.player.getBukkitEntity(), inventory, this); ++ org.bukkit.craftbukkit.inventory.CraftInventoryAnvil inventory = new org.bukkit.craftbukkit.inventory.CraftInventoryAnvil( ++ access.getLocation(), this.inputSlots, this.resultSlots); ++ bukkitEntity = new CraftAnvilView(this.player.getBukkitEntity(), inventory, this); ++ bukkitEntity.updateFromLegacy(inventory); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBeacon.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBeacon.patch index 8cdf0af023..33ba78a90f 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBeacon.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBeacon.patch @@ -6,7 +6,7 @@ +// CraftBukkit start +import net.minecraft.world.entity.player.PlayerInventory; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftBeaconView; +// CraftBukkit end + public class ContainerBeacon extends Container { @@ -17,7 +17,7 @@ private final ContainerAccess access; private final IContainerProperties beaconData; + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftBeaconView bukkitEntity = null; + private PlayerInventory player; + // CraftBukkit end @@ -59,13 +59,13 @@ + + // CraftBukkit start + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftBeaconView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + org.bukkit.craftbukkit.inventory.CraftInventory inventory = new org.bukkit.craftbukkit.inventory.CraftInventoryBeacon(this.beacon); -+ bukkitEntity = new CraftInventoryView(this.player.player.getBukkitEntity(), inventory, this); ++ bukkitEntity = new CraftBeaconView(this.player.player.getBukkitEntity(), inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBrewingStand.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBrewingStand.patch index 5fbfc4815c..0c45c771e2 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBrewingStand.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerBrewingStand.patch @@ -6,7 +6,7 @@ +// CraftBukkit start +import org.bukkit.craftbukkit.inventory.CraftInventoryBrewer; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftBrewingStandView; +// CraftBukkit end + public class ContainerBrewingStand extends Container { @@ -17,7 +17,7 @@ private final Slot ingredientSlot; + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftBrewingStandView bukkitEntity = null; + private PlayerInventory player; + // CraftBukkit end + @@ -46,13 +46,13 @@ + + // CraftBukkit start + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftBrewingStandView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + CraftInventoryBrewer inventory = new CraftInventoryBrewer(this.brewingStand); -+ bukkitEntity = new CraftInventoryView(this.player.player.getBukkitEntity(), inventory, this); ++ bukkitEntity = new CraftBrewingStandView(this.player.player.getBukkitEntity(), inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerEnchantTable.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerEnchantTable.patch index 2e0a8723ed..6b02e272ec 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerEnchantTable.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerEnchantTable.patch @@ -1,19 +1,16 @@ --- a/net/minecraft/world/inventory/ContainerEnchantTable.java +++ b/net/minecraft/world/inventory/ContainerEnchantTable.java -@@ -31,6 +31,22 @@ +@@ -31,6 +31,19 @@ import net.minecraft.world.level.block.BlockEnchantmentTable; import net.minecraft.world.level.block.Blocks; +// CraftBukkit start +import java.util.Map; -+import net.minecraft.world.item.enchantment.Enchantment; +import org.bukkit.Location; -+import org.bukkit.NamespacedKey; +import org.bukkit.craftbukkit.enchantments.CraftEnchantment; +import org.bukkit.craftbukkit.inventory.CraftInventoryEnchanting; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.inventory.CraftItemStack; -+import org.bukkit.craftbukkit.util.CraftNamespacedKey; ++import org.bukkit.craftbukkit.inventory.view.CraftEnchantmentView; +import org.bukkit.enchantments.EnchantmentOffer; +import org.bukkit.event.enchantment.EnchantItemEvent; +import org.bukkit.event.enchantment.PrepareItemEnchantEvent; @@ -23,18 +20,18 @@ public class ContainerEnchantTable extends Container { static final MinecraftKey EMPTY_SLOT_LAPIS_LAZULI = MinecraftKey.withDefaultNamespace("item/empty_slot_lapis_lazuli"); -@@ -41,6 +57,10 @@ +@@ -41,6 +54,10 @@ public final int[] costs; public final int[] enchantClue; public final int[] levelClue; + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftEnchantmentView bukkitEntity = null; + private Player player; + // CraftBukkit end public ContainerEnchantTable(int i, PlayerInventory playerinventory) { this(i, playerinventory, ContainerAccess.NULL); -@@ -54,6 +74,13 @@ +@@ -54,6 +71,13 @@ super.setChanged(); ContainerEnchantTable.this.slotsChanged(this); } @@ -48,7 +45,7 @@ }; this.random = RandomSource.create(); this.enchantmentSeed = ContainerProperty.standalone(); -@@ -61,13 +88,13 @@ +@@ -61,13 +85,13 @@ this.enchantClue = new int[]{-1, -1, -1}; this.levelClue = new int[]{-1, -1, -1}; this.access = containeraccess; @@ -64,7 +61,7 @@ @Override public boolean mayPlace(ItemStack itemstack) { return itemstack.is(Items.LAPIS_LAZULI); -@@ -101,6 +128,9 @@ +@@ -101,6 +125,9 @@ this.addDataSlot(ContainerProperty.shared(this.levelClue, 0)); this.addDataSlot(ContainerProperty.shared(this.levelClue, 1)); this.addDataSlot(ContainerProperty.shared(this.levelClue, 2)); @@ -74,7 +71,7 @@ } @Override -@@ -108,7 +138,7 @@ +@@ -108,7 +135,7 @@ if (iinventory == this.enchantSlots) { ItemStack itemstack = iinventory.getItem(0); @@ -83,7 +80,7 @@ this.access.execute((world, blockposition) -> { Registry> registry = world.registryAccess().registryOrThrow(Registries.ENCHANTMENT).asHolderIdMap(); int i = 0; -@@ -148,6 +178,41 @@ +@@ -148,6 +175,41 @@ } } @@ -125,7 +122,7 @@ this.broadcastChanges(); }); } else { -@@ -175,21 +240,46 @@ +@@ -175,21 +237,46 @@ ItemStack itemstack2 = itemstack; List list = this.getEnchantmentList(world.registryAccess(), itemstack, i, this.costs[i]); @@ -178,7 +175,7 @@ itemstack1.consume(j, entityhuman); if (itemstack1.isEmpty()) { this.enchantSlots.setItem(1, ItemStack.EMPTY); -@@ -256,6 +346,7 @@ +@@ -256,6 +343,7 @@ @Override public boolean stillValid(EntityHuman entityhuman) { @@ -186,20 +183,20 @@ return stillValid(this.access, entityhuman, Blocks.ENCHANTING_TABLE); } -@@ -306,4 +397,17 @@ +@@ -306,4 +394,17 @@ return itemstack; } + + // CraftBukkit start + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftEnchantmentView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + CraftInventoryEnchanting inventory = new CraftInventoryEnchanting(this.enchantSlots); -+ bukkitEntity = new CraftInventoryView(this.player, inventory, this); ++ bukkitEntity = new CraftEnchantmentView(this.player, inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerFurnace.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerFurnace.patch index f79e8754ad..2bffe3f9c3 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerFurnace.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerFurnace.patch @@ -6,7 +6,7 @@ +// CraftBukkit start +import org.bukkit.craftbukkit.inventory.CraftInventoryFurnace; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftFurnaceView; +// CraftBukkit end + public abstract class ContainerFurnace extends ContainerRecipeBook { @@ -17,17 +17,17 @@ private final RecipeBookType recipeBookType; + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftFurnaceView bukkitEntity = null; + private PlayerInventory player; + + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftFurnaceView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + CraftInventoryFurnace inventory = new CraftInventoryFurnace((TileEntityFurnace) this.container); -+ bukkitEntity = new CraftInventoryView(this.player.player.getBukkitEntity(), inventory, this); ++ bukkitEntity = new CraftFurnaceView(this.player.player.getBukkitEntity(), inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLectern.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLectern.patch index 795ee21d6d..5c2cf6b850 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLectern.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLectern.patch @@ -9,7 +9,7 @@ +import net.minecraft.world.entity.player.PlayerInventory; +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.inventory.CraftInventoryLectern; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftLecternView; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTakeLecternBookEvent; +// CraftBukkit end @@ -17,17 +17,17 @@ public class ContainerLectern extends Container { + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftLecternView bukkitEntity = null; + private Player player; + + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftLecternView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + CraftInventoryLectern inventory = new CraftInventoryLectern(this.lectern); -+ bukkitEntity = new CraftInventoryView(this.player, inventory, this); ++ bukkitEntity = new CraftLecternView(this.player, inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLoom.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLoom.patch index b0ca235dd6..002f671a44 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLoom.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerLoom.patch @@ -7,24 +7,24 @@ +// CraftBukkit start +import org.bukkit.Location; +import org.bukkit.craftbukkit.inventory.CraftInventoryLoom; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftLoomView; +import org.bukkit.entity.Player; +// CraftBukkit end + public class ContainerLoom extends Container { + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftLoomView bukkitEntity = null; + private Player player; + + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftLoomView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + CraftInventoryLoom inventory = new CraftInventoryLoom(this.inputContainer, this.outputContainer); -+ bukkitEntity = new CraftInventoryView(this.player, inventory, this); ++ bukkitEntity = new CraftLoomView(this.player, inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerMerchant.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerMerchant.patch index 388b56b945..c7e8e5cd92 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerMerchant.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerMerchant.patch @@ -4,7 +4,7 @@ import net.minecraft.world.item.trading.MerchantRecipe; import net.minecraft.world.item.trading.MerchantRecipeList; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; // CraftBukkit ++import org.bukkit.craftbukkit.inventory.view.CraftMerchantView; // CraftBukkit + public class ContainerMerchant extends Container { @@ -14,13 +14,13 @@ private boolean canRestock; + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftMerchantView bukkitEntity = null; + private PlayerInventory player; + + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftMerchantView getBukkitView() { + if (bukkitEntity == null) { -+ bukkitEntity = new CraftInventoryView(this.player.player.getBukkitEntity(), new org.bukkit.craftbukkit.inventory.CraftInventoryMerchant(trader, tradeContainer), this); ++ bukkitEntity = new CraftMerchantView(this.player.player.getBukkitEntity(), new org.bukkit.craftbukkit.inventory.CraftInventoryMerchant(trader, tradeContainer), this, trader); + } + return bukkitEntity; + } diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerStonecutter.patch b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerStonecutter.patch index 46a9a52aec..ac843b875d 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/ContainerStonecutter.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/ContainerStonecutter.patch @@ -7,7 +7,7 @@ +// CraftBukkit start +import org.bukkit.Location; +import org.bukkit.craftbukkit.inventory.CraftInventoryStonecutter; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftStonecutterView; +import org.bukkit.entity.Player; +// CraftBukkit end + @@ -19,17 +19,17 @@ public final IInventory container; final InventoryCraftResult resultContainer; + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftStonecutterView bukkitEntity = null; + private Player player; + + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftStonecutterView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + CraftInventoryStonecutter inventory = new CraftInventoryStonecutter(this.container, this.resultContainer); -+ bukkitEntity = new CraftInventoryView(this.player, inventory, this); ++ bukkitEntity = new CraftStonecutterView(this.player, inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/nms-patches/net/minecraft/world/inventory/CrafterMenu.patch b/paper-server/nms-patches/net/minecraft/world/inventory/CrafterMenu.patch index 156735c76a..2340a444eb 100644 --- a/paper-server/nms-patches/net/minecraft/world/inventory/CrafterMenu.patch +++ b/paper-server/nms-patches/net/minecraft/world/inventory/CrafterMenu.patch @@ -6,22 +6,22 @@ +// CraftBukkit start +import org.bukkit.craftbukkit.inventory.CraftInventoryCrafter; -+import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.view.CraftCrafterView; +// CraftBukkit end + public class CrafterMenu extends Container implements ICrafting { + // CraftBukkit start -+ private CraftInventoryView bukkitEntity = null; ++ private CraftCrafterView bukkitEntity = null; + + @Override -+ public CraftInventoryView getBukkitView() { ++ public CraftCrafterView getBukkitView() { + if (bukkitEntity != null) { + return bukkitEntity; + } + + CraftInventoryCrafter inventory = new CraftInventoryCrafter(this.container, this.resultContainer); -+ bukkitEntity = new CraftInventoryView(this.player.getBukkitEntity(), inventory, this); ++ bukkitEntity = new CraftCrafterView(this.player.getBukkitEntity(), inventory, this); + return bukkitEntity; + } + // CraftBukkit end diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index bc2dd894a6..8171606d73 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -260,6 +260,7 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.Recipe; import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.view.AnvilView; import org.bukkit.potion.PotionEffect; import org.bukkit.util.Vector; @@ -1541,7 +1542,7 @@ public class CraftEventFactory { return event; } - public static PrepareAnvilEvent callPrepareAnvilEvent(InventoryView view, ItemStack item) { + public static PrepareAnvilEvent callPrepareAnvilEvent(AnvilView view, ItemStack item) { PrepareAnvilEvent event = new PrepareAnvilEvent(view, CraftItemStack.asCraftMirror(item).clone()); event.getView().getPlayer().getServer().getPluginManager().callEvent(event); event.getInventory().setItem(2, event.getResult()); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java index 6d27ac2841..4dbf4669be 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryAnvil.java @@ -1,20 +1,32 @@ package org.bukkit.craftbukkit.inventory; import com.google.common.base.Preconditions; +import java.util.function.Consumer; import net.minecraft.world.IInventory; -import net.minecraft.world.inventory.ContainerAnvil; import org.bukkit.Location; +import org.bukkit.craftbukkit.inventory.view.CraftAnvilView; +import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.AnvilInventory; public class CraftInventoryAnvil extends CraftResultInventory implements AnvilInventory { - private final Location location; - private final ContainerAnvil container; + private static final int DEFAULT_REPAIR_COST = 0; + private static final int DEFAULT_REPAIR_COST_AMOUNT = 0; + private static final int DEFAULT_MAXIMUM_REPAIR_COST = 40; - public CraftInventoryAnvil(Location location, IInventory inventory, IInventory resultInventory, ContainerAnvil container) { + private final Location location; + private String renameText; + private int costAmount; + private int repairAmount; + private int maximumRepairCost; + + public CraftInventoryAnvil(Location location, IInventory inventory, IInventory resultInventory) { super(inventory, resultInventory); this.location = location; - this.container = container; + this.renameText = null; + this.costAmount = DEFAULT_REPAIR_COST_AMOUNT; + this.repairAmount = DEFAULT_REPAIR_COST; + this.maximumRepairCost = DEFAULT_MAXIMUM_REPAIR_COST; } @Override @@ -24,37 +36,81 @@ public class CraftInventoryAnvil extends CraftResultInventory implements AnvilIn @Override public String getRenameText() { - return container.itemName; + syncWithArbitraryViewValue((cav) -> this.renameText = cav.getRenameText()); + return this.renameText; } @Override public int getRepairCostAmount() { - return container.repairItemCountCost; + syncWithArbitraryViewValue((cav) -> this.costAmount = cav.getRepairItemCountCost()); + return this.repairAmount; } @Override public void setRepairCostAmount(int amount) { - container.repairItemCountCost = amount; + this.repairAmount = amount; + syncViews((cav) -> cav.setRepairItemCountCost(amount)); } @Override public int getRepairCost() { - return container.cost.get(); + syncWithArbitraryViewValue((cav) -> this.repairAmount = cav.getRepairCost()); + return this.costAmount; } @Override public void setRepairCost(int i) { - container.cost.set(i); + this.costAmount = i; + syncViews((cav) -> cav.setRepairCost(i)); } @Override public int getMaximumRepairCost() { - return container.maximumRepairCost; + syncWithArbitraryViewValue((cav) -> this.maximumRepairCost = cav.getMaximumRepairCost()); + return this.maximumRepairCost; } @Override public void setMaximumRepairCost(int levels) { Preconditions.checkArgument(levels >= 0, "Maximum repair cost must be positive (or 0)"); - container.maximumRepairCost = levels; + this.maximumRepairCost = levels; + syncViews((cav) -> cav.setMaximumRepairCost(levels)); + } + + public boolean isRepairCostSet() { + return this.costAmount != DEFAULT_REPAIR_COST; + } + + public boolean isRepairCostAmountSet() { + return this.repairAmount != DEFAULT_REPAIR_COST_AMOUNT; + } + + public boolean isMaximumRepairCostSet() { + return this.maximumRepairCost != DEFAULT_MAXIMUM_REPAIR_COST; + } + + // used to lazily update and apply values from the view to the inventory + private void syncViews(Consumer consumer) { + for (HumanEntity viewer : getViewers()) { + if (viewer.getOpenInventory() instanceof CraftAnvilView cav) { + consumer.accept(cav); + } + } + } + + /* + * This method provides the best effort guess on whatever the value could be + * It is possible these values are wrong given there are more than 1 views of this inventory, + * however it is a limitation seeing as these anvil values are supposed to be in the Container + * not the inventory. + */ + private void syncWithArbitraryViewValue(Consumer consumer) { + if (getViewers().isEmpty()) { + return; + } + final HumanEntity entity = getViewers().get(0); + if (entity != null && entity.getOpenInventory() instanceof CraftAnvilView cav) { + consumer.accept(cav); + } } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryView.java index 6d36909643..3d54a9ad52 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryView.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryView.java @@ -15,14 +15,14 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; -public class CraftInventoryView extends CraftAbstractInventoryView { - private final Container container; +public class CraftInventoryView extends CraftAbstractInventoryView { + protected final T container; private final CraftHumanEntity player; private final CraftInventory viewing; private final String originalTitle; private String title; - public CraftInventoryView(HumanEntity player, Inventory viewing, Container container) { + public CraftInventoryView(HumanEntity player, Inventory viewing, T container) { // TODO: Should we make sure it really IS a CraftHumanEntity first? And a CraftInventory? this.player = (CraftHumanEntity) player; this.viewing = (CraftInventory) viewing; diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftAnvilView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftAnvilView.java new file mode 100644 index 0000000000..283897b5a3 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftAnvilView.java @@ -0,0 +1,66 @@ +package org.bukkit.craftbukkit.inventory.view; + +import net.minecraft.world.inventory.ContainerAnvil; +import org.bukkit.craftbukkit.inventory.CraftInventoryAnvil; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.AnvilView; +import org.jetbrains.annotations.Nullable; + +public class CraftAnvilView extends CraftInventoryView implements AnvilView { + + public CraftAnvilView(final HumanEntity player, final Inventory viewing, final ContainerAnvil container) { + super(player, viewing, container); + } + + @Nullable + @Override + public String getRenameText() { + return container.itemName; + } + + @Override + public int getRepairItemCountCost() { + return container.repairItemCountCost; + } + + @Override + public int getRepairCost() { + return container.getCost(); + } + + @Override + public int getMaximumRepairCost() { + return container.maximumRepairCost; + } + + @Override + public void setRepairItemCountCost(final int cost) { + container.repairItemCountCost = cost; + } + + @Override + public void setRepairCost(final int cost) { + container.cost.set(cost); + } + + @Override + public void setMaximumRepairCost(final int cost) { + container.maximumRepairCost = cost; + } + + public void updateFromLegacy(CraftInventoryAnvil legacy) { + if (legacy.isRepairCostSet()) { + setRepairCost(legacy.getRepairCost()); + } + + if (legacy.isRepairCostAmountSet()) { + setRepairItemCountCost(legacy.getRepairCostAmount()); + } + + if (legacy.isMaximumRepairCostSet()) { + setMaximumRepairCost(legacy.getMaximumRepairCost()); + } + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftBeaconView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftBeaconView.java new file mode 100644 index 0000000000..4deedd6193 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftBeaconView.java @@ -0,0 +1,45 @@ +package org.bukkit.craftbukkit.inventory.view; + +import net.minecraft.world.inventory.ContainerBeacon; +import net.minecraft.world.level.block.entity.TileEntityBeacon; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.craftbukkit.potion.CraftPotionEffectType; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.BeaconView; +import org.bukkit.potion.PotionEffectType; +import org.jetbrains.annotations.Nullable; + +public class CraftBeaconView extends CraftInventoryView implements BeaconView { + + public CraftBeaconView(final HumanEntity player, final Inventory viewing, final ContainerBeacon container) { + super(player, viewing, container); + } + + @Override + public int getTier() { + return container.getLevels(); + } + + @Nullable + @Override + public PotionEffectType getPrimaryEffect() { + return container.getPrimaryEffect() != null ? CraftPotionEffectType.minecraftHolderToBukkit(container.getPrimaryEffect()) : null; + } + + @Nullable + @Override + public PotionEffectType getSecondaryEffect() { + return container.getSecondaryEffect() != null ? CraftPotionEffectType.minecraftHolderToBukkit(container.getSecondaryEffect()) : null; + } + + @Override + public void setPrimaryEffect(@Nullable final PotionEffectType effectType) { + container.setData(TileEntityBeacon.DATA_PRIMARY, ContainerBeacon.encodeEffect(CraftPotionEffectType.bukkitToMinecraftHolder(effectType))); + } + + @Override + public void setSecondaryEffect(@Nullable final PotionEffectType effectType) { + container.setData(TileEntityBeacon.DATA_SECONDARY, ContainerBeacon.encodeEffect(CraftPotionEffectType.bukkitToMinecraftHolder(effectType))); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftBrewingStandView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftBrewingStandView.java new file mode 100644 index 0000000000..dec88fccaa --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftBrewingStandView.java @@ -0,0 +1,38 @@ +package org.bukkit.craftbukkit.inventory.view; + +import com.google.common.base.Preconditions; +import net.minecraft.world.inventory.ContainerBrewingStand; +import net.minecraft.world.level.block.entity.TileEntityBrewingStand; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.BrewingStandView; + +public class CraftBrewingStandView extends CraftInventoryView implements BrewingStandView { + + public CraftBrewingStandView(final HumanEntity player, final Inventory viewing, final ContainerBrewingStand container) { + super(player, viewing, container); + } + + @Override + public int getFuelLevel() { + return container.getFuel(); + } + + @Override + public int getBrewingTicks() { + return container.getBrewingTicks(); + } + + @Override + public void setFuelLevel(final int fuelLevel) { + Preconditions.checkArgument(fuelLevel > 0, "The given fuel level must be greater than 0"); + container.setData(TileEntityBrewingStand.DATA_FUEL_USES, fuelLevel); + } + + @Override + public void setBrewingTicks(final int brewingTicks) { + Preconditions.checkArgument(brewingTicks > 0, "The given brewing ticks must be greater than 0"); + container.setData(TileEntityBrewingStand.DATA_BREW_TIME, brewingTicks); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftCrafterView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftCrafterView.java new file mode 100644 index 0000000000..d3fca1831a --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftCrafterView.java @@ -0,0 +1,32 @@ +package org.bukkit.craftbukkit.inventory.view; + +import com.google.common.base.Preconditions; +import net.minecraft.world.inventory.CrafterMenu; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.CrafterView; + +public class CraftCrafterView extends CraftInventoryView implements CrafterView { + + public CraftCrafterView(final HumanEntity player, final Inventory viewing, final CrafterMenu container) { + super(player, viewing, container); + } + + @Override + public boolean isSlotDisabled(final int slot) { + return container.isSlotDisabled(slot); + } + + @Override + public boolean isPowered() { + return container.isPowered(); + } + + @Override + public void setSlotDisabled(final int slot, final boolean disabled) { + Preconditions.checkArgument(slot >= 0 && slot < 9, "Invalid slot index %s for Crafter", slot); + + container.setSlotState(slot, disabled); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftEnchantmentView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftEnchantmentView.java new file mode 100644 index 0000000000..9b04570381 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftEnchantmentView.java @@ -0,0 +1,59 @@ +package org.bukkit.craftbukkit.inventory.view; + +import com.google.common.base.Preconditions; +import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.Registries; +import net.minecraft.world.inventory.ContainerEnchantTable; +import net.minecraft.world.item.enchantment.Enchantment; +import org.bukkit.craftbukkit.CraftRegistry; +import org.bukkit.craftbukkit.enchantments.CraftEnchantment; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.enchantments.EnchantmentOffer; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.EnchantmentView; +import org.jetbrains.annotations.NotNull; + +public class CraftEnchantmentView extends CraftInventoryView implements EnchantmentView { + + public CraftEnchantmentView(final HumanEntity player, final Inventory viewing, final ContainerEnchantTable container) { + super(player, viewing, container); + } + + @Override + public int getEnchantmentSeed() { + return container.getEnchantmentSeed(); + } + + @NotNull + @Override + public EnchantmentOffer[] getOffers() { + Registry> registry = CraftRegistry.getMinecraftRegistry().registryOrThrow(Registries.ENCHANTMENT).asHolderIdMap(); + EnchantmentOffer[] offers = new EnchantmentOffer[3]; + for (int i = 0; i < 3; i++) { + org.bukkit.enchantments.Enchantment enchantment = (container.enchantClue[i] >= 0) ? CraftEnchantment.minecraftHolderToBukkit(registry.byId(container.enchantClue[i])) : null; + offers[i] = (enchantment != null) ? new EnchantmentOffer(enchantment, container.levelClue[i], container.costs[i]) : null; + } + return offers; + } + + @Override + public void setOffers(@NotNull final EnchantmentOffer[] offers) { + Preconditions.checkArgument(offers.length != 3, "There must be 3 offers given"); + Registry> registry = CraftRegistry.getMinecraftRegistry().registryOrThrow(Registries.ENCHANTMENT).asHolderIdMap(); + for (int i = 0; i < offers.length; i++) { + final EnchantmentOffer offer = offers[i]; + if (offer == null) { + container.enchantClue[i] = -1; + container.levelClue[i] = -1; + container.costs[i] = 0; + continue; + } + + container.enchantClue[i] = registry.getIdOrThrow(CraftEnchantment.bukkitToMinecraftHolder(offer.getEnchantment())); + container.levelClue[i] = offer.getEnchantmentLevel(); + container.costs[i] = offer.getCost(); + } + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftFurnaceView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftFurnaceView.java new file mode 100644 index 0000000000..ef76e921c2 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftFurnaceView.java @@ -0,0 +1,42 @@ +package org.bukkit.craftbukkit.inventory.view; + +import net.minecraft.world.inventory.ContainerFurnace; +import net.minecraft.world.level.block.entity.TileEntityFurnace; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.FurnaceView; + +public class CraftFurnaceView extends CraftInventoryView implements FurnaceView { + + public CraftFurnaceView(final HumanEntity player, final Inventory viewing, final ContainerFurnace container) { + super(player, viewing, container); + } + + @Override + public float getCookTime() { + return container.getBurnProgress(); + } + + @Override + public float getBurnTime() { + return container.getLitProgress(); + } + + @Override + public boolean isBurning() { + return container.isLit(); + } + + @Override + public void setCookTime(final int cookProgress, final int cookDuration) { + container.setData(TileEntityFurnace.DATA_COOKING_PROGRESS, cookProgress); + container.setData(TileEntityFurnace.DATA_COOKING_TOTAL_TIME, cookDuration); + } + + @Override + public void setBurnTime(final int burnProgress, final int burnDuration) { + container.setData(TileEntityFurnace.DATA_LIT_TIME, burnProgress); + container.setData(TileEntityFurnace.DATA_LIT_DURATION, burnDuration); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftLecternView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftLecternView.java new file mode 100644 index 0000000000..c10ca59e05 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftLecternView.java @@ -0,0 +1,27 @@ +package org.bukkit.craftbukkit.inventory.view; + +import com.google.common.base.Preconditions; +import net.minecraft.world.inventory.ContainerLectern; +import net.minecraft.world.level.block.entity.TileEntityLectern; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.LecternView; + +public class CraftLecternView extends CraftInventoryView implements LecternView { + + public CraftLecternView(final HumanEntity player, final Inventory viewing, final ContainerLectern container) { + super(player, viewing, container); + } + + @Override + public int getPage() { + return container.getPage(); + } + + @Override + public void setPage(final int page) { + Preconditions.checkArgument(page >= 0, "The minimum page is 0"); + container.setData(TileEntityLectern.DATA_PAGE, page); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftLoomView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftLoomView.java new file mode 100644 index 0000000000..49b5a4bf2b --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftLoomView.java @@ -0,0 +1,35 @@ +package org.bukkit.craftbukkit.inventory.view; + +import java.util.ArrayList; +import java.util.List; +import net.minecraft.core.Holder; +import net.minecraft.world.inventory.ContainerLoom; +import net.minecraft.world.level.block.entity.EnumBannerPatternType; +import org.bukkit.block.banner.PatternType; +import org.bukkit.craftbukkit.block.banner.CraftPatternType; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.view.LoomView; + +public class CraftLoomView extends CraftInventoryView implements LoomView { + + public CraftLoomView(final HumanEntity player, final Inventory viewing, final ContainerLoom container) { + super(player, viewing, container); + } + + @Override + public List getSelectablePatterns() { + final List> selectablePatterns = container.getSelectablePatterns(); + final List patternTypes = new ArrayList<>(selectablePatterns.size()); + for (final Holder selectablePattern : selectablePatterns) { + patternTypes.add(CraftPatternType.minecraftHolderToBukkit(selectablePattern)); + } + return patternTypes; + } + + @Override + public int getSelectedPatternIndex() { + return container.getSelectedBannerPatternIndex(); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftMerchantView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftMerchantView.java new file mode 100644 index 0000000000..2a0354051a --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftMerchantView.java @@ -0,0 +1,26 @@ +package org.bukkit.craftbukkit.inventory.view; + +import net.minecraft.world.inventory.ContainerMerchant; +import net.minecraft.world.item.trading.IMerchant; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.Merchant; +import org.bukkit.inventory.view.MerchantView; +import org.jetbrains.annotations.NotNull; + +public class CraftMerchantView extends CraftInventoryView implements MerchantView { + + private final IMerchant trader; + + public CraftMerchantView(final HumanEntity player, final Inventory viewing, final ContainerMerchant container, final IMerchant trader) { + super(player, viewing, container); + this.trader = trader; + } + + @NotNull + @Override + public Merchant getMerchant() { + return this.trader.getCraftMerchant(); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftStonecutterView.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftStonecutterView.java new file mode 100644 index 0000000000..b97be68bd2 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/view/CraftStonecutterView.java @@ -0,0 +1,40 @@ +package org.bukkit.craftbukkit.inventory.view; + +import java.util.ArrayList; +import java.util.List; +import net.minecraft.world.inventory.ContainerStonecutter; +import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.crafting.RecipeStonecutting; +import org.bukkit.craftbukkit.inventory.CraftInventoryView; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.StonecuttingRecipe; +import org.bukkit.inventory.view.StonecutterView; +import org.jetbrains.annotations.NotNull; + +public class CraftStonecutterView extends CraftInventoryView implements StonecutterView { + + public CraftStonecutterView(final HumanEntity player, final Inventory viewing, final ContainerStonecutter container) { + super(player, viewing, container); + } + + @Override + public int getSelectedRecipeIndex() { + return container.getSelectedRecipeIndex(); + } + + @NotNull + @Override + public List getRecipes() { + final List recipes = new ArrayList<>(); + for (final RecipeHolder recipe : container.getRecipes()) { + recipes.add((StonecuttingRecipe) recipe.toBukkitRecipe()); + } + return recipes; + } + + @Override + public int getRecipeAmount() { + return container.getNumRecipes(); + } +}