mirror of
https://github.com/TotalFreedomMC/OpenInv.git
synced 2024-12-22 16:05:03 +00:00
Reduce duplicate code in core plugin
This commit is contained in:
parent
0f266c8a64
commit
57097e626e
3 changed files with 29 additions and 164 deletions
|
@ -19,7 +19,6 @@ package com.lishid.openinv;
|
|||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.lishid.openinv.commands.AnyChestPluginCommand;
|
||||
import com.lishid.openinv.commands.OpenEnderPluginCommand;
|
||||
import com.lishid.openinv.commands.OpenInvPluginCommand;
|
||||
import com.lishid.openinv.commands.SearchEnchantPluginCommand;
|
||||
import com.lishid.openinv.commands.SearchInvPluginCommand;
|
||||
|
@ -53,6 +52,8 @@ import java.util.concurrent.Future;
|
|||
import javax.annotation.Nullable;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -489,6 +490,10 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
|
|||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
// Save default configuration if not present.
|
||||
this.saveDefaultConfig();
|
||||
|
||||
// Get plugin manager
|
||||
PluginManager pm = this.getServer().getPluginManager();
|
||||
|
||||
|
@ -502,7 +507,7 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
|
|||
return;
|
||||
}
|
||||
|
||||
this.saveDefaultConfig();
|
||||
// Update existing configuration. May require internal access.
|
||||
new ConfigUpdater(this).checkForUpdates();
|
||||
|
||||
// Register listeners
|
||||
|
@ -514,8 +519,9 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
|
|||
pm.registerEvents(new InventoryDragListener(this), this);
|
||||
|
||||
// Register commands to their executors
|
||||
this.getCommand("openinv").setExecutor(new OpenInvPluginCommand(this));
|
||||
this.getCommand("openender").setExecutor(new OpenEnderPluginCommand(this));
|
||||
OpenInvPluginCommand openInv = new OpenInvPluginCommand(this);
|
||||
this.getCommand("openinv").setExecutor(openInv);
|
||||
this.getCommand("openender").setExecutor(openInv);
|
||||
SearchInvPluginCommand searchInv = new SearchInvPluginCommand(this);
|
||||
this.getCommand("searchinv").setExecutor(searchInv);
|
||||
this.getCommand("searchender").setExecutor(searchInv);
|
||||
|
|
|
@ -1,148 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2011-2018 lishid. All rights reserved.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.internal.ISpecialEnderChest;
|
||||
import com.lishid.openinv.util.Permissions;
|
||||
import java.util.HashMap;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class OpenEnderPluginCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
private final HashMap<Player, String> openEnderHistory = new HashMap<Player, String>();
|
||||
|
||||
public OpenEnderPluginCommand(final OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "You can't use this from the console.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
|
||||
this.plugin.showHelp((Player) sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
final Player player = (Player) sender;
|
||||
|
||||
// History management
|
||||
String history = this.openEnderHistory.get(player);
|
||||
|
||||
if (history == null || history.isEmpty()) {
|
||||
history = player.getName();
|
||||
this.openEnderHistory.put(player, history);
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
||||
// Read from history if target is not named
|
||||
if (args.length < 1) {
|
||||
name = history;
|
||||
} else {
|
||||
name = args[0];
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final OfflinePlayer offlinePlayer = OpenEnderPluginCommand.this.plugin.matchPlayer(name);
|
||||
|
||||
if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!player.isOnline()) {
|
||||
return;
|
||||
}
|
||||
OpenEnderPluginCommand.this.openInventory(player, offlinePlayer);
|
||||
}
|
||||
}.runTask(OpenEnderPluginCommand.this.plugin);
|
||||
|
||||
}
|
||||
}.runTaskAsynchronously(this.plugin);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void openInventory(final Player player, final OfflinePlayer target) {
|
||||
|
||||
Player onlineTarget;
|
||||
boolean online = target.isOnline();
|
||||
|
||||
if (!online) {
|
||||
// Try loading the player's data
|
||||
onlineTarget = this.plugin.loadPlayer(target);
|
||||
|
||||
if (onlineTarget == null) {
|
||||
player.sendMessage(ChatColor.RED + "Player not found!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
onlineTarget = target.getPlayer();
|
||||
}
|
||||
|
||||
if (!onlineTarget.equals(player)) {
|
||||
if (!Permissions.ENDERCHEST_ALL.hasPermission(player)) {
|
||||
player.sendMessage(ChatColor.RED + "You do not have permission to access other players' enderchests.");
|
||||
return;
|
||||
}
|
||||
if (!Permissions.CROSSWORLD.hasPermission(player)
|
||||
&& !player.getWorld().equals(onlineTarget.getWorld())) {
|
||||
player.sendMessage(ChatColor.RED + onlineTarget.getDisplayName() + " is not in your world!");
|
||||
return;
|
||||
}
|
||||
if (!Permissions.OVERRIDE.hasPermission(player)
|
||||
&& Permissions.EXEMPT.hasPermission(onlineTarget)) {
|
||||
player.sendMessage(ChatColor.RED + onlineTarget.getDisplayName() + "'s inventory is protected!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Record the target
|
||||
this.openEnderHistory.put(player, this.plugin.getPlayerID(target));
|
||||
|
||||
// Create the inventory
|
||||
final ISpecialEnderChest chest;
|
||||
try {
|
||||
chest = this.plugin.getSpecialEnderChest(onlineTarget, online);
|
||||
} catch (Exception e) {
|
||||
player.sendMessage(ChatColor.RED + "An error occurred creating " + onlineTarget.getDisplayName() + "'s inventory!");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
// Open the inventory
|
||||
plugin.openInventory(player, chest);
|
||||
}
|
||||
|
||||
}
|
|
@ -17,7 +17,7 @@
|
|||
package com.lishid.openinv.commands;
|
||||
|
||||
import com.lishid.openinv.OpenInv;
|
||||
import com.lishid.openinv.internal.ISpecialPlayerInventory;
|
||||
import com.lishid.openinv.internal.ISpecialInventory;
|
||||
import com.lishid.openinv.util.Permissions;
|
||||
import java.util.HashMap;
|
||||
import org.bukkit.ChatColor;
|
||||
|
@ -32,6 +32,7 @@ public class OpenInvPluginCommand implements CommandExecutor {
|
|||
|
||||
private final OpenInv plugin;
|
||||
private final HashMap<Player, String> openInvHistory = new HashMap<Player, String>();
|
||||
private final HashMap<Player, String> openEnderHistory = new HashMap<Player, String>();
|
||||
|
||||
public OpenInvPluginCommand(final OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
|
@ -50,13 +51,14 @@ public class OpenInvPluginCommand implements CommandExecutor {
|
|||
}
|
||||
|
||||
final Player player = (Player) sender;
|
||||
final boolean openinv = command.getName().equals("openinv");
|
||||
|
||||
// History management
|
||||
String history = this.openInvHistory.get(player);
|
||||
String history = (openinv ? this.openInvHistory : this.openEnderHistory).get(player);
|
||||
|
||||
if (history == null || history.isEmpty()) {
|
||||
history = player.getName();
|
||||
this.openInvHistory.put(player, history);
|
||||
(openinv ? this.openInvHistory : this.openEnderHistory).put(player, history);
|
||||
}
|
||||
|
||||
final String name;
|
||||
|
@ -84,7 +86,7 @@ public class OpenInvPluginCommand implements CommandExecutor {
|
|||
if (!player.isOnline()) {
|
||||
return;
|
||||
}
|
||||
OpenInvPluginCommand.this.openInventory(player, offlinePlayer);
|
||||
OpenInvPluginCommand.this.openInventory(player, offlinePlayer, openinv);
|
||||
}
|
||||
}.runTask(OpenInvPluginCommand.this.plugin);
|
||||
|
||||
|
@ -94,7 +96,7 @@ public class OpenInvPluginCommand implements CommandExecutor {
|
|||
return true;
|
||||
}
|
||||
|
||||
private void openInventory(final Player player, final OfflinePlayer target) {
|
||||
private void openInventory(final Player player, final OfflinePlayer target, boolean openinv) {
|
||||
|
||||
|
||||
Player onlineTarget;
|
||||
|
@ -114,12 +116,18 @@ public class OpenInvPluginCommand implements CommandExecutor {
|
|||
|
||||
// Permissions checks
|
||||
if (onlineTarget.equals(player)) {
|
||||
// Self-open check
|
||||
if (!Permissions.OPENSELF.hasPermission(player)) {
|
||||
player.sendMessage(ChatColor.RED + "You're not allowed to openinv yourself.");
|
||||
// Inventory: Additional permission required to open own inventory
|
||||
if (openinv && !Permissions.OPENSELF.hasPermission(player)) {
|
||||
player.sendMessage(ChatColor.RED + "You're not allowed to open your own inventory!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Enderchest: Additional permission required to open others' ender chests
|
||||
if (!openinv && !Permissions.ENDERCHEST_ALL.hasPermission(player)) {
|
||||
player.sendMessage(ChatColor.RED + "You do not have permission to access other players' ender chests.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Protected check
|
||||
if (!Permissions.OVERRIDE.hasPermission(player)
|
||||
&& Permissions.EXEMPT.hasPermission(onlineTarget)) {
|
||||
|
@ -129,7 +137,6 @@ public class OpenInvPluginCommand implements CommandExecutor {
|
|||
|
||||
// Crossworld check
|
||||
if (!Permissions.CROSSWORLD.hasPermission(player)
|
||||
&& !Permissions.OVERRIDE.hasPermission(player)
|
||||
&& !onlineTarget.getWorld().equals(player.getWorld())) {
|
||||
player.sendMessage(
|
||||
ChatColor.RED + onlineTarget.getDisplayName() + " is not in your world!");
|
||||
|
@ -138,12 +145,12 @@ public class OpenInvPluginCommand implements CommandExecutor {
|
|||
}
|
||||
|
||||
// Record the target
|
||||
this.openInvHistory.put(player, this.plugin.getPlayerID(target));
|
||||
(openinv ? this.openInvHistory : this.openEnderHistory).put(player, this.plugin.getPlayerID(target));
|
||||
|
||||
// Create the inventory
|
||||
final ISpecialPlayerInventory inv;
|
||||
final ISpecialInventory inv;
|
||||
try {
|
||||
inv = this.plugin.getSpecialInventory(onlineTarget, online);
|
||||
inv = openinv ? this.plugin.getSpecialInventory(onlineTarget, online) : this.plugin.getSpecialEnderChest(onlineTarget, online);
|
||||
} catch (Exception e) {
|
||||
player.sendMessage(ChatColor.RED + "An error occurred creating " + onlineTarget.getDisplayName() + "'s inventory!");
|
||||
e.printStackTrace();
|
||||
|
|
Loading…
Reference in a new issue