Skip to content
Permalink
Browse files

Support virtual inventories for get_inventory_name()

  • Loading branch information
PseudoKnight committed Nov 24, 2019
1 parent 85f41ee commit 06a064dc92cea89f8fbd2140c3652e26fa1f9559
@@ -125,7 +125,7 @@
*/
MCInventory GetLocationInventory(MCLocation location);

MCInventoryHolder CreateInventoryHolder(String id);
MCInventoryHolder CreateInventoryHolder(String id, String title);

/**
* Run whenever the server is shutting down (or restarting). There is no guarantee provided as to what thread the
@@ -583,8 +583,8 @@ public MCInventory GetLocationInventory(MCLocation location) {
}

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

@Override
@@ -153,7 +153,7 @@ public MCInventoryHolder getHolder() {
} else if(ih == null) {
for(Map.Entry<String, MCInventory> entry : InventoryManagement.VIRTUAL_INVENTORIES.entrySet()) {
if(entry.getValue().equals(this)) {
return new BukkitMCVirtualInventoryHolder(entry.getKey());
return new BukkitMCVirtualInventoryHolder(entry.getKey(), getTitle());
}
}
}
@@ -162,6 +162,10 @@ public MCInventoryHolder getHolder() {

@Override
public String getTitle() {
return ((Nameable) i.getHolder()).getCustomName();
Nameable n = ((Nameable) i.getHolder());
if(n == null) {
return null;
}
return n.getCustomName();
}
}
@@ -3,15 +3,16 @@
import com.laytonsmith.abstraction.MCInventory;
import com.laytonsmith.abstraction.MCVirtualInventoryHolder;
import com.laytonsmith.core.functions.InventoryManagement;
import org.bukkit.Nameable;
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(String id, String title) {
this.vholder = new VirtualHolder(id, title);
}

public BukkitMCVirtualInventoryHolder(InventoryHolder ih) {
@@ -33,16 +34,28 @@ public Object getHandle() {
return this.vholder;
}

public class VirtualHolder implements InventoryHolder {
private String id;
public class VirtualHolder implements InventoryHolder, Nameable {
private final String id;
private final String title;

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

@Override
public Inventory getInventory() {
return (Inventory) InventoryManagement.VIRTUAL_INVENTORIES.get(this.id).getHandle();
}

@Override
public String getCustomName() {
return title;
}

@Override
public void setCustomName(String name) {
// not modifiable at this time
}
}
}
@@ -1047,7 +1047,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
"Could not find the inventory of the given player (are you running in cmdline mode?)", t);
}

for(int i = 35; i >= 0; i--) {
for(int i = 0; i <= 40; i++) {
MCItemStack iis = inv.getItem(i);
if(remaining <= 0) {
break;
@@ -1830,7 +1830,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
MCInventory inventory = InventoryManagement.GetInventory(args[0], w, t);
try {
return new CString(inventory.getTitle(), t);
} catch (NullPointerException | ClassCastException ex) {
} catch (ClassCastException ex) {
throw new CREIllegalArgumentException("This inventory is not capable of being named.", t);
}
}
@@ -1847,8 +1847,9 @@ public String getName() {

@Override
public String docs() {
return "string {specifier} Returns the name of the inventory specified. If the block or entity"
+ " can't have an inventory or a name, an IllegalArgumentException is thrown.";
return "string {specifier} Returns the name of the inventory holder specified. If the block or entity"
+ " can't have an inventory or a name, an IllegalArgumentException is thrown."
+ " If the inventory was not given a title, an empty string is returned.";
}

@Override
@@ -2707,7 +2708,7 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
}
}

MCInventoryHolder holder = StaticLayer.GetConvertor().CreateInventoryHolder(id);
MCInventoryHolder holder = StaticLayer.GetConvertor().CreateInventoryHolder(id, title);
MCInventory inv;
if(type == null) {
inv = Static.getServer().createInventory(holder, size, title);
@@ -1182,7 +1182,7 @@ public MCInventory GetLocationInventory(MCLocation location) {
}

@Override
public MCInventoryHolder CreateInventoryHolder(String id) {
public MCInventoryHolder CreateInventoryHolder(String id, String title) {
throw new UnsupportedOperationException("This method is not supported from a shell.");
}

@@ -762,7 +762,7 @@ public MCInventory GetLocationInventory(MCLocation location) {
}

@Override
public MCInventoryHolder CreateInventoryHolder(String id) {
public MCInventoryHolder CreateInventoryHolder(String id, String title) {
throw new UnsupportedOperationException("Not supported yet.");
}

0 comments on commit 06a064d

Please sign in to comment.
You can’t perform that action at this time.