Graph system fully implemented. Pending testing of all types of

inventories.
This commit is contained in:
sigonasr2 2017-04-05 00:00:50 -05:00
parent 87dbf99a31
commit a8aa59ed25
4 changed files with 261 additions and 122 deletions

Binary file not shown.

View File

@ -7,6 +7,7 @@ import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -284,8 +285,14 @@ public class InventoryUtils {
} }
public static int getInventoryNumberHash(Inventory destination) { public static int getInventoryNumberHash(Inventory destination) {
Location loc = destination.getLocation();
int id=-99;
if (loc!=null) {
String hash = "-"+(Math.signum(destination.getLocation().getBlockX()+destination.getLocation().getBlockZ())>0?1:0)+Integer.toString(Math.abs(destination.getLocation().getBlockX())%1000)+Integer.toString(Math.abs(destination.getLocation().getBlockY())%1000)+Integer.toString(Math.abs(destination.getLocation().getBlockZ())%1000); String hash = "-"+(Math.signum(destination.getLocation().getBlockX()+destination.getLocation().getBlockZ())>0?1:0)+Integer.toString(Math.abs(destination.getLocation().getBlockX())%1000)+Integer.toString(Math.abs(destination.getLocation().getBlockY())%1000)+Integer.toString(Math.abs(destination.getLocation().getBlockZ())%1000);
return Integer.parseInt(hash); id = Integer.parseInt(hash);
TwosideKeeper.itemCubeGraph.addVertex(id);
}
return id;
} }
public static ItemStack[] RemoveAllNullItems(ItemStack[] contents) { public static ItemStack[] RemoveAllNullItems(ItemStack[] contents) {

View File

@ -439,6 +439,32 @@ public class ItemCubeUtils {
TwosideKeeper.log(" "+edge.toString(), 0); TwosideKeeper.log(" "+edge.toString(), 0);
} }
} }
public static void setupGraphForChest(Inventory inventory) {
if (inventory!=null && inventory.getHolder() instanceof Player &&
(PlayerStructure.GetPlayerStructure(((Player)inventory.getHolder())).isViewingItemCube ||
PlayerStructure.GetPlayerStructure(((Player)inventory.getHolder())).opened_another_cube)&&
inventory.getTitle()!=null && inventory.getTitle().contains("Item Cube #")) {
return; //This is an item cube, don't setup a graph for it.
}
UndirectedGraph<Integer,DefaultEdge> graph = TwosideKeeper.itemCubeGraph;
graph.addVertex(InventoryUtils.getInventoryNumberHash(inventory)); //Root Vertex.
for (ItemStack it : inventory.getContents()) {
if (ItemUtils.isValidItem(it) && isItemCube(it)) {
int id = getItemCubeID(it);
graph.addVertex(id);
graph.addEdge(InventoryUtils.getInventoryNumberHash(inventory), id);
IterateAndAddToGraph(id,graph);
}
}
for (DefaultEdge edge : graph.edgeSet()) {
TwosideKeeper.log(" "+edge.toString(), 0);
}
}
public static void IterateAndAddToGraph(int id, UndirectedGraph<Integer, DefaultEdge> graph) { public static void IterateAndAddToGraph(int id, UndirectedGraph<Integer, DefaultEdge> graph) {
List<ItemStack> contents = getItemCubeContents(id); List<ItemStack> contents = getItemCubeContents(id);
for (ItemStack it : contents) { for (ItemStack it : contents) {
@ -582,10 +608,4 @@ public class ItemCubeUtils {
} }
return false; return false;
} }
/*public static void setupGraphForChest(Inventory inv) {
if (inv.getType()==InventoryType.CHEST ||
inv.getType()==InventoryType.) {
}
}*/
} }

View File

@ -4882,18 +4882,54 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
} }
if (ItemCubeUtils.isItemCube(ev.getItemDrop().getItemStack())) { if (ItemCubeUtils.isItemCube(ev.getItemDrop().getItemStack())) {
ItemCubeUtils.removeAndUpdateAllEdgesDroppedItem(ItemCubeUtils.getItemCubeID(ev.getItemDrop().getItemStack()),ev.getPlayer()); //ItemCubeUtils.removeAndUpdateAllEdgesDroppedItem(ItemCubeUtils.getItemCubeID(ev.getItemDrop().getItemStack()),ev.getPlayer());
//ItemCubeUtils.DestroyAllTargetEdges(ItemCubeUtils.getItemCubeID(ev.getItemDrop().getItemStack()), TwosideKeeper.itemCubeGraph);
int cubeid = ItemCubeUtils.getItemCubeID(ev.getItemDrop().getItemStack());
if (ev.getPlayer().getOpenInventory()!=null && if (ev.getPlayer().getOpenInventory()!=null &&
ev.getPlayer().getOpenInventory().getTitle()!=null && ev.getPlayer().getOpenInventory().getTitle()!=null &&
ev.getPlayer().getOpenInventory().getTitle().contains("Item Cube #") && ev.getPlayer().getOpenInventory().getTitle().contains("Item Cube #") &&
PlayerStructure.GetPlayerStructure(ev.getPlayer()).isViewingItemCube) { PlayerStructure.GetPlayerStructure(ev.getPlayer()).isViewingItemCube) {
//We are viewing an item cube. Update it. //We are viewing an item cube. Update it.
int id = Integer.parseInt(ev.getPlayer().getOpenInventory().getTitle().split("#")[1]); int id = Integer.parseInt(ev.getPlayer().getOpenInventory().getTitle().split("#")[1]);
ItemCubeUtils.removeAndUpdateAllEdges(id,ev.getPlayer()); //ItemCubeUtils.removeAndUpdateAllEdges(id,ev.getPlayer());
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(id, cubeid);
if (edge!=null) {
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} else
edge = TwosideKeeper.itemCubeGraph.removeEdge(PlayerStructure.getPlayerNegativeHash(ev.getPlayer()), cubeid);
if (edge!=null) {
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
}
} else {
int id = InventoryUtils.getInventoryNumberHash(ev.getPlayer().getOpenInventory().getTopInventory());
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(id, cubeid);
if (edge!=null) {
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} else
edge = TwosideKeeper.itemCubeGraph.removeEdge(PlayerStructure.getPlayerNegativeHash(ev.getPlayer()), cubeid);
if (edge!=null) {
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} }
} }
/* else {
ItemCubeUtils.removeAndUpdateAllEdges(InventoryUtils.getInventoryNumberHash(ev.getPlayer().getOpenInventory().getTopInventory()),ev.getPlayer());
}*/
}
/*if (ItemCubeUtils.isItemCube(ev.getItemDrop().getItemStack())) {
if (ev.getPlayer().getOpenInventory()!=null &&
ev.getPlayer().getOpenInventory().getTitle()!=null &&
ev.getPlayer().getOpenInventory().getTitle().contains("Item Cube #") &&
PlayerStructure.GetPlayerStructure(ev.getPlayer()).isViewingItemCube) {
int id = Integer.parseInt(ev.getPlayer().getOpenInventory().getTitle().split("#")[1]);
int cubeid = ItemCubeUtils.getItemCubeID(ev.getItemDrop().getItemStack());
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(id, cubeid);
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} else {
}
}*/
return; return;
} }
@ -4904,7 +4940,7 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
//GenericFunctions.updateSetItemsInInventory(ev.getInventory()); //GenericFunctions.updateSetItemsInInventory(ev.getInventory());
GenericFunctions.updateSetItemsInInventory(ev.getView().getBottomInventory()); GenericFunctions.updateSetItemsInInventory(ev.getView().getBottomInventory());
GenericFunctions.updateSetItemsInInventory(ev.getView().getTopInventory()); GenericFunctions.updateSetItemsInInventory(ev.getView().getTopInventory());
//ItemCubeUtils.setupGraphForChest(ev.getInventory()); ItemCubeUtils.setupGraphForChest(ev.getInventory());
} }
@EventHandler(priority=EventPriority.LOW,ignoreCancelled = true) @EventHandler(priority=EventPriority.LOW,ignoreCancelled = true)
@ -5618,6 +5654,11 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
if (result.size()>0) { if (result.size()>0) {
ev.setCursor((ItemStack)result.get(0)); ev.setCursor((ItemStack)result.get(0));
} else { } else {
if (ItemCubeUtils.isItemCube(ev.getCursor())) {
int id = ItemCubeUtils.getItemCubeID(ev.getCursor());
DefaultEdge edge = TwosideKeeper.itemCubeGraph.addEdge(idnumb, id);
TwosideKeeper.log("Added edge "+edge, 0);
}
ev.setCursor(new ItemStack(Material.AIR)); ev.setCursor(new ItemStack(Material.AIR));
log("Cursor should be air.",5); log("Cursor should be air.",5);
} }
@ -5654,6 +5695,11 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
if (result.size()>0) { if (result.size()>0) {
ev.setCursor((ItemStack)result.get(0)); ev.setCursor((ItemStack)result.get(0));
} else { } else {
if (ItemCubeUtils.isItemCube(ev.getCursor())) {
int id = ItemCubeUtils.getItemCubeID(ev.getCursor());
DefaultEdge edge = TwosideKeeper.itemCubeGraph.addEdge(idnumb, id);
TwosideKeeper.log("Added edge "+edge, 0);
}
ev.setCursor(new ItemStack(Material.AIR)); ev.setCursor(new ItemStack(Material.AIR));
} }
List<ItemStack> itemslist = new ArrayList<ItemStack>(); List<ItemStack> itemslist = new ArrayList<ItemStack>();
@ -5768,13 +5814,15 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
} }
} }
if (ev.getClickedInventory()!=null) { if (ev.getClickedInventory()!=null &&
ev.getInventory().getType()!=InventoryType.CRAFTING) {
Player p = (Player)ev.getWhoClicked(); Player p = (Player)ev.getWhoClicked();
if (ev.getAction()==InventoryAction.HOTBAR_SWAP || ev.getAction()==InventoryAction.HOTBAR_MOVE_AND_READD) { if ((ev.getAction()==InventoryAction.HOTBAR_SWAP || ev.getAction()==InventoryAction.HOTBAR_MOVE_AND_READD)) {
Inventory clickedinv = p.getOpenInventory().getTopInventory(); Inventory clickedinv = p.getOpenInventory().getTopInventory();
if (clickedinv!=null && ev.getRawSlot()<clickedinv.getSize()) {
ItemStack item1 = p.getInventory().getItem(ev.getHotbarButton()); //Bottom to Top ItemStack item1 = p.getInventory().getItem(ev.getHotbarButton()); //Bottom to Top
ItemStack item2 = p.getOpenInventory().getItem(ev.getRawSlot()); //Top to Bottom ItemStack item2 = p.getOpenInventory().getItem(ev.getRawSlot()); //Top to Bottom
if (clickedinv!=null && clickedinv.getTitle()!=null && clickedinv.getTitle().contains("Item Cube #") && if (clickedinv.getTitle()!=null && clickedinv.getTitle().contains("Item Cube #") &&
ev.getRawSlot()<p.getOpenInventory().getTopInventory().getSize()) { ev.getRawSlot()<p.getOpenInventory().getTopInventory().getSize()) {
int id = Integer.parseInt(clickedinv.getTitle().split("#")[1]); int id = Integer.parseInt(clickedinv.getTitle().split("#")[1]);
if (ItemCubeUtils.isItemCube(item1)) { if (ItemCubeUtils.isItemCube(item1)) {
@ -5799,7 +5847,6 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
ev.setCursor(ev.getCursor()); ev.setCursor(ev.getCursor());
return; return;
} }
return;
} else { } else {
ev.setCancelled(true); ev.setCancelled(true);
ev.setCursor(ev.getCursor()); ev.setCursor(ev.getCursor());
@ -5819,6 +5866,28 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
return; return;
} }
} }
} else {
if (ItemCubeUtils.isItemCube(item1)) {
int cubeid = ItemCubeUtils.getItemCubeID(item1);
int invid = InventoryUtils.getInventoryNumberHash(clickedinv);
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(PlayerStructure.getPlayerNegativeHash(p), cubeid);
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
edge = TwosideKeeper.itemCubeGraph.addEdge(invid, cubeid);
TwosideKeeper.log("Added edge "+edge, TwosideKeeper.GRAPH_DEBUG);
TwosideKeeper.log("Vertices in "+clickedinv.getType()+" (ID:"+invid+"):", TwosideKeeper.GRAPH_DEBUG);
for (DefaultEdge e : TwosideKeeper.itemCubeGraph.edgesOf(invid)) {
TwosideKeeper.log(" "+e, 0);
}
}
if (ItemCubeUtils.isItemCube(item2)) {
int cubeid = ItemCubeUtils.getItemCubeID(item2);
int invid = InventoryUtils.getInventoryNumberHash(clickedinv);
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(invid, cubeid);
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
edge = TwosideKeeper.itemCubeGraph.addEdge(PlayerStructure.getPlayerNegativeHash(p), cubeid);
TwosideKeeper.log("Added edge "+edge, TwosideKeeper.GRAPH_DEBUG);
}
}
} }
} }
@ -5826,11 +5895,11 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
if (ev.getClickedInventory().getType()==InventoryType.PLAYER) { if (ev.getClickedInventory().getType()==InventoryType.PLAYER) {
//Check the top slot to see if it's an item cube inventory. //Check the top slot to see if it's an item cube inventory.
Inventory clickedinv = p.getOpenInventory().getTopInventory(); Inventory clickedinv = p.getOpenInventory().getTopInventory();
int clickedid = ItemCubeUtils.getItemCubeID(ev.getCurrentItem());
if (!InventoryUtils.hasFullInventory(clickedinv)) {
if (pd.isViewingItemCube && clickedinv.getTitle()!=null && if (pd.isViewingItemCube && clickedinv.getTitle()!=null &&
clickedinv.getTitle().contains("Item Cube #")) { clickedinv.getTitle().contains("Item Cube #")) {
if (!InventoryUtils.hasFullInventory(clickedinv)) {
int id = Integer.parseInt(clickedinv.getTitle().split("#")[1]); int id = Integer.parseInt(clickedinv.getTitle().split("#")[1]);
int clickedid = ItemCubeUtils.getItemCubeID(ev.getCurrentItem());
TwosideKeeper.log("ID is "+id+":"+clickedid, 0); TwosideKeeper.log("ID is "+id+":"+clickedid, 0);
if (id!=clickedid) { if (id!=clickedid) {
DefaultEdge edge = TwosideKeeper.itemCubeGraph.addEdge(id, clickedid); DefaultEdge edge = TwosideKeeper.itemCubeGraph.addEdge(id, clickedid);
@ -5858,7 +5927,16 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
ev.setCursor(ev.getCursor()); ev.setCursor(ev.getCursor());
return; return;
} }
} else { //This is not an item cube.
int invid = InventoryUtils.getInventoryNumberHash(clickedinv);
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(PlayerStructure.getPlayerNegativeHash(p), clickedid);
TwosideKeeper.log("Removed edge "+TwosideKeeper.itemCubeGraph.getEdgeSource(edge)+","+TwosideKeeper.itemCubeGraph.getEdgeTarget(edge), TwosideKeeper.GRAPH_DEBUG);
edge = TwosideKeeper.itemCubeGraph.addEdge(invid, clickedid);
TwosideKeeper.log("Added edge "+TwosideKeeper.itemCubeGraph.getEdgeSource(edge)+","+TwosideKeeper.itemCubeGraph.getEdgeTarget(edge), TwosideKeeper.GRAPH_DEBUG);
} }
return;
} else {
return;
} }
} else } else
{ {
@ -5873,12 +5951,20 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
int id = Integer.parseInt(clickedinv.getTitle().split("#")[1]); int id = Integer.parseInt(clickedinv.getTitle().split("#")[1]);
edge = TwosideKeeper.itemCubeGraph.removeEdge(id, clickedid); edge = TwosideKeeper.itemCubeGraph.removeEdge(id, clickedid);
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG); TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} else {
//This is not an item cube.
int invid = InventoryUtils.getInventoryNumberHash(clickedinv);
edge = TwosideKeeper.itemCubeGraph.removeEdge(invid, clickedid);
TwosideKeeper.log("Removed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} }
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
ev.setCancelled(true); ev.setCancelled(true);
ev.setCursor(ev.getCursor()); ev.setCursor(ev.getCursor());
return; return;
} }
return;
} else {
return;
} }
} }
} }
@ -5915,6 +6001,16 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
ev.setCursor(ev.getCursor()); ev.setCursor(ev.getCursor());
return; return;
} }
} else {
int clickedid = ItemCubeUtils.getItemCubeID(ev.getCursor());
try {
DefaultEdge edge = TwosideKeeper.itemCubeGraph.addEdge(InventoryUtils.getInventoryNumberHash(clickedinv), clickedid);
TwosideKeeper.log("Added edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} catch (IllegalArgumentException ex) {
ev.setCancelled(true);
ev.setCursor(ev.getCursor());
return;
}
} }
} }
@ -5934,6 +6030,10 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
int clickedid = ItemCubeUtils.getItemCubeID(ev.getCurrentItem()); int clickedid = ItemCubeUtils.getItemCubeID(ev.getCurrentItem());
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(PlayerStructure.getPlayerNegativeHash(p), clickedid); DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(PlayerStructure.getPlayerNegativeHash(p), clickedid);
TwosideKeeper.log("Destroyed edge "+edge, TwosideKeeper.GRAPH_DEBUG); TwosideKeeper.log("Destroyed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} else {
int clickedid = ItemCubeUtils.getItemCubeID(ev.getCurrentItem());
DefaultEdge edge = TwosideKeeper.itemCubeGraph.removeEdge(InventoryUtils.getInventoryNumberHash(clickedinv), clickedid);
TwosideKeeper.log("Destroyed edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} }
} }
} }
@ -5941,6 +6041,7 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
@EventHandler(priority=EventPriority.LOW,ignoreCancelled = true) @EventHandler(priority=EventPriority.LOW,ignoreCancelled = true)
public void onInventoryDrag(InventoryDragEvent ev) { public void onInventoryDrag(InventoryDragEvent ev) {
if (ev.getInventory()!=null && ev.getInventory().getType()!=InventoryType.CRAFTING) {
for (Integer i : ev.getNewItems().keySet()) { for (Integer i : ev.getNewItems().keySet()) {
ItemStack item = ev.getNewItems().get(i); ItemStack item = ev.getNewItems().get(i);
if (ItemCubeUtils.isItemCube(item)) { if (ItemCubeUtils.isItemCube(item)) {
@ -5981,6 +6082,17 @@ public class TwosideKeeper extends JavaPlugin implements Listener {
ev.setCursor(ev.getCursor()); ev.setCursor(ev.getCursor());
return; return;
} }
} else {
int clickedid = ItemCubeUtils.getItemCubeID(item);
try {
DefaultEdge edge = TwosideKeeper.itemCubeGraph.addEdge(InventoryUtils.getInventoryNumberHash(clickedinv), clickedid);
TwosideKeeper.log("Added edge "+edge, TwosideKeeper.GRAPH_DEBUG);
} catch (IllegalArgumentException ex) {
ev.setCancelled(true);
ev.setCursor(ev.getCursor());
return;
}
}
} }
} }
} }