Skip to content

Commit

Permalink
Fix trade for 1.21 servers (#34)
Browse files Browse the repository at this point in the history
* Updated: Move /trade to use NamespacedKey for AttributeModifiers

* Updated: Fix missed comments

* Updated: Slight improvement to the command page
  • Loading branch information
BlaneyXYZ authored Jul 30, 2024
1 parent cea385b commit 6c51109
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
7 changes: 4 additions & 3 deletions docs/commands/trade.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ command:
added: Pre-0.2.7
aliases: []
configuration: []
description: Opens a mutual chest between two players.
description: Opens a mutual inventory between two players
permissions:
- rcmds.trade
supports:
name-completion: false
name-completion: true
time-format: false
usage: /trade [player]
layout: command
title: /trade
---

In-depth content, and...
```/trade``` opens the trade inventory window with ```(player)```

### Examples

```/trade Notch``` - Opens the trade inventory window with ```Notch```.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.royaldev.royalcommands.gui.inventory;

import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.Material;
Expand All @@ -26,9 +27,9 @@
*/
public class InventoryGUI {

private static final String TAG_NAME = "IG:Tag";
private static final String TAG_NAME = "ig-tag";
private final Inventory base;
private final Map<UUID, ClickHandler> clickHandlers = new HashMap<>();
private final Map<NamespacedKey, ClickHandler> clickHandlers = new HashMap<>();
private final UUID identifier = UUID.randomUUID();

/**
Expand Down Expand Up @@ -61,17 +62,17 @@ public InventoryGUI(final Inventory base) {
}

/**
* Tags an ItemStack with the given UUID, which can be used to quickly get the item again.
* Tags an ItemStack with the given Key, which can be used to quickly get the item again.
*
* @param is ItemStack to tag
* @param uuid UUID to tag the item with
* @param key Key to tag the item with
* @return The tagged ItemStack
*/
private ItemStack tagItem(final ItemStack is, final UUID uuid) {
private ItemStack tagItem(final ItemStack is, final NamespacedKey key) {
if (is == null || is.getType() == Material.AIR) return is;
ItemMeta meta = is.getItemMeta();
if(!meta.hasAttributeModifiers()){
meta.addAttributeModifier(Attribute.GENERIC_FOLLOW_RANGE, new AttributeModifier(uuid, InventoryGUI.TAG_NAME, 0D, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND));
meta.addAttributeModifier(Attribute.GENERIC_FOLLOW_RANGE, new AttributeModifier(key, 0D, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND.getGroup()));
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
is.setItemMeta(meta);
}return is;
Expand All @@ -88,26 +89,26 @@ private ItemStack tagItem(final ItemStack is, final UUID uuid) {
* @param guiItem Item to add
*/
public void addItem(final ClickHandler clickHandler, final int x, final int y, final GUIItem guiItem) {
this.addItem(UUID.randomUUID(), clickHandler, x, y, guiItem);
this.addItem(new NamespacedKey(InventoryGUI.TAG_NAME, String.valueOf(UUID.randomUUID())), clickHandler, x, y, guiItem);
}

/**
* Adds an item to the GUI. The given {@link ClickHandler} will be associated with this item. The item will be
* tagged with the given UUID.
* tagged with the given Key.
*
* @param uuid UUID to tag the item with
* @param key Key to tag the item with
* @param clickHandler ClickHandler to use for the item
* @param x X-coordinate of the position the item will be added at
* @param y Y-coordinate
* @param guiItem Item to add
*/
public void addItem(final UUID uuid, final ClickHandler clickHandler, final int x, final int y, final GUIItem guiItem) {
public void addItem(final NamespacedKey key, final ClickHandler clickHandler, final int x, final int y, final GUIItem guiItem) {
final int slot = this.getSlot(x, y);
if (slot > this.getBase().getSize() - 1) {
throw new IllegalArgumentException("Location does not exist.");
}
final ItemStack is = this.tagItem(guiItem.makeItemStack(), uuid);
this.getClickHandlers().put(uuid, clickHandler);
final ItemStack is = this.tagItem(guiItem.makeItemStack(), key);
this.getClickHandlers().put(key, clickHandler);
this.getBase().setItem(slot, is);
}

Expand All @@ -134,16 +135,16 @@ public Inventory getBase() {
* @return ClickHandler or null if none is associated
*/
public ClickHandler getClickHandler(final ItemStack is) {
final UUID uuid = this.getTag(is);
return uuid == null ? null : this.getClickHandlers().get(uuid);
final NamespacedKey key = this.getTag(is);
return key == null ? null : this.getClickHandlers().get(key);
}

/**
* Returns the map of UUID tags to ClickHandlers.
* Returns the map of Key tags to ClickHandlers.
*
* @return Map
*/
public Map<UUID, ClickHandler> getClickHandlers() {
public Map<NamespacedKey, ClickHandler> getClickHandlers() {
return this.clickHandlers;
}

Expand All @@ -157,15 +158,15 @@ public UUID getIdentifier() {
}

/**
* Gets an ItemStack from this GUI by its UUID tag.
* Gets an ItemStack from this GUI by its Key tag.
*
* @param uuid UUID tag of item
* @return ItemStack or null if no matching UUID
* @param key Key tag of item
* @return ItemStack or null if no matching Key
*/
public ItemStack getItemStack(final UUID uuid) {
if (uuid == null) return null;
public ItemStack getItemStack(final NamespacedKey key) {
if (key == null) return null;
for (final ItemStack is : this.getBase()) {
if (!uuid.equals(this.getTag(is))) continue;
if (!key.equals(this.getTag(is))) continue;
return is;
}
return null;
Expand All @@ -183,18 +184,18 @@ public int getSlot(final int x, final int y) {
}

/**
* Gets the UUID tag of the given ItemStack.
* Gets the Key tag of the given ItemStack.
*
* @param is ItemStack to get the tag of
* @return UUID or null if no tag
* @return Key or null if no tag
*/
public UUID getTag(final ItemStack is) {
public NamespacedKey getTag(final ItemStack is) {
if (is == null || is.getType() == Material.AIR) return null;
ItemMeta meta = is.getItemMeta();
if(meta.hasAttributeModifiers()){
for (final AttributeModifier a : meta.getAttributeModifiers().values()) {
if (!a.getName().equals(InventoryGUI.TAG_NAME)) continue;
return a.getUniqueId();
if (!a.getKey().getNamespace().equals(InventoryGUI.TAG_NAME)) continue;
return a.getKey();
}
}return null;
}
Expand Down Expand Up @@ -222,17 +223,17 @@ public void replaceItemStack(final ItemStack original, final ItemStack replaceme
}

/**
* Replaces the ItemStack with the given UUID tag with the replacement ItemStack.
* Replaces the ItemStack with the given Key tag with the replacement ItemStack.
*
* @param uuid Tag of ItemStack to replace
* @param key Tag of ItemStack to replace
* @param replacement Replacement ItemStack
*/
public void replaceItemStack(final UUID uuid, final ItemStack replacement) {
if (uuid == null) throw new IllegalArgumentException("UUID cannot be null");
public void replaceItemStack(final NamespacedKey key, final ItemStack replacement) {
if (key == null) throw new IllegalArgumentException("Key cannot be null");
for (int i = 0; i < this.getBase().getSize(); i++) {
final ItemStack is = this.getBase().getItem(i);
if (!uuid.equals(this.getTag(is))) continue;
this.getBase().setItem(i, this.tagItem(replacement, uuid));
if (!key.equals(this.getTag(is))) continue;
this.getBase().setItem(i, this.tagItem(replacement, key));
break;
}
}
Expand All @@ -254,15 +255,15 @@ public ItemStack setItemMeta(final ItemStack is, final String name, final String
}

/**
* Sets the name of the ItemStack tagged with the given UUID.
* Sets the name of the ItemStack tagged with the given Key.
*
* @param uuid UUID of the ItemStack
* @param key Key of the ItemStack
* @param name New name
*/
public void setName(final UUID uuid, final String name) {
final ItemStack is = this.getItemStack(uuid);
if (is == null) throw new IllegalArgumentException("No such ItemStack UUID found");
this.replaceItemStack(uuid, this.setItemMeta(is, name));
public void setName(final NamespacedKey key, final String name) {
final ItemStack is = this.getItemStack(key);
if (is == null) throw new IllegalArgumentException("No such ItemStack Key found");
this.replaceItemStack(key, this.setItemMeta(is, name));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.royaldev.royalcommands.rcommands.trade;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -50,10 +51,11 @@
public class Trade {

private static final Map<Pair<UUID, UUID>, Trade> trades = new HashMap<>();
private static final String TAG_NAME = "ig-tag";
private final Map<Party, UUID> parties = new HashMap<>();
private final Map<Party, Boolean> acceptances = new HashMap<>();
private final InventoryGUI inventoryGUI;
private final UUID acceptButtonUUID = UUID.randomUUID();
private final NamespacedKey acceptButtonUUID = new NamespacedKey(Trade.TAG_NAME, String.valueOf(this.acceptButtonUUID));

public Trade(final UUID trader, final UUID tradee) {
this.parties.put(Party.TRADER, trader);
Expand Down

0 comments on commit 6c51109

Please sign in to comment.