Skip to content

Commit

Permalink
Add new inventory functions and virtual inventories.
Browse files Browse the repository at this point in the history
New functions: popen_inventory(), pinventory_holder(), get_inventory_viewers(), get_virtual_inventories(), create_virtual_inventory(), and delete_virtual_inventory().
New "virtual" prefilter for inventory_click, inventory_drag, inventory_open, and inventory_close events.
New inventory "holder" data for inventory_open and inventory_close events.
  • Loading branch information
PseudoKnight committed Jul 15, 2018
1 parent 9fdb6fe commit 90ddb51
Show file tree
Hide file tree
Showing 17 changed files with 776 additions and 304 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/Convertor.java
Expand Up @@ -128,6 +128,8 @@ public interface Convertor {
*/
MCInventory GetLocationInventory(MCLocation location);

MCInventoryHolder CreateInventoryHolder(String id);

/**
* Run whenever the server is shutting down (or restarting). There is no guarantee provided as to what thread the
* runnables actually run on, so you should ensure that the runnable executes it's actions on the appropriate thread
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/MCDoubleChest.java
@@ -0,0 +1,5 @@
package com.laytonsmith.abstraction;

public interface MCDoubleChest extends MCInventoryHolder {
MCLocation getLocation();
}
4 changes: 1 addition & 3 deletions src/main/java/com/laytonsmith/abstraction/MCServer.java
Expand Up @@ -56,12 +56,10 @@ public interface MCServer extends AbstractionObject {

MCCommandMap getCommandMap();

MCInventory createInventory(MCInventoryHolder owner, MCInventoryType type);
MCInventory createInventory(MCInventoryHolder owner, MCInventoryType type, String title);

MCInventory createInventory(MCInventoryHolder owner, int size, String title);

MCInventory createInventory(MCInventoryHolder owner, int size);

/**
* Provides access to local user data associated with a name. Depending on the implementation, a web lookup with the
* official API may or may not be performed.
Expand Down
@@ -0,0 +1,5 @@
package com.laytonsmith.abstraction;

public interface MCVirtualInventoryHolder extends MCInventoryHolder {
String getID();
}
Expand Up @@ -12,6 +12,7 @@
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.MCFireworkBuilder;
import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.MCInventoryHolder;
import com.laytonsmith.abstraction.MCItemMeta;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCLocation;
Expand Down Expand Up @@ -89,13 +90,11 @@
import org.bukkit.World;
import org.bukkit.block.Banner;
import org.bukkit.block.Beacon;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.BrewingStand;
import org.bukkit.block.Chest;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Dispenser;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Dropper;
import org.bukkit.block.Furnace;
import org.bukkit.block.Hopper;
Expand Down Expand Up @@ -556,27 +555,24 @@ public MCInventory GetEntityInventory(MCEntity e) {
if(entity instanceof InventoryHolder) {
if(entity instanceof Player) {
return new BukkitMCPlayerInventory(((Player) entity).getInventory());
} else {
return new BukkitMCInventory(((InventoryHolder) entity).getInventory());
}
} else {
return null;
return new BukkitMCInventory(((InventoryHolder) entity).getInventory());
}
return null;
}

@Override
public MCInventory GetLocationInventory(MCLocation location) {
Block b = ((Location) location.getHandle()).getBlock();
if(b.getState() instanceof InventoryHolder) {
if(b.getState() instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) b.getState();
return new BukkitMCDoubleChest(dc.getLeftSide().getInventory(), dc.getRightSide().getInventory());
} else {
return new BukkitMCInventory(((InventoryHolder) b.getState()).getInventory());
}
} else {
return null;
BlockState bs = ((Location) location.getHandle()).getBlock().getState();
if(bs instanceof InventoryHolder) {
return new BukkitMCInventory(((InventoryHolder) bs).getInventory());
}
return null;
}

@Override
public MCInventoryHolder CreateInventoryHolder(String id) {
return new BukkitMCVirtualInventoryHolder(id);
}

@Override
Expand Down
@@ -1,72 +1,30 @@
package com.laytonsmith.abstraction.bukkit;

import com.laytonsmith.abstraction.MCItemStack;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import com.laytonsmith.abstraction.MCDoubleChest;
import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.MCLocation;
import org.bukkit.block.DoubleChest;

public class BukkitMCDoubleChest extends BukkitMCInventory {
public class BukkitMCDoubleChest implements MCDoubleChest {

Inventory left;
Inventory right;
DoubleChest dc;

public BukkitMCDoubleChest(Inventory left, Inventory right) {
super(left);
public BukkitMCDoubleChest(DoubleChest chest) {
this.dc = chest;
}

@Override
public int getSize() {
return left.getSize() + right.getSize();
public MCInventory getInventory() {
return new BukkitMCInventory(this.dc.getInventory());
}

@Override
public MCItemStack getItem(int slot) {
ItemStack is;
if(slot < left.getSize()) {
is = left.getItem(slot);
} else {
is = right.getItem(slot - left.getSize());
}
return new BukkitMCItemStack(is);
public MCLocation getLocation() {
return new BukkitMCLocation(this.dc.getLocation());
}

@Override
public void setItem(int slot, MCItemStack stack) {
ItemStack is = (ItemStack) stack.getHandle();
if(slot < left.getSize()) {
left.setItem(slot, is);
} else {
right.setItem(slot - left.getSize(), is);
}
}

@Override
public String toString() {
return left.toString() + ":" + right.toString();
}

@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
final BukkitMCDoubleChest other = (BukkitMCDoubleChest) obj;
if(this.left != other.left && (this.left == null || !this.left.equals(other.left))) {
return false;
}
if(this.right != other.right && (this.right == null || !this.right.equals(other.right))) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + (this.left != null ? this.left.hashCode() : 0);
hash = 59 * hash + (this.right != null ? this.right.hashCode() : 0);
return hash;
public Object getHandle() {
return this.dc;
}
}
Expand Up @@ -12,9 +12,13 @@
import com.laytonsmith.core.LogLevel;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.CRE.CRERangeException;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
Expand Down Expand Up @@ -135,7 +139,17 @@ public void updateViewers() {

@Override
public MCInventoryHolder getHolder() {
return new BukkitMCInventoryHolder(i.getHolder());
InventoryHolder ih = i.getHolder();
if(ih instanceof BlockState) {
return (MCInventoryHolder) BukkitConvertor.BukkitGetCorrectBlockState((BlockState) ih);
} else if(ih instanceof Entity) {
return (MCInventoryHolder) BukkitConvertor.BukkitGetCorrectEntity((Entity) ih);
} else if(ih instanceof BukkitMCVirtualInventoryHolder.VirtualHolder) {
return new BukkitMCVirtualInventoryHolder(ih);
} else if(ih instanceof DoubleChest) {
return new BukkitMCDoubleChest((DoubleChest) ih);
}
return new BukkitMCInventoryHolder(ih);
}

@Override
Expand Down
Expand Up @@ -5,7 +5,6 @@
import com.laytonsmith.abstraction.MCCommandMap;
import com.laytonsmith.abstraction.MCCommandSender;
import com.laytonsmith.abstraction.MCConsoleCommandSender;
import com.laytonsmith.abstraction.MCHumanEntity;
import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.MCInventoryHolder;
import com.laytonsmith.abstraction.MCItemFactory;
Expand All @@ -17,7 +16,6 @@
import com.laytonsmith.abstraction.MCScoreboard;
import com.laytonsmith.abstraction.MCServer;
import com.laytonsmith.abstraction.MCWorld;
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCHumanEntity;
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer;
import com.laytonsmith.abstraction.bukkit.pluginmessages.BukkitMCMessenger;
import com.laytonsmith.abstraction.enums.MCBarColor;
Expand Down Expand Up @@ -437,47 +435,30 @@ public int hashCode() {
}

@Override
public MCInventory createInventory(MCInventoryHolder holder, MCInventoryType type) {
InventoryHolder ih = null;

if(holder instanceof MCPlayer) {
ih = ((BukkitMCPlayer) holder)._Player();
} else if(holder instanceof MCHumanEntity) {
ih = ((BukkitMCHumanEntity) holder).asHumanEntity();
} else if(holder.getHandle() instanceof InventoryHolder) {
public MCInventory createInventory(MCInventoryHolder holder, MCInventoryType type, String title) {
InventoryHolder ih;
if(holder == null) {
ih = null;
} else {
ih = (InventoryHolder) holder.getHandle();
}

return new BukkitMCInventory(Bukkit.createInventory(ih, InventoryType.valueOf(type.name())));
}

@Override
public MCInventory createInventory(MCInventoryHolder holder, int size) {
InventoryHolder ih = null;

if(holder instanceof MCPlayer) {
ih = ((BukkitMCPlayer) holder)._Player();
} else if(holder instanceof MCHumanEntity) {
ih = ((BukkitMCHumanEntity) holder).asHumanEntity();
} else if(holder.getHandle() instanceof InventoryHolder) {
ih = (InventoryHolder) holder.getHandle();
if(title == null) {
return new BukkitMCInventory(Bukkit.createInventory(ih, InventoryType.valueOf(type.name())));
}

return new BukkitMCInventory(Bukkit.createInventory(ih, size));
return new BukkitMCInventory(Bukkit.createInventory(ih, InventoryType.valueOf(type.name()), title));
}

@Override
public MCInventory createInventory(MCInventoryHolder holder, int size, String title) {
InventoryHolder ih = null;

if(holder instanceof MCPlayer) {
ih = ((BukkitMCPlayer) holder)._Player();
} else if(holder instanceof MCHumanEntity) {
ih = ((BukkitMCHumanEntity) holder).asHumanEntity();
} else if(holder.getHandle() instanceof InventoryHolder) {
InventoryHolder ih;
if(holder == null) {
ih = null;
} else {
ih = (InventoryHolder) holder.getHandle();
}

if(title == null) {
return new BukkitMCInventory(Bukkit.createInventory(ih, size));
}
return new BukkitMCInventory(Bukkit.createInventory(ih, size, title));
}

Expand Down
@@ -0,0 +1,48 @@
package com.laytonsmith.abstraction.bukkit;

import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.MCVirtualInventoryHolder;
import com.laytonsmith.core.functions.InventoryManagement;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

public class BukkitMCVirtualInventoryHolder implements MCVirtualInventoryHolder {

private VirtualHolder vholder;

public BukkitMCVirtualInventoryHolder(String id) {
this.vholder = new VirtualHolder(id);
}

public BukkitMCVirtualInventoryHolder(InventoryHolder ih) {
this.vholder = (VirtualHolder) ih;
}

@Override
public MCInventory getInventory() {
return new BukkitMCInventory(vholder.getInventory());
}

@Override
public String getID() {
return vholder.id;
}

@Override
public Object getHandle() {
return this.vholder;
}

public class VirtualHolder implements InventoryHolder {
private String id;

VirtualHolder(String id) {
this.id = id;
}

@Override
public Inventory getInventory() {
return (Inventory) InventoryManagement.VIRTUAL_INVENTORIES.get(this.id).getHandle();
}
}
}
@@ -1,6 +1,8 @@
package com.laytonsmith.abstraction.bukkit.entities;

import com.laytonsmith.abstraction.AbstractionObject;
import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.bukkit.BukkitMCInventory;
import com.laytonsmith.abstraction.entities.MCVillager;
import com.laytonsmith.abstraction.enums.MCProfession;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCProfession;
Expand Down Expand Up @@ -31,4 +33,9 @@ public MCProfession getProfession() {
public void setProfession(MCProfession profession) {
getHandle().setProfession(BukkitMCProfession.getConvertor().getConcreteEnum(profession));
}

@Override
public MCInventory getInventory() {
return new BukkitMCInventory(getHandle().getInventory());
}
}
@@ -1,11 +1,10 @@
package com.laytonsmith.abstraction.entities;

import com.laytonsmith.abstraction.MCAgeable;
import com.laytonsmith.abstraction.MCInventoryHolder;
import com.laytonsmith.abstraction.enums.MCProfession;

public interface MCVillager extends MCAgeable {

public interface MCVillager extends MCAgeable, MCInventoryHolder {
MCProfession getProfession();

void setProfession(MCProfession profession);
}

0 comments on commit 90ddb51

Please sign in to comment.