Need advice

Discussion in 'Plugin Development' started by JanTuck, Sep 25, 2016.

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

    JanTuck

    I have no problem whatsoever i just need advice on this class. Also best way to do this?


    Code:java
    1.  
    2. package Libs;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.GameMode;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.PlayerInventory;
    11.  
    12. import java.util.Collections;
    13. import java.util.UUID;
    14.  
    15. /**
    16. * Created by jan on 9/25/16.
    17. */
    18. public class Staffmode {
    19.  
    20. private Location savedLocation;
    21. private ItemStack[] savedContents = new ItemStack[41];
    22. private Player savedPlayer;
    23. private GameMode savedGamemode;
    24. private double savedHealth;
    25. private int savedFood;
    26.  
    27. public Staffmode(Player player) {
    28. this.savedContents = player.getInventory().getContents();
    29. this.savedLocation = player.getLocation();
    30. this.savedPlayer = player;
    31. this.savedGamemode = player.getGameMode();
    32. this.savedHealth = player.getHealth();
    33. this.savedFood = player.getFoodLevel();
    34. }
    35.  
    36. public void staffModeOff() {
    37. savedPlayer.getInventory().setContents(savedContents);
    38. savedPlayer.teleport(savedLocation);
    39. savedPlayer.setGameMode(savedGamemode);
    40. savedPlayer.setHealth(savedHealth);
    41. savedPlayer.setFoodLevel(savedFood);
    42. }
    43.  
    44. public void staffModeOn() {
    45. savedPlayer.getInventory().clear();
    46. savedPlayer.setHealth(20);
    47. savedPlayer.setFoodLevel(20);
    48. savedPlayer.setGameMode(GameMode.CREATIVE);
    49. PlayerInventory playerInventory = savedPlayer.getInventory();
    50. playerInventory.addItem(ItemLib.giveItemMeta(Material.COMPASS, ChatColor.GOLD + "AdminCompass", Collections.singletonList(ChatColor.GOLD + "Use this to teleport around")));
    51. playerInventory.addItem(ItemLib.giveItemMeta(Material.BED, ChatColor.GOLD + "Player GUI", Collections.singletonList(ChatColor.GOLD + "Right click to get a list of Players")));
    52. playerInventory.setItem(8, ItemLib.giveItemMeta(Material.PAPER, ChatColor.GOLD + "AdminStatus", Collections.singletonList(ChatColor.GOLD + "AdminMode:" + ChatColor.AQUA + " Active")));
    53. }
    54.  
    55. public UUID getUUID() {
    56. return savedPlayer.getUniqueId();
    57. }
    58. }
    59.  



    How i use it

    Code:java
    1.  
    2. if (commandSender instanceof Player) {
    3. Player player = (Player) commandSender;
    4. boolean playerIsInStaffMode = false;
    5. for (int i = 0; i < staffModeList.size(); i++) {
    6. Staffmode loadedStaffMode = staffModeList.get(i);
    7. if (loadedStaffMode.getUUID().equals(player.getUniqueId())) {
    8. loadedStaffMode.staffModeOff();
    9. staffModeList.remove(i);
    10. playerIsInStaffMode = true;
    11. player.sendMessage(ChatColor.GOLD+">> "+ChatColor.AQUA+"Staff mode turned off");
    12. break;
    13. }
    14. }
    15. if (!playerIsInStaffMode) {
    16. Staffmode saveStaffMode = new Staffmode(player);
    17. staffModeList.add(saveStaffMode);
    18. saveStaffMode.staffModeOn();
    19. player.sendMessage(ChatColor.GOLD+">> "+ChatColor.AQUA+"Staff mode turned on");
    20. }
    21. }
    22.  
     
    Last edited: Sep 25, 2016
  2. Offline

    I Al Istannen

    @JanTuck
    Please use the Insert -> Code option. Paste it there. Then just change the tags from code to syntax. This way the indentation is correct.
     
  3. Offline

    JanTuck

  4. Offline

    DoggyCode™

    Formatting is horrible, sorry.
     
  5. Offline

    JanTuck

  6. @JanTuck
    Apart from a few naming convention misses, the class looks good to me.
     
  7. Offline

    Zombie_Striker

    @JanTuck
    I would recommend you change the names of all these variables. They are very descriptive, and it is hard to fully understand what all the variables are.

    Instead of doing this, break out of the for loop.

    This bit has been duplicated twice. If this is in your actual code (which I doubt since this would have errors in it), then remove this duplicated bit of code.

    Besides this, it looks like it should work. Nothing stands out as being inefficient.
     
  8. Offline

    I Al Istannen

    @JanTuck
    Much better.

    • That should be equals.
    • Make the variables private
    • Follow naming convetions (e.g. "LastLoc").
    • Make a defensive copy for the mutable passed values. I think this is also true for the passed Array
    • Follow naming conventions "ItemMeta" is a method.
    • Use varargs for the lore in your ItemLib class. This way you don't need to construct a new list everytime
    • Maybe just use Collections.singletonList(<string>) if you really want to pass a list?
    • You save a player instance. Be sure to null it when the player leaves. Or just store the UUID instead.
    EDIT: And "staffmodeon" should be "staffModeOn" or something.
     
  9. Offline

    JanTuck

    What is it fixed in above?

    @I Al Istannen
    is that good?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 25, 2016
  10. Offline

    DoggyCode™

    A LOT BETTER
     
  11. Offline

    JanTuck

Thread Status:
Not open for further replies.

Share This Page