Skip to content

Commit

Permalink
Reduce redundant saves for ender chests as well
Browse files Browse the repository at this point in the history
While they don't suffer the same owner-related issues that player inventories did, there's no need to save so many times.
  • Loading branch information
Jikoo committed Apr 11, 2016
1 parent eeb28b4 commit 1fbaa5b
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public interface ISpecialEnderChest {

public Inventory getBukkitInventory();

public boolean inventoryRemovalCheck();
public boolean inventoryRemovalCheck(boolean save);

public void setPlayerOnline(Player p);
public void setPlayerOnline(Player player);

/**
* Sets the Player associated with this ISpecialEnderChest offline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private final CraftPlayer owner;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);
Expand All @@ -60,30 +60,33 @@ public Inventory getBukkitInventory() {
}

@Override
public boolean inventoryRemovalCheck() {
owner.saveData();
return transaction.isEmpty() && !playerOnline;
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
owner.saveData();
}
return offline;
}

@Override
public void setPlayerOnline(Player p) {
public void setPlayerOnline(Player player) {
if (!playerOnline) {
try {
InventoryEnderChest playerEnderChest = ((CraftPlayer) p).getHandle().getEnderChest();
owner = (CraftPlayer) player;
InventoryEnderChest playerEnderChest = owner.getHandle().getEnderChest();
Field field = playerEnderChest.getClass().getField("items");
field.setAccessible(true);
field.set(playerEnderChest, this.items);
}
catch (Exception e) {}
p.saveData();
playerOnline = true;
}
}

@Override
public boolean setPlayerOffline() {
playerOnline = false;
return this.inventoryRemovalCheck();
return inventoryRemovalCheck(false);
}

@Override
Expand All @@ -99,7 +102,7 @@ public void onOpen(CraftHumanEntity who) {
@Override
public void onClose(CraftHumanEntity who) {
transaction.remove(who);
this.inventoryRemovalCheck();
this.inventoryRemovalCheck(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private final CraftPlayer owner;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);
Expand All @@ -60,30 +60,33 @@ public Inventory getBukkitInventory() {
}

@Override
public boolean inventoryRemovalCheck() {
owner.saveData();
return transaction.isEmpty() && !playerOnline;
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
owner.saveData();
}
return offline;
}

@Override
public void setPlayerOnline(Player p) {
public void setPlayerOnline(Player player) {
if (!playerOnline) {
try {
InventoryEnderChest playerEnderChest = ((CraftPlayer) p).getHandle().getEnderChest();
owner = (CraftPlayer) player;
InventoryEnderChest playerEnderChest = owner.getHandle().getEnderChest();
Field field = playerEnderChest.getClass().getField("items");
field.setAccessible(true);
field.set(playerEnderChest, this.items);
}
catch (Exception e) {}
p.saveData();
playerOnline = true;
}
}

@Override
public boolean setPlayerOffline() {
playerOnline = false;
return inventoryRemovalCheck();
return inventoryRemovalCheck(false);
}

@Override
Expand All @@ -99,7 +102,7 @@ public void onOpen(CraftHumanEntity who) {
@Override
public void onClose(CraftHumanEntity who) {
transaction.remove(who);
this.inventoryRemovalCheck();
this.inventoryRemovalCheck(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private final CraftPlayer owner;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);
Expand All @@ -60,30 +60,33 @@ public Inventory getBukkitInventory() {
}

@Override
public boolean inventoryRemovalCheck() {
owner.saveData();
return transaction.isEmpty() && !playerOnline;
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
owner.saveData();
}
return offline;
}

@Override
public void setPlayerOnline(Player p) {
public void setPlayerOnline(Player player) {
if (!playerOnline) {
try {
InventoryEnderChest playerEnderChest = ((CraftPlayer) p).getHandle().getEnderChest();
owner = (CraftPlayer) player;
InventoryEnderChest playerEnderChest = owner.getHandle().getEnderChest();
Field field = playerEnderChest.getClass().getField("items");
field.setAccessible(true);
field.set(playerEnderChest, this.items);
}
catch (Exception e) {}
p.saveData();
playerOnline = true;
}
}

@Override
public boolean setPlayerOffline() {
playerOnline = false;
return inventoryRemovalCheck();
return inventoryRemovalCheck(false);
}

@Override
Expand All @@ -99,7 +102,7 @@ public void onOpen(CraftHumanEntity who) {
@Override
public void onClose(CraftHumanEntity who) {
transaction.remove(who);
this.inventoryRemovalCheck();
this.inventoryRemovalCheck(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private final CraftPlayer owner;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);
Expand All @@ -60,30 +60,33 @@ public Inventory getBukkitInventory() {
}

@Override
public boolean inventoryRemovalCheck() {
owner.saveData();
return transaction.isEmpty() && !playerOnline;
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
owner.saveData();
}
return offline;
}

@Override
public void setPlayerOnline(Player p) {
public void setPlayerOnline(Player player) {
if (!playerOnline) {
try {
InventoryEnderChest playerEnderChest = ((CraftPlayer) p).getHandle().getEnderChest();
owner = (CraftPlayer) player;
InventoryEnderChest playerEnderChest = owner.getHandle().getEnderChest();
Field field = playerEnderChest.getClass().getField("items");
field.setAccessible(true);
field.set(playerEnderChest, this.items);
}
catch (Exception e) {}
p.saveData();
playerOnline = true;
}
}

@Override
public boolean setPlayerOffline() {
playerOnline = false;
return inventoryRemovalCheck();
return inventoryRemovalCheck(false);
}

@Override
Expand All @@ -99,7 +102,7 @@ public void onOpen(CraftHumanEntity who) {
@Override
public void onClose(CraftHumanEntity who) {
transaction.remove(who);
this.inventoryRemovalCheck();
this.inventoryRemovalCheck(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private final CraftPlayer owner;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);
Expand All @@ -60,30 +60,33 @@ public Inventory getBukkitInventory() {
}

@Override
public boolean inventoryRemovalCheck() {
owner.saveData();
return transaction.isEmpty() && !playerOnline;
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
owner.saveData();
}
return offline;
}

@Override
public void setPlayerOnline(Player p) {
public void setPlayerOnline(Player player) {
if (!playerOnline) {
try {
InventoryEnderChest playerEnderChest = ((CraftPlayer) p).getHandle().getEnderChest();
owner = (CraftPlayer) player;
InventoryEnderChest playerEnderChest = owner.getHandle().getEnderChest();
Field field = playerEnderChest.getClass().getField("items");
field.setAccessible(true);
field.set(playerEnderChest, this.items);
}
catch (Exception e) {}
p.saveData();
playerOnline = true;
}
}

@Override
public boolean setPlayerOffline() {
playerOnline = false;
return inventoryRemovalCheck();
return inventoryRemovalCheck(false);
}

@Override
Expand All @@ -99,7 +102,7 @@ public void onOpen(CraftHumanEntity who) {
@Override
public void onClose(CraftHumanEntity who) {
transaction.remove(who);
this.inventoryRemovalCheck();
this.inventoryRemovalCheck(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private final CraftPlayer owner;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);
Expand All @@ -60,30 +60,33 @@ public Inventory getBukkitInventory() {
}

@Override
public boolean inventoryRemovalCheck() {
owner.saveData();
return transaction.isEmpty() && !playerOnline;
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
owner.saveData();
}
return offline;
}

@Override
public void setPlayerOnline(Player p) {
public void setPlayerOnline(Player player) {
if (!playerOnline) {
try {
InventoryEnderChest playerEnderChest = ((CraftPlayer) p).getHandle().getEnderChest();
owner = (CraftPlayer) player;
InventoryEnderChest playerEnderChest = owner.getHandle().getEnderChest();
Field field = playerEnderChest.getClass().getField("items");
field.setAccessible(true);
field.set(playerEnderChest, this.items);
}
catch (Exception e) {}
p.saveData();
playerOnline = true;
}
}

@Override
public boolean setPlayerOffline() {
playerOnline = false;
return inventoryRemovalCheck();
return inventoryRemovalCheck(false);
}

@Override
Expand All @@ -99,7 +102,7 @@ public void onOpen(CraftHumanEntity who) {
@Override
public void onClose(CraftHumanEntity who) {
transaction.remove(who);
this.inventoryRemovalCheck();
this.inventoryRemovalCheck(true);
}

@Override
Expand Down
Loading

0 comments on commit 1fbaa5b

Please sign in to comment.