clean up and use PluginHelper...

Signed-off-by: 502647092 <jtb1@163.com>
master
502647092 2015-09-30 19:59:45 +08:00
parent 9079566849
commit 7b128bd8cc
22 changed files with 674 additions and 1077 deletions

View File

@ -1,20 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

62
pom.xml
View File

@ -3,16 +3,13 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.CityCraft</groupId>
<artifactId>LuckLottery</artifactId>
<version>0.0.7-SNAPSHOT</version>
<version>1.0</version>
<build>
<finalName>${project.name}</finalName>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
@ -24,6 +21,39 @@
<target>1.7</target>
</configuration>
</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>
<include>org.mcstats.*:*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.mcstats</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}.mcstats</shadedPattern>
</relocation>
<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>
</build>
<repositories>
@ -39,6 +69,14 @@
<id>vault-repo</id>
<url>http://nexus.theyeticave.net/content/repositories/pub_releases</url>
</repository>
<repository>
<id>citycraft-repo</id>
<url>http://ci.citycraft.cn:8800/jenkins/plugin/repository/everything/</url>
</repository>
<repository>
<id>Plugin Metrics</id>
<url>http://repo.mcstats.org/content/repositories/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
@ -47,6 +85,12 @@
<type>jar</type>
<version>1.8.3-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>PluginHelper</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultAPI</artifactId>
@ -54,6 +98,12 @@
<scope>system</scope>
<systemPath>${project.basedir}/lib/Vault.jar</systemPath>
</dependency>
<dependency>
<groupId>org.mcstats.bukkit</groupId>
<artifactId>metrics</artifactId>
<version>R8-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -1,27 +0,0 @@
package cn.citycraft.LuckLottery.config;
import org.bukkit.plugin.Plugin;
public class Config extends ConfigLoader {
private static String CONFIG_NAME = "config.yml";
private static FileConfig instance;
public Config(Plugin p, String ver) {
super(p, CONFIG_NAME, ver);
instance = super.getInstance();
}
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).replaceAll("&", "§");
return message;
}
}

View File

@ -1,102 +0,0 @@
package cn.citycraft.LuckLottery.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, res);
}
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,110 +0,0 @@
package cn.citycraft.LuckLottery.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

@ -1,47 +0,0 @@
package cn.citycraft.LuckLottery.config;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.bukkit.plugin.Plugin;
public class OfflineDate extends ConfigLoader {
private static String CONFIG_NAME = "offlinedate.yml";
private static FileConfig instance;
private static File file;
public OfflineDate(Plugin p) {
super(p, CONFIG_NAME, false);
file = new File(p.getDataFolder(), CONFIG_NAME);
instance = super.getInstance();
}
public static void load(Plugin p) {
new OfflineDate(p);
}
public static FileConfig getInstance() {
return instance;
}
public static List<String> getMessage(String p) {
return instance.getStringList(p);
}
public static void addMessage(String p, String message) {
List<String> messages = instance.getStringList(p);
messages.add(message);
instance.set(p, messages);
save();
}
public static void save(){
try {
instance.save(file);
} catch (IOException e) {
saveError(file);
e.printStackTrace();
}
}
}

View File

@ -1,48 +0,0 @@
package cn.citycraft.LuckLottery.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class PlayerConfig extends ConfigLoader {
private static String CONFIG_FOLDER = "userdate";
private static FileConfig instance;
private static File file;
public PlayerConfig(Plugin p, String player) {
super(p, CONFIG_FOLDER + File.separator + player + ".yml", false);
file = new File(p.getDataFolder(), CONFIG_FOLDER + File.separator
+ player + ".yml");
instance = super.getInstance();
}
public static FileConfig getInstance(Plugin p, Player player) {
new PlayerConfig(p, player.getName());
return instance;
}
public static FileConfig getInstance(Plugin p, String player) {
new PlayerConfig(p, player);
return instance;
}
public static String getMessage(String path) {
String message = instance.getString(path).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,35 +0,0 @@
package cn.citycraft.LuckLottery.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.Plugin;
public class PlayerDate extends ConfigLoader {
private static String CONFIG_NAME = "playerdate.yml";
private static FileConfig instance;
private static File file;
public PlayerDate(Plugin p) {
super(p, CONFIG_NAME, false);
file = new File(p.getDataFolder(), CONFIG_NAME);
instance = super.getInstance();
}
public static void load(Plugin p) {
new PlayerDate(p);
}
public static FileConfig getInstance() {
return instance;
}
public static void save() {
try {
instance.save(file);
} catch (IOException e) {
saveError(file);
e.printStackTrace();
}
}
}

View File

@ -1,71 +0,0 @@
package cn.citycraft.LuckLottery.runnable;
import java.util.List;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import cn.citycraft.LuckLottery.LuckLottery;
import cn.citycraft.LuckLottery.config.Config;
import cn.citycraft.LuckLottery.config.OfflineDate;
import cn.citycraft.LuckLottery.utils.ChatUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
public class LotteryReward implements Runnable {
boolean update;
public LotteryReward(boolean b) {
update = b;
}
@SuppressWarnings("deprecation")
@Override
public void run() {
if (LotteryUtils.getSystemLottery() == null
|| LotteryUtils.getSystemLottery().isEmpty()) {
LotteryUtils.updateSystemLottery();
return;
}
ChatUtils.broadcastMessage(ChatColor.GOLD + "本轮彩票开奖: " + ChatColor.RED
+ LotteryUtils.getSystemLottery().toString());
ChatUtils.broadcastMessage(ChatColor.BLUE + "使用命令: " + ChatColor.RED
+ "/ll" + ChatColor.BLUE + " 或闹钟菜单可以购买彩票!");
for (Entry<String, List<List<String>>> players : LotteryUtils.playerLottery
.entrySet()) {
OfflinePlayer p = Bukkit.getOfflinePlayer(players.getKey());
List<List<String>> pl = players.getValue();
if (pl != null && !pl.isEmpty())
for (List<String> playerlottery : pl) {
int win = LotteryUtils.getSameNumber(playerlottery);
int winprices = 0;
winprices = Config.getInstance().getInt("Reward." + win,
200);
if (winprices > 0) {
ChatUtils.broadcastMessage(ChatColor.GREEN
+ p.getName() + "的彩票: " + ChatColor.YELLOW
+ playerlottery.toString() + ChatColor.GREEN
+ "获得了" + ChatColor.GOLD + winprices
+ ChatColor.GREEN + "元!");
String message = ChatColor.GREEN + "您的彩票: "
+ ChatColor.YELLOW + playerlottery.toString()
+ ChatColor.GREEN + "获得了" + ChatColor.GOLD
+ winprices + ChatColor.GREEN + "元!";
if (p.isOnline()) {
// ChatUtils.sendMessage((Player) p, message);
} else {
OfflineDate.addMessage(p.getName(), message);
}
LuckLottery.economy.depositPlayer(p, winprices);
}
}
LotteryUtils.playerLottery.put(players.getKey(), null);
}
LotteryUtils.clearLottery();
if (update)
LotteryUtils.updateSystemLottery();
}
}

View File

@ -1,112 +0,0 @@
package cn.citycraft.LuckLottery.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import com.google.common.base.Charsets;
/**
*
*
* @author
* 20158144:01:15
*/
public class VersionChecker implements Listener {
Plugin plugin;
public String checkurl = "https://coding.net/u/502647092/p/%s/git/raw/%s/src/plugin.yml";
public String branch = "master";
/**
* @param plugin
* -
*/
public VersionChecker(Plugin plugin) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
this.versioncheck(null);
}
/**
* @param plugin
* -
* @param branch
* -
*/
public VersionChecker(Plugin plugin, String branch) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
this.branch = branch;
this.versioncheck(null);
}
/**
*
*
* @param pluginName
* -
* @param branch
* -
* @return
*/
public String getCheckUrl(String pluginName, String branch) {
return String.format(checkurl, pluginName, branch);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
if (e.getPlayer().isOp()) {
this.versioncheck(e.getPlayer());
}
}
/**
*
*
* @param player
* - (null)
*/
public void versioncheck(final Player player) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
String readURL = getCheckUrl(plugin.getName(), branch);
FileConfiguration config;
String currentVersion = plugin.getDescription().getVersion();
try {
URL url = new URL(readURL);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8));
config = YamlConfiguration.loadConfiguration(br);
String newVersion = config.getString("version");
br.close();
if (!newVersion.equals(currentVersion)) {
String[] msg = new String[] {
ChatColor.GREEN + plugin.getName() + " 插件最新版本 v" + newVersion,
ChatColor.RED + "服务器运行版本: v" + currentVersion,
ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE + plugin.getDescription().getWebsite()
};
if (player != null) {
player.sendMessage(msg);
} else {
plugin.getServer().getConsoleSender().sendMessage(msg);
}
}
} catch (IOException e) {
plugin.getLogger().warning("版本更新检查失败!");
}
}
});
}
}

View File

@ -1,25 +1,38 @@
package cn.citycraft.LuckLottery;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.LuckLottery.command.LuckLotteryCommand;
import cn.citycraft.LuckLottery.config.Config;
import cn.citycraft.LuckLottery.config.OfflineDate;
import cn.citycraft.LuckLottery.config.PlayerDate;
import cn.citycraft.LuckLottery.listen.PlayerListen;
import cn.citycraft.LuckLottery.runnable.LotteryReward;
import cn.citycraft.LuckLottery.utils.ChatUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
import cn.citycraft.LuckLottery.utils.VersionChecker;
import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.PluginHelper.utils.VersionChecker;
import net.milkbowl.vault.economy.Economy;
public class LuckLottery extends JavaPlugin {
public static boolean isEconomy;
public static Economy economy = null;
public static boolean isEconomy;
public static LuckLottery plugin;
protected FileConfig config;
protected FileConfig offlinedata;
protected FileConfig playerdata;
@Override
public FileConfig getConfig() {
return config;
}
public FileConfig getOfflinedata() {
return offlinedata;
}
public FileConfig getPlayerdata() {
return playerdata;
}
@Override
public void onDisable() {
@ -30,7 +43,7 @@ public class LuckLottery extends JavaPlugin {
@Override
public void onEnable() {
PluginManager pm = this.getServer().getPluginManager();
final PluginManager pm = this.getServer().getPluginManager();
if (pm.getPlugin("Vault") == null && !pm.getPlugin("Vault").isEnabled()) {
this.getLogger().warning("未找到前置插件Vault 关闭插件...");
this.getServer().getPluginManager().disablePlugin(this);
@ -44,30 +57,29 @@ public class LuckLottery extends JavaPlugin {
this.getServer().getPluginManager().disablePlugin(this);
return;
}
int rewardtime = Config.getInstance().getInt("RewardTime", 10);
this.getServer().getScheduler()
.runTaskTimer(plugin, new LotteryReward(true), 10, rewardtime * 60 * 20);
final int rewardtime = config.getInt("RewardTime", 10);
this.getServer().getScheduler().runTaskTimer(plugin, new LotteryReward(this, true), 10, rewardtime * 60 * 20);
this.getLogger().info("彩票系统已开启...");
new VersionChecker(this);
pm.registerEvents(new PlayerListen(), this);
pm.registerEvents(new PlayerListen(this), this);
getCommand("ll").setExecutor(new LuckLotteryCommand(this));
}
@Override
public void onLoad() {
plugin = this;
Config.load(this, "1.2");
OfflineDate.load(this);
PlayerDate.load(this);
config = new FileConfig(this);
offlinedata = new FileConfig(this, "offlinedata");
playerdata = new FileConfig(this, "playerdate.yml");
LotteryUtils.init(this);
LotteryUtils.reloadPlayerLottery();
ChatUtils.setPluginname(Config.getMessage("pluginname"));
LotteryUtils.setNumbersame(Config.getInstance().getBoolean("numbersame"));
LotteryUtils.setPrice(Config.getInstance().getInt("price"));
ChatUtils.setPluginname(config.getMessage("pluginname"));
LotteryUtils.setNumbersame(config.getBoolean("numbersame"));
LotteryUtils.setPrice(config.getInt("price"));
}
public boolean setupEconomy() {
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager()
.getRegistration(net.milkbowl.vault.economy.Economy.class);
final RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
if (economyProvider != null) {
economy = economyProvider.getProvider();
}

View File

@ -1,121 +1,121 @@
package cn.citycraft.LuckLottery.command;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import cn.citycraft.LuckLottery.LuckLottery;
import cn.citycraft.LuckLottery.utils.ChatUtils;
import cn.citycraft.LuckLottery.utils.InvUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
import cn.citycraft.LuckLottery.utils.PermissionUtils;
public class LuckLotteryCommand implements CommandExecutor {
LuckLottery plugin;
public LuckLotteryCommand(LuckLottery main) {
plugin = main;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String string,
String[] args) {
switch (args.length) {
case 0:
if (isPlayer(sender)) {
final Player p = (Player) sender;
int price = LotteryUtils.getPrice();
if (LuckLottery.economy.hasAccount(p)
&& LuckLottery.economy.has(p, price)) {
InvUtils.openGui(p);
} else {
ChatUtils.sendMessage(p, ChatColor.GOLD
+ "你没有足够的金钱购买彩票,每张彩票" + ChatColor.RED + price
+ ChatColor.GOLD + "元!");
}
}
return true;
case 1:
switch (args[0]) {
case "load":
if (isPlayer(sender)) {
Player p = (Player) sender;
LotteryUtils.loadPlayerLottery(p);
ChatUtils.sendMessage(p, ChatColor.BLUE + "已加载您的彩票数据...");
}
return true;
case "show":
if (isPlayer(sender)) {
Player p = (Player) sender;
LotteryUtils.showPlayerLottery(p);
}
return true;
case "clear":
if (PermissionUtils.Check(sender, PermissionUtils.Clear)) {
ChatUtils.sendMessage(sender, ChatColor.LIGHT_PURPLE
+ "警告: 即将清理所有彩票数据,此操作将无法取消!");
ChatUtils.sendMessage(sender, ChatColor.RED
+ "命令: 请使用/ll clear confirm 确定清理!");
}
return true;
case "showall":
if (PermissionUtils.Check(sender, PermissionUtils.ShowAll)) {
LotteryUtils.showAllPlayerLottery(sender);
}
return true;
case "random":
if (PermissionUtils.Check(sender, PermissionUtils.Random)) {
LotteryUtils.updateSystemLottery(sender);
}
return true;
case "reward":
if (PermissionUtils.Check(sender, PermissionUtils.Reward)) {
LotteryUtils.rewardLastLottery();
ChatUtils
.sendMessage(sender, ChatColor.GREEN + "已结算上一轮彩票!");
}
return true;
case "look":
if (PermissionUtils.Check(sender, PermissionUtils.Reward)) {
LotteryUtils.showSystemLottery(sender);
}
return true;
case "reload":
if (PermissionUtils.Check(sender, PermissionUtils.Reload)) {
PluginManager pm = plugin.getServer().getPluginManager();
ChatUtils.sendMessage(sender, ChatColor.GREEN + "配置文件已重载!");
pm.disablePlugin(plugin);
pm.enablePlugin(plugin);
}
return true;
case "help":
ChatUtils.sendHelp(sender);
return true;
}
break;
case 2:
switch (args[0]) {
case "clear":
if (PermissionUtils.Check(sender, PermissionUtils.Clear)) {
ChatUtils.sendMessage(sender, ChatColor.DARK_RED
+ "警告: 已经清理所有彩票数据,此操作无法撤销!");
LotteryUtils.clearPlayerLottery(sender);
}
return true;
}
default:
}
return false;
}
public boolean isPlayer(CommandSender p) {
if (p instanceof Player)
return true;
return false;
}
}
package cn.citycraft.LuckLottery.command;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import cn.citycraft.LuckLottery.LuckLottery;
import cn.citycraft.LuckLottery.utils.ChatUtils;
import cn.citycraft.LuckLottery.utils.InvUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
import cn.citycraft.LuckLottery.utils.PermissionUtils;
public class LuckLotteryCommand implements CommandExecutor {
LuckLottery plugin;
public LuckLotteryCommand(LuckLottery main) {
plugin = main;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String string,
String[] args) {
switch (args.length) {
case 0:
if (isPlayer(sender)) {
final Player p = (Player) sender;
int price = LotteryUtils.getPrice();
if (LuckLottery.economy.hasAccount(p)
&& LuckLottery.economy.has(p, price)) {
InvUtils.openGui(p);
} else {
ChatUtils.sendMessage(p, ChatColor.GOLD
+ "你没有足够的金钱购买彩票,每张彩票" + ChatColor.RED + price
+ ChatColor.GOLD + "元!");
}
}
return true;
case 1:
switch (args[0]) {
case "load":
if (isPlayer(sender)) {
Player p = (Player) sender;
LotteryUtils.loadPlayerLottery(p);
ChatUtils.sendMessage(p, ChatColor.BLUE + "已加载您的彩票数据...");
}
return true;
case "show":
if (isPlayer(sender)) {
Player p = (Player) sender;
LotteryUtils.showPlayerLottery(p);
}
return true;
case "clear":
if (PermissionUtils.Check(sender, PermissionUtils.Clear)) {
ChatUtils.sendMessage(sender, ChatColor.LIGHT_PURPLE
+ "警告: 即将清理所有彩票数据,此操作将无法取消!");
ChatUtils.sendMessage(sender, ChatColor.RED
+ "命令: 请使用/ll clear confirm 确定清理!");
}
return true;
case "showall":
if (PermissionUtils.Check(sender, PermissionUtils.ShowAll)) {
LotteryUtils.showAllPlayerLottery(sender);
}
return true;
case "random":
if (PermissionUtils.Check(sender, PermissionUtils.Random)) {
LotteryUtils.updateSystemLottery(sender);
}
return true;
case "reward":
if (PermissionUtils.Check(sender, PermissionUtils.Reward)) {
LotteryUtils.rewardLastLottery();
ChatUtils
.sendMessage(sender, ChatColor.GREEN + "已结算上一轮彩票!");
}
return true;
case "look":
if (PermissionUtils.Check(sender, PermissionUtils.Reward)) {
LotteryUtils.showSystemLottery(sender);
}
return true;
case "reload":
if (PermissionUtils.Check(sender, PermissionUtils.Reload)) {
PluginManager pm = plugin.getServer().getPluginManager();
ChatUtils.sendMessage(sender, ChatColor.GREEN + "配置文件已重载!");
pm.disablePlugin(plugin);
pm.enablePlugin(plugin);
}
return true;
case "help":
ChatUtils.sendHelp(sender);
return true;
}
break;
case 2:
switch (args[0]) {
case "clear":
if (PermissionUtils.Check(sender, PermissionUtils.Clear)) {
ChatUtils.sendMessage(sender, ChatColor.DARK_RED
+ "警告: 已经清理所有彩票数据,此操作无法撤销!");
LotteryUtils.clearPlayerLottery(sender);
}
return true;
}
default:
}
return false;
}
public boolean isPlayer(CommandSender p) {
if (p instanceof Player)
return true;
return false;
}
}

View File

@ -0,0 +1,27 @@
package cn.citycraft.LuckLottery.config;
import java.util.List;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.config.FileConfig;
public class OfflineDate {
private static String CONFIG_NAME = "offlinedate.yml";
private static FileConfig instance;
public OfflineDate(final Plugin p) {
instance = new FileConfig(p, CONFIG_NAME);
}
public static void addMessage(final String p, final String message) {
final List<String> messages = instance.getStringList(p);
messages.add(message);
instance.set(p, messages);
instance.save();
}
public static List<String> getMessage(final String p) {
return instance.getStringList(p);
}
}

View File

@ -22,45 +22,35 @@ import cn.citycraft.LuckLottery.utils.InvUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
public class PlayerListen implements Listener {
LuckLottery plugin;
@EventHandler(
priority = EventPriority.HIGH)
public void onPlayerInteract(PlayerJoinEvent e) {
Player p = e.getPlayer();
List<String> messages = OfflineDate.getMessage(p.getName());
if (messages != null && !messages.isEmpty()) {
Bukkit.getServer().getScheduler()
.runTaskLaterAsynchronously(LuckLottery.plugin, new LaterShow(p, messages), 5);
OfflineDate.getInstance().set(p.getName(), null);
OfflineDate.save();
}
public PlayerListen(final LuckLottery luckLottery) {
this.plugin = luckLottery;
}
@EventHandler(
priority = EventPriority.HIGHEST,
ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent e) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryClick(final InventoryClickEvent e) {
if (InvUtils.isLotteryGUI(e.getInventory())) {
e.setCancelled(true);
Player p = (Player) e.getWhoClicked();
ItemStack ci = e.getCurrentItem();
Inventory inv = e.getInventory();
int solt = e.getRawSlot();
if (ci == null || ci.getType() == Material.AIR)
final Player p = (Player) e.getWhoClicked();
final ItemStack ci = e.getCurrentItem();
final Inventory inv = e.getInventory();
final int solt = e.getRawSlot();
if (ci == null || ci.getType() == Material.AIR) {
return;
if (!ci.getItemMeta().hasDisplayName())
}
if (!ci.getItemMeta().hasDisplayName()) {
return;
String clickName = ci.getItemMeta().getDisplayName();
}
final String clickName = ci.getItemMeta().getDisplayName();
switch (clickName) {
case InvUtils.Create:
if (!LuckLottery.economy.hasAccount(p)
|| !LuckLottery.economy.has(p, LotteryUtils.getPrice())) {
ChatUtils.sendMessage(p, ChatColor.GOLD + "你没有足够的金钱购买彩票,每张彩票" + ChatColor.RED
+ LotteryUtils.getPrice() + ChatColor.GOLD + "元!");
if (!LuckLottery.economy.hasAccount(p) || !LuckLottery.economy.has(p, LotteryUtils.getPrice())) {
ChatUtils.sendMessage(p, ChatColor.GOLD + "你没有足够的金钱购买彩票,每张彩票" + ChatColor.RED + LotteryUtils.getPrice() + ChatColor.GOLD + "元!");
p.closeInventory();
return;
}
List<String> lottery = InvUtils.getLotteryNumber(inv);
final List<String> lottery = InvUtils.getLotteryNumber(inv);
LotteryUtils.addLottery(p, lottery);
ChatUtils.sendMessage(p, "§6您当前购买的彩票号码为: " + ChatColor.GREEN + lottery.toString());
ChatUtils.howToShow(p);
@ -72,8 +62,9 @@ public class PlayerListen implements Listener {
break;
default:
if (solt < 36 && InvUtils.isLotteryItem(ci)) {
if (setSelect(inv, ci))
if (setSelect(inv, ci)) {
inv.setItem(solt, InvUtils.A);
}
}
break;
}
@ -85,7 +76,18 @@ public class PlayerListen implements Listener {
}
}
private boolean setSelect(Inventory inv, ItemStack is) {
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(final PlayerJoinEvent e) {
final Player p = e.getPlayer();
final List<String> messages = OfflineDate.getMessage(p.getName());
if (messages != null && !messages.isEmpty()) {
Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(LuckLottery.plugin, new LaterShow(p, messages), 5);
plugin.getOfflinedata().set(p.getName(), null);
plugin.getOfflinedata().save();
}
}
private boolean setSelect(final Inventory inv, final ItemStack is) {
for (int i = 45; i < 53; i++) {
if (inv.getItem(i) == null) {
inv.setItem(i, is);

View File

@ -1,26 +1,26 @@
package cn.citycraft.LuckLottery.runnable;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import cn.citycraft.LuckLottery.utils.ChatUtils;
public class LaterShow implements Runnable {
Player p;
List<String> messages;
public LaterShow(Player p, List<String> messages) {
this.p = p;
this.messages = messages;
}
@Override
public void run() {
ChatUtils.sendMessage(p, ChatColor.RED + "在您离线期间彩票开奖结果:");
ChatUtils.sendMessage(p, messages);
}
}
package cn.citycraft.LuckLottery.runnable;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import cn.citycraft.LuckLottery.utils.ChatUtils;
public class LaterShow implements Runnable {
Player p;
List<String> messages;
public LaterShow(Player p, List<String> messages) {
this.p = p;
this.messages = messages;
}
@Override
public void run() {
ChatUtils.sendMessage(p, ChatColor.RED + "在您离线期间彩票开奖结果:");
ChatUtils.sendMessage(p, messages);
}
}

View File

@ -0,0 +1,62 @@
package cn.citycraft.LuckLottery.runnable;
import java.util.List;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import cn.citycraft.LuckLottery.LuckLottery;
import cn.citycraft.LuckLottery.config.OfflineDate;
import cn.citycraft.LuckLottery.utils.ChatUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
public class LotteryReward implements Runnable {
LuckLottery plugin;
boolean update;
public LotteryReward(final LuckLottery plugin, final boolean b) {
this.plugin = plugin;
update = b;
}
@SuppressWarnings("deprecation")
@Override
public void run() {
if (LotteryUtils.getSystemLottery() == null || LotteryUtils.getSystemLottery().isEmpty()) {
LotteryUtils.updateSystemLottery();
return;
}
ChatUtils.broadcastMessage(ChatColor.GOLD + "本轮彩票开奖: " + ChatColor.RED + LotteryUtils.getSystemLottery().toString());
ChatUtils.broadcastMessage(ChatColor.BLUE + "使用命令: " + ChatColor.RED + "/ll" + ChatColor.BLUE + " 或闹钟菜单可以购买彩票!");
for (final Entry<String, List<List<String>>> players : LotteryUtils.playerLottery.entrySet()) {
final OfflinePlayer p = Bukkit.getOfflinePlayer(players.getKey());
final List<List<String>> pl = players.getValue();
if (pl != null && !pl.isEmpty()) {
for (final List<String> playerlottery : pl) {
final int win = LotteryUtils.getSameNumber(playerlottery);
int winprices = 0;
winprices = plugin.getConfig().getInt("Reward." + win, 200);
if (winprices > 0) {
ChatUtils.broadcastMessage(
ChatColor.GREEN + p.getName() + "的彩票: " + ChatColor.YELLOW + playerlottery.toString() + ChatColor.GREEN + "获得了" + ChatColor.GOLD + winprices + ChatColor.GREEN + "元!");
final String message = ChatColor.GREEN + "您的彩票: " + ChatColor.YELLOW + playerlottery.toString() + ChatColor.GREEN + "获得了" + ChatColor.GOLD + winprices + ChatColor.GREEN + "元!";
if (p.isOnline()) {
// ChatUtils.sendMessage((Player) p, message);
} else {
OfflineDate.addMessage(p.getName(), message);
}
LuckLottery.economy.depositPlayer(p, winprices);
}
}
}
LotteryUtils.playerLottery.put(players.getKey(), null);
}
LotteryUtils.clearLottery();
if (update) {
LotteryUtils.updateSystemLottery();
}
}
}

View File

@ -1,77 +1,77 @@
package cn.citycraft.LuckLottery.utils;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class ChatUtils {
protected static String pluginname = "";
public static String[] helps = new String[]{
"§6=========彩票插件帮助========",
"§a/ll 打开彩票购买界面",
"§b/ll show 查看已购买彩票",
"§3/ll showall查看所以玩家已购买彩票§4(管理员命令)",
"§c/ll random 重新生成彩票§4(管理员命令)",
"§d/ll reward 结算上一轮彩票§4(管理员命令)",
"§e/ll clear 清除所有已购买彩票§4(管理员命令)",
"§5/ll look 查看本轮彩票号码§4(管理员命令)",
"§c/ll reload 重载彩票插件§4(管理员命令)"
};
public static String getPluginname() {
return pluginname;
}
public static void setPluginname(String pluginname) {
ChatUtils.pluginname = pluginname;
}
public static void sendMessage(Player p, String message) {
p.sendMessage(pluginname + message);
}
public static void sendMessage(CommandSender p, String message) {
p.sendMessage(pluginname + message);
}
public static void sendMessage(Player p, String[] messages) {
for (String msg : messages) {
sendMessage(p, msg);
}
}
public static void sendMessage(Player p, List<String> messages) {
for (String msg : messages) {
sendMessage(p, msg);
}
}
public static void sendHelp(Player p){
p.sendMessage(helps);
}
public static void sendHelp(CommandSender sender) {
sender.sendMessage(helps);
}
public static void broadcastMessage(String message) {
Bukkit.broadcastMessage(pluginname + message);
}
public static void howToShow(Player p) {
sendMessage(p, ChatColor.GOLD + "命令: " + ChatColor.RED + "/ll show "
+ ChatColor.GOLD + "查看已购买彩票!");
}
public static void noPermission(Player p) {
ChatUtils.sendMessage(p, PermissionUtils.No_Permission);
}
public static void noPermission(CommandSender p) {
ChatUtils.sendMessage(p, PermissionUtils.No_Permission);
}
}
package cn.citycraft.LuckLottery.utils;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class ChatUtils {
protected static String pluginname = "";
public static String[] helps = new String[]{
"§6=========彩票插件帮助========",
"§a/ll 打开彩票购买界面",
"§b/ll show 查看已购买彩票",
"§3/ll showall查看所以玩家已购买彩票§4(管理员命令)",
"§c/ll random 重新生成彩票§4(管理员命令)",
"§d/ll reward 结算上一轮彩票§4(管理员命令)",
"§e/ll clear 清除所有已购买彩票§4(管理员命令)",
"§5/ll look 查看本轮彩票号码§4(管理员命令)",
"§c/ll reload 重载彩票插件§4(管理员命令)"
};
public static String getPluginname() {
return pluginname;
}
public static void setPluginname(String pluginname) {
ChatUtils.pluginname = pluginname;
}
public static void sendMessage(Player p, String message) {
p.sendMessage(pluginname + message);
}
public static void sendMessage(CommandSender p, String message) {
p.sendMessage(pluginname + message);
}
public static void sendMessage(Player p, String[] messages) {
for (String msg : messages) {
sendMessage(p, msg);
}
}
public static void sendMessage(Player p, List<String> messages) {
for (String msg : messages) {
sendMessage(p, msg);
}
}
public static void sendHelp(Player p){
p.sendMessage(helps);
}
public static void sendHelp(CommandSender sender) {
sender.sendMessage(helps);
}
public static void broadcastMessage(String message) {
Bukkit.broadcastMessage(pluginname + message);
}
public static void howToShow(Player p) {
sendMessage(p, ChatColor.GOLD + "命令: " + ChatColor.RED + "/ll show "
+ ChatColor.GOLD + "查看已购买彩票!");
}
public static void noPermission(Player p) {
ChatUtils.sendMessage(p, PermissionUtils.No_Permission);
}
public static void noPermission(CommandSender p) {
ChatUtils.sendMessage(p, PermissionUtils.No_Permission);
}
}

View File

@ -1,222 +1,221 @@
package cn.citycraft.LuckLottery.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import cn.citycraft.LuckLottery.LuckLottery;
import cn.citycraft.LuckLottery.config.PlayerDate;
import cn.citycraft.LuckLottery.runnable.LotteryReward;
public class LotteryUtils {
protected static List<String> systemLottery = new ArrayList<String>();
public static Map<String, List<List<String>>> playerLottery = new HashMap<String, List<List<String>>>();
protected static boolean numbersame;
protected static int price;
public static List<List<String>> addLottery(Player player,
List<String> lottery) {
List<List<String>> playerhas = playerLottery.get(player.getName());
if (playerhas == null) {
playerhas = new ArrayList<List<String>>();
}
playerhas.add(lottery);
playerLottery.put(player.getName(), playerhas);
PlayerDate.getInstance().set(player.getName(), playerhas);
PlayerDate.save();
return playerhas;
}
public static void saveLottery() {
for (Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
String p = pl.getKey();
List<List<String>> l = pl.getValue();
PlayerDate.getInstance().set(p, l);
}
PlayerDate.save();
}
@SuppressWarnings("unchecked")
public static void loadPlayerLottery(Player p) {
List<?> pl = PlayerDate.getInstance().getList(p.getName());
if (pl != null) {
playerLottery.put(p.getName(), (List<List<String>>) pl);
}
}
@SuppressWarnings("unchecked")
public static void reloadPlayerLottery() {
Set<String> apl = PlayerDate.getInstance().getKeys(false);
for (String p : apl) {
List<?> pl = PlayerDate.getInstance().getList(p);
if (pl != null) {
playerLottery.put(p, (List<List<String>>) pl);
}
}
}
public static void showPlayerLottery(Player p) {
List<List<String>> playerhas = playerLottery.get(p.getName());
if (playerhas != null && !playerhas.isEmpty()) {
ChatUtils.sendMessage(p, "§6您当前购买的彩票有:");
for (List<String> lry : playerhas) {
ChatUtils.sendMessage(p, "§a" + lry.toString());
}
} else {
ChatUtils.sendMessage(p, "§c您当前没有购买的彩票");
}
}
public static void showAllPlayerLottery(CommandSender sender) {
ChatUtils.sendMessage(sender, "§c正在检索玩家彩票数据...");
boolean has = false;
for (Entry<String, List<List<String>>> phas : playerLottery.entrySet()) {
ChatUtils.sendMessage(sender, "§6玩家: §a" + phas.getKey()
+ "§6购买的彩票有");
for (List<String> lry : phas.getValue()) {
has = true;
ChatUtils.sendMessage(sender, "§a" + lry.toString());
}
}
if (!has) {
ChatUtils.sendMessage(sender, "§d没有玩家购买本轮彩票...");
}
}
public static void clearLottery() {
for (Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
String p = pl.getKey();
PlayerDate.getInstance().set(p, null);
}
playerLottery.clear();
PlayerDate.save();
}
public static void clearPlayerLottery(CommandSender sender) {
ChatUtils.sendMessage(sender, "§c提示: 以清除所有玩家彩票数据...");
clearLottery();
}
public static void rewardLastLottery() {
Bukkit.getServer()
.getScheduler()
.runTaskAsynchronously(LuckLottery.plugin,
new LotteryReward(true));
}
public static List<String> getSystemLottery() {
return systemLottery;
}
public static void setSystemLottery(List<String> systemLottery) {
LotteryUtils.systemLottery = systemLottery;
}
public static int getPrice() {
return price;
}
public static void setPrice(int price) {
LotteryUtils.price = price;
}
public static void updateSystemLottery() {
LotteryUtils.systemLottery = LotteryUtils.getRandomNumber();
}
public static void updateSystemLottery(CommandSender p) {
LotteryUtils.systemLottery = LotteryUtils.getRandomNumber();
ChatUtils.sendMessage(p, ChatColor.BLUE + "已随机生成彩票: "
+ ChatColor.YELLOW + LotteryUtils.systemLottery.toString());
}
public static void showSystemLottery(CommandSender p) {
ChatUtils.sendMessage(p, ChatColor.DARK_PURPLE + "此轮彩票号码为: "
+ ChatColor.YELLOW + LotteryUtils.systemLottery.toString());
}
public static int getSameNumber(List<String> lottery) {
int samenum = 0;
List<String> system = systemLottery;
if (numbersame) {
for (int i = 0; i < lottery.size(); i++) {
if (lottery.get(i).equals(system.get(i))) {
samenum++;
}
}
} else {
for (String pn : lottery) {
for (String sn : system) {
if (pn.equals(sn))
samenum++;
}
}
}
if (lottery.get(lottery.size() - 1).equalsIgnoreCase(
system.get(system.size() - 1)))
samenum += 9;
return samenum;
}
/**
* @return numbersame
*/
public static boolean isNumbersame() {
return numbersame;
}
/**
* @param numbersame
* numbersame
*/
public static void setNumbersame(boolean numbersame) {
LotteryUtils.numbersame = numbersame;
}
public static int getSameNumber(List<String> lottery, List<String> system) {
int samenum = 0;
if (numbersame) {
for (int i = 0; i < lottery.size(); i++) {
if (lottery.get(i).equals(system.get(i))) {
samenum++;
}
}
} else {
for (String pn : lottery) {
for (String sn : system) {
if (pn.equals(sn))
samenum++;
}
}
}
if (lottery.get(lottery.size() - 1).equalsIgnoreCase(
system.get(system.size() - 1)))
samenum += 9;
return samenum;
}
public static List<String> getRandomNumber() {
Random rnd = new Random();
List<String> normal = new ArrayList<String>();
for (int i = 1; i < 8; i++) {
String rr = rnd.nextInt(32) + 1 + "";
while (normal.contains(rr)) {
rr = rnd.nextInt(32) + 1 + "";
}
normal.add(rr);
}
return normal;
}
}
package cn.citycraft.LuckLottery.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import cn.citycraft.LuckLottery.LuckLottery;
import cn.citycraft.LuckLottery.runnable.LotteryReward;
import cn.citycraft.PluginHelper.config.FileConfig;
public class LotteryUtils {
public static Map<String, List<List<String>>> playerLottery = new HashMap<String, List<List<String>>>();
protected static boolean numbersame;
protected static FileConfig playerdata;
protected static LuckLottery plugin;
protected static int price;
protected static List<String> systemLottery = new ArrayList<String>();
public static List<List<String>> addLottery(final Player player, final List<String> lottery) {
List<List<String>> playerhas = playerLottery.get(player.getName());
if (playerhas == null) {
playerhas = new ArrayList<List<String>>();
}
playerhas.add(lottery);
playerLottery.put(player.getName(), playerhas);
playerdata.set(player.getName(), playerhas);
playerdata.save();
return playerhas;
}
public static void clearLottery() {
for (final Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
final String p = pl.getKey();
playerdata.set(p, null);
}
playerLottery.clear();
playerdata.save();
}
public static void clearPlayerLottery(final CommandSender sender) {
ChatUtils.sendMessage(sender, "§c提示: 以清除所有玩家彩票数据...");
clearLottery();
}
public static int getPrice() {
return price;
}
public static List<String> getRandomNumber() {
final Random rnd = new Random();
final List<String> normal = new ArrayList<String>();
for (int i = 1; i < 8; i++) {
String rr = rnd.nextInt(32) + 1 + "";
while (normal.contains(rr)) {
rr = rnd.nextInt(32) + 1 + "";
}
normal.add(rr);
}
return normal;
}
public static int getSameNumber(final List<String> lottery) {
int samenum = 0;
final List<String> system = systemLottery;
if (numbersame) {
for (int i = 0; i < lottery.size(); i++) {
if (lottery.get(i).equals(system.get(i))) {
samenum++;
}
}
} else {
for (final String pn : lottery) {
for (final String sn : system) {
if (pn.equals(sn)) {
samenum++;
}
}
}
}
if (lottery.get(lottery.size() - 1).equalsIgnoreCase(system.get(system.size() - 1))) {
samenum += 9;
}
return samenum;
}
public static int getSameNumber(final List<String> lottery, final List<String> system) {
int samenum = 0;
if (numbersame) {
for (int i = 0; i < lottery.size(); i++) {
if (lottery.get(i).equals(system.get(i))) {
samenum++;
}
}
} else {
for (final String pn : lottery) {
for (final String sn : system) {
if (pn.equals(sn)) {
samenum++;
}
}
}
}
if (lottery.get(lottery.size() - 1).equalsIgnoreCase(system.get(system.size() - 1))) {
samenum += 9;
}
return samenum;
}
public static List<String> getSystemLottery() {
return systemLottery;
}
public static void init(final LuckLottery luckLottery) {
plugin = luckLottery;
playerdata = plugin.getPlayerdata();
}
/**
* @return numbersame
*/
public static boolean isNumbersame() {
return numbersame;
}
@SuppressWarnings("unchecked")
public static void loadPlayerLottery(final Player p) {
final List<?> pl = playerdata.getList(p.getName());
if (pl != null) {
playerLottery.put(p.getName(), (List<List<String>>) pl);
}
}
@SuppressWarnings("unchecked")
public static void reloadPlayerLottery() {
final Set<String> apl = playerdata.getKeys(false);
for (final String p : apl) {
final List<?> pl = playerdata.getList(p);
if (pl != null) {
playerLottery.put(p, (List<List<String>>) pl);
}
}
}
public static void rewardLastLottery() {
Bukkit.getServer().getScheduler().runTaskAsynchronously(LuckLottery.plugin, new LotteryReward(plugin, true));
}
public static void saveLottery() {
for (final Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
final String p = pl.getKey();
final List<List<String>> l = pl.getValue();
playerdata.set(p, l);
}
playerdata.save();
}
/**
* @param numbersame
* numbersame
*/
public static void setNumbersame(final boolean numbersame) {
LotteryUtils.numbersame = numbersame;
}
public static void setPrice(final int price) {
LotteryUtils.price = price;
}
public static void setSystemLottery(final List<String> systemLottery) {
LotteryUtils.systemLottery = systemLottery;
}
public static void showAllPlayerLottery(final CommandSender sender) {
ChatUtils.sendMessage(sender, "§c正在检索玩家彩票数据...");
boolean has = false;
for (final Entry<String, List<List<String>>> phas : playerLottery.entrySet()) {
ChatUtils.sendMessage(sender, "§6玩家: §a" + phas.getKey() + "§6购买的彩票有");
for (final List<String> lry : phas.getValue()) {
has = true;
ChatUtils.sendMessage(sender, "§a" + lry.toString());
}
}
if (!has) {
ChatUtils.sendMessage(sender, "§d没有玩家购买本轮彩票...");
}
}
public static void showPlayerLottery(final Player p) {
final List<List<String>> playerhas = playerLottery.get(p.getName());
if (playerhas != null && !playerhas.isEmpty()) {
ChatUtils.sendMessage(p, "§6您当前购买的彩票有:");
for (final List<String> lry : playerhas) {
ChatUtils.sendMessage(p, "§a" + lry.toString());
}
} else {
ChatUtils.sendMessage(p, "§c您当前没有购买的彩票");
}
}
public static void showSystemLottery(final CommandSender p) {
ChatUtils.sendMessage(p, ChatColor.DARK_PURPLE + "此轮彩票号码为: " + ChatColor.YELLOW + LotteryUtils.systemLottery.toString());
}
public static void updateSystemLottery() {
LotteryUtils.systemLottery = LotteryUtils.getRandomNumber();
}
public static void updateSystemLottery(final CommandSender p) {
LotteryUtils.systemLottery = LotteryUtils.getRandomNumber();
ChatUtils.sendMessage(p, ChatColor.BLUE + "已随机生成彩票: " + ChatColor.YELLOW + LotteryUtils.systemLottery.toString());
}
}

View File

@ -1,8 +1,9 @@
name: LuckLottery
main: cn.citycraft.LuckLottery.LuckLottery
name: ${project.artifactId}
description: ${project.description}
main: ${project.groupId}.${project.artifactId}.${project.artifactId}
version: ${project.version}
author: 喵♂呜
website: http://ci.citycraft.cn:8800/jenkins/job/LuckLottery/
version: 0.1.0
website: http://ci.citycraft.cn:8800/jenkins/job/${project.artifactId}/
depended: [Vault]
commands:
lucklottery: