Skip to content

Commit

Permalink
Update to experimental new InventoryView API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikoo committed Dec 18, 2024
1 parent 763e830 commit dceb5f6
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 184 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.jikoo</groupId>
<artifactId>planarenchanting</artifactId>
<version>2.0.2-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>

<name>PlanarEnchanting</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.github.jikoo.planarenchanting.anvil;

import com.github.jikoo.planarenchanting.util.ItemUtil;
import java.util.function.BiPredicate;
import java.util.function.ToIntFunction;
import com.github.jikoo.planarenchanting.util.ItemUtil;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.view.AnvilView;
import org.jetbrains.annotations.NotNull;

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ public boolean itemRepairedBy(@NotNull ItemStack repaired, @NotNull ItemStack re
/**
* Set the method determining whether an item is repaired by another item. This is not the same as
* a repair via combination of like items! Like items always attempt to combine durability. If
* you require different behavior, override {@link #apply(AnvilInventory)} and do not call
* you require different behavior, override {@link #apply(AnvilView)} and do not call
* {@link AnvilFunction#REPAIR_WITH_COMBINATION}.
*
* <p>N.B. Only {@link org.bukkit.inventory.meta.Damageable Damageable} items can be repaired.
Expand All @@ -148,8 +148,8 @@ public void setItemRepairedBy(
*
* @return the {@code AnvilResult}
*/
public @NotNull AnvilResult apply(@NotNull AnvilInventory inventory) {
AnvilOperationState state = new AnvilOperationState(this, inventory);
public @NotNull AnvilResult apply(@NotNull AnvilView view) {
AnvilOperationState state = new AnvilOperationState(this, view);

if (ItemUtil.isEmpty(state.getBase().getItem())) {
return AnvilResult.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.github.jikoo.planarenchanting.util.ItemUtil;
import com.github.jikoo.planarenchanting.util.MetaCachedStack;
import org.bukkit.Bukkit;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.inventory.view.AnvilView;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -14,7 +14,7 @@
public class AnvilOperationState {

private final @NotNull AnvilOperation operation;
private final @NotNull AnvilInventory inventory;
private final @NotNull AnvilView view;
private final @NotNull MetaCachedStack base;
private final @NotNull MetaCachedStack addition;
protected final @NotNull MetaCachedStack result;
Expand All @@ -25,27 +25,27 @@ public class AnvilOperationState {
* Create an {@code AnvilOperationState} instance for the given operation and inventory.
*
* @param operation the {@link AnvilOperation} mutating the state
* @param inventory the {@link AnvilInventory} the state is derived from
* @param view the {@link AnvilView} the state is derived from
*/
public AnvilOperationState(@NotNull AnvilOperation operation, @NotNull AnvilInventory inventory) {
public AnvilOperationState(@NotNull AnvilOperation operation, @NotNull AnvilView view) {
this.operation = operation;
this.inventory = inventory;
this.base = new MetaCachedStack(this.inventory.getItem(0));
this.addition = new MetaCachedStack(this.inventory.getItem(1));
this.view = view;
this.base = new MetaCachedStack(this.view.getItem(0));
this.addition = new MetaCachedStack(this.view.getItem(1));
this.result = new MetaCachedStack(this.base.getItem().clone());
}

/**
* Get the {@link AnvilInventory} the state is derived from.
* Get the {@link AnvilView} the state is derived from.
*
* @return the {@code AnvilInventory}
* @return the {@code AnvilView}
*/
public AnvilInventory getAnvil() {
return this.inventory;
public AnvilView getAnvil() {
return this.view;
}

/**
* Get the base input item from the {@link AnvilInventory}.
* Get the base input item from the {@link AnvilView}.
*
* @return the base input item
*/
Expand All @@ -54,7 +54,7 @@ public AnvilInventory getAnvil() {
}

/**
* Get the secondary input item from the {@link AnvilInventory}.
* Get the secondary input item from the {@link AnvilView}.
*
* @return the secondary input item
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
/**
* A container for the result of an anvil operation.
*
* @see org.bukkit.inventory.AnvilInventory#setItem(int, ItemStack)
* @see org.bukkit.inventory.AnvilInventory#setRepairCost(int)
* @see org.bukkit.inventory.AnvilInventory#setRepairCostAmount(int)
* @see org.bukkit.inventory.view.AnvilView#setItem(int, ItemStack)
* @see org.bukkit.inventory.view.AnvilView#setRepairCost(int)
* @see org.bukkit.inventory.view.AnvilView#setRepairItemCountCost(int)
* @param item the result slot {@link ItemStack}
* @param levelCost the number of levels to be consumed by the operation
* @param materialCost the amount of items to be consumed from the addition slot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentOffer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.view.EnchantmentView;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -255,27 +254,14 @@ private static int getButtonLevel(@NotNull Random random, int button, int shelve
* problems that prevent the client from enchanting ordinarily un-enchantable objects.
*
* @param plugin the plugin sending the update
* @param player the player enchanting
* @param view the view of the enchantment table
* @param offers the enchantment offers
*/
public static void updateButtons(@NotNull Plugin plugin, @NotNull Player player,
public static void updateButtons(
@NotNull Plugin plugin,
@NotNull EnchantmentView view,
@Nullable EnchantmentOffer @NotNull [] offers) {
Bukkit.getScheduler().runTaskLater(plugin, () -> {
for (int i = 1; i <= 3; ++i) {
EnchantmentOffer offer = offers[i - 1];
if (offer != null) {
player.setWindowProperty(
InventoryView.Property.valueOf("ENCHANT_BUTTON" + i),
offer.getCost());
player.setWindowProperty(
InventoryView.Property.valueOf("ENCHANT_LEVEL" + i),
offer.getEnchantmentLevel());
player.setWindowProperty(
InventoryView.Property.valueOf("ENCHANT_ID" + i),
getEnchantmentId(offer.getEnchantment()));
}
}
}, 1L);
Bukkit.getScheduler().runTaskLater(plugin, () -> view.setOffers(offers), 1L);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public final void onPrepareItemEnchant(@NotNull PrepareItemEnchantEvent event) {
}

// Force button refresh. This is required for normally unenchantable items.
EnchantingTable.updateButtons(plugin, event.getEnchanter(), event.getOffers());
EnchantingTable.updateButtons(plugin, event.getView(), event.getOffers());
}

@EventHandler
Expand Down
Loading

0 comments on commit dceb5f6

Please sign in to comment.