1
1
mirror of https://github.com/geekfrog/PermissionsTime.git synced 2024-11-22 07:28:47 +00:00

新特性

可以定义物品的种类, 特殊的如头颅可以指定哪个玩家的头颅.(1.8+)
附魔发光的特效.
This commit is contained in:
GeekFrog 2017-07-19 18:40:41 +08:00
parent d0dd089d23
commit e166cd5456
11 changed files with 260 additions and 57 deletions

View File

@ -43,6 +43,9 @@
- **支持跨服** - **支持跨服**
- **gui显示自己的权限包** - **gui显示自己的权限包**
- **语言支持整理** - **语言支持整理**
- **支持自定义哪个玩家的头颅**
- **支持物品发光(附魔效果)**
- **玩家进入游戏时赋予权限更早**
- 手动删除过期的或无效数据 - 手动删除过期的或无效数据
- 取消前置插件 - 取消前置插件

View File

@ -12,8 +12,17 @@ packages:
# Permission package display name. # Permission package display name.
displayName: '&4Test Package' displayName: '&4Test Package'
# 显示的物品类型. # 显示的物品类型.
# 可以使用id, 但不可与type同时使用.
# 可以定义物品的种类, 特殊的如头颅可以指定哪个玩家的头颅.
# Show the type of item. # Show the type of item.
# You can use 'id', but you can't use 'type' at the same time.
# You can define the type of item, such as skull can set which player's head.
#id: 397
#type: SKULL_ITEM:5
#type: SKULL_ITEM:MHF_Present1
type: NETHER_STAR type: NETHER_STAR
# 是否有附魔发光的特效
glowing: false
# 标签. # 标签.
# Lores. # Lores.
lores: lores:

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>gg.frog.mc</groupId> <groupId>gg.frog.mc</groupId>
<artifactId>permissionstime</artifactId> <artifactId>permissionstime</artifactId>
<version>0.1.1-SNAPSHOT</version> <version>0.1.2-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>PermissionsTime</name> <name>PermissionsTime</name>
<description>支持跨服的权限限时插件</description> <description>支持跨服的权限限时插件</description>
@ -38,7 +38,7 @@
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId> <artifactId>bukkit</artifactId>
<version>1.12-pre6-SNAPSHOT</version> <version>1.8-R0.1-SNAPSHOT</version>
<type>jar</type> <type>jar</type>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>

View File

@ -19,6 +19,7 @@ import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean;
import gg.frog.mc.permissionstime.utils.StrUtil; import gg.frog.mc.permissionstime.utils.StrUtil;
import gg.frog.mc.permissionstime.utils.config.PluginConfig; import gg.frog.mc.permissionstime.utils.config.PluginConfig;
import gg.frog.mc.permissionstime.utils.nms.ItemUtil;
public class PackagesCfg extends PluginConfig { public class PackagesCfg extends PluginConfig {
@ -66,35 +67,42 @@ public class PackagesCfg extends PluginConfig {
if (ppb != null) { if (ppb != null) {
Material type = null; Material type = null;
int exid = 0; int exid = 0;
String skullOwner = null;
if (ppb.getType() != null) { if (ppb.getType() != null) {
String[] args = ppb.getType().split(":"); String[] args = ppb.getType().split(":");
try { type = Material.getMaterial(args[0].toUpperCase(Locale.ENGLISH));
switch (args.length) { if (args.length == 2) {
case 2: try {
exid = Integer.parseInt(args[1]); exid = Integer.parseInt(args[1]);
case 1: } catch (NumberFormatException e) {
type = Material.getMaterial(args[0].toUpperCase(Locale.ENGLISH)); if (Material.SKULL_ITEM.equals(type)) {
exid = 3;
skullOwner = args[1];
} else {
e.printStackTrace();
}
} }
} catch (NumberFormatException e) {
e.printStackTrace();
} }
} else if (ppb.getId() != null) { } else if (ppb.getId() != null) {
String[] args = ppb.getId().split(":"); String[] args = ppb.getId().split(":");
try { int id = Integer.parseInt(args[0]);
switch (args.length) { type = Material.getMaterial(id);
case 2: if (args.length == 2) {
exid = Integer.parseInt(args[1]); try {
case 1: exid = Integer.parseInt(args[1]);
int id = Integer.parseInt(args[0]); } catch (NumberFormatException e) {
type = Material.getMaterial(id); if (Material.SKULL_ITEM.equals(type)) {
exid = 3;
skullOwner = args[1];
} else {
e.printStackTrace();
}
} }
} catch (NumberFormatException e) {
e.printStackTrace();
} }
} }
if (type != null) { if (type != null) {
ItemStack items = new ItemStack(type, 1, (short) 0, (byte) exid); ItemStack item = new ItemStack(type, 1, (short) 0, (byte) exid);
ItemMeta meta = items.getItemMeta(); ItemMeta meta = item.getItemMeta();
meta.setDisplayName(StrUtil.messageFormat(ppb.getDisplayName() + "&r(" + name + ")")); meta.setDisplayName(StrUtil.messageFormat(ppb.getDisplayName() + "&r(" + name + ")"));
List<String> lores = new ArrayList<String>(); List<String> lores = new ArrayList<String>();
for (String lore : ppb.getLores()) { for (String lore : ppb.getLores()) {
@ -102,8 +110,14 @@ public class PackagesCfg extends PluginConfig {
} }
lores.add(""); lores.add("");
meta.setLore(lores); meta.setLore(lores);
items.setItemMeta(meta); item.setItemMeta(meta);
return items; if (ppb.getGlowing() && !meta.hasEnchants()) {
item = ItemUtil.addEnchantLight(item);
}
if (skullOwner != null) {
item = ItemUtil.addSkullOwner(item, skullOwner);
}
return item;
} }
} }
return null; return null;

View File

@ -8,7 +8,6 @@ import gg.frog.mc.permissionstime.config.PluginCfg;
import gg.frog.mc.permissionstime.database.impl.MySQLPlayerDataDao; import gg.frog.mc.permissionstime.database.impl.MySQLPlayerDataDao;
import gg.frog.mc.permissionstime.database.impl.SqlitePlayerDataDao; import gg.frog.mc.permissionstime.database.impl.SqlitePlayerDataDao;
import gg.frog.mc.permissionstime.model.db.PlayerDataBean; import gg.frog.mc.permissionstime.model.db.PlayerDataBean;
import gg.frog.mc.permissionstime.utils.StrUtil;
import lib.PatPeter.SQLibrary.Database; import lib.PatPeter.SQLibrary.Database;
import lib.PatPeter.SQLibrary.MySQL; import lib.PatPeter.SQLibrary.MySQL;
import lib.PatPeter.SQLibrary.SQLite; import lib.PatPeter.SQLibrary.SQLite;

View File

@ -3,10 +3,11 @@ package gg.frog.mc.permissionstime.listener;
import java.util.List; import java.util.List;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import gg.frog.mc.permissionstime.PluginMain; import gg.frog.mc.permissionstime.PluginMain;
@ -24,28 +25,17 @@ public class MainListener implements Listener {
this.pm = pm; this.pm = pm;
} }
/** @EventHandler(priority = EventPriority.LOWEST)
* 一个监听器例子 public void onJoin(PlayerLoginEvent event) {
* try {
* @param e List<PlayerDataBean> pdbList = pm.getSqlManager().getTime(event.getPlayer().getUniqueId().toString());
*/ PermissionPackageBean.reloadPlayerPermissions(event.getPlayer(), pdbList, pm, false);
@EventHandler } catch (Exception e) {
public void onJoin(PlayerJoinEvent event) { e.printStackTrace();
new Thread(new Runnable() { event.getPlayer().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.MSG_FAIL_SET_PERMISSION));
@Override }
public void run() {
try {
List<PlayerDataBean> pdbList = pm.getSqlManager().getTime(event.getPlayer().getUniqueId().toString());
PermissionPackageBean.reloadPlayerPermissions(event.getPlayer(), pdbList, pm);
} catch (Exception e) {
e.printStackTrace();
event.getPlayer().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.MSG_FAIL_SET_PERMISSION));
}
}
}).start();
} }
@EventHandler
public void onQuit(PlayerQuitEvent event) { public void onQuit(PlayerQuitEvent event) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
@ -59,7 +49,6 @@ public class MainListener implements Listener {
}).start(); }).start();
} }
@EventHandler
public void onKick(PlayerKickEvent event) { public void onKick(PlayerKickEvent event) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override

View File

@ -14,8 +14,11 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import gg.frog.mc.permissionstime.PluginMain; import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.config.LangCfg;
import gg.frog.mc.permissionstime.config.PackagesCfg; import gg.frog.mc.permissionstime.config.PackagesCfg;
import gg.frog.mc.permissionstime.config.PluginCfg;
import gg.frog.mc.permissionstime.model.db.PlayerDataBean; import gg.frog.mc.permissionstime.model.db.PlayerDataBean;
import gg.frog.mc.permissionstime.utils.StrUtil;
import gg.frog.mc.permissionstime.utils.config.IConfigBean; import gg.frog.mc.permissionstime.utils.config.IConfigBean;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
@ -30,8 +33,9 @@ public class PermissionPackageBean implements IConfigBean {
private String displayName = null; private String displayName = null;
private String id; private String id;
private String type; private String type;
private Boolean glowing;
private List<String> lores = new ArrayList<>(); private List<String> lores = new ArrayList<>();
private Boolean global = null; private Boolean global;
private List<String> permissions = new ArrayList<>(); private List<String> permissions = new ArrayList<>();
private List<String> groups = new ArrayList<>(); private List<String> groups = new ArrayList<>();
private static Map<String, BukkitTask> taskMap = new ConcurrentHashMap<>(); private static Map<String, BukkitTask> taskMap = new ConcurrentHashMap<>();
@ -60,6 +64,14 @@ public class PermissionPackageBean implements IConfigBean {
this.type = type; this.type = type;
} }
public Boolean getGlowing() {
return glowing;
}
public void setGlowing(Boolean glowing) {
this.glowing = glowing;
}
public List<String> getLores() { public List<String> getLores() {
return lores; return lores;
} }
@ -98,6 +110,7 @@ public class PermissionPackageBean implements IConfigBean {
config.set("displayName", displayName); config.set("displayName", displayName);
config.set("id", id); config.set("id", id);
config.set("type", type); config.set("type", type);
config.set("glowing", glowing);
config.set("lores", lores); config.set("lores", lores);
config.set("global", global); config.set("global", global);
config.set("permissions", permissions); config.set("permissions", permissions);
@ -116,6 +129,7 @@ public class PermissionPackageBean implements IConfigBean {
if (id == null && type == null) { if (id == null && type == null) {
type = "NETHER_STAR"; type = "NETHER_STAR";
} }
glowing = config.getBoolean("glowing");
lores = config.getStringList("lores"); lores = config.getStringList("lores");
global = config.getBoolean("global"); global = config.getBoolean("global");
permissions = config.getStringList("permissions"); permissions = config.getStringList("permissions");
@ -124,7 +138,7 @@ public class PermissionPackageBean implements IConfigBean {
@Override @Override
public String toString() { public String toString() {
return "PermissionPackageBean [displayName=" + displayName + ", id=" + id + ", type=" + type + ", lores=" + lores + ", global=" + global + ", permissions=" + permissions + ", groups=" + groups + "]"; return "PermissionPackageBean [displayName=" + displayName + ", id=" + id + ", type=" + type + ", glowing=" + glowing + ", lores=" + lores + ", global=" + global + ", permissions=" + permissions + ", groups=" + groups + "]";
} }
private void givePlayer(OfflinePlayer player, Server server, Permission permission) { private void givePlayer(OfflinePlayer player, Server server, Permission permission) {
@ -196,6 +210,10 @@ public class PermissionPackageBean implements IConfigBean {
} }
public static void reloadPlayerPermissions(OfflinePlayer player, List<PlayerDataBean> pdbList, PluginMain plugin) { public static void reloadPlayerPermissions(OfflinePlayer player, List<PlayerDataBean> pdbList, PluginMain plugin) {
reloadPlayerPermissions(player, pdbList, plugin, true);
}
public static void reloadPlayerPermissions(OfflinePlayer player, List<PlayerDataBean> pdbList, PluginMain plugin, boolean async) {
long delay = -1; long delay = -1;
long now = new Date().getTime(); long now = new Date().getTime();
PermissionPackageBean addPpb = new PermissionPackageBean(); PermissionPackageBean addPpb = new PermissionPackageBean();
@ -219,13 +237,23 @@ public class PermissionPackageBean implements IConfigBean {
subPpb.getGroups().removeAll(p.getGroups()); subPpb.getGroups().removeAll(p.getGroups());
} }
} }
plugin.getServer().getScheduler().runTask(plugin, new Runnable() { if (async) {
@Override plugin.getServer().getScheduler().runTask(plugin, new Runnable() {
public void run() { @Override
subPpb.clearPlayer(player, plugin.getServer(), plugin.getPermission()); public void run() {
addPpb.givePlayer(player, plugin.getServer(), plugin.getPermission()); try {
} subPpb.clearPlayer(player, plugin.getServer(), plugin.getPermission());
}); addPpb.givePlayer(player, plugin.getServer(), plugin.getPermission());
} catch (Exception e) {
e.printStackTrace();
player.getPlayer().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + LangCfg.MSG_FAIL_SET_PERMISSION));
}
}
});
} else {
subPpb.clearPlayer(player, plugin.getServer(), plugin.getPermission());
addPpb.givePlayer(player, plugin.getServer(), plugin.getPermission());
}
BukkitTask task = taskMap.get(player.getUniqueId().toString()); BukkitTask task = taskMap.get(player.getUniqueId().toString());
if (task != null) { if (task != null) {
plugin.getServer().getScheduler().cancelTask(task.getTaskId()); plugin.getServer().getScheduler().cancelTask(task.getTaskId());

View File

@ -18,6 +18,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import gg.frog.mc.permissionstime.PluginMain; import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.config.PluginCfg;
import gg.frog.mc.permissionstime.utils.StrUtil;
/** /**
* 配置操作 * 配置操作
@ -105,7 +107,7 @@ public abstract class PluginConfig {
try { try {
getConfig().save(configFile); getConfig().save(configFile);
} catch (IOException ex) { } catch (IOException ex) {
PluginMain.LOG.log(Level.SEVERE, "Could not save config to " + configFile, ex); PluginMain.LOG.log(Level.SEVERE, StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Could not save config to " + configFile), ex);
} }
} }
@ -114,7 +116,7 @@ public abstract class PluginConfig {
getConfig().save(configFile); getConfig().save(configFile);
reloadConfig(); reloadConfig();
} catch (IOException ex) { } catch (IOException ex) {
PluginMain.LOG.log(Level.SEVERE, "Could not save config to " + configFile, ex); PluginMain.LOG.log(Level.SEVERE, StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Could not save config to " + configFile), ex);
} }
} }

View File

@ -0,0 +1,124 @@
package gg.frog.mc.permissionstime.utils.nms;
import java.lang.reflect.Method;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import gg.frog.mc.permissionstime.PluginMain;
import gg.frog.mc.permissionstime.config.PluginCfg;
import gg.frog.mc.permissionstime.utils.StrUtil;
public class ItemUtil {
private static Class<?> nbtBaseClass;
private static Class<?> nbtTagCompoundClass;
private static Class<?> nbtTagStringClass;
private static Class<?> nbtTagIntClass;
// private static Class<?> nbtTagShortClass;
// private static Class<?> nbtTagListClass;
private static Class<?> nmsItemstackClass;
private static Method asNmsCopyMethod;
private static Method asCraftMirrorMethod;
private static Method hasTagMethod;
private static Method getTagMethod;
private static Method setTagMethod;
private static Method nbtSetMethod;
// private static Method nbtListAddSetMethod;
private static boolean setupOk;
static {
try {
nmsItemstackClass = NMSUtil.getNmsClass("ItemStack");// add
nbtBaseClass = NMSUtil.getNmsClass("NBTBase");// dy
nbtTagStringClass = NMSUtil.getNmsClass("NBTTagString");// dx
nbtTagIntClass = NMSUtil.getNmsClass("NBTTagInt");// dp
nbtTagCompoundClass = NMSUtil.getNmsClass("NBTTagCompound");// dh
// nbtTagShortClass = NMSUtil.getNmsClass("NBTTagShort");
// nbtTagListClass = NMSUtil.getNmsClass("NBTTagList");
asNmsCopyMethod = NMSUtil.getObcClass("inventory.CraftItemStack").getMethod("asNMSCopy", new Class[] { ItemStack.class });
asCraftMirrorMethod = NMSUtil.getObcClass("inventory.CraftItemStack").getMethod("asCraftMirror", new Class[] { nmsItemstackClass });
hasTagMethod = nmsItemstackClass.getMethod("hasTag", new Class[0]);// p
getTagMethod = nmsItemstackClass.getMethod("getTag", new Class[0]);// q
setTagMethod = nmsItemstackClass.getMethod("setTag", new Class[] { nbtTagCompoundClass });// d
nbtSetMethod = nbtTagCompoundClass.getMethod("set", new Class[] { String.class, nbtBaseClass });// a
// nbtListAddSetMethod = nbtTagListClass.getMethod("add", new Class[] { nbtBaseClass });
setupOk = true;
} catch (Exception e) {
e.printStackTrace();
// try {
// nmsItemstackClass = Class.forName("add");
// nbtBaseClass = Class.forName("dy");
// nbtTagStringClass = Class.forName("dx");
// nbtTagIntClass = Class.forName("dp");
// nbtTagCompoundClass = Class.forName("dh");
// asNmsCopyMethod = NMSUtil.getObcClass("inventory.CraftItemStack").getMethod("asNMSCopy", new Class[] { ItemStack.class });
// asCraftMirrorMethod = NMSUtil.getObcClass("inventory.CraftItemStack").getMethod("asCraftMirror", new Class[] { nmsItemstackClass });
// hasTagMethod = nmsItemstackClass.getMethod("func_77942_o", new Class[0]);
// getTagMethod = nmsItemstackClass.getMethod("func_77978_p", new Class[0]);
// setTagMethod = nmsItemstackClass.getMethod("func_77982_d", new Class[] { nbtTagCompoundClass });
// nbtSetMethod = nbtTagCompoundClass.getMethod("func_74782_a", new Class[] { String.class, nbtBaseClass });
// setupOk = true;
// } catch (Exception ex) {
// ex.printStackTrace();
// PluginMain.LOG.warning(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "ItemUtil setup fail. Some functions are unavailable."));
// }
PluginMain.LOG.warning(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "ItemUtil setup fail. Some functions are unavailable."));
}
}
public static ItemStack addEnchantLight(ItemStack item) {
item.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 0);
if (!NMSUtil.getServerVersion().startsWith("v1_7_") && setupOk) {
try {
Object nmsItemstack = asNmsCopyMethod.invoke(null, new Object[] { item });
if (nmsItemstack == null) {
return item;
}
Object nbtCompound = null;
if (((Boolean) hasTagMethod.invoke(nmsItemstack, new Object[0])).booleanValue()) {
nbtCompound = getTagMethod.invoke(nmsItemstack, new Object[0]);
} else {
nbtCompound = nbtTagCompoundClass.newInstance();
setTagMethod.invoke(nmsItemstack, new Object[] { nbtCompound });
}
if (nbtCompound == null) {
return item;
}
Object nbtHideFlags = nbtTagIntClass.getConstructor(int.class).newInstance(1);
nbtSetMethod.invoke(nbtCompound, new Object[] { "HideFlags", nbtHideFlags });
return (ItemStack) asCraftMirrorMethod.invoke(null, new Object[] { nmsItemstack });
} catch (Exception e) {
e.printStackTrace();
}
}
return item;
}
public static ItemStack addSkullOwner(ItemStack item, String name) {
if (setupOk) {
try {
Object nmsItemstack = asNmsCopyMethod.invoke(null, new Object[] { item });
if (nmsItemstack == null) {
return item;
}
Object nbtCompound = null;
if (((Boolean) hasTagMethod.invoke(nmsItemstack, new Object[0])).booleanValue()) {
nbtCompound = getTagMethod.invoke(nmsItemstack, new Object[0]);
} else {
nbtCompound = nbtTagCompoundClass.newInstance();
setTagMethod.invoke(nmsItemstack, new Object[] { nbtCompound });
}
if (nbtCompound == null) {
return item;
}
Object nbtString = nbtTagStringClass.getConstructor(String.class).newInstance(name);
nbtSetMethod.invoke(nbtCompound, new Object[] { "SkullOwner", nbtString });
return (ItemStack) asCraftMirrorMethod.invoke(null, new Object[] { nmsItemstack });
} catch (Exception e) {
e.printStackTrace();
}
}
return item;
}
}

View File

@ -0,0 +1,26 @@
package gg.frog.mc.permissionstime.utils.nms;
import org.bukkit.Bukkit;
public class NMSUtil {
private static String serverVersion;
static {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
serverVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
}
public static String getServerVersion() {
return serverVersion;
}
public static Class<?> getNmsClass(String name) throws ClassNotFoundException {
return Class.forName("net.minecraft.server." + getServerVersion() + "." + name);
}
public static Class<?> getObcClass(String name) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + getServerVersion() + "." + name);
}
}

View File

@ -12,8 +12,17 @@ packages:
# Permission package display name. # Permission package display name.
displayName: '&4Test Package' displayName: '&4Test Package'
# 显示的物品类型. # 显示的物品类型.
# 可以使用id, 但不可与type同时使用.
# 可以定义物品的种类, 特殊的如头颅可以指定哪个玩家的头颅.
# Show the type of item. # Show the type of item.
# You can use 'id', but you can't use 'type' at the same time.
# You can define the type of item, such as skull can set which player's head.
#id: 397
#type: SKULL_ITEM:5
#type: SKULL_ITEM:MHF_Present1
type: NETHER_STAR type: NETHER_STAR
# 是否有附魔发光的特效
glowing: false
# 标签. # 标签.
# Lores. # Lores.
lores: lores: