Skip to content

Commit

Permalink
Added commands and listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
itzCrih committed Dec 15, 2023
1 parent dcc40c1 commit 5c98c81
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 4 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -68,5 +72,11 @@
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
112 changes: 108 additions & 4 deletions src/main/java/it/itzcrih/coralwinter/CoralWinter.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,132 @@
package it.itzcrih.coralwinter;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import it.itzcrih.coralwinter.commands.CoralWinterCommand;
import it.itzcrih.coralwinter.commands.SantaShovelCommand;
import it.itzcrih.coralwinter.commands.cwReloadCommand;
import it.itzcrih.coralwinter.config.ConfigLoader;
import it.itzcrih.coralwinter.listeners.SnowBreakListener;
import it.itzcrih.coralwinter.listeners.SnowballDamageListener;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.lang.reflect.InvocationTargetException;
import java.util.Random;

/**
* CoralWinter
* <p>
* A CoralMC Snowball fight plugin replica
* made with love by
*
* @author itzCrih
*/

public final class CoralWinter extends JavaPlugin {
private static CoralWinter instance;
private static ConfigLoader config;
private ProtocolManager protocolManager;

@Override
public void onEnable() {
instance = this;
getLogger().info("--------------------------------------");
getLogger().info("Enabling CoralWinter v" + getDescription().getVersion() + "...");
initConfig();
checkPluginYML();

if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
protocolManager = ProtocolLibrary.getProtocolManager();
getServer().getConsoleSender().sendMessage("ProtocolLib detected!");

new BukkitRunnable() {
@Override
public void run() {
makeItSnow();
}
}.runTaskTimer(this, 0, 20);
} else {
getLogger().severe("ProtocolLib non trovato!");
getServer().getConsoleSender().sendMessage("Se desideri far funzionare le particelle di neve devi installarlo.");
}

getLogger().info("------------------------------");
getLogger().info("CoralWinter v" + getDescription().getVersion());
getLogger().info("CoralMC plugin replica made by itzCrih");
getLogger().info("--------------------------------------");
getLogger().info("------------------------------");

getCommand("coralwinter").setExecutor(new CoralWinterCommand());
getCommand("santashovel").setExecutor(new SantaShovelCommand());
getCommand("cwreload").setExecutor(new cwReloadCommand());

Bukkit.getPluginManager().registerEvents(new SnowBreakListener(), this);
Bukkit.getPluginManager().registerEvents(new SnowballDamageListener(), this);

getLogger().info(ChatColor.GREEN + "CoralWinter has been enabled successfully!");
}

@Override
public void onDisable() {
instance = null;
getLogger().info("CoralWinter has been disabled successfully!");
}

public static CoralWinter getInstance() {
return instance;
}

public void initConfig() throws RuntimeException {
instance = this;
try {
config = new ConfigLoader(this);
} catch (Exception e) {
throw new RuntimeException(config.getMessages().getString("error.config_loading"));
}
}

public static ConfigLoader getConfigLoader() {
return config;
}

private void checkPluginYML() {
if (!this.getDescription().getAuthors().contains("itzCrih") || !this.getDescription().getName().contains("CoralWinter")) {
for (int i = 0; i < 25; i++) {
Bukkit.getServer().broadcastMessage(ChatColor.RED + "Why are you changing the");
Bukkit.getServer().broadcastMessage(ChatColor.RED + "plugin.yml? ( ͡° ͜ʖ ͡°)╭∩╮");
}
Bukkit.getPluginManager().disablePlugin(this);
}
}

private void makeItSnow() {
for (Player player : Bukkit.getOnlinePlayers()) {
Location playerLocation = player.getLocation();
Random random = new Random();

for (int i = 0; i < 50; i++) {
double radius = 10;
double angle = random.nextDouble() * Math.PI * 2;
double offsetX = Math.cos(angle) * radius * random.nextDouble();
double offsetZ = Math.sin(angle) * radius * random.nextDouble();

PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.WORLD_PARTICLES);
packet.getParticles().write(0, EnumWrappers.Particle.SNOW_SHOVEL);
packet.getFloat().write(0, (float) (playerLocation.getX() + offsetX));
packet.getFloat().write(1, (float) (playerLocation.getY() + 2 + random.nextDouble() * 2));
packet.getFloat().write(2, (float) (playerLocation.getZ() + offsetZ));
packet.getFloat().write(3, (float) 0);
packet.getFloat().write(4, -0.05f);
packet.getFloat().write(5, (float) 0);
packet.getFloat().write(6, 0.1f);
packet.getIntegers().write(0, 1);

protocolManager.sendServerPacket(player, packet);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package it.itzcrih.coralwinter.commands;

import it.itzcrih.coralwinter.CoralWinter;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class CoralWinterCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("coralwinter")) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (player.hasPermission("coralwinter.command.help")) {
sendHelpMessages(player);
} else {
player.sendMessage(ChatColor.GRAY + "Running " + ChatColor.AQUA + "CoralWinter " + ChatColor.GRAY + "v" + CoralWinter.getInstance().getDescription().getVersion() + " by itzCrih");
}
return true;
} else {
sender.sendMessage("This command can be executed only as a player");
return true;
}
}
return false;
}

private void sendHelpMessages(Player player) {
player.sendMessage("§b§lCORAL§f§lWINTER §7v" + CoralWinter.getInstance().getDescription().getVersion());
player.sendMessage("");
player.sendMessage("§3/cwreload §7- Reload config and messages");
player.sendMessage("§3/santashovel §7- Get your own shovel");
player.sendMessage("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package it.itzcrih.coralwinter.commands;

import it.itzcrih.coralwinter.CoralWinter;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;

public class SantaShovelCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("santashovel")) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (player.hasPermission("coralwinter.command.santashovel")) {
giveSantaShovel(player);
} else {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CoralWinter.getConfigLoader().getMessages().getString("errors.no_perm")));
}
return true;
} else {
sender.sendMessage("This command can be executed only as a player");
return true;
}
}
return false;
}

private void giveSantaShovel(Player player) {
ItemStack santaShovel = createSantaShovel();

player.getInventory().addItem(santaShovel);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CoralWinter.getConfigLoader().getMessages().getString("commands.shovel_received")));
}

private ItemStack createSantaShovel() {
ItemStack santaShovel = new ItemStack(Material.DIAMOND_SPADE, 1);
ItemMeta meta = santaShovel.getItemMeta();

String displayName = CoralWinter.getConfigLoader().getConfig().getString("santashovel.display-name");
List<String> lore = CoralWinter.getConfigLoader().getConfig().getStringList("santashovel.lore");

meta.setDisplayName(displayName != null ? colorize(displayName) : "Santa's Shovel");
if (lore != null && !lore.isEmpty()) {
meta.setLore(colorize(lore));
}

santaShovel.setItemMeta(meta);
return santaShovel;
}

private String colorize(String message) {
return message.replace("&", "§");
}

private List<String> colorize(List<String> messages) {
messages.replaceAll(this::colorize);
return messages;
}
}
30 changes: 30 additions & 0 deletions src/main/java/it/itzcrih/coralwinter/commands/cwReloadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package it.itzcrih.coralwinter.commands;

import it.itzcrih.coralwinter.CoralWinter;
import it.itzcrih.coralwinter.config.ConfigLoader;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class cwReloadCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = (Player) sender;

if (!sender.hasPermission("coralwinter.command.reload")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CoralWinter.getConfigLoader().getMessages().getString("errors.no_perm")));
return true;
}

sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CoralWinter.getConfigLoader().getMessages().getString("commands.reloaded_successfully")));
player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0f, 1.0f);

ConfigLoader config = CoralWinter.getConfigLoader();
config.reloadConfig();
config.reloadMessages();
return false;
}
}
64 changes: 64 additions & 0 deletions src/main/java/it/itzcrih/coralwinter/config/ConfigLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package it.itzcrih.coralwinter.config;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

public class ConfigLoader {

private final JavaPlugin plugin;
private FileConfiguration config;
private FileConfiguration messages;

public ConfigLoader(JavaPlugin plugin) {
this.plugin = plugin;
reloadConfig();
reloadMessages();
}

public void reloadConfig() {
File configFile = new File(plugin.getDataFolder(), "settings.yml");
if (!configFile.exists()) {
plugin.saveResource("settings.yml", false);
}
config = YamlConfiguration.loadConfiguration(configFile);
}

public void saveConfig() {
File configFile = new File(plugin.getDataFolder(), "settings.yml");
try {
config.save(configFile);
} catch (IOException e) {
e.printStackTrace();
}
}

public void reloadMessages() {
File messagesFile = new File(plugin.getDataFolder(), "messages.yml");
if (!messagesFile.exists()) {
plugin.saveResource("messages.yml", false);
}
messages = YamlConfiguration.loadConfiguration(messagesFile);
}

public void saveMessages() {
File messagesFile = new File(plugin.getDataFolder(), "messages.yml");
try {
messages.save(messagesFile);
} catch (IOException e) {
e.printStackTrace();
}
}

public FileConfiguration getConfig() {
return config;
}

public FileConfiguration getMessages() {
return messages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package it.itzcrih.coralwinter.listeners;

import it.itzcrih.coralwinter.CoralWinter;
import it.itzcrih.coralwinter.utils.SnowUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;

public class SnowBreakListener implements Listener {

@EventHandler
public void onSnowBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
Block block = event.getBlock();

if (SnowUtils.canBreakSnowBlock(player, block)) {
SnowUtils.giveSnowball(player);
block.setType(Material.AIR);
event.setCancelled(true);

Bukkit.getScheduler().runTaskLater(CoralWinter.getInstance(), () -> {
if (block.getType() == Material.AIR) {
block.setType(Material.SNOW);
}
}, 7 * 20);
}
}
}
Loading

0 comments on commit 5c98c81

Please sign in to comment.