Skip to content

Commit

Permalink
#1431: Add API for InventoryView derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles Holder authored and md-5 committed Jul 27, 2024
1 parent 0cc6acb commit 9c3bd43
Show file tree
Hide file tree
Showing 23 changed files with 536 additions and 71 deletions.
15 changes: 8 additions & 7 deletions nms-patches/net/minecraft/world/inventory/ContainerAnvil.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,7 +17,7 @@
private final Slot ingredientSlot;

+ // CraftBukkit start
+ private CraftInventoryView bukkitEntity = null;
+ private CraftBrewingStandView bukkitEntity = null;
+ private PlayerInventory player;
+ // CraftBukkit end
+
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -74,7 +71,7 @@
}

@Override
@@ -108,7 +138,7 @@
@@ -108,7 +135,7 @@
if (iinventory == this.enchantSlots) {
ItemStack itemstack = iinventory.getItem(0);

Expand All @@ -83,7 +80,7 @@
this.access.execute((world, blockposition) -> {
Registry<Holder<Enchantment>> registry = world.registryAccess().registryOrThrow(Registries.ENCHANTMENT).asHolderIdMap();
int i = 0;
@@ -148,6 +178,41 @@
@@ -148,6 +175,41 @@
}
}

Expand Down Expand Up @@ -125,7 +122,7 @@
this.broadcastChanges();
});
} else {
@@ -175,21 +240,46 @@
@@ -175,21 +237,46 @@
ItemStack itemstack2 = itemstack;
List<WeightedRandomEnchant> list = this.getEnchantmentList(world.registryAccess(), itemstack, i, this.costs[i]);

Expand Down Expand Up @@ -178,28 +175,28 @@
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) {
+ if (!this.checkReachable) return true; // CraftBukkit
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SingleRecipeInput, RecipeCooking> {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
+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
+
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
Expand Down
8 changes: 4 additions & 4 deletions nms-patches/net/minecraft/world/inventory/ContainerLoom.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
+ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
+
Expand All @@ -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
Expand Down
Loading

0 comments on commit 9c3bd43

Please sign in to comment.