Skip to content

Commit

Permalink
Configuration to disable saving of offline players, bugfixing
Browse files Browse the repository at this point in the history
Edits made to online players will still work. Edits made to offline players will work if they log in before the ISpecialPlayerInventory is unloaded (currently before the plugin unloads).
Closes #6
Refactored IPlayerDataManager to PlayerDataManager, it's no longer an interface.
Fixed a bug with players' online state initially being inverted
  • Loading branch information
Jikoo committed Apr 18, 2016
1 parent 7256494 commit a929eee
Show file tree
Hide file tree
Showing 56 changed files with 319 additions and 197 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ This fork of OpenInv was created when the lag caused by offline player lookups p
### Changes of Note
- Removed updater
- Removed wand
- New configuration option `DisableSaving`: Inventory edits will not be saved unless the user is online.

## Previous Versions
Any version of Minecraft this fork has been existent for should have a [release](https://github.com/Jikoo/OpenInv/releases) tagged. Features are not backported, however.
This fork supports any version after 1.4.5. Binaries are available in the [releases tab](https://github.com/Jikoo/OpenInv/releases). Please allow a brief period after the release of a new version for updates.

## License
```
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/lishid/openinv/OpenInv.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import com.lishid.openinv.commands.SilentChestPluginCommand;
import com.lishid.openinv.internal.IAnySilentChest;
import com.lishid.openinv.internal.IInventoryAccess;
import com.lishid.openinv.internal.IPlayerDataManager;
import com.lishid.openinv.internal.ISpecialEnderChest;
import com.lishid.openinv.internal.ISpecialPlayerInventory;
import com.lishid.openinv.internal.InternalAccessor;
import com.lishid.openinv.internal.PlayerDataManager;

/**
* Open other player's inventory
Expand All @@ -50,7 +50,7 @@ public class OpenInv extends JavaPlugin {
private final Map<String, ISpecialEnderChest> enderChests = new HashMap<String, ISpecialEnderChest>();

private InternalAccessor accessor;
private IPlayerDataManager playerLoader;
private PlayerDataManager playerLoader;
private IInventoryAccess inventoryAccess;
private IAnySilentChest anySilentChest;

Expand All @@ -73,12 +73,26 @@ public void onEnable() {
anySilentChest = accessor.newAnySilentChest();

FileConfiguration config = getConfig();
config.set("NotifySilentChest", config.getBoolean("NotifySilentChest", true));
config.set("NotifyAnyChest", config.getBoolean("NotifyAnyChest", true));
boolean dirtyConfig = false;
if (!config.isBoolean("NotifySilentChest")) {
config.set("NotifySilentChest", true);
dirtyConfig = true;
}
if (!config.isBoolean("NotifyAnyChest")) {
config.set("NotifyAnyChest", true);
dirtyConfig = true;
}
if (!config.isBoolean("DisableSaving")) {
config.set("DisableSaving", false);
dirtyConfig = true;
}
config.addDefault("NotifySilentChest", true);
config.addDefault("NotifyAnyChest", true);
config.addDefault("DisableSaving", false);
config.options().copyDefaults(true);
saveConfig();
if (dirtyConfig) {
saveConfig();
}

pm.registerEvents(new OpenInvPlayerListener(this), this);
pm.registerEvents(new OpenInvInventoryListener(this), this);
Expand All @@ -95,7 +109,7 @@ public InternalAccessor getInternalAccessor() {
return this.accessor;
}

public IPlayerDataManager getPlayerLoader() {
public PlayerDataManager getPlayerLoader() {
return this.playerLoader;
}

Expand All @@ -115,12 +129,12 @@ public ISpecialPlayerInventory getInventoryFor(Player player) {
return null;
}

public ISpecialPlayerInventory getOrCreateInventoryFor(Player player, boolean offline) {
public ISpecialPlayerInventory getOrCreateInventoryFor(Player player, boolean online) {
String id = getPlayerLoader().getPlayerDataID(player);
if (inventories.containsKey(id)) {
return inventories.get(id);
}
ISpecialPlayerInventory inv = getInternalAccessor().newSpecialPlayerInventory(player, offline);
ISpecialPlayerInventory inv = getInternalAccessor().newSpecialPlayerInventory(player, online);
inventories.put(id, inv);
return inv;
}
Expand All @@ -140,12 +154,12 @@ public ISpecialEnderChest getEnderChestFor(Player player) {
return null;
}

public ISpecialEnderChest getOrCreateEnderChestFor(Player player, boolean offline) {
public ISpecialEnderChest getOrCreateEnderChestFor(Player player, boolean online) {
String id = getPlayerLoader().getPlayerDataID(player);
if (enderChests.containsKey(id)) {
return enderChests.get(id);
}
ISpecialEnderChest inv = getInternalAccessor().newSpecialEnderChest(player, offline);
ISpecialEnderChest inv = getInternalAccessor().newSpecialEnderChest(player, online);
enderChests.put(id, inv);
return inv;
}
Expand All @@ -157,6 +171,10 @@ public void removeLoadedEnderChest(Player player) {
}
}

public boolean disableSaving() {
return getConfig().getBoolean("DisableSaving", false);
}

public boolean notifySilentChest() {
return getConfig().getBoolean("NotifySilentChest", true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ public OpenInvInventoryListener(OpenInv plugin) {

@EventHandler(priority = EventPriority.NORMAL)
public void onInventoryClick(InventoryClickEvent event) {
// If this is the top inventory
// if (event.getView().convertSlot(event.getRawSlot()) == event.getRawSlot())
// {
if (!plugin.getInventoryAccess().check(event.getInventory(), event.getWhoClicked())) {
event.setCancelled(true);
}
// }
}

@EventHandler(priority = EventPriority.NORMAL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public void run() {
private void openInventory(Player player, OfflinePlayer target) {

Player onlineTarget;
if (!target.isOnline()) {
boolean online = target.isOnline();

if (!online) {
// Try loading the player's data
onlineTarget = plugin.getPlayerLoader().loadPlayer(target);

Expand All @@ -129,7 +131,7 @@ private void openInventory(Player player, OfflinePlayer target) {
openEnderHistory.put(player, onlineTarget.getName());

// Create the inventory
ISpecialEnderChest chest = plugin.getOrCreateEnderChestFor(onlineTarget, !target.isOnline());
ISpecialEnderChest chest = plugin.getOrCreateEnderChestFor(onlineTarget, online);

// Open the inventory
player.openInventory(chest.getBukkitInventory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ private void openInventory(Player player, OfflinePlayer target) {


Player onlineTarget;
boolean online = target.isOnline();

if (!target.isOnline()) {
if (!online) {
// Try loading the player's data
onlineTarget = plugin.getPlayerLoader().loadPlayer(target);

Expand Down Expand Up @@ -143,7 +144,7 @@ private void openInventory(Player player, OfflinePlayer target) {
openInvHistory.put(player, onlineTarget.getName());

// Create the inventory
ISpecialPlayerInventory inv = plugin.getOrCreateInventoryFor(onlineTarget, !target.isOnline());
ISpecialPlayerInventory inv = plugin.getOrCreateInventoryFor(onlineTarget, online);

// Open the inventory
player.openInventory(inv.getBukkitInventory());
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/lishid/openinv/internal/InternalAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ private void printErrorMessage() {
plugin.getLogger().warning("OpenInv encountered an error with the CraftBukkit version \"" + version + "\". Please look for an updated version of OpenInv.");
}

public IPlayerDataManager newPlayerDataManager() {
return (IPlayerDataManager) createObject(IPlayerDataManager.class, "PlayerDataManager");
public PlayerDataManager newPlayerDataManager() {
return (PlayerDataManager) createObject(PlayerDataManager.class, "PlayerDataManager");
}

public IInventoryAccess newInventoryAccess() {
Expand All @@ -71,7 +71,9 @@ public ISpecialPlayerInventory newSpecialPlayerInventory(Player player, boolean
try {
Class<?> internalClass = Class.forName("com.lishid.openinv.internal." + version + ".SpecialPlayerInventory");
if (ISpecialPlayerInventory.class.isAssignableFrom(internalClass)) {
return (ISpecialPlayerInventory) internalClass.getConstructor(Player.class, Boolean.class).newInstance(player, offline);
return (ISpecialPlayerInventory) internalClass
.getConstructor(OpenInv.class, Player.class, Boolean.class)
.newInstance(this.plugin, player, offline);
}
}
catch (Exception e) {
Expand All @@ -86,7 +88,9 @@ public ISpecialEnderChest newSpecialEnderChest(Player player, boolean offline) {
try {
Class<?> internalClass = Class.forName("com.lishid.openinv.internal." + version + ".SpecialEnderChest");
if (ISpecialEnderChest.class.isAssignableFrom(internalClass)) {
return (ISpecialEnderChest) internalClass.getConstructor(Player.class, Boolean.class).newInstance(player, offline);
return (ISpecialEnderChest) internalClass
.getConstructor(OpenInv.class, Player.class, Boolean.class)
.newInstance(this.plugin, player, offline);
}
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

public abstract class IPlayerDataManager {
public abstract class PlayerDataManager {
public final Player loadPlayer(OfflinePlayer offline) {
if (offline.isOnline()) {
return offline.getPlayer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import com.lishid.openinv.internal.IPlayerDataManager;

// Volatile
import net.minecraft.server.v1_4_5.EntityPlayer;
import net.minecraft.server.v1_4_5.ItemInWorldManager;
import net.minecraft.server.v1_4_5.MinecraftServer;

import org.bukkit.craftbukkit.v1_4_5.CraftServer;

public class PlayerDataManager extends IPlayerDataManager {
public class PlayerDataManager extends com.lishid.openinv.internal.PlayerDataManager {

@Override
public Player loadOfflinePlayer(OfflinePlayer offline) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.ISpecialEnderChest;

// Volatile
Expand All @@ -39,15 +40,18 @@
import org.bukkit.craftbukkit.v1_4_5.inventory.CraftInventory;

public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {

private final OpenInv plugin;
private final InventoryEnderChest enderChest;
private final CraftInventory inventory = new CraftInventory(this);
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);

public SpecialEnderChest(Player p, Boolean online) {
public SpecialEnderChest(OpenInv plugin, Player p, Boolean online) {
super(((CraftPlayer) p).getHandle().getEnderChest().getName(), ((CraftPlayer) p).getHandle().getEnderChest().getSize());
this.plugin = plugin;
CraftPlayer player = (CraftPlayer) p;
this.enderChest = player.getHandle().getEnderChest();
this.owner = player;
Expand All @@ -62,7 +66,7 @@ public Inventory getBukkitInventory() {
@Override
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
if (offline && save && !plugin.disableSaving()) {
owner.saveData();
}
return offline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.ISpecialPlayerInventory;

// Volatile
Expand All @@ -31,13 +32,16 @@
import org.bukkit.craftbukkit.v1_4_5.inventory.CraftInventory;

public class SpecialPlayerInventory extends PlayerInventory implements ISpecialPlayerInventory {
CraftPlayer owner;
public boolean playerOnline = false;

private final OpenInv plugin;
private final ItemStack[] extra = new ItemStack[5];
private final CraftInventory inventory = new CraftInventory(this);
private CraftPlayer owner;
private boolean playerOnline = false;

public SpecialPlayerInventory(Player p, Boolean online) {
public SpecialPlayerInventory(OpenInv plugin, Player p, Boolean online) {
super(((CraftPlayer) p).getHandle());
this.plugin = plugin;
this.owner = ((CraftPlayer) p);
this.playerOnline = online;
this.items = player.inventory.items;
Expand All @@ -52,7 +56,7 @@ public Inventory getBukkitInventory() {
@Override
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
if (offline && save && !plugin.disableSaving()) {
owner.saveData();
}
return offline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import com.lishid.openinv.internal.IPlayerDataManager;

// Volatile
import net.minecraft.server.v1_4_6.EntityPlayer;
import net.minecraft.server.v1_4_6.MinecraftServer;
import net.minecraft.server.v1_4_6.PlayerInteractManager;

import org.bukkit.craftbukkit.v1_4_6.CraftServer;

public class PlayerDataManager extends IPlayerDataManager {
public class PlayerDataManager extends com.lishid.openinv.internal.PlayerDataManager {

@Override
public Player loadOfflinePlayer(OfflinePlayer offline) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.ISpecialEnderChest;

// Volatile
Expand All @@ -39,15 +40,18 @@
import org.bukkit.craftbukkit.v1_4_6.inventory.CraftInventory;

public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest {

private final OpenInv plugin;
private final InventoryEnderChest enderChest;
private final CraftInventory inventory = new CraftInventory(this);
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
public boolean playerOnline = false;
private CraftPlayer owner;
private final InventoryEnderChest enderChest;
private int maxStack = MAX_STACK;
private final CraftInventory inventory = new CraftInventory(this);

public SpecialEnderChest(Player p, Boolean online) {
public SpecialEnderChest(OpenInv plugin, Player p, Boolean online) {
super(((CraftPlayer) p).getHandle().getEnderChest().getName(), ((CraftPlayer) p).getHandle().getEnderChest().getSize());
this.plugin = plugin;
CraftPlayer player = (CraftPlayer) p;
this.enderChest = player.getHandle().getEnderChest();
this.owner = player;
Expand All @@ -62,7 +66,7 @@ public Inventory getBukkitInventory() {
@Override
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
if (offline && save && !plugin.disableSaving()) {
owner.saveData();
}
return offline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.ISpecialPlayerInventory;

// Volatile
Expand All @@ -31,13 +32,16 @@
import org.bukkit.craftbukkit.v1_4_6.inventory.CraftInventory;

public class SpecialPlayerInventory extends PlayerInventory implements ISpecialPlayerInventory {
CraftPlayer owner;
public boolean playerOnline = false;

private final OpenInv plugin;
private final ItemStack[] extra = new ItemStack[5];
private final CraftInventory inventory = new CraftInventory(this);
private CraftPlayer owner;
private boolean playerOnline = false;

public SpecialPlayerInventory(Player p, Boolean online) {
public SpecialPlayerInventory(OpenInv plugin, Player p, Boolean online) {
super(((CraftPlayer) p).getHandle());
this.plugin = plugin;
this.owner = ((CraftPlayer) p);
this.playerOnline = online;
this.items = player.inventory.items;
Expand All @@ -52,7 +56,7 @@ public Inventory getBukkitInventory() {
@Override
public boolean inventoryRemovalCheck(boolean save) {
boolean offline = transaction.isEmpty() && !playerOnline;
if (offline && save) {
if (offline && save && !plugin.disableSaving()) {
owner.saveData();
}
return offline;
Expand Down
Loading

0 comments on commit a929eee

Please sign in to comment.