how do I add a wait function to my code

Discussion in 'Plugin Development' started by Crazy_Sheep, Oct 17, 2020.

  1. Offline

    Crazy_Sheep

    ok so I wanna make a plugin that makes minecraft really hard
    but I want to add that when you respawn you get a 1 min god mode
    and after that 1 min it go's away but every thing I try it does not work

    here is my code:

    Code:
    package me.crazysheep.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.attribute.Attribute;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Husk;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Main extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable() {
            getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "Ultra Hard Core Plugin Enabled");
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "Ultra Hard Core Plugin Disabled");
        }
       
        @EventHandler
        public static void playerjoin(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            p.setHealth(1);
            p.setHealthScale(1);
            p.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(1);
        }
       
        @EventHandler
        public static void creaturespawnc(CreatureSpawnEvent event) {
            Creeper creeper = (Creeper) event.getEntity();
            if(event.getEntityType() == EntityType.CREEPER) {
                creeper.setPowered(true);
            }
        }
            @EventHandler
            public static void creaturespawnz(CreatureSpawnEvent event) {
                Zombie zombie = (Zombie) event.getEntity();
                if (event.getEntityType() == EntityType.ZOMBIE) {
                    zombie.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999, 10));
                    zombie.setCustomName("Sonic");
                    zombie.setCustomNameVisible(true);
                }
            }
                @EventHandler
                public static void creaturespawnh(CreatureSpawnEvent event) {
                    Husk husk = (Husk) event.getEntity();
                    if (event.getEntityType() == EntityType.HUSK) {
                        husk.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999, 15));
                        husk.setCustomName("SuperSonic");
                        husk.setCustomNameVisible(true);
                    }
        }
                @EventHandler
                public static void playerrespawn(PlayerRespawnEvent event) throws InterruptedException {
                    Player pl = event.getPlayer();
                    pl.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(9999999);
                    pl.setHealth(9999999);
                      Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(new Main(), new Runnable(){
                          public void run(){
                                pl.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(1);
                                pl.setHealth(1);
                          }
                      }, 1L);
                }
    
    }
    
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    Strahan

    Also you know you don't need entirely separate methods for the creature spawning, right? Just put your logic in one method. Plus you are casting Entity in the methods before you even check if that's what the Entity is, which will cause crashes.

    Example:
    Code:
    @EventHandler
    public void onCreatureSpawn(CreatureSpawnEvent e) {
      switch (e.getEntityType()) {
      case CREEPER:
        ((Creeper)e.getEntity()).setPowered(true);
        break;
     
      case HUSK:
        Husk husk = (Husk)e.getEntity();
        husk.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999, 15));
        husk.setCustomName("SuperSonic");
        husk.setCustomNameVisible(true);
        break;
     
      case ZOMBIE:
        Zombie zombie = (Zombie)e.getEntity();
        zombie.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 999999, 10));
        zombie.setCustomName("Sonic");
        zombie.setCustomNameVisible(true);
        break;
     
      default:
        return;
      }
    }
    PS also don't make everything static.

    PPS the way you are doing "god mode" is also pretty flawed, because they still will react to damage and their heart indicator will likely look crazy. You'd be better off to make a Set<UUID> and on respawn, add them to the set and start the runnable to remove them from the set after the time expires. Then in the EntityDamageEvent, check if the Set contains the event's getEntity UUID. If not return. Then set cancelled true.
     
    Last edited: Oct 18, 2020

Share This Page