diff --git a/pom.xml b/pom.xml index 2534a75..03bf19f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.thedawnph servercore_za - 1.0-SNAPSHOT + 1.1.1 jar servercore_za diff --git a/src/main/java/net/thedawnph/servercore_za/OneNightSleep.java b/src/main/java/net/thedawnph/servercore_za/OneNightSleep.java new file mode 100644 index 0000000..8569cdc --- /dev/null +++ b/src/main/java/net/thedawnph/servercore_za/OneNightSleep.java @@ -0,0 +1,15 @@ +package net.thedawnph.servercore_za; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerBedEnterEvent; + +public class OneNightSleep implements Listener { + @EventHandler + public void onPlayerSleep(PlayerBedEnterEvent event) { + + if (event.getBedEnterResult() == PlayerBedEnterEvent.BedEnterResult.OK) { + event.getPlayer().getWorld().setTime(23000); + } + } +} diff --git a/src/main/java/net/thedawnph/servercore_za/Servercore_za.java b/src/main/java/net/thedawnph/servercore_za/Servercore_za.java index 3982b88..df1e5b6 100644 --- a/src/main/java/net/thedawnph/servercore_za/Servercore_za.java +++ b/src/main/java/net/thedawnph/servercore_za/Servercore_za.java @@ -33,6 +33,8 @@ public final class Servercore_za extends JavaPlugin { @Override public void onDisable() { + // reset scheduler + getServer().getScheduler().cancelTasks(this); LOGGER.info("ZA ServerCore disabled"); } @@ -40,6 +42,7 @@ public final class Servercore_za extends JavaPlugin { private void registerEvents() { getServer().getPluginManager().registerEvents(new ZombieKill(), this); // getServer().getPluginManager().registerEvents(new DisableMonsters(), this); // working in progress - getServer().getPluginManager().registerEvents(new StartNighttimeTask(), this); + getServer().getPluginManager().registerEvents(new OneNightSleep(), this); + new StartNighttimeTask(this); } } diff --git a/src/main/java/net/thedawnph/servercore_za/StartNighttimeTask.java b/src/main/java/net/thedawnph/servercore_za/StartNighttimeTask.java index 039eae7..7e6dd33 100644 --- a/src/main/java/net/thedawnph/servercore_za/StartNighttimeTask.java +++ b/src/main/java/net/thedawnph/servercore_za/StartNighttimeTask.java @@ -3,40 +3,84 @@ package net.thedawnph.servercore_za; import com.ericdebouwer.zombieapocalypse.api.ApocalypseAPI; import org.bukkit.Bukkit; import org.bukkit.World; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.event.Listener; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import java.util.Objects; -public class StartNighttimeTask implements Listener { - private FileConfiguration getConfig() { - return JavaPlugin.getPlugin(Servercore_za.class).getConfig(); +public class StartNighttimeTask { + + private final ApocalypseAPI apocalypseAPI = ApocalypseAPI.getInstance(); + private final JavaPlugin plugin; + private boolean isApocalyptic = false; + + public StartNighttimeTask(JavaPlugin plugin) { + this.plugin = plugin; + startScheduler(); } - ApocalypseAPI apocalypseAPI = ApocalypseAPI.getInstance(); - boolean isApocalyptic = apocalypseAPI.isApocalypse(Objects.requireNonNull(getConfig().getString("worldname"))); - public StartNighttimeTask() { - Bukkit.getScheduler().runTaskTimer(Servercore_za.getPlugin(Servercore_za.class), () -> { - World world = Bukkit.getWorld(Objects.requireNonNull(getConfig().getString("worldname"))); + private void startScheduler() { + Bukkit.getScheduler().runTaskTimer(plugin, () -> { + String worldName = plugin.getConfig().getString("worldname"); + if (worldName == null) { + Bukkit.getLogger().severe("No worldname specified in the configuration!"); + return; + } + + World world = Bukkit.getWorld(worldName); if (world == null) { - Bukkit.getLogger().severe("World not found!"); + Bukkit.getLogger().severe("World not found: " + worldName); return; } long time = world.getTime(); // Check if it is nighttime - if (time >= 13000) { - if (!isApocalyptic) { - // Play bell sound for all players - Bukkit.getOnlinePlayers().forEach(player -> - player.playSound(player.getLocation(), "event.raid.horn", 1.0f, 1.0f) - ); - apocalypseAPI.startApocalypse(Objects.requireNonNull(getConfig().getString("worldname")), getConfig().getInt("ZA-duration"), getConfig().getInt("ZA-mobs"), true); + if (time >= 12541 && time <= 23458) { + if (!Bukkit.getOnlinePlayers().isEmpty() && !isApocalyptic) { + // Play raid horn sound for all players + for (Player player : Bukkit.getOnlinePlayers()) { + player.playSound(player.getLocation(), "item.goat_horn.sound.5", 16.0f, 1.0f); + } + + long duration = 0; + if (plugin.getConfig().getInt("ZA-duration") == 0) { + // Start the apocalypse + apocalypseAPI.startApocalypse( + worldName, true + ); + } else { + // Start the apocalypse + apocalypseAPI.startApocalypse( + worldName, + plugin.getConfig().getInt("ZA-duration"), + plugin.getConfig().getInt("ZA-mobs"), + true + ); + } + + + isApocalyptic = true; + } + } else { + if (isApocalyptic) { + // End the apocalypse + apocalypseAPI.endApocalypse(worldName, true); + + // Play raid horn sound for all players + for (Player player : Bukkit.getOnlinePlayers()) { + player.playSound(player.getLocation(), "item.goat_horn.sound.3", 16.0f, 1.0f); + } + + // Remove all dropped items from the world + world.getEntities().stream() + .filter(entity -> entity instanceof Item) + .forEach(Entity::remove); + + isApocalyptic = false; } } - }, 0, 13000L); + }, 0L, 40L); // Run every 2 seconds } -} - +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f8ef701..2960f37 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,6 +1,6 @@ worldname: "world" # Name of the world to spawn zombies in ZA-duration: 7 # In minutes, default is 7 minutes (or 1 minecraft night) -ZA-mobs: 2 # Number of mobs to spawn per chunk, default is 2 +ZA-mobs: 2 # Number of mobs to spawn per chunk, default is 2, ZA-duration must be configured zombie-drops: - "IRON_INGOT" - "GOLD_INGOT" diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 963fb79..d5ddf6b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: servercore_za -version: '1.0-SNAPSHOT' +version: '1.1.1' main: net.thedawnph.servercore_za.Servercore_za api-version: '1.20' authors: [ princepines ]