Advanced-Portals/src/com/sekwah/advancedportals/portals/Portal.java

483 lines
16 KiB
Java
Raw Normal View History

package com.sekwah.advancedportals.portals;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.AdvancedPortalsPlugin;
import com.sekwah.advancedportals.ConfigAccessor;
import com.sekwah.advancedportals.destinations.Destination;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
2015-11-15 23:52:29 +01:00
import org.bukkit.permissions.PermissionAttachment;
import java.util.Set;
import java.util.logging.Level;
public class Portal {
2015-11-15 23:52:29 +01:00
private static AdvancedPortalsPlugin plugin;
2015-11-15 23:52:29 +01:00
2015-11-09 23:53:19 +01:00
public static boolean portalsActive = false;
2015-11-15 23:52:29 +01:00
public static AdvancedPortal[] Portals;
private static boolean ShowBungeeMessage;
2015-11-15 23:52:29 +01:00
public Portal(AdvancedPortalsPlugin plugin) {
ConfigAccessor config = new ConfigAccessor(plugin, "Config.yml");
ShowBungeeMessage = config.getConfig().getBoolean("ShowBungeeWarpMessage");
Portal.plugin = plugin;
Portal.loadPortals();
}
/**
* This can be used to move the get keys to different sections
2015-11-15 23:52:29 +01:00
*
* ConfigurationSection section = config.getSection("sectionname");
2015-11-15 23:52:29 +01:00
*
* section.getKeys(false);
2015-11-15 23:52:29 +01:00
*
*/
2015-11-15 23:52:29 +01:00
public static void loadPortals(){
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
Set<String> PortalSet = config.getConfig().getKeys(false);
if(PortalSet.size() > 0){
Portals = new AdvancedPortal[PortalSet.toArray().length];
for(int i = 0; i <= PortalSet.toArray().length - 1; i++){
Portals[i] = new AdvancedPortal();
}
int portalId = 0;
for(Object portal: PortalSet.toArray()){
Material blockType = Material.PORTAL;
String BlockID = config.getConfig().getString(portal.toString() + ".triggerblock");
try
{
Integer.parseInt(BlockID);
System.out.println("Block names must be given not IDs");
}
catch(NumberFormatException e)
{
blockType = Material.getMaterial(BlockID);
}
if(blockType == null){
blockType = Material.PORTAL;
}
Portals[portalId].trigger = blockType;
Portals[portalId].portalName = portal.toString();
Portals[portalId].worldName = config.getConfig().getString(portal.toString() + ".world");
World world = Bukkit.getWorld(config.getConfig().getString(portal.toString() + ".world"));
Portals[portalId].pos1 = new Location(world, config.getConfig().getInt(portal.toString() + ".pos1.X"), config.getConfig().getInt(portal.toString() + ".pos1.Y"), config.getConfig().getInt(portal.toString() + ".pos1.Z"));
Portals[portalId].pos2 = new Location(world, config.getConfig().getInt(portal.toString() + ".pos2.X"), config.getConfig().getInt(portal.toString() + ".pos2.Y"), config.getConfig().getInt(portal.toString() + ".pos2.Z"));
portalId++;
}
portalsActive = true;
}
else{
portalsActive = false;
}
}
public static String create(Location pos1, Location pos2 , String name, String destination, Material triggerBlock, PortalArg... extraData) {
return create(pos1, pos2, name, destination, triggerBlock, null, extraData);
}
public static String create(Location pos1, Location pos2, String name, String destination, Material triggerBlock, String serverName, PortalArg... portalArgs) {
2015-11-15 23:52:29 +01:00
if(!pos1.getWorld().equals(pos2.getWorld())){
plugin.getLogger().log(Level.WARNING, "pos1 and pos2 must be in the same world!");
return "\u00A7cPortal creation error, pos1 and pos2 must be in the same world!";
}
2015-11-15 23:52:29 +01:00
int LowX = 0;
2015-11-15 23:52:29 +01:00
int LowY = 0;
int LowZ = 0;
2015-11-15 23:52:29 +01:00
int HighX = 0;
int HighY = 0;
int HighZ = 0;
2015-11-15 23:52:29 +01:00
if(pos1.getX() > pos2.getX()){
LowX = (int) pos2.getX();
HighX = (int) pos1.getX();
}
else{
LowX = (int) pos1.getX();
HighX = (int) pos2.getX();
}
if(pos1.getY() > pos2.getY()){
LowY = (int) pos2.getY();
HighY = (int) pos1.getY();
}
else{
LowY = (int) pos1.getY();
HighY = (int) pos2.getY();
}
if(pos1.getZ() > pos2.getZ()){
LowZ = (int) pos2.getZ();
HighZ = (int) pos1.getZ();
}
else{
LowZ = (int) pos1.getZ();
HighZ = (int) pos2.getZ();
}
2015-11-15 23:52:29 +01:00
Location checkpos1 = new Location(pos1.getWorld(), HighX, HighY, HighZ);
Location checkpos2 = new Location(pos2.getWorld(), LowX, LowY, LowZ);
2015-11-15 23:52:29 +01:00
if(checkPortalOverlap(checkpos1, checkpos2)){
plugin.getLogger().log(Level.WARNING, "Portals must not overlap!");
return "\u00A7cPortal creation error, portals must not overlap!";
}
2015-11-15 23:52:29 +01:00
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".world", pos1.getWorld().getName());
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".triggerblock", checkMaterial(triggerBlock));
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".destination", destination);
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".bungee", serverName);
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".pos1.X", HighX);
config.getConfig().set(name + ".pos1.Y", HighY);
config.getConfig().set(name + ".pos1.Z", HighZ);
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".pos2.X", LowX);
config.getConfig().set(name + ".pos2.Y", LowY);
config.getConfig().set(name + ".pos2.Z", LowZ);
2015-11-15 23:52:29 +01:00
for(PortalArg arg: portalArgs){
config.getConfig().set(name + ".portalArgs." + arg.argName, arg.value);
}
config.saveConfig();
2015-11-15 23:52:29 +01:00
loadPortals();
2015-11-15 23:52:29 +01:00
return "\u00A7aPortal creation successful!";
}
2015-11-15 23:52:29 +01:00
// make this actually work!
private static boolean checkPortalOverlap(Location pos1, Location pos2) {
2015-11-15 23:52:29 +01:00
if(portalsActive){
int portalId = 0;
for(@SuppressWarnings("unused") Object portal : Portal.Portals){
if(Portals[portalId].worldName.equals(pos2.getWorld().getName())){ // checks that the cubes arnt overlapping by seeing if all 4 corners are not in side another
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
2015-11-15 23:52:29 +01:00
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
2015-11-15 23:52:29 +01:00
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
2015-11-15 23:52:29 +01:00
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
2015-11-15 23:52:29 +01:00
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
2015-11-15 23:52:29 +01:00
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos1.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
2015-11-15 23:52:29 +01:00
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos1.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos2.getBlockZ())){return true;}
2015-11-15 23:52:29 +01:00
if(checkOverLapPortal(pos1, pos2, Portals[portalId].pos2.getBlockX(), Portals[portalId].pos2.getBlockY(), Portals[portalId].pos1.getBlockZ())){return true;}
}
portalId++;
}
}
return false;
}
private static boolean checkOverLapPortal(Location pos1, Location pos2, int posX, int posY, int posZ) {
2015-11-15 23:52:29 +01:00
if(pos1.getX() >= posX && pos1.getY() >= posX && pos1.getZ() >= posZ){
if((pos2.getX()) <= posX && pos2.getY() <= posY && pos2.getZ() <= posZ){
return true;
}
2015-11-15 23:52:29 +01:00
}
return false;
}
private static String checkMaterial(Material triggerBlock) {
if(triggerBlock.equals(Material.WATER)){
return "STATIONARY_WATER";
}
else if(triggerBlock.equals(Material.LAVA)){
return "STATIONARY_LAVA";
}
return triggerBlock.toString();
}
2015-11-15 23:52:29 +01:00
@SuppressWarnings("deprecation")
2015-11-15 23:52:29 +01:00
public static String create(Location pos1, Location pos2, String name, String destination, String serverName, PortalArg... extraData) { // add stuff for destination names or coordinates
ConfigAccessor config = new ConfigAccessor(plugin, "Config.yml");
2015-11-15 23:52:29 +01:00
Material triggerBlockType;
String BlockID = config.getConfig().getString("DefaultPortalTriggerBlock");
try
{
triggerBlockType = Material.getMaterial(Integer.parseInt(BlockID));
}
catch(Exception e)
{
triggerBlockType = Material.getMaterial(BlockID);
}
2015-11-15 23:52:29 +01:00
if(triggerBlockType == null){
triggerBlockType = Material.PORTAL;
}
2015-11-15 23:52:29 +01:00
// TODO add a for loop which scans through the addArgs and adds them to the config so that the application can use them
2015-11-15 23:52:29 +01:00
String result = create(pos1, pos2, name, destination, triggerBlockType, serverName, extraData);
return result;
}
2015-11-15 23:52:29 +01:00
public static void redefine(Location pos1, Location pos2, String name){
2015-11-15 23:52:29 +01:00
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".pos1.X", pos1.getX());
config.getConfig().set(name + ".pos1.Y", pos1.getY());
config.getConfig().set(name + ".pos1.Z", pos1.getZ());
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".pos2.X", pos2.getX());
config.getConfig().set(name + ".pos2.Y", pos2.getY());
config.getConfig().set(name + ".pos2.Z", pos2.getZ());
2015-11-15 23:52:29 +01:00
config.saveConfig();
2015-11-15 23:52:29 +01:00
loadPortals();
2015-11-15 23:52:29 +01:00
}
2015-11-15 23:52:29 +01:00
public static void remove(String name){
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
2015-11-15 23:52:29 +01:00
Object[] keys = config.getConfig().getKeys(true).toArray();
for(int i = keys.length - 1; i >= 0; i--){
String key = keys[i].toString();
if(key.startsWith(name + ".")){
config.getConfig().set(key, null);
}
}
config.getConfig().set(name, null);
// TODO add code to check if people have the portal selected and notify if removed.
2015-11-15 23:52:29 +01:00
/**Set<String> keys = config.getConfig().getKeys(true);
2015-11-15 23:52:29 +01:00
for(String key: keys){
if(key.startsWith(name)){
config.getConfig().set(key, null);
}
}*/
/**config.getConfig().set(name + ".world", null);
2015-11-15 23:52:29 +01:00
config.getConfig().set(name + ".triggerblock", null);
config.getConfig().set(name + ".destination", null);
config.getConfig().set(name + ".pos1.X", null);
config.getConfig().set(name + ".pos1.Y", null);
config.getConfig().set(name + ".pos1.Z", null);
config.getConfig().set(name + ".pos2.X", null);
config.getConfig().set(name + ".pos2.Y", null);
config.getConfig().set(name + ".pos2.Z", null);
config.getConfig().set(name + ".pos1", null);
config.getConfig().set(name + ".pos2", null);
config.getConfig().set(name, null);*/
config.saveConfig();
2015-11-15 23:52:29 +01:00
loadPortals();
}
2015-11-15 23:52:29 +01:00
public static boolean portalExists(String portalName){
2015-11-15 23:52:29 +01:00
ConfigAccessor portalconfig = new ConfigAccessor(plugin, "Portals.yml");
String posX = portalconfig.getConfig().getString(portalName + ".pos1.X");
2015-11-15 23:52:29 +01:00
if(posX == null){
return false;
}
else{
return true;
}
}
2015-11-15 23:52:29 +01:00
public static boolean activate(Player player, String portalName) {
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
2015-11-15 23:52:29 +01:00
// add other variables or filter code here, or somehow have a way to register them
2015-11-15 23:52:29 +01:00
if(config.getConfig().getString(portalName + ".bungee") != null){
if(ShowBungeeMessage){
player.sendMessage("\u00A7a[\u00A7eAdvancedPortals\u00A7a] Attempting to warp to \u00A7e" + config.getConfig().getString(portalName + ".bungee") + "\u00A7a.");
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Connect");
out.writeUTF(config.getConfig().getString(portalName + ".bungee"));
player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
return false;
}
else{
2015-11-15 23:52:29 +01:00
boolean showFailMessage = true;
if(config.getConfig().getString(portalName + ".portalArgs.command.1") != null){
showFailMessage = false;
int commandLine = 1;
String command = config.getConfig().getString(portalName + ".portalArgs.command." + commandLine);
do{
// (?i) makes the search case insensitive
command = command.replaceAll("@player", player.getName());
plugin.getLogger().log(Level.SEVERE, "Portal command: " + command);
if(command.startsWith("#")){
command = command.substring(1);
plugin.getLogger().log(Level.SEVERE, "Portal command: " + command);
plugin.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
else if(command.startsWith("!")){
command = command.substring(1);
boolean wasOp = player.isOp();
try{
player.setOp(true);
player.performCommand(command);
}
finally {
player.setOp(wasOp);
}
}
else if(command.startsWith("^")){
command = command.substring(1);
PermissionAttachment permissionAttachment = null;
try{
permissionAttachment = player.addAttachment(plugin, "*", true);
player.performCommand(command);
}
finally {
player.removeAttachment(permissionAttachment);
}
}
else{
player.performCommand(command);
}
command = config.getConfig().getString(portalName + ".portalArgs.command." + ++commandLine);
}while(command != null);
}
if(config.getConfig().getString(portalName + ".destination") != null){
ConfigAccessor configDesti = new ConfigAccessor(plugin, "Destinations.yml");
String destiName = config.getConfig().getString(portalName + ".destination");
2015-11-15 23:52:29 +01:00
String permission = config.getConfig().getString(portalName + ".portalArgs.permission");
if(permission == null || (permission != null && player.hasPermission(permission)) || player.isOp()){
if(configDesti.getConfig().getString(destiName + ".world") != null){
boolean warped = Destination.warp(player, destiName);
return warped;
}
else{
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The destination you are currently attempting to warp to doesnt exist!");
plugin.getLogger().log(Level.SEVERE, "The portal '" + portalName + "' has just had a warp "
+ "attempt and either the data is corrupt or that destination listed doesn't exist!");
return false;
}
}
else{
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] You do not have permission to use this portal!");
return false;
}
/*if(configDesti.getConfig().getString(destiName + ".world") != null){
String permission = config.getConfig().getString(portalName + ".portalArgs.permission");
if(permission == null || (permission != null && player.hasPermission(permission)) || player.isOp()){
boolean warped = Destination.warp(player, destiName);
return warped;
}
else{
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] You do not have permission to use this portal!");
return false;
}
}
else{
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The destination you are currently attempting to warp to doesnt exist!");
plugin.getLogger().log(Level.SEVERE, "The portal '" + portalName + "' has just had a warp "
+ "attempt and either the data is corrupt or that destination listed doesn't exist!");
return false;
2015-11-15 23:52:29 +01:00
}*/
}
else{
2015-11-15 23:52:29 +01:00
if(showFailMessage) {
player.sendMessage("\u00A7c[\u00A77AdvancedPortals\u00A7c] The portal you are trying to use doesn't have a destination!");
plugin.getLogger().log(Level.SEVERE, "The portal '" + portalName + "' has just had a warp "
+ "attempt and either the data is corrupt or portal doesn't exist!");
}
return false;
}
}
2015-11-15 23:52:29 +01:00
2015-08-18 01:03:56 +02:00
// add code for if the portal doesnt have a destination but a exemptPlayer location
2015-11-15 23:52:29 +01:00
}
2015-11-15 23:52:29 +01:00
public static void rename(String oldName, String newName){
2015-11-15 23:52:29 +01:00
// set it so it gets all data from one and puts it into another place
2015-11-15 23:52:29 +01:00
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
2015-11-15 23:52:29 +01:00
Set<String> keys = config.getConfig().getKeys(true);
for(String key: keys){
if(key.startsWith(oldName + ".")){
if(config.getConfig().getString(key) != null){
try
{
int intData = Integer.parseInt(config.getConfig().getString(key));
config.getConfig().set(key.replace(oldName + ".", newName + "."), intData);
}
catch(Exception e)
{
config.getConfig().set(key.replace(oldName + ".", newName + "."), config.getConfig().getString(key));
}
2015-11-15 23:52:29 +01:00
}
}
}
2015-11-15 23:52:29 +01:00
config.saveConfig();
2015-11-15 23:52:29 +01:00
remove(oldName);
2015-11-15 23:52:29 +01:00
}
2015-11-15 23:52:29 +01:00
2015-11-19 23:43:42 +01:00
public static boolean addCommand(String portalName, String portalCommand) {
ConfigAccessor config = new ConfigAccessor(plugin, "Portals.yml");
if(portalExists(portalName)){
int commandLine = 0;
while(config.getConfig().getString(portalName + ".portalArgs.command." + ++commandLine) != null); //Loops increasing commandLine till 1 is null
config.getConfig().set(portalName + ".portalArgs.command." + commandLine, portalCommand);
config.saveConfig();
loadPortals();
return true;
}
else{
return false;
}
}
}