All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minecraft.server.InventoryMerchant Maven / Gradle / Ivy

package net.minecraft.server;

// CraftBukkit start

import org.bukkit.craftbukkit.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.entity.CraftVillager;
import org.bukkit.entity.HumanEntity;

import java.util.List;
// CraftBukkit end

public class InventoryMerchant implements IInventory {

  private final IMerchant merchant;
  private final EntityHuman player;
  // CraftBukkit start - add fields and methods
  public List transaction = new java.util.ArrayList();
  private final ItemStack[] itemsInSlots = new ItemStack[3];
  private MerchantRecipe recipe;
  private int e;
  private int maxStack = MAX_STACK;

  public InventoryMerchant(EntityHuman entityhuman, IMerchant imerchant) {
    this.player = entityhuman;
    this.merchant = imerchant;
  }

  public ItemStack[] getContents() {
    return this.itemsInSlots;
  }

  public void onOpen(CraftHumanEntity who) {
    transaction.add(who);
  }

  public void onClose(CraftHumanEntity who) {
    transaction.remove(who);
  }

  public List getViewers() {
    return transaction;
  }

  public org.bukkit.inventory.InventoryHolder getOwner() {
    return (CraftVillager) ((EntityVillager) this.merchant).getBukkitEntity();
  }
  // CraftBukkit end

  public int getSize() {
    return this.itemsInSlots.length;
  }

  public ItemStack getItem(int i) {
    return this.itemsInSlots[i];
  }

  public ItemStack splitStack(int i, int j) {
    if (this.itemsInSlots[i] != null) {
      ItemStack itemstack;

      if (i == 2) {
        itemstack = this.itemsInSlots[i];
        this.itemsInSlots[i] = null;
        return itemstack;
      } else if (this.itemsInSlots[i].count <= j) {
        itemstack = this.itemsInSlots[i];
        this.itemsInSlots[i] = null;
        if (this.e(i)) {
          this.h();
        }

        return itemstack;
      } else {
        itemstack = this.itemsInSlots[i].cloneAndSubtract(j);
        if (this.itemsInSlots[i].count == 0) {
          this.itemsInSlots[i] = null;
        }

        if (this.e(i)) {
          this.h();
        }

        return itemstack;
      }
    } else {
      return null;
    }
  }

  private boolean e(int i) {
    return i == 0 || i == 1;
  }

  public ItemStack splitWithoutUpdate(int i) {
    if (this.itemsInSlots[i] != null) {
      ItemStack itemstack = this.itemsInSlots[i];

      this.itemsInSlots[i] = null;
      return itemstack;
    } else {
      return null;
    }
  }

  public void setItem(int i, ItemStack itemstack) {
    this.itemsInSlots[i] = itemstack;
    if (itemstack != null && itemstack.count > this.getMaxStackSize()) {
      itemstack.count = this.getMaxStackSize();
    }

    if (this.e(i)) {
      this.h();
    }

  }

  public String getName() {
    return "mob.villager";
  }

  public boolean hasCustomName() {
    return false;
  }

  public IChatBaseComponent getScoreboardDisplayName() {
    return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatMessage(this.getName(), new Object[0]);
  }

  public int getMaxStackSize() {
    return maxStack; // CraftBukkit
  }

  public void setMaxStackSize(int i) {
    maxStack = i;
  }

  public boolean a(EntityHuman entityhuman) {
    return this.merchant.v_() == entityhuman;
  }

  public void startOpen(EntityHuman entityhuman) {
  }

  public void closeContainer(EntityHuman entityhuman) {
  }

  public boolean b(int i, ItemStack itemstack) {
    return true;
  }

  public void update() {
    this.h();
  }

  public void h() {
    this.recipe = null;
    ItemStack itemstack = this.itemsInSlots[0];
    ItemStack itemstack1 = this.itemsInSlots[1];

    if (itemstack == null) {
      itemstack = itemstack1;
      itemstack1 = null;
    }

    if (itemstack == null) {
      this.setItem(2, null);
    } else {
      MerchantRecipeList merchantrecipelist = this.merchant.getOffers(this.player);

      if (merchantrecipelist != null) {
        MerchantRecipe merchantrecipe = merchantrecipelist.a(itemstack, itemstack1, this.e);

        if (merchantrecipe != null && !merchantrecipe.h()) {
          this.recipe = merchantrecipe;
          this.setItem(2, merchantrecipe.getBuyItem3().cloneItemStack());
        } else if (itemstack1 != null) {
          merchantrecipe = merchantrecipelist.a(itemstack1, itemstack, this.e);
          if (merchantrecipe != null && !merchantrecipe.h()) {
            this.recipe = merchantrecipe;
            this.setItem(2, merchantrecipe.getBuyItem3().cloneItemStack());
          } else {
            this.setItem(2, null);
          }
        } else {
          this.setItem(2, null);
        }
      }
    }

    this.merchant.a_(this.getItem(2));
  }

  public MerchantRecipe getRecipe() {
    return this.recipe;
  }

  public void d(int i) {
    this.e = i;
    this.h();
  }

  public int getProperty(int i) {
    return 0;
  }

  public void b(int i, int j) {
  }

  public int g() {
    return 0;
  }

  public void l() {
    for (int i = 0; i < this.itemsInSlots.length; ++i) {
      this.itemsInSlots[i] = null;
    }

  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy