I first created the inventory that will be opened:
private void openGUI(Player player) {
Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]");
ItemStack accept = new ItemStack(Material.EMERALD_BLOCK);
ItemMeta acceptMeta = accept.getItemMeta();
ItemStack decline = new ItemStack(Material.REDSTONE_BLOCK);
ItemMeta declineMeta = decline.getItemMeta();
acceptMeta.setDisplayName(ChatColor.GREEN + "Accept!");
accept.setItemMeta(acceptMeta);
declineMeta.setDisplayName(ChatColor.RED + "Decline!");
decline.setItemMeta(declineMeta);
inv.setItem(3, accept);
inv.setItem(5, decline);
player.openInventory(inv);
}
Then I created the command that will be run:
if (cmd.getName().equalsIgnoreCase("duel")) {
if (!(args.length == 1)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
return true;
} else if (args.length == 1) {
Player p = Bukkit.getServer().getPlayer(args[0]);
if (p != null) {
if (p.equals(sender)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
return true;
} else {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You sent a duel request to " + ChatColor.BLUE + p.getName());
p.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You recieved a duel request from " + ChatColor.BLUE + sender.getName());
openGUI(p);
}
}
} else {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Player not found!");
}
}
return true;
I Then created an event that is called when a players clicks their inventory.
#SuppressWarnings("deprecation")
#EventHandler
private void onClick(InventoryClickEvent e) {
if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("[DuelRequest]"))
return;
Player player = (Player) e.getWhoClicked();
e.setCancelled(true);
switch (e.getCurrentItem().getType()) {
case EMERALD_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You accepted the request");
player.getInventory().setItem(0, new ItemStack(Material.DIAMOND_SWORD));
player.getInventory().setItem(1, new ItemStack(Material.BOW));
player.getInventory().setItem(2, new ItemStack(Material.GOLDEN_APPLE, 2));
player.getInventory().setItem(3, new ItemStack(Material.ARROW, 32));
player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
break;
case REDSTONE_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You denied the request!");
break;
default:
player.closeInventory();
break;
}
}
So, what i want to happen is..
When a player types /duel , the (target) will have an inventory opened in front of them with an option to accept or decline the duel request. I have tested this and it works as expected. If they decline they will receive a message telling them they rejected it, if they accept they will be given a Kit to duel with. This also works as expected. However, i need to be able to give the sender of the /duel command the same kit, but i don't know how to. So... When the (target) accepts the duel, both players will receive a kit so they can both fight.
Is there a way of calling for the onClick event inside the onCommand?
Or is there a way to contact the sender of the command from inside the onClick event?
I am average with Bukkit coding but obviously still have a lot to learn so any help/constructive criticism is always helpful!
You could use a HashMap, to store the player that sent the duel request to a player:
Map<UUID, UUID> duels = new HashMap<UUID, UUID>();
HashMaps are used to store a value for another value, it's effectively a large collection of variables.
When the sender runs the command, you could use:
duels.put(target.getUniqueId(), sender.getUniqueId());
So, when the target accepts the duel, you could get the sender of the request with:
duels.get(target.getUniqueId());
Here's what your onCommand() could look like:
if (cmd.getName().equalsIgnoreCase("duel")) {
if (!(args.length == 1)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
return true;
} else if (args.length == 1) {
Player p = Bukkit.getServer().getPlayer(args[0]);
if (p != null) {
if (p.equals(sender)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
return true;
} else {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You sent a duel request to " + ChatColor.BLUE + p.getName());
p.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You recieved a duel request from " + ChatColor.BLUE + sender.getName());
openGUI(p);
//Put the player in the HashMap here
duels.put(p.getUniqueId(), ((Player) sender).getUniqueId());
}
}
} else {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Player not found!");
}
}
return true;
And your onClick() could look like this:
#SuppressWarnings("deprecation")
#EventHandler
//It's better if you make your events public
//and not private
public void onClick(InventoryClickEvent e) {
if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("[DuelRequest]"))
return;
Player player = (Player) e.getWhoClicked();
e.setCancelled(true);
//get the UUID stored in the duels HashMap for the player's UUID as a key
UUID uuid = duels.get(player.getUniqueId());
//Get the challenger from the UUID above
Player challenger = Bukkit.getPlayer(uuid);
switch (e.getCurrentItem().getType()) {
case EMERALD_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.GREEN + " You accepted the request");
player.getInventory().setItem(0, new ItemStack(Material.DIAMOND_SWORD));
player.getInventory().setItem(1, new ItemStack(Material.BOW));
player.getInventory().setItem(2, new ItemStack(Material.GOLDEN_APPLE, 2));
player.getInventory().setItem(3, new ItemStack(Material.ARROW, 32));
player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
//give the kit to the challenger here
//Maybe make a method, giveKit(Player p), to clean up your code
//Then you could run giveKit(challenger) and giveKit(player)
break;
case REDSTONE_BLOCK:
player.closeInventory();
player.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You denied the request!");
//tell the challenger that player denied their duel request
challenger.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + player.getName + " denied your duel request");
break;
default:
player.closeInventory();
break;
}
}
If you wanted to check if a UUID exists in the HashMap, you could use duels.containsKey(uuid)
Make sure to never store Player objects inside of HashMaps, because it can cause memory leaks if the player leaves the server, for instance. The best way to store player information is by storing their UUID, or by storing their username.
Related
I have since updated this code per suggestions for this forum. I am still confused as to how to get my .txt file selection to out print all instances of the name entered. my file in which all my .txt files are contained is named, namesbystate. To access this and all instances of the names entered are where I am getting issues. I am wondering if I replace myFile with namesbystate as a pathway extension or not?
package babynamestatesocial;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class BabyNameStateSocial {
private Scanner x;
public static void main(String[] args) throws FileNotFoundException {
// Scanner variable set up to intake user input for state selection and person's name
Scanner scan = new Scanner(System.in);
System.out.println("Available state files are: \n" +
"AK " + "AL " + "AR " + "AZ " + "CA " + "CO " + "\n" +
"CT " + "DC " + "DE " + "FL " + "GA " + "HI " + "\n" +
"IA " + "ID " + "IL " + "IN " + "KS " + "KY " + "\n" +
"LA " + "MA " + "MD " + "ME " + "MI " + "MN " + "\n" +
"MO " + "MS " + "MT " + "NC " + "ND " + "NE " + "\n" +
"NH " + "NJ " + "NM " + "NV " + "NY " + "OH " + "\n" +
"OK " + "OR " + "PA " + "RI " + "SC " + "SD " + "\n" +
"TN " + "TX " + "UT " + "VA " + "VT " + "WA " + "\n" +
"WI " + "WV " + "WY " + "\n");
System.out.print("Enter a state to read names from: " + "\n");
String filename = scan.nextLine() + ".txt";
System.out.println("Which name would you like to look up?");
String personName = scan.nextLine();
File myFile = new File(filename);
openFile(myFile,personName);
}
private static void openFile(File myFile, String personName){
try {
Scanner sc = new Scanner(myFile);
while (sc.hasNext()) {
// nextLine variable now has the line from the file in it that matches the name the person input
String nextLine = sc.nextLine();
if (nextLine.contains(personName)) {
}
}
} catch(FileNotFoundException e) {
System.out.print(e.getMessage());
}
}
}
Something like this will get you to where you want to be. I do not have the format of your state text files so I couldn't write the full program for you
(Edit - I just changed the code slightly. Instead of sc.next(), I should have written sc.nextLine(). The following program runs successfully with that edit):
package babynamestatesocial;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class BabyNameStateSocial {
private Scanner x;
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
System.out.println("Available state files are: \n" +
"AK " + "AL " + "AR " + "AZ " + "CA " + "CO " + "\n" +
"CT " + "DC " + "DE " + "FL " + "GA " + "HI " + "\n" +
"IA " + "ID " + "IL " + "IN " + "KS " + "KY " + "\n" +
"LA " + "MA " + "MD " + "ME " + "MI " + "MN " + "\n" +
"MO " + "MS " + "MT " + "NC " + "ND " + "NE " + "\n" +
"NH " + "NJ " + "NM " + "NV " + "NY " + "OH " + "\n" +
"OK " + "OR " + "PA " + "RI " + "SC " + "SD " + "\n" +
"TN " + "TX " + "UT " + "VA " + "VT " + "WA " + "\n" +
"WI " + "WV " + "WY " + "\n");
System.out.print("Enter a state to read names from: " + "\n");
String filename = scan.nextLine() + ".txt";
System.out.println("Which name would you like to look up?");
String personName = scan.nextLine();
File myFile = new File(filename);
openFile(myFile,personName);
}
private static void openFile(File myFile, String personName){
try {
Scanner sc = new Scanner(myFile);
while (sc.hasNext()) {
String nextLine = sc.nextLine();
if (nextLine.contains(personName)) {
//nextLine variable now has the line from the file in it that matches the name the person input so you need to parse that line and do something with it
}
}
} catch(Exception e) {
System.out.print(e.getMessage());
}
}
}
I am trying to drag & drop 2 elements into my workspace but they are dropped over each other, how can i specify the position of dropping the elements?
I am using dragAndDrop() function
Actions act=new Actions(driver);
act.dragAndDrop(From, To).build().perform();
enter image description here
Using dragAndDrop() method you can perform drag and drop operations on only one location at a time. it is usually used to perform operations on web element which is capable of dropping. please find the reference http://demoqa.com/droppable/ for various types of dropable elements.
To implement drag and drop operation -
Example:
WebElement From=driver.findElement(By.xpath( <<source xpath>> ));
WebElement To=driver.findElement(By.xpath( <<destination xpath>> ));
Actions actions=new Actions(driver);
actions.dragAndDrop(From, To).build().perform();
to drag and drop another element, you need to perform above all steps again.
Hope this helps :)
Try Using below code :
public static void dragAndDropViaJQueryHelper(WebDriver driver, String dragSourceJQuerySelector, String dropTargetJQuerySelector) {
String jqueryScript = "(function( jquery ) {\r\n" +
" jquery.fn.simulateDragDrop = function(options) {\r\n" +
" return this.each(function() {\r\n" +
" new jquery.simulateDragDrop(this, options);\r\n" +
" });\r\n" +
" };\r\n" +
" jquery.simulateDragDrop = function(elem, options) {\r\n" +
" this.options = options;\r\n" +
" this.simulateEvent(elem, options);\r\n" +
" };\r\n" +
" jquery.extend(jquery.simulateDragDrop.prototype, {\r\n" +
" simulateEvent: function(elem, options) {\r\n" +
" /*Simulating drag start*/\r\n" +
" var type = 'dragstart';\r\n" +
" var event = this.createEvent(type);\r\n" +
" this.dispatchEvent(elem, type, event);\r\n" +
"\r\n" +
" /*Simulating drop*/\r\n" +
" type = 'drop';\r\n" +
" var dropEvent = this.createEvent(type, {});\r\n" +
" dropEvent.dataTransfer = event.dataTransfer;\r\n" +
" this.dispatchEvent(jquery(options.dropTarget)[0], type, dropEvent);\r\n" +
"\r\n" +
" /*Simulating drag end*/\r\n" +
" type = 'dragend';\r\n" +
" var dragEndEvent = this.createEvent(type, {});\r\n" +
" dragEndEvent.dataTransfer = event.dataTransfer;\r\n" +
" this.dispatchEvent(elem, type, dragEndEvent);\r\n" +
" },\r\n" +
" createEvent: function(type) {\r\n" +
" var event = document.createEvent(\"CustomEvent\");\r\n" +
" event.initCustomEvent(type, true, true, null);\r\n" +
" event.dataTransfer = {\r\n" +
" data: {\r\n" +
" },\r\n" +
" setData: function(type, val){\r\n" +
" this.data[type] = val;\r\n" +
" },\r\n" +
" getData: function(type){\r\n" +
" return this.data[type];\r\n" +
" }\r\n" +
" };\r\n" +
" return event;\r\n" +
" },\r\n" +
" dispatchEvent: function(elem, type, event) {\r\n" +
" if(elem.dispatchEvent) {\r\n" +
" elem.dispatchEvent(event);\r\n" +
" }else if( elem.fireEvent ) {\r\n" +
" elem.fireEvent(\"on\"+type, event);\r\n" +
" }\r\n" +
" }\r\n" +
" });\r\n" +
"})(jQuery);";
((JavascriptExecutor) driver).executeScript(jqueryScript);
String dragAndDropScript = "jQuery('" + dragSourceJQuerySelector + "').simulateDragDrop({ dropTarget: '" + dropTargetJQuerySelector + "'});";
((JavascriptExecutor) driver).executeScript(dragAndDropScript);
}
Just pass css or xpath selectors in parameters.
Hope that helps you.
If that does not help you I can help you out with some other solutions also.
I asked a similar question to this today, but I need help with further development.
The HashMaps:
Map<UUID, UUID> duels = new HashMap<UUID, UUID>();
Map<UUID, UUID> selecting = new HashMap<UUID, UUID>();
The command:
if (cmd.getName().equalsIgnoreCase("duel")) {
if (!(args.length == 1)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
return true;
} else if (args.length == 1) {
Player p = Bukkit.getServer().getPlayer(args[0]);
if (p != null) {
if (p.equals(sender)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
return true;
} else {
if (duels.containsKey(p) || duels.containsKey(sender)) {
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.RED + "Either you or " + ChatColor.BLUE + p.getName() + ChatColor.RED + " are already in a duel!");
return true;
} else
openKitSelector((Player) sender);
selecting.put(p.getUniqueId(), ((Player) sender).getUniqueId());
The KitSelector inventory:
public void openKitSelector(Player p) {
Inventory selector = Bukkit.createInventory(p, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "KitSelector" + ChatColor.DARK_RED + "]");
ItemStack diamond = new ItemStack(Material.DIAMOND_SWORD);
ItemMeta diamondMeta = diamond.getItemMeta();
diamondMeta.setDisplayName(ChatColor.DARK_AQUA + "Diamond Kit");
diamond.setItemMeta(diamondMeta);
ItemStack iron = new ItemStack(Material.IRON_SWORD);
ItemMeta ironMeta = iron.getItemMeta();
ironMeta.setDisplayName(ChatColor.DARK_GREEN + "Iron Kit");
iron.setItemMeta(ironMeta);
selector.setItem(0, diamond);
selector.setItem(1, iron);
p.openInventory(selector);
}
The Accept/Deny inventory:
private void openGUI(Player player) {
Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]");
ItemStack accept = new ItemStack(Material.EMERALD_BLOCK);
ItemMeta acceptMeta = accept.getItemMeta();
ItemStack decline = new ItemStack(Material.REDSTONE_BLOCK);
ItemMeta declineMeta = decline.getItemMeta();
acceptMeta.setDisplayName(ChatColor.GREEN + "Accept!");
accept.setItemMeta(acceptMeta);
declineMeta.setDisplayName(ChatColor.RED + "Decline!");
decline.setItemMeta(declineMeta);
inv.setItem(3, accept);
inv.setItem(5, decline);
player.openInventory(inv);
}
So what I need to happen is, when someone types /duel <player> , they will get an inventory up with a selection of items that will give them kits, which I have done correctly and tested. Then when they select a kit, the target of the /duel command will get an inventory with an accept item and decline item.
I got everything working up to the part were the target gets the inventory for accept or deny as I am not really experienced with HashMaps, and I am a little confused.
If you wanted to get the target of a duel request from the selecting HashMap, you could use:
UUID id = selecting.get(player.getUniqueId());
Player target = Bukkit.getPlayer(id);
So, if you wanted to open the kit selector for the target, you could use:
//get the UUID of the Player that is being targeted by the Player player
UUID id = selecting.get(player.getUniqueId());
//get the target Player from the UUID above
Player target = Bukkit.getPlayer(id);
//open the kit selector for the target Player above
openKitSelector(target);
To open the target's inventory after the duel requester has selected their kit, you would have to listen for InventoryCloseEvent:
#EventHandler
public void inventoryClose(InventoryCloseEvent e){
//called when a player closes their inventory
}
So, your code could look like this:
#EventHandler
public void inventoryClose(InventoryCloseEvent e){
//check if the inventory is the duel inventory
if(e.getInventory().getName().equals(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "DuelRequest" + ChatColor.DARK_RED + "]"){
//get the player who opened the inventory
Player p = (Player) e.getWhoClicked();
//now, check if the selecting HashMap contains the Player above
if(selecting.containsKey(p.getUniqueId())){
//get the target of the duel
Player target = Bukkit.getPlayer(selecting.get(p.getUniqueId()));
//open the kit selector for the target Player above
openKitSelector(target);
//now, remove the target's, and the sender's UUID from the selecting
//HashMap, to make sure that we don't accidentally open the kit
//selector for a player who is currently in a duel.
selecting.remove(p.getUniqueId());
//make sure the selecting HashMap contains the target's UUID before
//attempting to remove it
if(selecting.containsKey(target.getUniqueId())){
selecting.remove(target.getUniqueId());
}
}
}
}
If you wanted to make sure that the player clicks something in the inventory, you could use InventoryClickEvent:
#EventHandler
public void inventoryClick(InventoryClickEvent e){
//called when a player clicks something in a inventory
}
Also, you will have to switch around the order in which items are in the HashMap. Instead of using the target as the key, use the sender as the key:
selecting.put(((Player) sender).getUniqueId(), p.getUniqueId());
So, your onCommand could look like this:
if(cmd.getName().equalsIgnoreCase("duel")){
if(!(args.length == 1)){
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " Usage: /duel <Player>");
return true;
}
else if (args.length == 1){
Player p = Bukkit.getServer().getPlayer(args[0]);
if(p != null){
if(p.equals(sender)){
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "]" + ChatColor.RED + " You cannot duel yourself!");
return true;
}
else{
if(duels.containsKey(p) || duels.containsKey(sender)){
sender.sendMessage(ChatColor.DARK_RED + "[" + ChatColor.DARK_GREEN + "HuntsCraft" + ChatColor.DARK_RED + "] " + ChatColor.RED + "Either you or " + ChatColor.BLUE + p.getName() + ChatColor.RED + " are already in a duel!");
return true;
}
else{
openKitSelector((Player) sender);
//this line was changed from
//selecting.put(p.getUniqueId(), ((Player) sender).getUniqueId())
//to the new value, with the sender as the key,
//and the target as the value.
selecting.put(((Player) sender).getUniqueId(), p.getUniqueId());
}
}
}
}
}
So I have some code which gives an error when executing...
if (!(sender.hasPermission("nexodus.an"))) {
player.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " You can not do this command!");
return true;
} else if (args.length == 0) {
player.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " You must specify a predetermined message to broadcast!");
player.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " 1: Server Restarting");
player.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " 2: Login servers are currently down!");
player.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " 3: Reloading for GameModes");
return true;
} else if (args.length > 1) {
if (args[0].equalsIgnoreCase("1")) {
Bukkit.broadcastMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " NexodusMC will be restarting in 5 seconds.");
} else if (args[0].equalsIgnoreCase("2")) {
Bukkit.broadcastMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " Login servers are currently down. Logging off my prevent you from getting back on, sorry!");
//Line above is line 53 ||
} else if (args[0].equalsIgnoreCase("3")) {
Bukkit.broadcastMessage(ChatColor.BLACK + "[" + ChatColor.DARK_PURPLE + "NexodusMC" + ChatColor.BLACK + "]" + ChatColor.GREEN + " Server reload due to adding/removing/editing gamemodes!");
}
return true;
}
Here is my error log.
org.bukkit.command.CommandException: Unhandled exception executing command 'an' in plugin NexodusHub vSwag
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at me.Bling.NexodusMain.Main.onCommand(Main.java:53) # 53 is the error line.
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
... 15 more
What am I doing wrong?
Fixed
Changed
} else if (args.length > 1) {
to
} else if (args.length > 0) {
Basically, I have code that uses the same few lines in different scenarios, and it makes the code a bit messy (especially since I probably overcomplicated what I made, but that's another issue). What I wanted to do is store that piece of code as another function and calling it in the longer one. WHich should work as far as I know, except, the longer function has variables that aren't set in the shorter one, and if they were, I'm pretty sure it would change the final result of the function.
Here is the longer code:
public static void combat(Character a,Character b){
int battleturn = 1;
int maxTurns=20;
int draw1 = 0;
//stop after 20 turns, or stop when one player has 0 HP.
while (a.health > 0 && b.health > 0 && maxTurns > 0){
/* run a round of combat*/
if (b.health < 0.25 * b.maxHealth){
if (b.getFlee(a)){
System.out.println(">>>>>>>>>>The enemy has fled successfully<<<<<<<<<<");
break;
}else{
System.out.println("Battle turn " + battleturn + ", <attack> or <flee>?");
Scanner input = new
Scanner(System.in);
String move = input.next();
while(!move.equals("attack") && !move.equals("flee")){
System.out.println("Error: Please input <attack> or <flee>.");
input = new Scanner(System.in);
move = input.next();
}
if (move.equals("attack")){
System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has "
+ b.getHealth() + "/" + b.getMaxHealth() + " health.");
System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
a.getHealth() + "/" + a.getMaxHealth() + " health");
maxTurns--;
battleturn++;
}else if(move.equals("flee")){
if (a.getFlee(b)){
draw1++;
System.out.println(">>>>>>>>>>You have fled!<<<<<<<<<<");
break;
}else{
System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has " +
b.getHealth() + "/" + b.getMaxHealth() + " health.");
System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
a.getHealth() + "/" + a.getMaxHealth() + " health");
maxTurns--;
battleturn++;
}
}
}
}else{
System.out.println("Battle turn " + battleturn + ", <attack> or <flee>?");
Scanner input = new
Scanner(System.in);
String move = input.next();
while(!move.equals("attack") && !move.equals("flee")){
System.out.println("Error: Please input <attack> or <flee>.");
input = new Scanner(System.in);
move = input.next();
}
if (move.equals("attack")){
System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name+ "." + " Enemy has " +
b.getHealth() + "/" + b.getMaxHealth() + "health.");
System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
a.getHealth() + "/" + a.getMaxHealth() + " health");
maxTurns--;
battleturn++;
}else if(move.equals("flee")){
if (a.getFlee(b)){
draw1++;
System.out.println(">>>>>>>>>>You have fled!<<<<<<<<<<");
break;
}else{
System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name+ "." + " Enemy has " +
b.getHealth() + "/" + b.getMaxHealth() + " health.");
System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
a.getHealth() + "/" + a.getMaxHealth() + " health");
maxTurns--;
battleturn++;
}
}
}
}
}
As you can see there is a part of code that is repeated, and that is.
System.out.println("Battle turn " + battleturn + ", <attack> or <flee>?");
Scanner input = new
Scanner(System.in);
String move = input.next();
while(!move.equals("attack") && !move.equals("flee")){
System.out.println("Error: Please input <attack> or <flee>.");
input = new Scanner(System.in);
move = input.next();
}
if (move.equals("attack")){
System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has "
+ b.getHealth() + "/" + b.getMaxHealth() + " health.");
System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
a.getHealth() + "/" + a.getMaxHealth() + " health");
maxTurns--;
battleturn++;
}else if(move.equals("flee")){
if (a.getFlee(b)){
draw1++;
System.out.println(">>>>>>>>>>You have fled!<<<<<<<<<<");
break;
}else{
System.out.println(a.name + " dealt " + a.combatRound(b) + " damage to " + b.name + "." + " Enemy has " +
b.getHealth() + "/" + b.getMaxHealth() + " health.");
System.out.println(b.name + " dealt " + b.combatRound(a) + " damage to " + a.name + "." + " You have " +
a.getHealth() + "/" + a.getMaxHealth() + " health");
maxTurns--;
battleturn++;
}
}
}
It won't compile if I set that chunk of code as a method, because it doesn't have the variables battleturn, maxturns, draw1, but if I put them in there, the amount of battle turns messes up.
Any ideas?
Java applications should be modular: each class fulfilling its own function, each method generally performing a single operation.
In a method you can use either class variables or its own local variables.
If you need several methods work with the same data, it should either be part of a class (instance and/or static variables) or passed to each method as parameters.
Make sure that you do not define class variables in a method. This will create local variables that will shadow class variables.
This might help you accomplish what you're trying to do.
private static void reportDamage(Character a,
Character b) {
System.out.println(a.name + " dealt "
+ a.combatRound(b) + " damage to " + b.name
+ "." + " Enemy has " + b.getHealth() + "/"
+ b.getMaxHealth() + " health.");
}
I suggest changing your combat method like this.
int battleturn = 0;
int maxTurns = 20;
// stop after 20 turns, or stop when one player has 0
// HP.
Scanner input = new Scanner(System.in);
try {
while (a.health > 0 && b.health > 0
&& battleturn < maxturn) {
battleturn++;
/* run a round of combat */
if (b.getFlee(a)) {
System.out.println(">>>>>>>>>>"
+ "The enemy has fled successfully"
+ "<<<<<<<<<<");
break;
} else {
System.out.println("Battle turn "
+ battleturn + ", <attack> or <flee>?");
boolean isFlee = false;
boolean isAttack = false;
String move = input.next();
for (;;) {
isAttack = "attack".equalsIgnoreCase(move);
isFlee = "flee".equalsIgnoreCase(move);
if (isFlee || isAttack) {
break;
}
System.out.println("Error: "
+ "Please input <attack> or <flee>.");
move = input.next();
}
if (isAttack) {
reportDamage(a, b);
reportDamage(b, a);
} else { // isFlee
if (a.getFlee(b)) {
System.out.println(">>>>>>>>>>"
+ "You have fled successfully"
+ "<<<<<<<<<<");
break;
} else {
// b is fleeing.
// reportDamage(a, b);
reportDamage(b, a);
}
}
}
}
} finally {
input.close();
}