Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B+C] Add Merchant Trade API - Adds BUKKIT-5663 #1077

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.FireworkEffect;
import org.bukkit.configuration.Configuration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.TradeOffer;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
Expand All @@ -33,6 +34,7 @@ public class ConfigurationSerialization {
registerClass(Color.class);
registerClass(PotionEffect.class);
registerClass(FireworkEffect.class);
registerClass(TradeOffer.class);
}

protected ConfigurationSerialization(Class<? extends ConfigurationSerializable> clazz) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/bukkit/entity/HumanEntity.java
@@ -1,12 +1,16 @@
package org.bukkit.entity;

import java.util.Collection;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.TradeOffer;
import org.bukkit.merchant.Merchant;
import org.bukkit.permissions.Permissible;

/**
Expand Down Expand Up @@ -89,6 +93,28 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, Permissible, Inv
*/
public InventoryView openEnchanting(Location location, boolean force);

/**
* Opens a trade inventory window with the player's inventory on the bottom
* and the respective offers from the {@link org.bukkit.inventory.MerchantInventory#getOffers()}.
*
* @param merchant the merchant to trade with
* @param force if false, and the merchant is too far away from the player, the
* inventory will be closed if this player is too far
* @return the newly opened inventory view, or null if it could not be opened
*/
public InventoryView openTrade(Merchant merchant, boolean force);

/**
* Opens a trade inventory window with the player's inventory on the bottom and
* the specified offers listed in said trade inventory window.
*
* @param offers the offers to send to the player
* @param customName The custom name to label the trade window
* @return the newly opened inventory view, or null if it could not be opened
* @throws IllegalArgumentException if the offers are null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required.

*/
public InventoryView openTrade(Collection<TradeOffer> offers, String customName);

/**
* Opens an inventory window to the specified inventory view.
*
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/bukkit/entity/Villager.java
@@ -1,9 +1,11 @@
package org.bukkit.entity;

import org.bukkit.merchant.Merchant;

/**
* Represents a villager NPC
*/
public interface Villager extends Ageable, NPC {
public interface Villager extends Ageable, NPC, Merchant {

/**
* Gets the current profession of this villager.
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/bukkit/event/merchant/MerchantAddOfferEvent.java
@@ -0,0 +1,66 @@
package org.bukkit.event.merchant;

import java.util.List;

import org.apache.commons.lang.Validate;

import org.bukkit.merchant.Merchant;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.TradeOffer;

/**
* This event is called whenever a TradeOffer is being added to a Merchant's
* list of offers. If cancelled, the TradeOffer will not be added to the list.
*/
public class MerchantAddOfferEvent extends MerchantEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();

private List<TradeOffer> offers;
private TradeOffer offerToAdd;
private boolean cancelled = false;

public MerchantAddOfferEvent(Merchant merchant, TradeOffer toAdd) {
super(merchant);
Validate.notNull(toAdd, "Cannot add a null TradeOffer!");
this.offers = merchant.getInventory().getOffers();
this.offerToAdd = toAdd;
}

/**
* Gets an immutable copy of the list of TradeOffers the merchant currently has.
*
* @return a copy of the current offers from the merchant
*/
public List<TradeOffer> getCurrentOffers() {
return offers;
}

/**
* Gets the immutable TradeOffer being added to the Merchant's current offers.
*
* @return the offer to add
*/
public TradeOffer getOfferToAdd() {
return offerToAdd;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

@Override
public boolean isCancelled() {
return this.cancelled;
}

@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/bukkit/event/merchant/MerchantEvent.java
@@ -0,0 +1,28 @@
package org.bukkit.event.merchant;

import org.apache.commons.lang.Validate;

import org.bukkit.event.Event;
import org.bukkit.merchant.Merchant;

/**
* Represents a Merchant related event.
*/
public abstract class MerchantEvent extends Event {

protected Merchant merchant;

MerchantEvent(Merchant merchant) {
Validate.notNull(merchant, "Cannot have a null Merchant!");
this.merchant = merchant;
}

/**
* Gets the merchant involved with this event.
*
* @return The merchant
*/
public Merchant getMerchant() {
return this.merchant;
}
}
37 changes: 37 additions & 0 deletions src/main/java/org/bukkit/inventory/MerchantInventory.java
@@ -1,4 +1,41 @@
package org.bukkit.inventory;

import java.util.List;

import org.bukkit.merchant.Merchant;

/**
* Represents the inventory of a merchant which contains a list of {@link TradeOffer}s.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not have to fully qualify the links. As well the wording is a bit off in general. I would start off by saying something like Represents the inventory of a merchant and go from there.

public interface MerchantInventory extends Inventory {

/**
* Gets the merchant belonging to this inventory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Period.

*
* @return the merchant
*/
public Merchant getMerchant();

/**
* Gets a copy of the offers presented to a Player.
*
* @return a copied list of TradeOffers being given to the Player
*/
public List<TradeOffer> getOffers();

/**
* Adds the given TradeOffer to this Inventory's list of trade
* offers.
*
* @param offer to add
*/
public void addOffer(TradeOffer offer);

/**
* Removes the specified offer if it is contained within the list of
* offers by this inventory.
*
* @param offer to remove
*/
public void removeOffer(TradeOffer offer);
}