Skip to content

Commit 9fdbe02

Browse files
authored
Merge pull request #494 from PseudoKnight/master
Add new inventory functions and virtual inventories.
2 parents 9fdb6fe + 90ddb51 commit 9fdbe02

File tree

17 files changed

+776
-304
lines changed

17 files changed

+776
-304
lines changed

src/main/java/com/laytonsmith/abstraction/Convertor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public interface Convertor {
128128
*/
129129
MCInventory GetLocationInventory(MCLocation location);
130130

131+
MCInventoryHolder CreateInventoryHolder(String id);
132+
131133
/**
132134
* Run whenever the server is shutting down (or restarting). There is no guarantee provided as to what thread the
133135
* runnables actually run on, so you should ensure that the runnable executes it's actions on the appropriate thread
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.laytonsmith.abstraction;
2+
3+
public interface MCDoubleChest extends MCInventoryHolder {
4+
MCLocation getLocation();
5+
}

src/main/java/com/laytonsmith/abstraction/MCServer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ public interface MCServer extends AbstractionObject {
5656

5757
MCCommandMap getCommandMap();
5858

59-
MCInventory createInventory(MCInventoryHolder owner, MCInventoryType type);
59+
MCInventory createInventory(MCInventoryHolder owner, MCInventoryType type, String title);
6060

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

63-
MCInventory createInventory(MCInventoryHolder owner, int size);
64-
6563
/**
6664
* Provides access to local user data associated with a name. Depending on the implementation, a web lookup with the
6765
* official API may or may not be performed.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.laytonsmith.abstraction;
2+
3+
public interface MCVirtualInventoryHolder extends MCInventoryHolder {
4+
String getID();
5+
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitConvertor.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.laytonsmith.abstraction.MCEntity;
1313
import com.laytonsmith.abstraction.MCFireworkBuilder;
1414
import com.laytonsmith.abstraction.MCInventory;
15+
import com.laytonsmith.abstraction.MCInventoryHolder;
1516
import com.laytonsmith.abstraction.MCItemMeta;
1617
import com.laytonsmith.abstraction.MCItemStack;
1718
import com.laytonsmith.abstraction.MCLocation;
@@ -89,13 +90,11 @@
8990
import org.bukkit.World;
9091
import org.bukkit.block.Banner;
9192
import org.bukkit.block.Beacon;
92-
import org.bukkit.block.Block;
9393
import org.bukkit.block.BlockState;
9494
import org.bukkit.block.BrewingStand;
9595
import org.bukkit.block.Chest;
9696
import org.bukkit.block.CreatureSpawner;
9797
import org.bukkit.block.Dispenser;
98-
import org.bukkit.block.DoubleChest;
9998
import org.bukkit.block.Dropper;
10099
import org.bukkit.block.Furnace;
101100
import org.bukkit.block.Hopper;
@@ -556,27 +555,24 @@ public MCInventory GetEntityInventory(MCEntity e) {
556555
if(entity instanceof InventoryHolder) {
557556
if(entity instanceof Player) {
558557
return new BukkitMCPlayerInventory(((Player) entity).getInventory());
559-
} else {
560-
return new BukkitMCInventory(((InventoryHolder) entity).getInventory());
561558
}
562-
} else {
563-
return null;
559+
return new BukkitMCInventory(((InventoryHolder) entity).getInventory());
564560
}
561+
return null;
565562
}
566563

567564
@Override
568565
public MCInventory GetLocationInventory(MCLocation location) {
569-
Block b = ((Location) location.getHandle()).getBlock();
570-
if(b.getState() instanceof InventoryHolder) {
571-
if(b.getState() instanceof DoubleChest) {
572-
DoubleChest dc = (DoubleChest) b.getState();
573-
return new BukkitMCDoubleChest(dc.getLeftSide().getInventory(), dc.getRightSide().getInventory());
574-
} else {
575-
return new BukkitMCInventory(((InventoryHolder) b.getState()).getInventory());
576-
}
577-
} else {
578-
return null;
566+
BlockState bs = ((Location) location.getHandle()).getBlock().getState();
567+
if(bs instanceof InventoryHolder) {
568+
return new BukkitMCInventory(((InventoryHolder) bs).getInventory());
579569
}
570+
return null;
571+
}
572+
573+
@Override
574+
public MCInventoryHolder CreateInventoryHolder(String id) {
575+
return new BukkitMCVirtualInventoryHolder(id);
580576
}
581577

582578
@Override
Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,30 @@
11
package com.laytonsmith.abstraction.bukkit;
22

3-
import com.laytonsmith.abstraction.MCItemStack;
4-
import org.bukkit.inventory.Inventory;
5-
import org.bukkit.inventory.ItemStack;
3+
import com.laytonsmith.abstraction.MCDoubleChest;
4+
import com.laytonsmith.abstraction.MCInventory;
5+
import com.laytonsmith.abstraction.MCLocation;
6+
import org.bukkit.block.DoubleChest;
67

7-
public class BukkitMCDoubleChest extends BukkitMCInventory {
8+
public class BukkitMCDoubleChest implements MCDoubleChest {
89

9-
Inventory left;
10-
Inventory right;
10+
DoubleChest dc;
1111

12-
public BukkitMCDoubleChest(Inventory left, Inventory right) {
13-
super(left);
12+
public BukkitMCDoubleChest(DoubleChest chest) {
13+
this.dc = chest;
1414
}
1515

1616
@Override
17-
public int getSize() {
18-
return left.getSize() + right.getSize();
17+
public MCInventory getInventory() {
18+
return new BukkitMCInventory(this.dc.getInventory());
1919
}
2020

2121
@Override
22-
public MCItemStack getItem(int slot) {
23-
ItemStack is;
24-
if(slot < left.getSize()) {
25-
is = left.getItem(slot);
26-
} else {
27-
is = right.getItem(slot - left.getSize());
28-
}
29-
return new BukkitMCItemStack(is);
22+
public MCLocation getLocation() {
23+
return new BukkitMCLocation(this.dc.getLocation());
3024
}
3125

3226
@Override
33-
public void setItem(int slot, MCItemStack stack) {
34-
ItemStack is = (ItemStack) stack.getHandle();
35-
if(slot < left.getSize()) {
36-
left.setItem(slot, is);
37-
} else {
38-
right.setItem(slot - left.getSize(), is);
39-
}
40-
}
41-
42-
@Override
43-
public String toString() {
44-
return left.toString() + ":" + right.toString();
45-
}
46-
47-
@Override
48-
public boolean equals(Object obj) {
49-
if(obj == null) {
50-
return false;
51-
}
52-
if(getClass() != obj.getClass()) {
53-
return false;
54-
}
55-
final BukkitMCDoubleChest other = (BukkitMCDoubleChest) obj;
56-
if(this.left != other.left && (this.left == null || !this.left.equals(other.left))) {
57-
return false;
58-
}
59-
if(this.right != other.right && (this.right == null || !this.right.equals(other.right))) {
60-
return false;
61-
}
62-
return true;
63-
}
64-
65-
@Override
66-
public int hashCode() {
67-
int hash = 7;
68-
hash = 59 * hash + (this.left != null ? this.left.hashCode() : 0);
69-
hash = 59 * hash + (this.right != null ? this.right.hashCode() : 0);
70-
return hash;
27+
public Object getHandle() {
28+
return this.dc;
7129
}
7230
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCInventory.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
import com.laytonsmith.core.LogLevel;
1313
import com.laytonsmith.core.constructs.Target;
1414
import com.laytonsmith.core.exceptions.CRE.CRERangeException;
15+
import org.bukkit.block.BlockState;
16+
import org.bukkit.block.DoubleChest;
17+
import org.bukkit.entity.Entity;
1518
import org.bukkit.entity.HumanEntity;
1619
import org.bukkit.entity.Player;
1720
import org.bukkit.inventory.Inventory;
21+
import org.bukkit.inventory.InventoryHolder;
1822
import org.bukkit.inventory.ItemStack;
1923

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

136140
@Override
137141
public MCInventoryHolder getHolder() {
138-
return new BukkitMCInventoryHolder(i.getHolder());
142+
InventoryHolder ih = i.getHolder();
143+
if(ih instanceof BlockState) {
144+
return (MCInventoryHolder) BukkitConvertor.BukkitGetCorrectBlockState((BlockState) ih);
145+
} else if(ih instanceof Entity) {
146+
return (MCInventoryHolder) BukkitConvertor.BukkitGetCorrectEntity((Entity) ih);
147+
} else if(ih instanceof BukkitMCVirtualInventoryHolder.VirtualHolder) {
148+
return new BukkitMCVirtualInventoryHolder(ih);
149+
} else if(ih instanceof DoubleChest) {
150+
return new BukkitMCDoubleChest((DoubleChest) ih);
151+
}
152+
return new BukkitMCInventoryHolder(ih);
139153
}
140154

141155
@Override

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCServer.java

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.laytonsmith.abstraction.MCCommandMap;
66
import com.laytonsmith.abstraction.MCCommandSender;
77
import com.laytonsmith.abstraction.MCConsoleCommandSender;
8-
import com.laytonsmith.abstraction.MCHumanEntity;
98
import com.laytonsmith.abstraction.MCInventory;
109
import com.laytonsmith.abstraction.MCInventoryHolder;
1110
import com.laytonsmith.abstraction.MCItemFactory;
@@ -17,7 +16,6 @@
1716
import com.laytonsmith.abstraction.MCScoreboard;
1817
import com.laytonsmith.abstraction.MCServer;
1918
import com.laytonsmith.abstraction.MCWorld;
20-
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCHumanEntity;
2119
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer;
2220
import com.laytonsmith.abstraction.bukkit.pluginmessages.BukkitMCMessenger;
2321
import com.laytonsmith.abstraction.enums.MCBarColor;
@@ -437,47 +435,30 @@ public int hashCode() {
437435
}
438436

439437
@Override
440-
public MCInventory createInventory(MCInventoryHolder holder, MCInventoryType type) {
441-
InventoryHolder ih = null;
442-
443-
if(holder instanceof MCPlayer) {
444-
ih = ((BukkitMCPlayer) holder)._Player();
445-
} else if(holder instanceof MCHumanEntity) {
446-
ih = ((BukkitMCHumanEntity) holder).asHumanEntity();
447-
} else if(holder.getHandle() instanceof InventoryHolder) {
438+
public MCInventory createInventory(MCInventoryHolder holder, MCInventoryType type, String title) {
439+
InventoryHolder ih;
440+
if(holder == null) {
441+
ih = null;
442+
} else {
448443
ih = (InventoryHolder) holder.getHandle();
449444
}
450-
451-
return new BukkitMCInventory(Bukkit.createInventory(ih, InventoryType.valueOf(type.name())));
452-
}
453-
454-
@Override
455-
public MCInventory createInventory(MCInventoryHolder holder, int size) {
456-
InventoryHolder ih = null;
457-
458-
if(holder instanceof MCPlayer) {
459-
ih = ((BukkitMCPlayer) holder)._Player();
460-
} else if(holder instanceof MCHumanEntity) {
461-
ih = ((BukkitMCHumanEntity) holder).asHumanEntity();
462-
} else if(holder.getHandle() instanceof InventoryHolder) {
463-
ih = (InventoryHolder) holder.getHandle();
445+
if(title == null) {
446+
return new BukkitMCInventory(Bukkit.createInventory(ih, InventoryType.valueOf(type.name())));
464447
}
465-
466-
return new BukkitMCInventory(Bukkit.createInventory(ih, size));
448+
return new BukkitMCInventory(Bukkit.createInventory(ih, InventoryType.valueOf(type.name()), title));
467449
}
468450

469451
@Override
470452
public MCInventory createInventory(MCInventoryHolder holder, int size, String title) {
471-
InventoryHolder ih = null;
472-
473-
if(holder instanceof MCPlayer) {
474-
ih = ((BukkitMCPlayer) holder)._Player();
475-
} else if(holder instanceof MCHumanEntity) {
476-
ih = ((BukkitMCHumanEntity) holder).asHumanEntity();
477-
} else if(holder.getHandle() instanceof InventoryHolder) {
453+
InventoryHolder ih;
454+
if(holder == null) {
455+
ih = null;
456+
} else {
478457
ih = (InventoryHolder) holder.getHandle();
479458
}
480-
459+
if(title == null) {
460+
return new BukkitMCInventory(Bukkit.createInventory(ih, size));
461+
}
481462
return new BukkitMCInventory(Bukkit.createInventory(ih, size, title));
482463
}
483464

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.laytonsmith.abstraction.bukkit;
2+
3+
import com.laytonsmith.abstraction.MCInventory;
4+
import com.laytonsmith.abstraction.MCVirtualInventoryHolder;
5+
import com.laytonsmith.core.functions.InventoryManagement;
6+
import org.bukkit.inventory.Inventory;
7+
import org.bukkit.inventory.InventoryHolder;
8+
9+
public class BukkitMCVirtualInventoryHolder implements MCVirtualInventoryHolder {
10+
11+
private VirtualHolder vholder;
12+
13+
public BukkitMCVirtualInventoryHolder(String id) {
14+
this.vholder = new VirtualHolder(id);
15+
}
16+
17+
public BukkitMCVirtualInventoryHolder(InventoryHolder ih) {
18+
this.vholder = (VirtualHolder) ih;
19+
}
20+
21+
@Override
22+
public MCInventory getInventory() {
23+
return new BukkitMCInventory(vholder.getInventory());
24+
}
25+
26+
@Override
27+
public String getID() {
28+
return vholder.id;
29+
}
30+
31+
@Override
32+
public Object getHandle() {
33+
return this.vholder;
34+
}
35+
36+
public class VirtualHolder implements InventoryHolder {
37+
private String id;
38+
39+
VirtualHolder(String id) {
40+
this.id = id;
41+
}
42+
43+
@Override
44+
public Inventory getInventory() {
45+
return (Inventory) InventoryManagement.VIRTUAL_INVENTORIES.get(this.id).getHandle();
46+
}
47+
}
48+
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCVillager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.laytonsmith.abstraction.bukkit.entities;
22

33
import com.laytonsmith.abstraction.AbstractionObject;
4+
import com.laytonsmith.abstraction.MCInventory;
5+
import com.laytonsmith.abstraction.bukkit.BukkitMCInventory;
46
import com.laytonsmith.abstraction.entities.MCVillager;
57
import com.laytonsmith.abstraction.enums.MCProfession;
68
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCProfession;
@@ -31,4 +33,9 @@ public MCProfession getProfession() {
3133
public void setProfession(MCProfession profession) {
3234
getHandle().setProfession(BukkitMCProfession.getConvertor().getConcreteEnum(profession));
3335
}
36+
37+
@Override
38+
public MCInventory getInventory() {
39+
return new BukkitMCInventory(getHandle().getInventory());
40+
}
3441
}

0 commit comments

Comments
 (0)