1
0
mirror of https://e.coding.net/circlecloud/CityBuild.git synced 2024-11-21 10:48:49 +00:00

使用PluginHelper和内部Residence...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-11-25 15:47:44 +08:00
parent 51400658c0
commit 6abc21083e
10 changed files with 110 additions and 394 deletions

Binary file not shown.

63
pom.xml
View File

@ -7,13 +7,10 @@
<name>CityBuild</name> <name>CityBuild</name>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>
<sourceDirectory>src</sourceDirectory>
<resources> <resources>
<resource> <resource>
<directory>src</directory> <directory>src/main/resources</directory>
<excludes> <filtering>true</filtering>
<exclude>**/*.java</exclude>
</excludes>
</resource> </resource>
</resources> </resources>
<plugins> <plugins>
@ -25,30 +22,70 @@
<target>1.7</target> <target>1.7</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>cn.citycraft:PluginHelper</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>cn.citycraft.PluginHelper</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
<properties>
<jenkins.url>http://ci.citycraft.cn:8080</jenkins.url>
<update.description></update.description>
<env.BUILD_NUMBER>1</env.BUILD_NUMBER>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url> <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository> </repository>
<repository>
<id>citycraft-repo</id>
<url>${jenkins.url}/plugin/repository/everything/</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>com.bekvon.bukkit.residence</groupId> <groupId>cn.citycraft</groupId>
<artifactId>PluginHelper</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>Residence</artifactId> <artifactId>Residence</artifactId>
<version>2.6.6.6</version> <type>jar</type>
<scope>system</scope> <version>2.8.1.3</version>
<systemPath>${project.basedir}/lib/Residence.jar</systemPath>
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project> </project>

View File

@ -1,55 +0,0 @@
package cn.citycraft.CityBuild.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.Plugin;
public class Config extends ConfigLoader {
private static String CONFIG_NAME = "config.yml";
private static FileConfig instance;
private static File file;
public Config(Plugin p) {
super(p, CONFIG_NAME);
file = new File(p.getDataFolder(), CONFIG_NAME);
instance = super.getInstance();
}
public Config(Plugin p, String ver) {
super(p, CONFIG_NAME, ver);
instance = super.getInstance();
}
public static void load(Plugin p) {
new Config(p);
}
public static void load(Plugin p, String ver) {
new Config(p, ver);
}
public static FileConfig getInstance() {
return instance;
}
public static String getMessage(String path) {
String message = instance.getString(path);
if (message != null)
message = message.replaceAll("&", "§");
return message;
}
public static String[] getStringArray(String path) {
return instance.getStringList(path).toArray(new String[0]);
}
public static void save(){
try {
instance.save(file);
} catch (IOException e) {
saveError(file);
e.printStackTrace();
}
}
}

View File

@ -1,102 +0,0 @@
package cn.citycraft.CityBuild.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.Plugin;
public class ConfigLoader extends FileConfig {
protected static FileConfig config;
protected static boolean tip = true;
protected static Plugin plugin;
public ConfigLoader(Plugin p, File file) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, null, true);
}
public ConfigLoader(Plugin p, File file, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, null, res);
}
public ConfigLoader(Plugin p, File file, String ver) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, ver, true);
}
public ConfigLoader(Plugin p, File file, String ver, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, file, ver, res);
}
public ConfigLoader(Plugin p, String filename) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), null,
true);
}
public ConfigLoader(Plugin p, String filename, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), null, res);
}
public ConfigLoader(Plugin p, String filename, String ver) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true);
}
public ConfigLoader(Plugin p, String filename, String ver, boolean res) {
ConfigLoader.plugin = p;
config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true);
}
public static FileConfig getInstance() {
return config;
}
public FileConfig loadConfig(Plugin p, File file, String ver, boolean res) {
tip = res ;
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
p.getLogger().info("创建新的文件夹" + file.getParentFile().getAbsolutePath() + "...");
}
if (!file.exists()) {
fileCreate(p, file, res);
} else {
if (ver != null) {
FileConfig configcheck = init(file);
String version = configcheck.getString("version");
if (version == null || !version.equals(ver)) {
p.saveResource(file.getName(), true);
p.getLogger().warning(
"配置文件: " + file.getName() + " 版本过低 正在升级...");
}
}
}
if (tip)
p.getLogger().info(
"载入配置文件: " + file.getName()
+ (ver != null ? " 版本: " + ver : ""));
return init(file);
}
private void fileCreate(Plugin p, File file, boolean res) {
if (res) {
p.saveResource(file.getName(), false);
} else {
try {
p.getLogger().info("创建新的配置文件" + file.getAbsolutePath() + "...");
file.createNewFile();
} catch (IOException e) {
p.getLogger().info("配置文件" + file.getName() + "创建失败...");
e.printStackTrace();
}
}
}
public static void saveError(File file) {
plugin.getLogger().info("配置文件" + file.getName() + "保存错误...");
}
}

View File

@ -1,43 +0,0 @@
package cn.citycraft.CityBuild.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.Plugin;
public class Data extends ConfigLoader {
private static String CONFIG_NAME = "data.yml";
private static FileConfig instance;
private static File file;
public static FileConfig getInstance() {
return instance;
}
public static String getMessage(String path) {
String message = instance.getString(path);
if (message != null) {
message = message.replaceAll("&", "§");
}
return message;
}
public static void load(Plugin p) {
new Data(p);
}
public static void save() {
try {
instance.save(file);
} catch (IOException e) {
saveError(file);
e.printStackTrace();
}
}
public Data(Plugin p) {
super(p, CONFIG_NAME, false);
file = new File(p.getDataFolder(), CONFIG_NAME);
instance = super.getInstance();
}
}

View File

@ -1,110 +0,0 @@
package cn.citycraft.CityBuild.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
/**
* An implementation of {@link Configuration} which saves all files in Yaml.
* Note that this implementation is not synchronized.
*/
public class FileConfig extends YamlConfiguration {
public static FileConfig init(File file) {
return FileConfig.loadConfiguration(file);
}
public static FileConfig loadConfiguration(File file) {
Validate.notNull(file, "File cannot be null");
FileConfig config = new FileConfig();
try {
config.load(file);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
} catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
}
return config;
}
protected final DumperOptions yamlOptions = new DumperOptions();
protected final Representer yamlRepresenter = new YamlRepresenter();
protected final Yaml yaml = new Yaml(new YamlConstructor(),
yamlRepresenter, yamlOptions);
@Override
public void load(File file) throws FileNotFoundException, IOException,
InvalidConfigurationException {
Validate.notNull(file, "File cannot be null");
final FileInputStream stream = new FileInputStream(file);
load(new InputStreamReader(stream, Charsets.UTF_8));
}
@Override
public void load(Reader reader) throws IOException, InvalidConfigurationException {
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
: new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
try {
String line;
while ((line = input.readLine()) != null) {
builder.append(line);
builder.append('\n');
}
} finally {
input.close();
}
loadFromString(builder.toString());
}
@Override
public void save(File file) throws IOException {
Validate.notNull(file, "File cannot be null");
Files.createParentDirs(file);
String data = saveToString();
Writer writer = new OutputStreamWriter(new FileOutputStream(file),
Charsets.UTF_8);
try {
writer.write(data);
} finally {
writer.close();
}
}
@Override
public String saveToString() {
yamlOptions.setIndent(options().indent());
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
String header = buildHeader();
String dump = yaml.dump(getValues(false));
if (dump.equals(BLANK_CONFIG)) {
dump = "";
}
return header + dump;
}
}

View File

@ -13,30 +13,31 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.CityBuild.config.Config;
import cn.citycraft.CityBuild.config.Data;
import cn.citycraft.CityBuild.listen.Build; import cn.citycraft.CityBuild.listen.Build;
import cn.citycraft.PluginHelper.config.FileConfig;
public class CityBuild extends JavaPlugin implements CommandExecutor { public class CityBuild extends JavaPlugin implements CommandExecutor {
public FileConfig config;
public FileConfig data;
public Map<Location, String> mhl; public Map<Location, String> mhl;
public String servername;
public String pluginname; public String pluginname;
public String servername;
public boolean tipplayer; public boolean tipplayer;
public String getfullmsg(String path) { public String getfullmsg(final String path) {
return servername + pluginname + " " + getmessage(path); return servername + pluginname + " " + getmessage(path);
} }
public String getmessage(String path) { public String getmessage(final String path) {
return Config.getMessage(path); return config.getMessage(path);
} }
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) { public boolean onCommand(final CommandSender sender, final Command cmd, final String string, final String[] args) {
if (args.length == 1) { if (args.length == 1) {
switch (args[0]) { switch (args[0]) {
case "reload": case "reload":
Data.save(); data.save();
onLoad(); onLoad();
sender.sendMessage(getfullmsg("Message.Reload")); sender.sendMessage(getfullmsg("Message.Reload"));
return true; return true;
@ -45,29 +46,27 @@ public class CityBuild extends JavaPlugin implements CommandExecutor {
if (mhl.isEmpty()) { if (mhl.isEmpty()) {
sender.sendMessage(getfullmsg("Message.NotHave")); sender.sendMessage(getfullmsg("Message.NotHave"));
} else { } else {
for (Entry<Location, String> info : mhl.entrySet()) { for (final Entry<Location, String> info : mhl.entrySet()) {
String name = info.getValue(); final String name = info.getValue();
Location loc = info.getKey(); final Location loc = info.getKey();
World w = loc.getWorld(); final World w = loc.getWorld();
int x = loc.getBlockX(); final int x = loc.getBlockX();
int z = loc.getBlockZ(); final int z = loc.getBlockZ();
sender.sendMessage(getfullmsg("Message.Info").replaceAll("%player%", name) sender.sendMessage(getfullmsg("Message.Info").replaceAll("%player%", name).replaceAll("%world%", w.getName()).replaceAll("%x%", x + "").replaceAll("%z%", z + ""));
.replaceAll("%world%", w.getName()).replaceAll("%x%", x + "")
.replaceAll("%z%", z + ""));
} }
} }
return true; return true;
case "clear": case "clear":
mhl.clear(); mhl.clear();
Data.getInstance().set("MaxHeightList", null); data.set("MaxHeightList", null);
Data.save(); data.save();
sender.sendMessage(getfullmsg("Message.NotHave")); sender.sendMessage(getfullmsg("Message.NotHave"));
return true; return true;
case "put": case "put":
Player p = (Player) sender; final Player p = (Player) sender;
mhl.put(p.getLocation(), p.getName()); mhl.put(p.getLocation(), p.getName());
Data.getInstance().set("MaxHeightList", mhl); data.set("MaxHeightList", mhl);
Data.save(); data.save();
} }
} }
return false; return false;
@ -75,8 +74,8 @@ public class CityBuild extends JavaPlugin implements CommandExecutor {
@Override @Override
public void onDisable() { public void onDisable() {
Data.getInstance().set("MaxHeightList", mhl); data.set("MaxHeightList", mhl);
Data.save(); data.save();
getLogger().info(pluginname + "城市世界建筑保护已卸载!"); getLogger().info(pluginname + "城市世界建筑保护已卸载!");
} }
@ -89,14 +88,14 @@ public class CityBuild extends JavaPlugin implements CommandExecutor {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void onLoad() { public void onLoad() {
Config.load(this, "1.1"); config = new FileConfig(this);
Data.load(this); data = new FileConfig("data.yml");
mhl = (Map<Location, String>) Config.getInstance().get("MaxHeightList"); mhl = (Map<Location, String>) config.get("MaxHeightList");
if (mhl == null) { if (mhl == null) {
mhl = new HashMap<>(); mhl = new HashMap<>();
} }
servername = getmessage("servername"); servername = getmessage("servername");
pluginname = getmessage("pluginname"); pluginname = getmessage("pluginname");
tipplayer = Config.getInstance().getBoolean("tipplayer"); tipplayer = config.getBoolean("tipplayer");
} }
} }

View File

@ -12,47 +12,45 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import cn.citycraft.CityBuild.CityBuild;
import cn.citycraft.CityBuild.config.Config;
import com.bekvon.bukkit.residence.Residence; import com.bekvon.bukkit.residence.Residence;
import com.bekvon.bukkit.residence.protection.ClaimedResidence; import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import cn.citycraft.CityBuild.CityBuild;
public class Build implements Listener { public class Build implements Listener {
CityBuild plugin; CityBuild plugin;
public Build(CityBuild main) { public Build(final CityBuild main) {
plugin = main; plugin = main;
} }
// PlayerInteractEvent.class // PlayerInteractEvent.class
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void blockBreak(BlockBreakEvent event) { public void blockBreak(final BlockBreakEvent event) {
event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), false)); event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), false));
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void blockplace(BlockPlaceEvent event) { public void blockplace(final BlockPlaceEvent event) {
event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), true)); event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), true));
} }
public boolean CancelEvent(Player p, Block b, boolean place) { public boolean CancelEvent(final Player p, final Block b, final boolean place) {
Location loc = b.getLocation(); final Location loc = b.getLocation();
ClaimedResidence res = Residence.getResidenceManager().getByLoc(loc); final ClaimedResidence res = Residence.getResidenceManager().getByLoc(loc);
if (res == null && !p.hasPermission("cb.build")) { if (res == null && !p.hasPermission("cb.build")) {
String blockname = b.getType().name(); final String blockname = b.getType().name();
World blockw = b.getWorld(); final World blockw = b.getWorld();
int blockx = b.getX(); final int blockx = b.getX();
int blocky = b.getY(); final int blocky = b.getY();
int blockz = b.getZ(); final int blockz = b.getZ();
for (String protectworld : Config.getInstance().getStringList("ProtectWorld")) { for (final String protectworld : plugin.config.getStringList("ProtectWorld")) {
if (protectworld.equalsIgnoreCase(blockw.getName())) { if (protectworld.equalsIgnoreCase(blockw.getName())) {
for (String allowblock : Config.getInstance().getStringList("AllowList")) { for (final String allowblock : plugin.config.getStringList("AllowList")) {
if (blockname.equalsIgnoreCase(allowblock)) { if (blockname.equalsIgnoreCase(allowblock)) {
for (String noticeblock : Config.getInstance().getStringList( for (final String noticeblock : plugin.config.getStringList("NoticeList")) {
"NoticeList")) {
if (blockname.equalsIgnoreCase(noticeblock) && place) { if (blockname.equalsIgnoreCase(noticeblock) && place) {
if (plugin.tipplayer) { if (plugin.tipplayer) {
p.getPlayer().sendMessage(plugin.getfullmsg("Message.Tip")); p.getPlayer().sendMessage(plugin.getfullmsg("Message.Tip"));
@ -60,32 +58,21 @@ public class Build implements Listener {
return false; return false;
} }
} }
int maxheight = Config.getInstance().getInt("MaxHeight"); final int maxheight = plugin.config.getInt("MaxHeight");
if (blocky > maxheight && blockname.equalsIgnoreCase("Dirt")) { if (blocky > maxheight && blockname.equalsIgnoreCase("Dirt")) {
if (plugin.tipplayer) { if (plugin.tipplayer) {
p.getPlayer().sendMessage( p.getPlayer().sendMessage(plugin.getfullmsg("Message.Max").replaceAll("%maxheight%", maxheight + ""));
plugin.getfullmsg("Message.Max").replaceAll(
"%maxheight%", maxheight + ""));
} }
Bukkit.broadcast( Bukkit.broadcast(plugin.getfullmsg("Message.Notice1").replaceAll("%player%", p.getDisplayName()).replaceAll("%maxheight%", maxheight + ""), "cb.notice");
plugin.getfullmsg("Message.Notice1") Bukkit.broadcast(plugin.getfullmsg("Message.Notice2").replaceAll("%world%", blockw.getName()).replaceAll("%x%", blockx + "").replaceAll("%z%", blockz + ""),
.replaceAll("%player%", p.getDisplayName())
.replaceAll("%maxheight%", maxheight + ""),
"cb.notice"); "cb.notice");
Bukkit.broadcast(
plugin.getfullmsg("Message.Notice2")
.replaceAll("%world%", blockw.getName())
.replaceAll("%x%", blockx + "")
.replaceAll("%z%", blockz + ""), "cb.notice");
plugin.mhl.put(loc, p.getName()); plugin.mhl.put(loc, p.getName());
return true; return true;
} }
return false; return false;
} }
} }
String[] message = { plugin.getfullmsg("Message.Wran"), final String[] message = { plugin.getfullmsg("Message.Wran"), plugin.getfullmsg("Message.Allow"), plugin.getfullmsg("Message.Relieve") };
plugin.getfullmsg("Message.Allow"),
plugin.getfullmsg("Message.Relieve") };
if (plugin.tipplayer) { if (plugin.tipplayer) {
p.getPlayer().sendMessage(message); p.getPlayer().sendMessage(message);
} }
@ -96,13 +83,13 @@ public class Build implements Listener {
return false; return false;
} }
public boolean CancelEvent(Player p, Material m) { public boolean CancelEvent(final Player p, final Material m) {
ClaimedResidence res = Residence.getResidenceManager().getByLoc(p.getLocation()); final ClaimedResidence res = Residence.getResidenceManager().getByLoc(p.getLocation());
if (res == null && !p.hasPermission("cb.build")) { if (res == null && !p.hasPermission("cb.build")) {
String blockname = m.name(); final String blockname = m.name();
for (String protectworld : Config.getInstance().getStringList("ProtectWorld")) { for (final String protectworld : plugin.config.getStringList("ProtectWorld")) {
if (protectworld.equalsIgnoreCase(p.getWorld().getName())) { if (protectworld.equalsIgnoreCase(p.getWorld().getName())) {
for (String notallowblock : Config.getInstance().getStringList("NotAllowList")) { for (final String notallowblock : plugin.config.getStringList("NotAllowList")) {
if (blockname.equalsIgnoreCase(notallowblock)) { if (blockname.equalsIgnoreCase(notallowblock)) {
if (plugin.tipplayer) { if (plugin.tipplayer) {
p.getPlayer().sendMessage(plugin.getfullmsg("Message.NotAllow")); p.getPlayer().sendMessage(plugin.getfullmsg("Message.NotAllow"));
@ -117,7 +104,7 @@ public class Build implements Listener {
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void playerInteract(PlayerInteractEvent event) { public void playerInteract(final PlayerInteractEvent event) {
event.setCancelled(CancelEvent(event.getPlayer(), event.getMaterial())); event.setCancelled(CancelEvent(event.getPlayer(), event.getMaterial()));
} }
} }

View File

@ -1,6 +1,9 @@
name: CityBuild name: ${project.artifactId}
main: cn.citycraft.CityBuild.CityBuild description: ${project.description}
version: 1.1 main: ${project.groupId}.${project.artifactId}.${project.artifactId}
version: ${project.version}-Build#${env.BUILD_NUMBER}
author: 喵♂呜
website: ${jenkins.url}/job/${project.artifactId}/
depend: [Residence] depend: [Residence]
commands: commands:
cb: cb: