Skip to content

Commit

Permalink
feat: Version 1.1.0 Overhaul - Resture Codebase (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Tapply authored Jun 7, 2024
1 parent 7b9c22c commit 184d3a0
Show file tree
Hide file tree
Showing 42 changed files with 1,946 additions and 708 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.jeqo</groupId>
<artifactId>bloons</artifactId>
<version>1.0.6-BETA</version>
<version>1.1.0-BETA</version>
<packaging>jar</packaging>

<name>Bloons</name>
Expand Down Expand Up @@ -80,6 +80,11 @@
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.17.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
98 changes: 49 additions & 49 deletions src/main/java/net/jeqo/bloons/Bloons.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,87 @@

import lombok.Getter;
import lombok.Setter;
import net.jeqo.bloons.commands.manager.CommandManager;
import net.jeqo.bloons.data.BalloonOwner;
import net.jeqo.bloons.data.UpdateChecker;
import net.jeqo.bloons.listeners.LeashHandlers;
import net.jeqo.bloons.listeners.MenuHandlers;
import net.jeqo.bloons.listeners.PlayerHandlers;
import net.jeqo.bloons.balloon.SingleBalloon;
import net.jeqo.bloons.commands.manager.CommandCore;
import net.jeqo.bloons.utils.UpdateChecker;
import net.jeqo.bloons.listeners.BalloonUnleashListener;
import net.jeqo.bloons.listeners.ListenerCore;
import net.jeqo.bloons.listeners.MenuClickListener;
import net.jeqo.bloons.listeners.PlayerListener;
import net.jeqo.bloons.logger.Logger;
import net.jeqo.bloons.utils.Metrics;
import net.jeqo.bloons.utils.Utils;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.UUID;

public final class Bloons extends JavaPlugin {

public static HashMap<UUID, BalloonOwner> playerBalloons = new HashMap<>();
public static HashMap<UUID, String> playerBalloonID = new HashMap<>();
@Getter @Setter
private static Bloons instance;
@Getter @Setter
private static CommandCore commandCore;
@Getter @Setter
private static ListenerCore listenerCore;

@Getter @Setter
public static HashMap<UUID, SingleBalloon> playerBalloons = new HashMap<>();
@Getter @Setter
public static HashMap<UUID, String> playerBalloonID = new HashMap<>();

@Override
public void onEnable() {
Utils.log("|---[ BLOONS ]-------------------------------------------------------|");
Utils.log("| Plugin loaded. |");
Utils.log("|-------------------------------------------------[ MADE BY JEQO ]---|");

// Create an instance of the plugin
setInstance(this);

new CommandManager(getInstance());
loadListeners();
// Send startup message
Logger.logStartup();

// Register core managers within the plugin
setCommandCore(new CommandCore(getInstance()));
setListenerCore(new ListenerCore(getInstance()));

// Stage listeners
getListenerCore().stageListener(new PlayerListener());
getListenerCore().stageListener(new BalloonUnleashListener());
getListenerCore().stageListener(new MenuClickListener());

// Register all handlers
getListenerCore().registerListeners();

// Startup the metrics and update checker
int pluginId = 16872;
new Metrics(this, pluginId);
updateChecker();

getConfig().options().copyDefaults(); saveDefaultConfig();
// Generate config(s) and set defaults
getConfig().options().copyDefaults();
saveDefaultConfig();
}

@Override
public void onDisable() {
Utils.log("|---[ BLOONS ]-------------------------------------------------------|");
Utils.log("| Shutting down... |");
Utils.log("|-------------------------------------------------[ MADE BY JEQO ]---|");
// Log shutdown message
Logger.logShutdown();

for (BalloonOwner owner : playerBalloons.values()) {
// Unregister all balloons and stop the task
for (SingleBalloon owner : playerBalloons.values()) {
owner.cancel();
}

HandlerList.unregisterAll(this);
// Unregister all listeners in the manager
getListenerCore().unregisterListeners();
}

int pluginId = 16872;
/**
* Checks for updates and notifies the user via a log to console
* getDescription() is still used because of the usage of a plugin.yml.
* Not planned to change
*/
public void updateChecker() {
new UpdateChecker(this, 106243).getVersion(version -> {
if (!this.getDescription().getVersion().equals(version)) {
Utils.warn("|---[ BLOONS ]-------------------------------------------------------|");
Utils.warn("| There is a new update available! |");
Utils.warn("| https://jeqo.net/spigot/bloons |");
Utils.warn("|-------------------------------------------------[ MADE BY JEQO ]---|");
Logger.logUpdateNotificationConsole();
}
});
}

private void loadListeners() {
getServer().getPluginManager().registerEvents(new LeashHandlers(), this);
getServer().getPluginManager().registerEvents(new PlayerHandlers(), this);
getServer().getPluginManager().registerEvents(new MenuHandlers(), this);
}
public static String getMessage(String id, String arg) {
return Utils.hex(String.format(getInstance().getConfig().getString("messages." + id, ""), arg));
}

public static String getMessage(String id) {
return Utils.hex(getInstance().getConfig().getString("messages." + id, ""));
}

public static String getString(String path) {
return getInstance().getConfig().getString(path);
}

public static Integer getInt(String path) {
return getInstance().getConfig().getInt(path);
}
}
220 changes: 220 additions & 0 deletions src/main/java/net/jeqo/bloons/balloon/SingleBalloon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package net.jeqo.bloons.balloon;

import lombok.Getter;
import lombok.Setter;
import net.jeqo.bloons.Bloons;
import net.jeqo.bloons.configuration.BalloonConfiguration;
import net.jeqo.bloons.logger.Logger;
import net.jeqo.bloons.utils.BalloonManagement;
import net.jeqo.bloons.utils.ColorManagement;
import net.jeqo.bloons.utils.MessageTranslations;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Chicken;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.EulerAngle;
import org.bukkit.util.Vector;

import java.util.concurrent.ThreadLocalRandom;

@Getter @Setter
public class SingleBalloon extends BukkitRunnable {
/** Al physical elements of balloon **/
private Player player;
private ItemStack balloonVisual;
private ArmorStand balloonArmorStand;
public Chicken balloonChicken;

/** Location Data **/
private Location playerLocation;
private Location moveLocation;

/** Movement Data **/
private int ticks = 0;
private float targetYaw = 0.0F;

public SingleBalloon(Player player, String balloonID) {
this.setPlayer(player);

// Configure the balloon visual elements
this.setBalloonVisual(getConfiguredBalloonVisual(balloonID));
}

/**
* What runs inside the extended bukkit runnable
* Core functionality of how the balloon moves
*/
public void run() {
if (this.getBalloonArmorStand() == null) initializeBalloon();

Location playerLocation = this.getPlayerLocation();
playerLocation.setYaw(this.getPlayerLocation().getYaw());

// If the ticks reach 20, set back to 0 and set a new target yaw
if (this.getTicks() == 20) {
this.setTargetYaw(ThreadLocalRandom.current().nextInt(10) - 5);
this.setTicks(0);
}

if (this.getTargetYaw() > playerLocation.getYaw()) {
playerLocation.setYaw(playerLocation.getYaw() + 0.2F);
} else if (this.getTargetYaw() < playerLocation.getYaw()) {
playerLocation.setYaw(playerLocation.getYaw() - 0.2F);
}

this.setMoveLocation(this.getBalloonArmorStand().getLocation().subtract(0.0D, 2.0D, 0.0D).clone());

Vector vector = playerLocation.toVector().subtract(this.getMoveLocation().toVector());
vector.multiply(0.3D);
this.setMoveLocation(this.getMoveLocation().add(vector));
double vectorZ = vector.getZ() * 50.0D * -1.0D;
double vectorX = vector.getX() * 50.0D * -1.0D;
this.getBalloonArmorStand().setHeadPose(new EulerAngle(Math.toRadians(vectorZ), Math.toRadians(playerLocation.getYaw()), Math.toRadians(vectorX)));

this.teleport(this.getMoveLocation());
this.setPlayerLocation(this.getPlayer().getLocation());
this.getPlayerLocation().setYaw(playerLocation.getYaw());
this.setTicks(this.getTicks() + 1);
}

/**
* Cancels the current bukkit runnable instance and kills off the entities
* @throws IllegalStateException If the task has already been cancelled
*/
public synchronized void cancel() throws IllegalStateException {
this.getBalloonArmorStand().remove();
this.getBalloonChicken().remove();
super.cancel();
}

/**
* Spawns the particle effect when the balloon is removed
*/
public void spawnRemoveParticle() {
this.getMoveLocation().getWorld().spawnParticle(Particle.CLOUD, this.getMoveLocation(), 5, 0.0D, 0.0D, 0.0D, 0.1D);
}

/**
* Teleports the balloon's entities to a specific location
* @param location The location to teleport the balloon to
*/
private void teleport(Location location) {
this.getBalloonArmorStand().teleport(location.add(0.0D, 2.0D, 0.0D));
this.getBalloonChicken().teleport(location.add(0.0D, 1.2D, 0.0D));
}

/**
* Initializes the balloon. Sets the current players location, and initializes the armor stand, and chicken entities
*/
private void initializeBalloon() {
this.setPlayerLocation(this.getPlayer().getLocation());
this.getPlayerLocation().setYaw(0.0F);

ItemMeta meta = this.getBalloonVisual().getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
this.getBalloonVisual().setItemMeta(meta);

this.initializeBalloonArmorStand();
this.initializeBalloonChicken();
}

/**
* Gets the item stack object of the visual appearance of the balloon
* @param balloonID The balloon ID to get the visual appearance of
* @return The item stack object of the visual appearance of the balloon
*/
public ItemStack getConfiguredBalloonVisual(String balloonID) {
MessageTranslations messageTranslations = new MessageTranslations(Bloons.getInstance());

ConfigurationSection balloonConfiguration = Bloons.getInstance().getConfig().getConfigurationSection("balloons." + balloonID);

if (balloonConfiguration == null) {
Logger.logWarning("The balloon " + balloonID + " is not set in the configuration!");
return null;
}

if (balloonConfiguration.getString("material") == null) {
Logger.logWarning("The material of the balloon " + balloonID + " is not set!");
return null;
}

ItemStack item = new ItemStack(Material.valueOf(balloonConfiguration.getString("material")));
ItemMeta meta = item.getItemMeta();
meta.setCustomModelData(balloonConfiguration.getInt("custom-model-data"));

if (messageTranslations.getString("balloons." + balloonID + ".color") != null) {
if (!messageTranslations.getString("balloons." + balloonID + ".color").equalsIgnoreCase("potion")) {
LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) meta;
leatherArmorMeta.setColor(ColorManagement.hexToColor(messageTranslations.getString("balloons." + balloonID + ".color")));
} else {
Logger.logWarning("The color of the balloonVisual " + balloonID + " is set, but the material is not a leather item!");
}
}
item.setItemMeta(meta);

return item;
}

/**
* Initializes the balloon's armor stand entity
*/
public void initializeBalloonArmorStand() {
this.setBalloonArmorStand(this.getPlayerLocation().getWorld().spawn(this.getPlayerLocation(), ArmorStand.class));
this.getBalloonArmorStand().setBasePlate(false);
this.getBalloonArmorStand().setVisible(false);
this.getBalloonArmorStand().setInvulnerable(true);
this.getBalloonArmorStand().setCanPickupItems(false);
this.getBalloonArmorStand().setGravity(false);
this.getBalloonArmorStand().setSmall(false);
this.getBalloonArmorStand().setMarker(true);
this.getBalloonArmorStand().setCollidable(false);
this.getBalloonArmorStand().getEquipment().setHelmet(this.getBalloonVisual());
this.getBalloonArmorStand().customName(Component.text(BalloonConfiguration.BALLOON_ARMOR_STAND_ID));
}

/**
* Initializes the balloon's chicken entity
*/
public void initializeBalloonChicken() {
this.setBalloonChicken(this.getPlayerLocation().getWorld().spawn(this.getPlayerLocation(), Chicken.class));
this.getBalloonChicken().setInvulnerable(true);
this.getBalloonChicken().setInvisible(true);
this.getBalloonChicken().setSilent(true);
this.getBalloonChicken().setBaby();
this.getBalloonChicken().setAgeLock(true);
this.getBalloonChicken().setAware(false);
this.getBalloonChicken().setCollidable(false);
this.getBalloonChicken().setLeashHolder(this.getPlayer());
this.getBalloonChicken().customName(Component.text(BalloonConfiguration.BALLOON_CHICKEN_ID));
}

/**
* Checks if a balloon needs to be removed or added
* @param player The player to check
* @param balloonID The balloon ID to check
*/
public static void checkBalloonRemovalOrAdd(final Player player, final String balloonID) {
new BukkitRunnable() {
public void run() {
SingleBalloon owner = Bloons.playerBalloons.get(player.getUniqueId());
if (owner != null) return;

BalloonManagement.removeBalloon(player, null);
SingleBalloon balloonOwner = new SingleBalloon(player, balloonID);
balloonOwner.runTaskTimer(Bloons.getInstance(), 0L, 1L);
Bloons.playerBalloons.put(player.getUniqueId(), balloonOwner);
Bloons.playerBalloonID.put(player.getUniqueId(), balloonID);

}
}.runTaskLater(Bloons.getInstance(), 1L);
}
}
Loading

0 comments on commit 184d3a0

Please sign in to comment.