118 lines
3.5 KiB
Java
118 lines
3.5 KiB
Java
package cn.citycraft.TeleportRandom;
|
|
|
|
import java.util.Random;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.World;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import cn.citycraft.config.Config;
|
|
|
|
public class TeleportRandom extends JavaPlugin implements CommandExecutor {
|
|
public String servername;
|
|
public String pluginname;
|
|
|
|
public String getmessage(String path) {
|
|
String message = Config.getMessage(path).replaceAll("&", "§");
|
|
return message;
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
|
|
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
|
|
this.onLoad();
|
|
sender.sendMessage(servername + pluginname + getmessage("Message.Reload"));
|
|
return true;
|
|
}
|
|
if (sender instanceof Player) {
|
|
Player p = (Player) sender;
|
|
if (!p.hasPermission("tpr.use")) {
|
|
sender.sendMessage(servername + pluginname + getmessage("Message.NoPerm"));
|
|
return true;
|
|
}
|
|
try {
|
|
switch (args.length) {
|
|
case 0:
|
|
RandomTP(0, p);
|
|
break;
|
|
case 1:
|
|
RandomTP(Integer.parseInt(args[0]), p);
|
|
break;
|
|
case 2:
|
|
RandomTP(Integer.parseInt(args[0]), Bukkit.getWorld(args[1]), p);
|
|
break;
|
|
}
|
|
} catch (Exception e) {
|
|
sender.sendMessage("§c非法的参数或不存在的世界!");
|
|
}
|
|
return true;
|
|
} else {
|
|
sender.sendMessage("§c控制台无法使用此命令!");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void onLoad() {
|
|
Config.load(this, "1.0");
|
|
servername = getmessage("servername");
|
|
pluginname = getmessage("pluginname");
|
|
}
|
|
|
|
public void RandomTP(int limit, Player p) {
|
|
RandomTP(limit, p.getWorld(), p);
|
|
}
|
|
|
|
public void RandomTP(int limit, World world, Player p) {
|
|
Random rr = new Random();
|
|
int lr = Config.getInstance().getInt("default");
|
|
if (limit == 0) {
|
|
p.sendMessage(servername + pluginname + getmessage("Message.default1"));
|
|
p.sendMessage(servername + pluginname + getmessage("Message.default2"));
|
|
} else {
|
|
lr = limit;
|
|
int lrLimit = Config.getInstance().getInt("Limit");
|
|
if (lr > lrLimit) {
|
|
lr = lrLimit;
|
|
p.sendMessage(servername + pluginname + getmessage("Message.Wran").replace("%limit%", lrLimit + ""));
|
|
}
|
|
}
|
|
int x = rr.nextInt(lr);
|
|
int z = rr.nextInt(lr);
|
|
int xf = rr.nextInt(x);
|
|
int yf = rr.nextInt(z);
|
|
if (xf % 2 != 0) {
|
|
x = -x;
|
|
}
|
|
if (yf % 2 != 0) {
|
|
z = -z;
|
|
}
|
|
int y = world.getHighestBlockYAt(x, z);
|
|
final Location trl = new Location(world, x, y - 1, z);
|
|
final Material rbm = trl.getBlock().getType();
|
|
String blockname = rbm.name();
|
|
for (String protectblock : Config.getInstance().getStringList("ProtectBlock")) {
|
|
if (protectblock.equalsIgnoreCase(blockname)) {
|
|
trl.getBlock().setType(Material.GLASS);
|
|
p.sendMessage(servername + pluginname + getmessage("Message.Protect"));
|
|
this.getServer().getScheduler().runTaskLater(this, new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
trl.getBlock().setType(rbm);
|
|
}
|
|
}, 200);
|
|
}
|
|
}
|
|
Location nrl = new Location(world, x, y + 3, z);
|
|
p.teleport(nrl);
|
|
p.sendMessage(servername + pluginname
|
|
+ getmessage("Message.Tip").replaceAll("%world%", world.getName()).replaceAll("%x%", x + "").replaceAll("%z%", z + ""));
|
|
}
|
|
}
|