Advertisement
kelson8

code testing

Sep 30th, 2020
1,631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. package me.Kelson;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.entity.FoodLevelChangeEvent;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. public class NoHungerTest extends JavaPlugin implements Listener {
  14.  
  15.     Commands plugin;
  16.  
  17.     public NoHungerTest(Commands passedPlugin) {
  18.         this.plugin = passedPlugin;
  19.     }
  20.     public NoHungerTest(){
  21.         // Goes in onEnable, DO NOT remove
  22.     }
  23.  
  24.     private boolean hungeroff = false;
  25.     public boolean onCommand(CommandSender sender, Command cmd,
  26.                              String CommandLabel, String[] args) {
  27.         Player player = (Player) sender;
  28.         Player target = Bukkit.getServer().getPlayer(args[0]);
  29.  
  30.         /*if(cmd.getName().equalsIgnoreCase("nohunger") && (player.hasPermission("kelson.nohunger")) || (player.isOp())){
  31.             boolean hungeron = true;
  32.             boolean hungeroff = false;
  33.             if(args.length == 0) {
  34.                 sender.sendMessage("Too few arguments, command usage: /nohunger <on/off>");
  35.  
  36.  
  37.             if (args.length == 1 && (args[0].equalsIgnoreCase("on"))) {
  38.                 // Run onFoodChange event, but how?
  39.  
  40.                 }
  41.             }
  42.         } //Disable/ delete all non working code in the future
  43.         */
  44.  
  45.  
  46.  
  47.         return true;
  48.     }
  49.     // Hmm, this isn't working at all, need to figure out how to get the event from a command which I don't know how to do.
  50.     @EventHandler
  51.     public void onFoodChange(FoodLevelChangeEvent event) {
  52.  
  53.         //If the player has permission or if the player is op it disables their hunger
  54.             /*if ((event.getEntity().hasPermission("no.hunger")) || (event.getEntity().isOp())
  55.                 && cmd.getName().equalsIgnoreCase("nohunger")) { //Would this possibly work? unknown yet... */
  56.         //Entity sender = event.getEntity(); //Possibly change this to player instead of sender.
  57.         //Player target = Bukkit.getServer().getEntity(args[0]);
  58.  
  59.  
  60.         //Player target = event.getEntity(args[0]);
  61.  
  62.  
  63.  
  64.         //private hungeroff = event.setCancelled(true);
  65.         //private hungeron = event.setCancelled(false);
  66.         Player sender = (Player) event.getEntity();
  67.  
  68.  
  69.  
  70.  
  71.         if(cmd.getName().equalsIgnoreCase("nohunger") && (event.getEntity().hasPermission("kelson.nohunger"))){
  72.             if (args.length == 0) {
  73.                 sender.sendMessage(ChatColor.RED + "Too few arguments, command usage: nohunger <on/off> [player]");
  74.                 if (args.length == 1 && (args[0].equalsIgnoreCase("on"))) { // Disables hunger
  75.                     // hungeroff(); // Test code with this? that might work and look a little less messy.
  76.                     event.setCancelled(true);
  77.  
  78.                 if (args.length == 1 && (args[0].equalsIgnoreCase("off"))) { // Enables hunger
  79.                     // hungeron();
  80.                     event.setCancelled(false);
  81.  
  82.                         /*
  83.                         if(args.length == 2 && (args[1].equals(target)) && (args[0].equalsIgnoreCase("on")) && (event.getEntity().hasPermission("kelson.nohunger.others.on"))){
  84.  
  85.                             event.setCancelled(true);
  86.                             target.sendMessage("Your hunger has been disabled by " + sender.getName());
  87.                             if(args.length == 2 && (args[1].equals(target)) && (args[0].equalsIgnoreCase("off")) && (event.getEntity().hasPermission("kelson.nohunger.others.off"))){
  88.                                 event.setCancelled(false);
  89.                                 target.sendMessage("Your hunger has been enabled by " + sender.getName());
  90.                         */
  91.                         //... this might work but i highly doubt it, the code above
  92.  
  93.                       /*
  94.                       if(target == null){
  95.                           sender.sendMessage(ChatColor.BLUE + target + ChatColor.RED + " is not online!"); // If the nohunger for other players code above works then this should work.
  96.  
  97.                       */
  98.  
  99.  
  100.                         if (args.length > 2) {
  101.                             sender.sendMessage("Error in command usage, use /nohunger <on/off> [player]"); //TODO Implement this to where you can turn other players hunger on and off.
  102.  
  103.  
  104.                             if (!event.getEntity().hasPermission("kelson.nohunger")) {     //Permission error, if the player doesn't have permission this should run.
  105.                                 sender.sendMessage(ChatColor.DARK_RED + "Error: you don't have permission to use this command");
  106.                             }
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.     }
  118.  
  119.  
  120.     //Test this command sometime and see if it'll work like this to turn nohunger on and off.
  121.  
  122.  
  123.     //TODO Make this code work with a command instead of automatically giving anyone with op and with the permission nohunger
  124.  
  125.     }
  126.  
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement