1
0
mirror of https://e.coding.net/circlecloud/ProtectItem.git synced 2024-12-27 20:48:51 +00:00

new Version Item Protect...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-10-20 20:58:13 +08:00
parent 12b6321bab
commit a33344dab2
15 changed files with 288 additions and 130 deletions

21
pom.xml
View File

@ -1,9 +1,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>cn.CityCraft</groupId> <groupId>cn.citycraft</groupId>
<artifactId>ProtectItem</artifactId> <artifactId>ProtectItem</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>1.0</version>
<name>ProtectItem</name> <name>ProtectItem</name>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>
@ -32,14 +32,9 @@
<artifactSet> <artifactSet>
<includes> <includes>
<include>cn.citycraft:PluginHelper</include> <include>cn.citycraft:PluginHelper</include>
<include>org.mcstats.*:*</include>
</includes> </includes>
</artifactSet> </artifactSet>
<relocations> <relocations>
<relocation>
<pattern>org.mcstats</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}.mcstats</shadedPattern>
</relocation>
<relocation> <relocation>
<pattern>cn.citycraft.PluginHelper</pattern> <pattern>cn.citycraft.PluginHelper</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern> <shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
@ -66,17 +61,13 @@
<id>citycraft-repo</id> <id>citycraft-repo</id>
<url>http://ci.citycraft.cn:8800/jenkins/plugin/repository/everything/</url> <url>http://ci.citycraft.cn:8800/jenkins/plugin/repository/everything/</url>
</repository> </repository>
<repository>
<id>Plugin Metrics</id>
<url>http://repo.mcstats.org/content/repositories/public</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<type>jar</type> <type>jar</type>
<version>1.8.3-R0.1-SNAPSHOT</version> <version>1.8.8-R0.1-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.citycraft</groupId> <groupId>cn.citycraft</groupId>
@ -84,12 +75,6 @@
<type>jar</type> <type>jar</type>
<version>1.0</version> <version>1.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.mcstats.bukkit</groupId>
<artifactId>metrics</artifactId>
<version>R8-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -1,98 +1,45 @@
package cn.citycraft.ProtectItem; package cn.citycraft.ProtectItem;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.ProtectItem.commands.ProtectItemCommand;
import cn.citycraft.ProtectItem.listen.ClickItemListen;
import cn.citycraft.ProtectItem.listen.DropItemListen; import cn.citycraft.ProtectItem.listen.DropItemListen;
import cn.citycraft.ProtectItem.listen.InteractItemListen;
import cn.citycraft.ProtectItem.listen.ItemHeldListen;
import cn.citycraft.ProtectItem.listen.PickupItemListen; import cn.citycraft.ProtectItem.listen.PickupItemListen;
import cn.citycraft.ProtectItem.utils.ActionType;
import cn.citycraft.ProtectItem.utils.ItemManager;
public class ProtectItem extends JavaPlugin { public class ProtectItem extends JavaPlugin {
public String servername; public ItemManager itemManager;
public String pluginname;
public static String getItemName(final ItemStack i) { public boolean actionCheck(final Player p, final ItemStack i, final ActionType action) {
final String name = i.getType().name(); if (itemManager.canAction(i, action)) {
final int dur = i.getDurability(); return true;
final String dura = i.getMaxStackSize() != 1 ? dur != 0 ? "_" + dur : "" : ""; } else if (itemManager.hasActionPerm(p, i, action)) {
final String iname = name + dura;
return iname;
}
public boolean checkevent(final Player p, final ItemStack i, final String type) {
if (p.hasPermission("pi.bypass")) {
return false;
}
final String iname = getItemName(i);
final Location l = p.getLocation();
final World pw = l.getWorld();
for (final String protectworld : getConfig().getStringList("ProtectWorld")) {
if (pw.getName().equalsIgnoreCase(protectworld)) {
for (final String protectitem : getConfig().getStringList("ProtectItem")) {
if (iname.equalsIgnoreCase(protectitem)) {
p.sendMessage(pluginname + getmessage("Message." + type));
return true;
}
}
}
}
return false;
}
public String getmessage(final String path) {
final String message = getConfig().getString(path).replaceAll("&", "§");
return message;
}
@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String string, final String[] args) {
final Player p = (Player) sender;
switch (args.length) {
case 0:
final ItemStack item = p.getItemInHand();
p.sendMessage("\u00a7a手上物品类型名称为: \u00a76" + getItemName(item));
return true; return true;
case 1:
if (p.hasPermission("pi.bypass")) {
if (args[0].equalsIgnoreCase("reload")) {
this.reloadConfig();
p.sendMessage("\u00a7a配置文件已重载...");
return true;
}
}
break;
default:
} }
return false; return false;
} }
public ItemManager getItemManager() {
return itemManager;
}
@Override @Override
public void onEnable() { public void onEnable() {
if (getConfig().getBoolean("Protect.Drop", true)) { itemManager = new ItemManager(this);
Bukkit.getPluginManager().registerEvents(new DropItemListen(this), this); final PluginManager pm = this.getServer().getPluginManager();
} pm.registerEvents(new ClickItemListen(this), this);
if (getConfig().getBoolean("Protect.Pickup", true)) { pm.registerEvents(new DropItemListen(this), this);
Bukkit.getPluginManager().registerEvents(new PickupItemListen(this), this); pm.registerEvents(new InteractItemListen(this), this);
} pm.registerEvents(new ItemHeldListen(this), this);
// if (getConfig().getBoolean("Protect.Break", true)) pm.registerEvents(new PickupItemListen(this), this);
// Bukkit.getPluginManager().registerEvents( this.getCommand("pi").setExecutor(new ProtectItemCommand(this));
// new BreakBlockListen(this), this);
// if (getConfig().getBoolean("Protect.Place", true))
// Bukkit.getPluginManager().registerEvents(
// new PlaceBlockListen(this), this);
getLogger().info("ProtectItem已加载!");
} }
@Override
public void onLoad() {
saveDefaultConfig();
reloadConfig();
servername = getmessage("servername");
pluginname = getmessage("pluginname") + " ";
}
} }

View File

@ -0,0 +1,30 @@
package cn.citycraft.ProtectItem.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.ProtectItem.ProtectItem;
public class CommandBan extends BaseCommand {
ProtectItem plugin;
public CommandBan(final ProtectItem main) {
super("ban");
this.plugin = main;
setOnlyPlayerExecutable();
setDescription("§c封禁手持物品");
setPossibleArguments("<封禁类型>");
}
@Override
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final Player p = (Player) sender;
final ItemStack item = p.getItemInHand();
plugin.getItemManager().addbanItem(item);
}
}

View File

@ -0,0 +1,26 @@
package cn.citycraft.ProtectItem.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.ProtectItem.ProtectItem;
public class CommandUnban extends BaseCommand {
ProtectItem plugin;
public CommandUnban(final ProtectItem main) {
super("unban");
this.plugin = main;
setOnlyPlayerExecutable();
setDescription("§a解禁手持物品");
setPossibleArguments("<解禁类型>");
}
@Override
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
}
}

View File

@ -0,0 +1,35 @@
package cn.citycraft.ProtectItem.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import cn.citycraft.PluginHelper.commands.DefaultCommand;
import cn.citycraft.PluginHelper.commands.HandlerSubCommand;
import cn.citycraft.ProtectItem.ProtectItem;
public class ProtectItemCommand implements CommandExecutor, DefaultCommand {
HandlerSubCommand hsc;
ProtectItem plugin;
public ProtectItemCommand(final ProtectItem main) {
plugin = main;
hsc = new HandlerSubCommand(main);
hsc.setDefaultCommand(this);
hsc.registerCommand(new CommandBan(main));
hsc.registerCommand(new CommandUnban(main));
}
@Override
public void defaultExecute(final CommandSender sender, final Command command, final String label) throws CommandException {
hsc.sendHelp(sender, label);
}
@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
return hsc.onCommand(sender, cmd, label, args);
}
}

View File

@ -0,0 +1,28 @@
package cn.citycraft.ProtectItem.listen;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import cn.citycraft.ProtectItem.ProtectItem;
import cn.citycraft.ProtectItem.utils.ActionType;
public class ClickItemListen implements Listener {
ProtectItem plugin;
public ClickItemListen(final ProtectItem main) {
plugin = main;
}
@EventHandler(ignoreCancelled = true)
public void onClickItem(final InventoryClickEvent e) {
final Player p = (Player) e.getWhoClicked();
final ItemStack i = e.getCurrentItem();
if (i != null && i.getType() != Material.AIR) {
e.setCancelled(plugin.actionCheck(p, i, ActionType.Click));
}
}
}

View File

@ -7,6 +7,7 @@ import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import cn.citycraft.ProtectItem.ProtectItem; import cn.citycraft.ProtectItem.ProtectItem;
import cn.citycraft.ProtectItem.utils.ActionType;
public class DropItemListen implements Listener { public class DropItemListen implements Listener {
ProtectItem plugin; ProtectItem plugin;
@ -19,6 +20,6 @@ public class DropItemListen implements Listener {
public void onDropItem(final PlayerDropItemEvent e) { public void onDropItem(final PlayerDropItemEvent e) {
final Player p = e.getPlayer(); final Player p = e.getPlayer();
final ItemStack i = e.getItemDrop().getItemStack(); final ItemStack i = e.getItemDrop().getItemStack();
e.setCancelled(plugin.checkevent(p, i, "Drop")); e.setCancelled(plugin.actionCheck(p, i, ActionType.Drop));
} }
} }

View File

@ -0,0 +1,25 @@
package cn.citycraft.ProtectItem.listen;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import cn.citycraft.ProtectItem.ProtectItem;
import cn.citycraft.ProtectItem.utils.ActionType;
public class InteractItemListen implements Listener {
ProtectItem plugin;
public InteractItemListen(final ProtectItem main) {
plugin = main;
}
@EventHandler(ignoreCancelled = true)
public void onPickupItem(final PlayerInteractEvent e) {
final Player p = e.getPlayer();
final ItemStack i = p.getItemInHand();
e.setCancelled(plugin.actionCheck(p, i, ActionType.Interact));
}
}

View File

@ -0,0 +1,28 @@
package cn.citycraft.ProtectItem.listen;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import cn.citycraft.ProtectItem.ProtectItem;
import cn.citycraft.ProtectItem.utils.ActionType;
public class ItemHeldListen implements Listener {
ProtectItem plugin;
public ItemHeldListen(final ProtectItem main) {
plugin = main;
}
@EventHandler(ignoreCancelled = true)
public void onItemHeld(final PlayerItemHeldEvent e) {
final Player p = e.getPlayer();
final Inventory inv = p.getInventory();
final int slot = e.getNewSlot();
final ItemStack i = inv.getItem(slot);
e.setCancelled(plugin.actionCheck(p, i, ActionType.Held));
}
}

View File

@ -7,6 +7,7 @@ import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import cn.citycraft.ProtectItem.ProtectItem; import cn.citycraft.ProtectItem.ProtectItem;
import cn.citycraft.ProtectItem.utils.ActionType;
public class PickupItemListen implements Listener { public class PickupItemListen implements Listener {
ProtectItem plugin; ProtectItem plugin;
@ -19,6 +20,6 @@ public class PickupItemListen implements Listener {
public void onPickupItem(final PlayerPickupItemEvent e) { public void onPickupItem(final PlayerPickupItemEvent e) {
final Player p = e.getPlayer(); final Player p = e.getPlayer();
final ItemStack i = e.getItem().getItemStack(); final ItemStack i = e.getItem().getItemStack();
e.setCancelled(plugin.checkevent(p, i, "Pickup")); e.setCancelled(plugin.actionCheck(p, i, ActionType.Pickup));
} }
} }

View File

@ -0,0 +1,9 @@
package cn.citycraft.ProtectItem.utils;
public enum ActionType {
Click(),
Drop(),
Held(),
Interact(),
Pickup()
}

View File

@ -0,0 +1,62 @@
package cn.citycraft.ProtectItem.utils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.ProtectItem.ProtectItem;
public class ItemManager {
FileConfig itemconfig;
HashMap<String, List<ActionType>> itemlist;
ProtectItem plugin;
@SuppressWarnings("unchecked")
public ItemManager(final ProtectItem main) {
plugin = main;
itemconfig = new FileConfig(plugin, "banitems");
itemlist = (HashMap<String, List<ActionType>>) itemconfig.get("banitems");
}
public void addbanItem(final ItemStack item) {
addbanItem(item, null);
}
public void addbanItem(final ItemStack item, final ActionType[] action) {
final String itemname = getItemName(item);
if (itemlist.containsKey(itemname)) {
final List<ActionType> actlist = itemlist.get(itemname);
for (final ActionType actionType : actlist) {
if (!actlist.contains(action)) {
actlist.add(actionType);
}
}
} else {
itemlist.put(itemname, Arrays.asList(action));
}
itemconfig.set("banitems", itemlist);
}
public boolean canAction(final ItemStack i, final ActionType action) {
final String itemname = getItemName(i);
final List<ActionType> actlist = itemlist.get(itemname);
final Boolean result = actlist.contains(action);
return result;
}
public String getItemName(final ItemStack i) {
final int dur = i.getDurability();
final String dura = i.getMaxStackSize() != 1 ? dur != 0 ? "_" + dur : "" : "";
return (i.getType().name() + dura).toLowerCase();
}
public boolean hasActionPerm(final Player p, final ItemStack i, final ActionType action) {
final String itemname = getItemName(i);
final String perm = String.format("%s.%s", itemname, action.toString().toLowerCase());
return p.hasPermission(itemname + ".*") ? true : p.hasPermission(perm);
}
}

View File

View File

@ -1,34 +1,5 @@
#本文件为保护插件的主配置文件 #本文件为保护插件的主配置文件
#服务器名称 #服务器名称
servername: '' servername: ''
#插件名称 #插件名称
pluginname: '&6[&3物品保护&6]&r' pluginname: '&6[&3物品保护&6]&r'
#提示消息
Message:
Reload: '&a配置文件已重新载入'
#方块安全提示
Drop: '&c当前世界禁止丢弃该物品'
Pickup: '&c当前世界禁止拾取该物品'
# Place: '&c当前世界禁止放置该物品'
# Break: '&c当前世界禁止破坏该物品'
#受到保护的类型
Protect:
Drop: true
Pickup: false
##Place: false
##Break: false
#受到保护的世界(不区分大小写)
ProtectWorld:
- world
#保护的物品(不区分大小写)
ProtectItem:
- XXXX
#例如(请输入物品类型名称,/pishow查看物品名称)
# - DIRT
# - GRASS
# - CHEST
# - TRAPPED_CHEST

View File

@ -0,0 +1,10 @@
#提示消息
Message:
Reload: '&a配置文件已重新载入'
#方块安全提示
Drop: '&c当前世界禁止丢弃该物品'
Pickup: '&c当前世界禁止拾取该物品'
Place: '&c当前世界禁止放置该物品'
Break: '&c当前世界禁止破坏该物品'
Use: '&c当前世界禁止使用该物品!'
Held: '&c当前世界禁止使用该物品!'