Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/ClojureProjectResolveSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/CodeMetropolis-sz10.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

458 changes: 458 additions & 0 deletions .idea/dbnavigator.xml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added demo-sz10
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@

public class Plugin extends JavaPlugin {


@Override
public void onEnable() {
//Plugin startup
System.out.println("[CodeMetropolis] Your teleport plugin has started");
//Message in the Spigot server runner terminal, tells the user the plugin has successfully started
getCommand("tpToBuilding").setExecutor(new Teleport());
//Teleport command executor
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package codemetropolis.integration.spigot.plugin;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;


/**
* @author Crunchify.com
* @source https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/
* This file gets the path of the XML file from config.properties
*/
public class CrunchifyGetPropertyValues {
String result = "";
InputStream inputStream;

public String getPropValues() throws IOException {

try {
Properties prop = new Properties();
String propFileName = "config.properties";

inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}

Date time = new Date(System.currentTimeMillis());

// get the property value and print it out
String url = prop.getProperty("url");


result = url;
System.out.println(result + " <=\n");
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return result;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package codemetropolis.integration.spigot.plugin;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
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 java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import java.io.File;
public class Teleport implements CommandExecutor {
//Class of teleport command

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
//Actions of the command

if(sender instanceof Player){
Player player = (Player) sender;
//Teleporting action can only happen to Players

if(args.length == 0){
player.sendMessage(ChatColor.RED + "Invalid Argument");
player.sendMessage(ChatColor.BLUE + "Usage: '/tptobuilding' + Name of preferred building");
//If the user does not provide arguments, alert messages appear in the chat

}else if(args.length >= 1){
try {
String conc = "";
for(int i = 0; i < args.length; i++) {
System.out.println(args[i]);
conc += (i != 0 ? " " + args[i] : args[i]);
}
fun(conc, player);
//Concatenating arguments given by user

}catch(Exception e) {
//Block of code to handle errors
}
}
}
return true;
}

public static void fun(String s, Player player) throws Exception {
//Method for getting coordinates from XML file (Coordinates needed to achieve teleport)

CrunchifyGetPropertyValues properties = new CrunchifyGetPropertyValues();
System.out.println(properties.getPropValues() + " ");
//Gets the path of the XML file which contains the coordinates

try {
Document document = getDocument(properties.getPropValues());
String xpathExpression = "/buildables/buildable[contains(@name, '" + s + "')]/position/@x";
//Using XPath expressions, a String variable is created containing the String given by the user command and the node names we are looking for inside the XML file (in this case the 'x' coordinate node)
String wxpathExpression = "/buildables/buildable/@id";
//Using XPath expressions, a String variable is created containing the node name we are looking for inside the XML file (in this case the 'id' node)

List<String> evaluatedArray = new ArrayList<String>();
//Array is created to store String of coordinates
List<String> coordinateList = new ArrayList<String>();
//Array is created to store final coordinates
evaluatedArray = evaluateXPath(document, wxpathExpression);
//Call of evaluateXPath method on the XML file
int found = 0;
//An int variable is created to indicate if the user's string was found in the XML
while (!evaluatedArray.isEmpty()) {

List<String> temp = new ArrayList<String>();
//A temporary array is created to store coordinates
temp = evaluateXPath(document, xpathExpression);
//Coordinates are loaded into the temporary array

if (!temp.isEmpty()) {
//Loops through the coordinates, cutting useless characters, leaving coordinate numbers and adding them to the final coordinateList
coordinateList.add(temp.get(0));
//Puts x coordinate number into final coordinate list
coordinateList.add(evaluateXPath(document, (xpathExpression.split("\\@x", 2)[0] + "@y")).get(0));
//Puts y coordinate number into final coordinate list, splitting off any unwanted characters
coordinateList.add(evaluateXPath(document, (xpathExpression.split("\\@x", 2)[0] + "@z")).get(0));
//Puts z coordinate number into final coordinate list, splitting off any unwanted characters
World world = player.getWorld();
//Gets which world the player is in currently (imporart for the location of the teleport)
Location loc = new Location(world, Double.parseDouble(temp.get(0))-3, Double.parseDouble(evaluateXPath(document, (xpathExpression.split("\\@x", 2)[0] + "@y")).get(0)), Double.parseDouble(evaluateXPath(document, (xpathExpression.split("\\@x", 2)[0] + "@z")).get(0))+4, -90, -30);
//A location variable is created, which gets the x,y,z coordinates.
player.teleport(loc);
//Teleport action to the preferred location is executed
found = 1;
//A correct building was found, the teleport was succesful, no further action needed
break;
}

xpathExpression = xpathExpression.split("\\[contains\\(\\@", 2)[0] + "/children/buildable[contains(@" + xpathExpression.split("\\[contains\\(\\@", 2)[1];
wxpathExpression = wxpathExpression.split("\\/\\@", 2)[0] + "/children/buildable/@" + wxpathExpression.split("\\/\\@", 2)[1];
evaluatedArray = evaluateXPath(document, wxpathExpression);
}
if (found == 0) {
player.sendMessage(ChatColor.RED + "Invalid Argument: Cannot find specific building");
//If a correct building name was NOT found in the XML, the teleport was NOT succesful, the user gets an alert chat message
}
}catch(Exception e){
player.sendMessage(ChatColor.DARK_PURPLE + (properties.getPropValues()));
player.sendMessage(ChatColor.RED + ("Path of the XML file is incorrect. Please check the 'config.properties' file"));
//If the path of the XML file was set up incorrectly in the config file, the user gets alerting chat messages
}
}

private static List<String> evaluateXPath(Document document, String xpathExpression) throws Exception
{
// Create XPathFactory object
XPathFactory xpathFactory = XPathFactory.newInstance();

// Create XPath object
XPath xpath = xpathFactory.newXPath();

List<String> values = new ArrayList<>();
try
{
// Create XPathExpression object
XPathExpression expr = xpath.compile(xpathExpression);

// Evaluate expression result on XML document
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);

for (int i = 0; i < nodes.getLength(); i++) {
values.add(nodes.item(i).getNodeValue());
}

} catch (XPathExpressionException e) {
e.printStackTrace();
}
return values;
}

private static Document getDocument(String fileName) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fileName);
return doc;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
url=C:\\examplePath\\placingToRendering.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ main: codemetropolis.integration.spigot.plugin.Plugin
version: 0.1
api-version: 1.16
commands:
tpToBuilding:
description: Teleport Player To Specific Building