Subtracting one time from another with SimpleDateFormat (bukkit/minecraft)

Discussion in 'Plugin Development' started by caledonian26, May 1, 2023.

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

    caledonian26

    Hey all,

    I have the following code below.

    I am recording the time at which a player presses a stone button (first event). I am also recording the time the player gets killed in the game (second event).

    I want to subtract the time at which the first event occurs from the time at which the second event occurs, i.e., calculate the time difference between the two events. :)

    I then want this time difference to be the interval at which another BukkitRunnable repeats, and so I want to store this time difference as a variable.

    When I write the following code, I get the error 'create local variable 'timeSeconds1'. How can I access the 'timeSeconds1' variable I created in the first event in the second event?

    I would be so grateful for a helping hand!

    Code:
        long timeSeconds1 = 0;
        long timeSeconds2 = 0;
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
         if (event.getClickedBlock().getType() == Material.STONE_BUTTON) {
            long timeMillis = System.currentTimeMillis();
            long timeSeconds1 = TimeUnit.MILLISECONDS.toSeconds(timeMillis);
            try {
               String content = String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.sss").format(new Date())) + " Player: " + event.getPlayer().getName() + " Material: " + event.getClickedBlock().getType() + "\n";
               if (!getDataFolder().exists()) {
                     getDataFolder().mkdirs();
                   }
               final File file = new File(dataFolder, "eventschase" + event.getPlayer().getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy").format(new Date())) + ".log");
               if (!file.exists()) {
                   file.createNewFile();
               }
               Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND);
            } catch(Exception e) {
                   e.printStackTrace();
               }
            final BukkitRunnable Br2 = new BukkitRunnable() {
                int count = 0;
    
                public void run() {
                  if(!event.getPlayer().isOnline()) {
                        this.cancel();
                    }
                  if(event.getPlayer().isDead()) {
                        this.cancel();
                    }
                  if (count == 0) cancel();
                  event.getPlayer().setHealth(0);    
                  count++;
                }
              };
            Br2.runTaskTimer(this, 1200, 1200);
           
            new BukkitRunnable() {
    
                public void run() {
                  if(!event.getPlayer().isOnline()) {
                        Br2.cancel();
                        this.cancel();
                    }
                  if(event.getPlayer().isDead()) {
                        Br2.cancel();
                        this.cancel();
                    }
                }
              }.runTaskTimer(this,0,1);
        }
    
        @EventHandler
        public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
            try {
                     long timeMillis2 = System.currentTimeMillis();
                     long timeSeconds2 = TimeUnit.MILLISECONDS.toSeconds(timeMillis2);
                     long timediff = timeSeconds2-timeSeconds1;
                     String content = String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.sss").format(new Date())) + " Killer: " + event.getPlayer().getName() + "Deathlocation:"+event.getPlayer().getLocation()+"\n";
                     if (!getDataFolder().exists()) {
                           getDataFolder().mkdirs();
                         }
                     final File file = new File(dataFolder, "eventskill" + event.getPlayer().getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy").format(new Date())) + ".log");
                     if (!file.exists()) {
                         file.createNewFile();
                     }
                     Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND);
                  } catch(Exception e) {
                         e.printStackTrace();
                     }
          }
        } 
    I have now solved my issue. :) The following worked:

    Code:
    List<String> vowels = new ArrayList<>();
    List<String> vowels2 = new ArrayList<>();
      
        @EventHandler
        public void onEntityDeath(EntityDeathEvent event) {
            if (event.getEntity().getLastDamageCause().getCause()!=DamageCause.LAVA) {
            Format f = new SimpleDateFormat("ss");
            String strSeconds = f.format(new Date());
            String strSeconds3 = vowels.get(0);
            int strSeconds2 = Integer.parseInt(strSeconds);
            int strSeconds4 = Integer.parseInt(strSeconds3);
               int difference = strSeconds2-strSeconds4;
               if (difference < 0) {
                   difference = Math.abs(difference);
               }
               String difference2 = Integer.toString(difference);
            vowels2.add(difference2);
            try {
                     String content = String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.sss").format(new Date())) + " Killer: " + event.getEntity().getName() + " Time taken to kill: " + difference*20 + " Location: " + event.getEntity().getLocation() + "\n";
                     if (!getDataFolder().exists()) {
                           getDataFolder().mkdirs();
                         }
                     final File file = new File(dataFolder, "eventskillnew" + event.getEntity().getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy").format(new Date())) + ".log");
                     if (!file.exists()) {
                         file.createNewFile();
                     }
                     Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND);
                  } catch(Exception e) {
                         e.printStackTrace();
                     }
            } 
        }
    
      if (event.getClickedBlock().getType() == Material.STONE_BUTTON) {
            Format f = new SimpleDateFormat("ss");
            String strSeconds = f.format(new Date());
            vowels.add(strSeconds);
            try {
               String content = String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.sss").format(new Date())) + " Player: " + event.getPlayer().getName() + " Material: " + event.getClickedBlock().getType() + "\n";
               if (!getDataFolder().exists()) {
                     getDataFolder().mkdirs();
                   }
               final File file = new File(dataFolder, "eventschase" + event.getPlayer().getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy").format(new Date())) + ".log");
               if (!file.exists()) {
                   file.createNewFile();
               }
               Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND);
            } catch(Exception e) {
                   e.printStackTrace();
               }
            final BukkitRunnable Br2 = new BukkitRunnable() {
                int count = 0;
    
                public void run() {
                  if(!event.getPlayer().isOnline()) {
                        this.cancel();
                    }
                  if(event.getPlayer().isDead()) {
                        this.cancel();
                    }
                  if (count == 0) cancel();
                  event.getPlayer().setHealth(0);
                  vowels.clear();
                  count++;
                }
              };
            Br2.runTaskTimer(this, 1200, 1200);
          
            new BukkitRunnable() {
    
                public void run() {
                  if(!event.getPlayer().isOnline()) {
                        Br2.cancel();
                        this.cancel();
                        vowels.clear();
                    }
                  if(event.getPlayer().isDead()) {
                        Br2.cancel();
                        this.cancel();
                        vowels.clear();
                    }
                }
              }.runTaskTimer(this,0,1);
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 1, 2023
  2. Offline

    OMGitzFROST

    Hey I’m glad you got it figured out, just wanted to suggest a utility class I use that I think could help you, take a look at it here. It also has other helpful clases for getting the time remaining between 2 dates, adding, subtracting from dates, etc, feel free to use whatever is useful in your own plugin as well, it’s an open source api I’m developing so it will be free to use for everyone

    https://github.com/OMGitzFROST/Mole...n/java/com/moleculepowered/api/util/Time.java
     
Thread Status:
Not open for further replies.

Share This Page