Moving items from inventory to inventory

Discussion in 'Plugin Development' started by ice374, Apr 26, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    ice374

    Hey, i need help :/

    I cant figure out how to move an item from one players hand to the inventory of the player who types the command /gethand <player that you want the item in hand from>

    Thanks in advance :)
     
  2. Offline

    chasechocolate

    If I understand you right, this is what you would use:
    Code:java
    1. Player target = Bukkit.getPlayer(<PLAYER NAME>); //Can also use args[index] if in a command
    2. if(target != null){ //If they are online
    3. ItemStack targetHand = target.getItemInHand();
    4. player.getInventory().addItem(targetHand);
    5. }
     
    ice374 likes this.
  3. Offline

    ice374

    Yes, it needs to be a command though, how do you add that args stuff?

    I want to be able to type /gethand < whoever's item in hand i want>

    At the moment my code looks like this but i need that args stuff to work so it fetches the hand of the player that i specify with /gethand

    Code:java
    1. if (commandLabel.equalsIgnoreCase("gethand")) { // i need to specify the player name somewhere here
    2. Player target = Bukkit.getPlayer(<PLAYER NAME>);
    3. if(target != null){
    4. ItemStack targetHand = target.getItemInHand();
    5. player.getInventory().addItem(targetHand);
    6. }


    Thanks so much Chase, your help is really appreciated :)
     
  4. Offline

    chasechocolate

    Glad I could help you :) But anyways, here's how you would do it with the arguments:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("gethand")){
    2. if(args.length == 1){
    3. Player target = Bukkit.getPlayer(args[0]);
    4.  
    5. if(target != null){ //Target player is online
    6. ItemStack targetHand = target.getItemInHand();
    7. player.getInventory().addItem(targetHand);
    8. return true;
    9. } else {
    10. player.sendMessage(ChatColor.RED + "Could not find the player " + args[0] + "!");
    11. return true;
    12. }
    13. } else {
    14. player.sendMessage(ChatColor.RED + "Usage: /gethand <player>");
    15. return true;
    16. }
    17. }
     
    ice374 likes this.
  5. Offline

    ice374

    chasechocolate

    how do i make a player do into a mode or something when they type /abc
    and the only way they can come out of that mode is by someone else typing /def <the 1st players name> or 3 miutes passing?
     
  6. Offline

    chasechocolate

    ice374 add their name (player.getName()) to a List<String>, and on the /def command, get the player's name (args[0]) and check if it is in the list and if it is, remove it. For the removing after 3 minutes, just start a BukkitRunnable after you add their name in the /abc command. Example:
    Code:java
    1. new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. if(<YOUR LIST>.contains(player.getName())){
    5. <YOUR LIST>.remove(player.getName());
    6. }
    7. }
    8. }.runTaskLater(<MAIN CLASS INSTANCE>, 3600L); //20 ticks per second * 180 seconds = 3600 ticks
     
    ice374 likes this.
  7. Offline

    ice374

    chasechocolate

    ok here is the code that i have got so far, sorry to bug you again but i cant get the hang of it

    I need to make this plugin so that the item that player1 is holding goes to player2's inventory
    IF
    player1 has ALREADY typed /auction and THEN player2 types /bid <player1's name>

    what i meant by a mode was when player1 types /auction i want that player to go into a mode that can be picked up when player2 types /bid <player1's name>

    and if player1 hasn't typed /auction then player1 wouldnt be in that mode
    and if player2 was to type /bid <player1's name> and player1 wasnt in "the mode" then player2 would get told that.

    after player2 has taken the items from player1 i want the items that player1 is holding to be set to null and if
    none types /bid <player1's name> within 3 minutes i wanted the bid to time out.

    P.S. this plugin is for a private server so if you could email me the fixed up code to strifetamer@gmail.com
    I will then be able to remove the below code once you have read it and replied, thanks again Chase you are a legend and you should try for staff rank. (i'm pushing those like buttons on every thread you have helped me in, even ones i didn't start)

    Code:java
    1.  
    2.  
    3. package me.ice374.tregbay;
    4.  
    5. import java.util.ArrayList;
    6. import java.util.List;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Sound;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class tregbaymain extends JavaPlugin {
    18.  
    19. private List<Player> AuctionCDYes = new ArrayList<Player>();
    20. AuctionCooldown cd = new AuctionCooldown();
    21.  
    22. public boolean onCommand (CommandSender sender, Command command,
    23. String commandLabel, String[] args){
    24.  
    25. if (commandLabel.equalsIgnoreCase("auction")) {
    26. Player player1 = (Player) sender;
    27. if (!AuctionCDYes.contains(player1));{
    28. cd.setList(AuctionCDYes);
    29. cd.setPlayer (player1);
    30. new Thread(cd).start();
    31. ItemStack inHand = player1.getItemInHand();
    32. Bukkit.broadcastMessage(ChatColor.BLACK + "[" + ChatColor.RED + "Treg" + ChatColor.BLUE + "B" + ChatColor.YELLOW + "a" + ChatColor.DARK_GREEN + "y" + ChatColor.BLACK + "] "
    33. + ChatColor.RESET + player1.getName()
    34. + ChatColor.DARK_GRAY + " is Auctioning off "
    35. + ChatColor.GOLD + inHand.getAmount() + " "
    36. + ChatColor.GOLD + player1.getItemInHand().getType().toString().toLowerCase() + "!");
    37.  
    38. player1.sendMessage(" ");
    39. player1.sendMessage(ChatColor.BLACK + "[" + ChatColor.RED + "Treg" + ChatColor.BLUE + "B" + ChatColor.YELLOW + "a" + ChatColor.DARK_GREEN + "y" + ChatColor.BLACK + "] " + ChatColor.GOLD + "Your items are now up for auction!");
    40. player1.sendMessage(ChatColor.BLACK + "[" + ChatColor.RED + "Treg" + ChatColor.BLUE + "B" + ChatColor.YELLOW + "a" + ChatColor.DARK_GREEN + "y" + ChatColor.BLACK + "] " + ChatColor.GRAY + "When the Auction is over you will hear a " + ChatColor.GOLD + "*DING*" + ChatColor.GRAY + ".");
    41. }
    42. }
    43. if(commandLabel.equalsIgnoreCase("bid")){
    44. Player player = (Player) sender;
    45. if(args.length == 1){
    46. Player target = Bukkit.getPlayer(args[0]);
    47. if(target != null){
    48. ItemStack targetHand = target.getItemInHand();
    49. player.getInventory().addItem(targetHand);
    50. return true;
    51. } else {
    52. player.sendMessage(ChatColor.RED + "Could not find the player " + args[0] + "!");
    53. return true;
    54. }
    55. } else {
    56. player.sendMessage(ChatColor.RED + "Usage: /bid <player>");
    57. return true;
    58. }
    59. }
    60.  
    61. //Just a bit of fun :)
    62. if(commandLabel.equalsIgnoreCase("creephiss")) {{
    63. for(Player p : Bukkit.getOnlinePlayers()) {
    64. p.playSound(p.getLocation(), Sound.FUSE, 1, 0);
    65. }
    66. }
    67. }
    68.  
    69. return false;
    70. }
    71.  
    72. public class AuctionCooldown implements Runnable {
    73.  
    74. public Player player1 = null;
    75. public List<Player> AuctionCDNo = new ArrayList<Player>();
    76. public void setPlayer(Player player) {
    77. player1 = player;
    78. }
    79. public void setList(List<Player> list) {
    80. AuctionCDNo = list;
    81. }
    82. public List<Player> getlist() {
    83. return AuctionCDNo;
    84. }
    85. public void run() {
    86. try {
    87. Thread.sleep(180000);
    88. AuctionCDNo.remove(player1);
    89. player1.sendMessage(ChatColor.BLACK + "[" + ChatColor.RED + "Treg" + ChatColor.BLUE + "B" + ChatColor.YELLOW + "a" + ChatColor.DARK_GREEN + "y" + ChatColor.BLACK + "] " + ChatColor.DARK_RED + "Your auction has now finished!");
    90. player1.playSound(player1.getLocation(), Sound.ORB_PICKUP, 1, 0);
    91. }catch (Exception ignored){
    92. }
    93. }
    94. }
    95. }
    96.  
     
Thread Status:
Not open for further replies.

Share This Page