TotalFreedomMod/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java

584 lines
17 KiB
Java
Raw Normal View History

2011-10-13 23:07:52 +00:00
package me.StevenLawson.TotalFreedomMod;
2011-11-04 23:14:17 +00:00
import java.io.Closeable;
2011-10-13 23:07:52 +00:00
import java.io.File;
2011-11-04 23:14:17 +00:00
import java.io.FileInputStream;
2011-10-13 23:07:52 +00:00
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
2011-11-04 23:14:17 +00:00
import java.io.OutputStream;
2011-10-13 23:07:52 +00:00
import java.net.InetSocketAddress;
2011-11-04 23:14:17 +00:00
import java.net.URI;
2011-11-07 13:11:13 +00:00
import java.util.ArrayList;
2011-11-04 23:14:17 +00:00
import java.util.Deque;
import java.util.Enumeration;
2011-10-24 02:43:52 +00:00
import java.util.HashMap;
2011-11-04 23:14:17 +00:00
import java.util.LinkedList;
2011-10-13 23:07:52 +00:00
import java.util.List;
2011-10-24 02:43:52 +00:00
import java.util.Map;
2011-10-13 23:07:52 +00:00
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
2011-11-04 23:14:17 +00:00
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
2011-10-13 23:07:52 +00:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
2011-10-13 23:07:52 +00:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
2011-10-24 02:43:52 +00:00
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
2011-10-13 23:07:52 +00:00
import org.bukkit.entity.*;
public class TFM_Util
{
private static final Logger log = Logger.getLogger("Minecraft");
private static Map<String, Integer> eject_tracker = new HashMap<String, Integer>();
private static final Map<String, CreatureType> mobtypes = new HashMap<String, CreatureType>();
private static final List<String> stop_commands = new ArrayList<String>();
static
{
mobtypes.put("chicken", CreatureType.CHICKEN);
mobtypes.put("cow", CreatureType.COW);
mobtypes.put("creeper", CreatureType.CREEPER);
mobtypes.put("pig", CreatureType.PIG);
mobtypes.put("sheep", CreatureType.SHEEP);
mobtypes.put("skeleton", CreatureType.SKELETON);
mobtypes.put("spider", CreatureType.SPIDER);
mobtypes.put("zombie", CreatureType.ZOMBIE);
mobtypes.put("wolf", CreatureType.WOLF);
stop_commands.add("stop");
stop_commands.add("off");
stop_commands.add("end");
stop_commands.add("halt");
stop_commands.add("die");
}
2011-10-24 02:43:52 +00:00
private TFM_Util()
{
throw new AssertionError();
}
public static void bcastMsg(String message, ChatColor color)
2011-10-13 23:07:52 +00:00
{
log.info(message);
for (Player p : Bukkit.getOnlinePlayers())
{
p.sendMessage(color + message);
}
}
2011-10-24 02:43:52 +00:00
public static void bcastMsg(String message)
2011-10-13 23:07:52 +00:00
{
log.info(ChatColor.stripColor(message));
for (Player p : Bukkit.getOnlinePlayers())
{
p.sendMessage(message);
}
}
public static String implodeStringList(String glue, List<String> pieces)
{
StringBuilder output = new StringBuilder();
for (int i = 0; i < pieces.size(); i++)
{
if (i != 0)
{
output.append(glue);
}
output.append(pieces.get(i));
}
return output.toString();
}
public static String formatLocation(Location in_loc)
{
return String.format("%s: (%d, %d, %d)",
in_loc.getWorld().getName(),
Math.round(in_loc.getX()),
Math.round(in_loc.getY()),
Math.round(in_loc.getZ()));
}
public static void gotoWorld(CommandSender sender, String targetworld)
{
if (sender instanceof Player)
{
Player sender_p = (Player) sender;
if (sender_p.getWorld().getName().equalsIgnoreCase(targetworld))
{
sender.sendMessage(ChatColor.GRAY + "Going to main world.");
Bukkit.getServer().dispatchCommand(sender, "world 0");
return;
}
for (World world : Bukkit.getWorlds())
{
if (world.getName().equalsIgnoreCase(targetworld))
{
sender.sendMessage(ChatColor.GRAY + "Going to world: " + targetworld);
Bukkit.getServer().dispatchCommand(sender, "mv tp " + targetworld);
return;
}
}
}
else
{
2011-10-16 06:00:37 +00:00
sender.sendMessage(TotalFreedomMod.NOT_FROM_CONSOLE);
2011-10-13 23:07:52 +00:00
}
}
public static void buildHistory(Location location, int length, TFM_UserInfo playerdata)
{
Block center_block = location.getBlock();
for (int x_offset = -length; x_offset <= length; x_offset++)
{
for (int y_offset = -length; y_offset <= length; y_offset++)
{
for (int z_offset = -length; z_offset <= length; z_offset++)
{
Block block = center_block.getRelative(x_offset, y_offset, z_offset);
playerdata.insertHistoryBlock(block.getLocation(), block.getType());
}
}
}
}
public static void generateCube(Location location, int length, Material material)
{
Block center_block = location.getBlock();
for (int x_offset = -length; x_offset <= length; x_offset++)
{
for (int y_offset = -length; y_offset <= length; y_offset++)
{
for (int z_offset = -length; z_offset <= length; z_offset++)
{
center_block.getRelative(x_offset, y_offset, z_offset).setType(material);
}
}
}
}
2011-10-14 05:31:21 +00:00
2011-10-13 23:07:52 +00:00
public static void setWorldTime(World world, long ticks)
{
long time = world.getTime();
time -= time % 24000;
world.setTime(time + 24000 + ticks);
}
2011-10-14 05:31:21 +00:00
2011-10-13 23:07:52 +00:00
public static void createDefaultConfiguration(String name, TotalFreedomMod tfm, File plugin_file)
{
File actual = new File(tfm.getDataFolder(), name);
if (!actual.exists())
{
log.info("[" + tfm.getDescription().getName() + "]: Installing default configuration file template: " + actual.getPath());
InputStream input = null;
try
{
JarFile file = new JarFile(plugin_file);
ZipEntry copy = file.getEntry(name);
if (copy == null)
{
log.severe("[" + tfm.getDescription().getName() + "]: Unable to read default configuration: " + actual.getPath());
return;
}
input = file.getInputStream(copy);
}
catch (IOException ioex)
{
log.severe("[" + tfm.getDescription().getName() + "]: Unable to read default configuration: " + actual.getPath());
}
if (input != null)
{
FileOutputStream output = null;
try
{
tfm.getDataFolder().mkdirs();
output = new FileOutputStream(actual);
byte[] buf = new byte[8192];
int length = 0;
while ((length = input.read(buf)) > 0)
{
output.write(buf, 0, length);
}
log.info("[" + tfm.getDescription().getName() + "]: Default configuration file written: " + actual.getPath());
}
catch (IOException ioex)
{
log.log(Level.SEVERE, "[" + tfm.getDescription().getName() + "]: Unable to write default configuration: " + actual.getPath(), ioex);
}
finally
{
try
{
if (input != null)
{
input.close();
}
}
catch (IOException ioex)
{
}
try
{
if (output != null)
{
output.close();
}
}
catch (IOException ioex)
{
}
}
}
}
}
2011-10-14 05:31:21 +00:00
2011-10-13 23:07:52 +00:00
public static boolean isUserSuperadmin(CommandSender user, TotalFreedomMod tfm)
{
try
{
if (!(user instanceof Player))
{
return true;
}
if (Bukkit.getOnlineMode())
{
2011-10-16 06:00:37 +00:00
if (tfm.superadmins.contains(user.getName().toLowerCase()))
2011-10-13 23:07:52 +00:00
{
return true;
}
}
Player p = (Player) user;
if (p != null)
{
InetSocketAddress ip_address_obj = p.getAddress();
if (ip_address_obj != null)
{
String user_ip = ip_address_obj.getAddress().toString().replaceAll("/", "").trim();
if (user_ip != null && !user_ip.isEmpty())
{
if (tfm.superadmin_ips.contains(user_ip))
{
return true;
}
}
}
}
}
catch (Exception ex)
{
log.severe("Exception in TFM_Util.isUserSuperadmin: " + ex.getMessage());
}
return false;
}
2011-10-24 02:43:52 +00:00
public static boolean checkPartialSuperadminIP(String user_ip, TotalFreedomMod tfm)
{
user_ip = user_ip.trim();
2011-11-04 23:14:17 +00:00
2011-10-24 02:43:52 +00:00
if (tfm.superadmin_ips.contains(user_ip))
{
return true;
}
else
{
String[] user_octets = user_ip.split("\\.");
if (user_octets.length != 4)
{
return false;
}
2011-11-04 00:12:34 +00:00
String match_ip = null;
2011-10-24 02:43:52 +00:00
for (String test_ip : tfm.superadmin_ips)
{
String[] test_octets = test_ip.split("\\.");
if (test_octets.length == 4)
{
if (user_octets[0].equals(test_octets[0]) && user_octets[1].equals(test_octets[1]) && user_octets[2].equals(test_octets[2]))
{
2011-11-04 00:12:34 +00:00
match_ip = test_ip;
2011-10-24 02:43:52 +00:00
break;
}
}
}
2011-11-04 23:14:17 +00:00
2011-11-04 00:12:34 +00:00
if (match_ip != null)
2011-10-24 02:43:52 +00:00
{
tfm.superadmin_ips.add(user_ip);
2011-11-04 23:14:17 +00:00
2011-11-04 00:12:34 +00:00
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(tfm.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
2011-11-04 23:14:17 +00:00
2011-11-04 00:12:34 +00:00
fileloop:
for (String user : config.getKeys(false))
{
List<String> user_ips = config.getStringList(user);
for (String ip : user_ips)
{
ip = ip.toLowerCase().trim();
if (ip.equals(match_ip))
{
log.info("New IP '" + user_ip + "' matches old IP '" + match_ip + "' via partial match, adding it to superadmin list.");
user_ips.add(user_ip);
config.set(user, user_ips);
break fileloop;
}
}
}
2011-11-04 23:14:17 +00:00
2011-11-04 00:12:34 +00:00
try
{
config.save(new File(tfm.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
}
catch (IOException ex)
{
log.log(Level.SEVERE, null, ex);
}
2011-10-24 02:43:52 +00:00
}
2011-11-04 23:14:17 +00:00
2011-11-04 00:12:34 +00:00
return match_ip != null;
2011-10-24 02:43:52 +00:00
}
}
2011-10-16 06:00:37 +00:00
public static int wipeDropEntities(boolean wipe_tnt)
2011-10-13 23:07:52 +00:00
{
int removed = 0;
for (World world : Bukkit.getWorlds())
{
for (Entity ent : world.getEntities())
{
2011-10-16 06:00:37 +00:00
if (ent instanceof Arrow || (ent instanceof TNTPrimed && wipe_tnt) || ent instanceof Item || ent instanceof ExperienceOrb)
2011-10-13 23:07:52 +00:00
{
ent.remove();
removed++;
}
}
}
return removed;
}
2011-10-14 05:31:21 +00:00
public static boolean deleteFolder(File file)
{
if (file.exists())
{
if (file.isDirectory())
{
for (File f : file.listFiles())
{
if (!TFM_Util.deleteFolder(f))
{
return false;
}
}
}
file.delete();
return !file.exists();
}
else
{
return false;
}
}
2011-10-24 02:43:52 +00:00
public static CreatureType getCreatureType(String mobname)
{
return TFM_Util.mobtypes.get(mobname.toLowerCase().trim());
}
2011-11-04 23:14:17 +00:00
public static void zip(File directory, File zipfile, boolean verbose, CommandSender sender) throws IOException
{
URI base = directory.toURI();
Deque<File> queue = new LinkedList<File>();
queue.push(directory);
OutputStream out = new FileOutputStream(zipfile);
Closeable res = out;
try
{
ZipOutputStream zout = new ZipOutputStream(out);
res = zout;
while (!queue.isEmpty())
{
directory = queue.pop();
for (File kid : directory.listFiles())
{
String name = base.relativize(kid.toURI()).getPath();
if (kid.isDirectory())
{
queue.push(kid);
name = name.endsWith("/") ? name : name + "/";
zout.putNextEntry(new ZipEntry(name));
}
else
{
zout.putNextEntry(new ZipEntry(name));
copy(kid, zout);
zout.closeEntry();
}
2011-11-04 23:14:17 +00:00
if (verbose)
{
sender.sendMessage("Zipping: " + name);
}
}
}
}
finally
{
res.close();
}
}
public static void unzip(File zipfile, File directory) throws IOException
{
2011-11-04 23:14:17 +00:00
ZipFile zfile = new ZipFile(zipfile);
Enumeration<? extends ZipEntry> entries = zfile.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = entries.nextElement();
File file = new File(directory, entry.getName());
if (entry.isDirectory())
{
file.mkdirs();
}
else
{
file.getParentFile().mkdirs();
InputStream in = zfile.getInputStream(entry);
try
{
copy(in, file);
}
finally
{
in.close();
}
}
}
}
private static void copy(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
while (true)
{
int readCount = in.read(buffer);
if (readCount < 0)
{
break;
}
out.write(buffer, 0, readCount);
}
}
private static void copy(File file, OutputStream out) throws IOException
{
InputStream in = new FileInputStream(file);
try
{
copy(in, out);
}
finally
{
in.close();
}
}
private static void copy(InputStream in, File file) throws IOException
{
OutputStream out = new FileOutputStream(file);
try
{
copy(in, out);
}
finally
{
out.close();
}
}
public static boolean isStopCommand(String command)
2011-11-07 13:11:13 +00:00
{
return stop_commands.contains(command.toLowerCase());
2011-11-07 13:11:13 +00:00
}
enum EjectMethod
{
STRIKE_ONE, STRIKE_TWO, STRIKE_THREE;
}
public static void autoEject(Player p, String kickMessage)
2011-11-07 13:11:13 +00:00
{
EjectMethod method = EjectMethod.STRIKE_ONE;
String player_ip = null;
try
{
player_ip = p.getAddress().getAddress().toString().replaceAll("/", "").trim();
Integer num_kicks = eject_tracker.get(player_ip);
if (num_kicks == null)
{
num_kicks = new Integer(0);
}
num_kicks = new Integer(num_kicks.intValue() + 1);
if (num_kicks <= 1)
{
method = EjectMethod.STRIKE_ONE;
}
else if (num_kicks == 2)
{
method = EjectMethod.STRIKE_TWO;
}
else if (num_kicks >= 3)
{
method = EjectMethod.STRIKE_THREE;
}
}
catch (Throwable ex)
{
}
p.setOp(false);
p.setGameMode(GameMode.SURVIVAL);
p.getInventory().clear();
p.kickPlayer(kickMessage);
switch (method)
{
case STRIKE_ONE:
{
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format("tempban %s 1m", p.getName()));
break;
}
case STRIKE_TWO:
{
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format("tempban %s 3m", p.getName()));
break;
}
case STRIKE_THREE:
{
Bukkit.banIP(player_ip);
Bukkit.getOfflinePlayer(p.getName()).setBanned(true);
break;
}
}
2011-11-07 13:11:13 +00:00
}
2011-10-13 23:07:52 +00:00
}