Skip to content

Commit

Permalink
Merge pull request #4 from Trico-Everfire/added_worlds
Browse files Browse the repository at this point in the history
Added World, LootTable and Shooting/Falling Star Time Support.
  • Loading branch information
IdreesInc authored Dec 28, 2021
2 parents 8898969 + be7c2e6 commit 0caf9e6
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 121 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ out
gen
*.lnk
target/
.classpath
.project
.settings/
.gitignore
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/com/idreesinc/celeste/Astronomer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;

import com.idreesinc.celeste.utilities.WorldLootGenerator;

import java.util.List;
import java.util.Random;

Expand All @@ -18,40 +20,46 @@ public void run() {
if (celeste.getServer().getOnlinePlayers().size() == 0) {
return;
}

List<World> worlds = celeste.getServer().getWorlds();
for (World world : worlds) {
if (!world.getEnvironment().equals(World.Environment.NORMAL)) {
continue;
}

for(String worldKey : celeste.worldLoot.worldLootConfigs.keySet()) {
World world = celeste.getServer().getWorld(worldKey);
if(world == null) {
celeste.getLogger().warning("Invalid world: "+worldKey+" please double check config");
continue;
}
WorldLootGenerator.WLConfiguration config = celeste.worldLoot.worldLootConfigs.get(worldKey);

if (world.getPlayers().size() == 0) {
continue;
}
if (!(world.getTime() >= 13000 && world.getTime() <= 23000)) {
if (!(world.getTime() >= config.starStartSpawnTime && world.getTime() <= config.starStopSpawnTime)) {
continue;
}
if (world.hasStorm()) {
continue;
}

double shootingStarChance;
double fallingStarChance;
if (celeste.getConfig().getBoolean("new-moon-meteor-shower") && (world.getFullTime() / 24000) % 8 == 4) {
if (config.newMoonMeteorShower && (world.getFullTime() / 24000) % 8 == 4) {
shootingStarChance =
celeste.getConfig().getDouble("shooting-stars-per-minute-during-meteor-showers") / 120d;
config.shootingStarsPerMinuteMeteorShower / 120d;
fallingStarChance =
celeste.getConfig().getDouble("falling-stars-per-minute-during-meteor-showers") / 120d;
config.fallingStarsPerMinuteMeteorShower / 120d;
} else {
shootingStarChance = celeste.getConfig().getDouble("shooting-stars-per-minute") / 120d;
fallingStarChance = celeste.getConfig().getDouble("falling-stars-per-minute") / 120d;
shootingStarChance = config.shootingStarsPerMinute / 120d;
fallingStarChance = config.fallingStarsPerMinute / 120d;
}

if (celeste.getConfig().getBoolean("shooting-stars-enabled") && new Random().nextDouble() <= shootingStarChance) {
if (config.hasShootingStars && 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) {
if (config.hasFallingingStars && new Random().nextDouble() <= fallingStarChance) {
CelestialSphere.createFallingStar(celeste, world.getPlayers().get(new Random().nextInt(world.getPlayers().size())));
}

}


}
}
42 changes: 8 additions & 34 deletions src/main/java/com/idreesinc/celeste/Celeste.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.idreesinc.celeste;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import com.idreesinc.celeste.commands.CommandCeleste;
import com.idreesinc.celeste.commands.CommandFallingStar;
import com.idreesinc.celeste.commands.CommandShootingStar;
import com.idreesinc.celeste.utilities.Metrics;
import com.idreesinc.celeste.utilities.UpdateChecker;
import com.idreesinc.celeste.utilities.WeightedRandomBag;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import com.idreesinc.celeste.utilities.WorldLootGenerator;

public class Celeste extends JavaPlugin {

public WeightedRandomBag<String> fallingStarDrops = new WeightedRandomBag<>();
//public WeightedRandomBag<String> fallingStarDrops = new WeightedRandomBag<>();
public WorldLootGenerator worldLoot = new WorldLootGenerator(this);

@Override
public void onEnable() {
Expand All @@ -23,8 +23,7 @@ public void onEnable() {
this.getCommand("celeste").setExecutor(new CommandCeleste(this));
this.getCommand("shootingstar").setExecutor(new CommandShootingStar(this));
this.getCommand("fallingstar").setExecutor(new CommandFallingStar(this));

calculateFallingStarDrops();
worldLoot.runWorldLootGenerator();

BukkitRunnable stargazingTask = new Astronomer(this);
stargazingTask.runTaskTimer(this, 0, 10);
Expand All @@ -34,7 +33,7 @@ public void onEnable() {

public void reload() {
reloadConfig();
calculateFallingStarDrops();
worldLoot.runWorldLootGenerator();
checkForUpdates();
}

Expand All @@ -52,29 +51,4 @@ public void checkForUpdates() {
});
}
}

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");
} else {
// Use default loot table
loot = this.getConfig().getDefaults().getConfigurationSection("falling-stars-loot");
}
if (loot != null) {
for (String key : loot.getKeys(false)) {
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");
}
}
}
14 changes: 9 additions & 5 deletions src/main/java/com/idreesinc/celeste/CelestialSphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import com.idreesinc.celeste.utilities.WorldLootGenerator;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Random;
Expand All @@ -26,11 +28,12 @@ public static void createShootingStar(Celeste celeste, Location location) {

public static void createShootingStar(Celeste celeste, Location location, boolean approximate) {
Location starLocation;
WorldLootGenerator.WLConfiguration config = celeste.worldLoot.worldLootConfigs.get(location.getWorld().getName());
double w = 100 * Math.sqrt(new Random().nextDouble());
double t = 2d * Math.PI * new Random().nextDouble();
double x = w * Math.cos(t);
double range = Math.max(0, celeste.getConfig().getInt("shooting-stars-max-height") - celeste.getConfig().getInt("shooting-stars-min-height"));
double y = Math.max(new Random().nextDouble() * range + celeste.getConfig().getInt("shooting-stars-min-height"), location.getY() + 50);
double range = Math.max(0, config.shootingStarsMaxHeight - config.shootingStarsMinHeight);
double y = Math.max(new Random().nextDouble() * range + config.shootingStarsMinHeight, location.getY() + 50);
double z = w * Math.sin(t);
if (approximate) {
starLocation = new Location(location.getWorld(), location.getX() + x, y, location.getZ() + z);
Expand All @@ -47,7 +50,7 @@ public static void createShootingStar(Celeste celeste, Location location, boolea
direction.getY(), direction.getZ(), speed, null, true);
}
if (celeste.getConfig().getBoolean("debug")) {
celeste.getLogger().info("Shooting star at " + stringifyLocation(starLocation));
celeste.getLogger().info("Shooting star at " + stringifyLocation(starLocation) + " in world " + starLocation.getWorld().getName());
}
}

Expand All @@ -65,8 +68,9 @@ public static void createFallingStar(Celeste celeste, final Location location) {

public static void createFallingStar(Celeste celeste, final Location location, boolean approximate) {
Location target = location;
WorldLootGenerator.WLConfiguration config = celeste.worldLoot.worldLootConfigs.get(location.getWorld().getName());
if (approximate) {
double fallingStarRadius = celeste.getConfig().getDouble("falling-stars-radius");
double fallingStarRadius = config.fallingStarRadius;
double w = fallingStarRadius * Math.sqrt(new Random().nextDouble());
double t = 2d * Math.PI * new Random().nextDouble();
double x = w * Math.cos(t);
Expand All @@ -76,7 +80,7 @@ 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));
celeste.getLogger().info("Falling star at " + stringifyLocation(target) + " in world " + target.getWorld().getName());
}
}

Expand Down
32 changes: 20 additions & 12 deletions src/main/java/com/idreesinc/celeste/FallingStar.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.idreesinc.celeste;

import java.util.Random;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.entity.AbstractSkeleton;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Random;
import com.idreesinc.celeste.utilities.WorldLootGenerator;

public class FallingStar extends BukkitRunnable {

Expand All @@ -23,7 +25,7 @@ public class FallingStar extends BukkitRunnable {

public FallingStar(Celeste celeste, Location location) {
this.celeste = celeste;
sparkTimer = celeste.getConfig().getInt("falling-stars-spark-time");
sparkTimer = celeste.worldLoot.worldLootConfigs.get(location.getWorld().getName()).fallingStarSparkTime;//getConfig().getInt("falling-stars-spark-time");
this.location = location;
dropLoc = new Location(location.getWorld(), location.getX(),
location.getWorld().getHighestBlockAt(location).getY() + 1, location.getZ());
Expand All @@ -45,18 +47,24 @@ public void run() {
0, 0, new Random().nextDouble(), 0,
0.2, null, true);
}
if (celeste.getConfig().getBoolean("falling-stars-sound-enabled") && !soundPlayed && y <= dropLoc.getY() + 75) {
location.getWorld().playSound(dropLoc, Sound.BLOCK_BELL_RESONATE, (float) celeste.getConfig().getDouble("falling-stars-volume"), 0.5f);
if (celeste.worldLoot.worldLootConfigs.get(location.getWorld().getName()).fallingStarSoundEnabled && !soundPlayed && y <= dropLoc.getY() + 75) {
location.getWorld().playSound(dropLoc, Sound.BLOCK_BELL_RESONATE, (float) celeste.worldLoot.worldLootConfigs.get(location.getWorld().getName()).fallingStarVolume, 0.5f);
soundPlayed = true;
}
if (y <= dropLoc.getY()) {
if (!lootDropped) {
if (celeste.fallingStarDrops.entries.size() > 0) {
ItemStack drop = new ItemStack(Material.valueOf(celeste.fallingStarDrops.getRandom()), 1);
location.getWorld().dropItem(dropLoc, drop);
}
ExperienceOrb orb = (ExperienceOrb) dropLoc.getWorld().spawnEntity(dropLoc, EntityType.EXPERIENCE_ORB);
orb.setExperience(celeste.getConfig().getInt("falling-stars-experience"));
WorldLootGenerator.WLConfiguration config = celeste.worldLoot.worldLootConfigs.get(dropLoc.getWorld().getName().toLowerCase());
if(config != null) {
for(String lootTableName : config.fallingStarLoot) {
Entity marker = (Entity) dropLoc.getWorld().spawnEntity(location, EntityType.MARKER);
celeste.getServer().dispatchCommand(celeste.getServer().getConsoleSender(),"execute at "+ marker.getUniqueId().toString() +" run loot spawn "+dropLoc.getX() +" "+dropLoc.getY()+" "+dropLoc.getZ()+" loot "+lootTableName);
marker.remove();
}
if(config.fallingStarExperience > 0) {
ExperienceOrb orb = (ExperienceOrb) dropLoc.getWorld().spawnEntity(dropLoc, EntityType.EXPERIENCE_ORB);
orb.setExperience(config.fallingStarExperience);
}
}
lootDropped = true;
}
if (y % (step * 5) == 0) {
Expand Down

This file was deleted.

Loading

0 comments on commit 0caf9e6

Please sign in to comment.