Skip to content

Commit

Permalink
Add reload command and new config options
Browse files Browse the repository at this point in the history
Adds a reload command to reload the config, new config options for toggling certain events and debug mode, and fixes spammy update checking
  • Loading branch information
IdreesInc committed Jul 24, 2020
1 parent 2fb52ac commit 3b56d26
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 31 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ Additionally, the worlds that shooting and falling stars can spawn in must meet
Installation is as simple as copying the newest build jar to your plugins folder. A configuration file is created by default, but if the file was created previously it may not include default values that were added in later updates. These values can be added easily by just copying and pasting the particular lines from the defaults below.
### Defaults
``` yaml
# Whether to perform an update check when starting or reloading the plugin
check-for-updates: true
# Whether to spawn shooting stars or not
shooting-stars-enabled: true
# Whether to spawn falling stars or not
falling-stars-enabled: true
# The average number of shooting stars to create per minute for each world
shooting-stars-per-minute: 6
# Whether to create falling stars or not
falling-stars-enabled: true
# The average number of falling stars to create per minute for each world
falling-stars-per-minute: 0.2
# The maximum distance around a player within which a falling star may spawn
Expand All @@ -55,6 +59,8 @@ falling-stars-per-minute-during-meteor-showers: 0.3
shooting-stars-summon-text: "Make a wish!"
# The message to send in response to summoning a falling star
falling-stars-summon-text: "Make a wish!"
# Enables debug mode, which adds log messages for shooting and falling stars among other things
debug: false
```
### Falling Star Loot
Falling stars drop loot wherever they fall, and spark for 10 seconds (200 ticks) by default to show their location. The loot they drop is randomly selected from the loot table in the config, with each material being given a weight. For instance, in the default config, there is a 60% chance for a diamond, 20% of an emerald, and 20% chance of a fire_charge. Experience also drops from falling stars, 100 points (not levels) by default.
Expand All @@ -72,7 +78,11 @@ falling-stars-loot:
```

## Commands
**/shootingstar [player]** summons a shooting star in the sky directly above the player. If no player is given, spawns one above the summoner.
**/shootingstar [player]** summons a shooting star in the sky directly above the player. If no player is given, spawns one above the summoner.
*Permission: celeste.shootingstar*

**/fallingstar [player]** summons a falling star directly above the player. If no player is given, spawns one above the summoner.
**/fallingstar [player]** summons a falling star directly above the player. If no player is given, spawns one above the summoner.
*Permission: celeste.shootingstar*

**/celeste reload** reloads the config file, recalculates the falling star loot, and checks for updates (if enabled)
*Permission: celeste.reload*
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.idreesinc</groupId>
<artifactId>Celeste</artifactId>
<version>1.4</version>
<version>1.5</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/idreesinc/celeste/Astronomer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ public void run() {
fallingStarChance = celeste.getConfig().getDouble("falling-stars-per-minute") / 120d;
}

if (new Random().nextDouble() <= shootingStarChance) {
CelestialSphere.createShootingStar(world.getPlayers().get(new Random().nextInt(world.getPlayers().size())));
// System.out.println("Spawning shooting star");
if (celeste.getConfig().getBoolean("shooting-stars-enabled") && new Random().nextDouble() <= shootingStarChance) {
CelestialSphere.createShootingStar(celeste, world.getPlayers().get(new Random().nextInt(world.getPlayers().size())));
}
if (celeste.getConfig().getBoolean("falling-stars-enabled") && new Random().nextDouble() <= fallingStarChance) {
CelestialSphere.createFallingStar(celeste, world.getPlayers().get(new Random().nextInt(world.getPlayers().size())));
// System.out.println("Spawning falling star");
}
}
}
Expand Down
49 changes: 38 additions & 11 deletions src/main/java/com/idreesinc/celeste/Celeste.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.idreesinc.celeste;

import com.idreesinc.celeste.commands.CommandCeleste;
import com.idreesinc.celeste.commands.CommandFallingStar;
import com.idreesinc.celeste.commands.CommandShootingStar;
import com.idreesinc.celeste.utilities.Metrics;
Expand All @@ -12,16 +13,51 @@

public class Celeste extends JavaPlugin {

public WeightedRandomBag<String> fallingStarDrops = new WeightedRandomBag<String>();
public WeightedRandomBag<String> fallingStarDrops = new WeightedRandomBag<>();

@Override
public void onEnable() {
this.saveDefaultConfig();
Metrics metrics = new Metrics(this, 8292);

this.getCommand("celeste").setExecutor(new CommandCeleste(this));
this.getCommand("shootingstar").setExecutor(new CommandShootingStar(this));
this.getCommand("fallingstar").setExecutor(new CommandFallingStar(this));

calculateFallingStarDrops();

BukkitRunnable stargazingTask = new Astronomer(this);
stargazingTask.runTaskTimer(this, 0, 10);

checkForUpdates();
}

public void reload() {
reloadConfig();
calculateFallingStarDrops();
checkForUpdates();
}

public void checkForUpdates() {
if (this.getConfig().getBoolean("check-for-updates")) {
new UpdateChecker(this, 81862).getVersion(version -> {
try {
double current = Double.parseDouble(this.getDescription().getVersion());
double api = Double.parseDouble(version);
if (current >= api) {
// this.getLogger().info("Celeste is up to date!");
} else {
this.getLogger().info("There is an update available for Celeste (" + current + " -> " + api + ")");
}
} catch (NumberFormatException e) {
}
});
}
}

public void calculateFallingStarDrops() {
ConfigurationSection loot;
fallingStarDrops = new WeightedRandomBag<>();
if (this.getConfig().isSet("falling-stars-loot")) {
// User has added their own loot table, do not combine with defaults
loot = this.getConfig().getConfigurationSection("falling-stars-loot");
Expand All @@ -34,22 +70,13 @@ public void onEnable() {
try {
Material.valueOf(key.toUpperCase());
fallingStarDrops.addEntry(key.toUpperCase(), loot.getDouble(key));
// System.out.println(key.toUpperCase() + " " + loot.getDouble(key));
} catch (IllegalArgumentException e) {
System.err.println("Error: Item with name " + key.toUpperCase() + " does not exist, skipping");
}
}
} else {
System.err.println("Error: Loot table for falling stars can't be found");
}
BukkitRunnable stargazingTask = new Astronomer(this);
stargazingTask.runTaskTimer(this, 0, 10);

new UpdateChecker(this, 81862).getVersion(version -> {
if (this.getDescription().getVersion().equalsIgnoreCase(version)) {
this.getLogger().info("Celeste is up to date!");
} else {
this.getLogger().info("There is an update available for Celeste (" + this.getDescription().getVersion() + " -> " + version + ")");
}
});
}
}
28 changes: 21 additions & 7 deletions src/main/java/com/idreesinc/celeste/CelestialSphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Random;

public class CelestialSphere {

public static void createShootingStar(Player player) {
createShootingStar(player, true);
public static void createShootingStar(Celeste celeste, Player player) {
createShootingStar(celeste, player, true);
}

public static void createShootingStar(Player player, boolean approximate) {
createShootingStar(player.getLocation(), approximate);
public static void createShootingStar(Celeste celeste, Player player, boolean approximate) {
createShootingStar(celeste, player.getLocation(), approximate);
}

public static void createShootingStar(Location location) {
createShootingStar(location, true);
public static void createShootingStar(Celeste celeste, Location location) {
createShootingStar(celeste, location, true);
}

public static void createShootingStar(Location location, boolean approximate) {
public static void createShootingStar(Celeste celeste, Location location, boolean approximate) {
Location starLocation;
double w = 100 * Math.sqrt(new Random().nextDouble());
double t = 2d * Math.PI * new Random().nextDouble();
Expand All @@ -43,6 +45,9 @@ public static void createShootingStar(Location location, boolean approximate) {
location.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, starLocation, 0, direction.getX(),
direction.getY(), direction.getZ(), speed, null, true);
}
if (celeste.getConfig().getBoolean("debug")) {
celeste.getLogger().info("Shooting star at " + stringifyLocation(starLocation));
}
}

public static void createFallingStar(Celeste celeste, Player player) {
Expand All @@ -69,5 +74,14 @@ public static void createFallingStar(Celeste celeste, final Location location, b
}
BukkitRunnable fallingStarTask = new FallingStar(celeste, target);
fallingStarTask.runTaskTimer(celeste, 0, 1);
if (celeste.getConfig().getBoolean("debug")) {
celeste.getLogger().info("Falling star at " + stringifyLocation(target));
}
}

private static String stringifyLocation(Location location) {
DecimalFormat format = new DecimalFormat("##.00");
format.setRoundingMode(RoundingMode.HALF_UP);
return "x: " + format.format(location.getX()) + " y: " + format.format(location.getY()) + " z: " + format.format(location.getZ());
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/idreesinc/celeste/commands/CommandCeleste.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.idreesinc.celeste.commands;

import com.idreesinc.celeste.Celeste;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class CommandCeleste implements CommandExecutor {

Celeste celeste;

public CommandCeleste(Celeste celeste) {
this.celeste = celeste;
}

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
celeste.reload();
sender.sendMessage("Celeste has been reloaded");
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage("\u00a7cError: Player not found.");
return true;
}
CelestialSphere.createShootingStar(Bukkit.getPlayer(args[0]), false);
CelestialSphere.createShootingStar(celeste, Bukkit.getPlayer(args[0]), false);
} else {
if (sender instanceof Player) {
Player player = (Player) sender;
CelestialSphere.createShootingStar(player, false);
CelestialSphere.createShootingStar(celeste, player, false);
} else {
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
shooting-stars-per-minute: 6
check-for-updates: true
shooting-stars-enabled: true
falling-stars-enabled: true
shooting-stars-per-minute: 6
falling-stars-per-minute: 0.2
falling-stars-radius: 75
falling-stars-spark-time: 200
Expand All @@ -13,3 +15,4 @@ shooting-stars-per-minute-during-meteor-showers: 15
falling-stars-per-minute-during-meteor-showers: 0.3
shooting-stars-summon-text: "Make a wish!"
falling-stars-summon-text: "Make a wish!"
debug: false
6 changes: 5 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
name: Celeste
version: 1.4
version: 1.5
api-version: 1.14
author: Idrees
main: com.idreesinc.celeste.Celeste

commands:
celeste:
description: Reloads the plugin configuration
usage: /<command> <reload>
permission: celeste.reload
shootingstar:
description: Creates a shooting star near the player that summons it, or optionally near another player
usage: /<command> [player]
Expand Down

0 comments on commit 3b56d26

Please sign in to comment.