Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hub: Code quality, enhancments and better maintainability #23

Open
wants to merge 15 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
.idea
/target
/.idea
*.iml
*.iml

# jdtls generation
.classpath
.factorypath
.project
.settings/
36 changes: 34 additions & 2 deletions zander-hub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,31 @@
<artifactId>zander-hub</artifactId>
<version>1.0</version>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<repositories>
<!-- PaperMC/WaterFall -->
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<!-- PremiumVanishAPI -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<!-- ProtocolLib -->
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -28,6 +47,19 @@
<version>1.20.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- PremiumVanishAPI -->
<dependency>
<groupId>com.github.LeonMangler</groupId>
<artifactId>PremiumVanishAPI</artifactId>
<version>2.9.0-4</version>
<scope>provided</scope>
</dependency>
<!-- ProtocolLib (dep PremiumVanishAPI) -->
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.3.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.modularsoft.zander.hub.configs.MessageConfig;

import java.io.File;

public class ConfigurationManager {
private static FileConfiguration welcomeFile;
private static MessageConfig messageConfig;

//
// Welcome File
Expand All @@ -23,7 +25,8 @@ public static void setupWelcomeFile() {
File existingWelcomeFile = new File(ZanderHubMain.plugin.getDataFolder(), "welcome.yml");

if (!existingWelcomeFile.exists()) {
// Reads the welcome.yml from the resource folder and then creates the file in the folder that
// Reads the welcome.yml from the resource folder and then creates the file in
// the folder that
// goes next to the plugin.
ZanderHubMain.plugin.saveResource("welcome.yml", false);
Bukkit.getServer().getConsoleSender().sendMessage(
Expand All @@ -43,17 +46,47 @@ public static FileConfiguration getWelcome() {

public static Location getHubLocation() {
World defaultworld = Bukkit.getServer().getWorlds().get(0);
World hubworld = Bukkit.getWorld(ZanderHubMain.plugin.getConfig().getString("hub.world", defaultworld.getName()));
World hubworld = Bukkit
.getWorld(ZanderHubMain.plugin.getConfig().getString("hub.world", defaultworld.getName()));
if (hubworld == null) {
Bukkit.getLogger().warning("No world by the name of " + defaultworld.getName() + " was found! Assuming default world.");
Bukkit.getLogger().warning(
"No world by the name of " + defaultworld.getName() + " was found! Assuming default world.");
hubworld = Bukkit.getServer().getWorlds().get(0);
}
double hubx = ZanderHubMain.plugin.getConfig().getDouble("hub.x", defaultworld.getSpawnLocation().getX());
double huby = ZanderHubMain.plugin.getConfig().getDouble("hub.y", defaultworld.getSpawnLocation().getY());
double hubz = ZanderHubMain.plugin.getConfig().getDouble("hub.z", defaultworld.getSpawnLocation().getZ());
float hubyaw = (float)ZanderHubMain.plugin.getConfig().getDouble("hub.yaw", 0);
float hubpitch = (float)ZanderHubMain.plugin.getConfig().getDouble("hub.pitch", 0);
float hubyaw = (float) ZanderHubMain.plugin.getConfig().getDouble("hub.yaw", 0);
float hubpitch = (float) ZanderHubMain.plugin.getConfig().getDouble("hub.pitch", 0);

return new Location(hubworld, hubx, huby, hubz, hubyaw, hubpitch);
}

public static void setupMessage() {
FileConfiguration config = ZanderHubMain.plugin.getConfig();

// ensure join/leave messages exist in 'config.yml'
boolean hasModifiedConfig = false;
if (!config.contains("messages.join")) {
ZanderHubMain.plugin.getConfig().set("messages.join", "&7%p% joined.");
hasModifiedConfig = true;
}
if (!config.contains("messages.leave")) {
ZanderHubMain.plugin.getConfig().set("messages.leave", "&7%p% left.");
hasModifiedConfig = true;
}
if (hasModifiedConfig) {
ZanderHubMain.plugin.saveConfig();
}

String messageJoin = ZanderHubMain.plugin.getConfig().getString("messages.join");
String messageLeave = ZanderHubMain.plugin.getConfig().getString("messages.leave");

messageConfig = new MessageConfig(ZanderHubMain.plugin);
messageConfig.setupJoinLeave(messageJoin, messageLeave);
}

public static MessageConfig getMessage() {
return messageConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.modularsoft.zander.hub.commands.fly;
import org.modularsoft.zander.hub.events.HubBoosterPlate;
import org.modularsoft.zander.hub.events.HubPlayerJoin;
Expand All @@ -12,8 +14,7 @@
import org.modularsoft.zander.hub.protection.HubCreatureSpawnProtection;
import org.modularsoft.zander.hub.protection.HubInteractionProtection;
import org.modularsoft.zander.hub.protection.HubProtection;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.modularsoft.zander.hub.events.HubPlayerLeave;

public class ZanderHubMain extends JavaPlugin {
public static ZanderHubMain plugin;
Expand All @@ -22,21 +23,22 @@ public void onEnable() {
plugin = this;

this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
// this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessageChannel(this));
// this.getServer().getMessenger().registerIncomingPluginChannel(this,
// "BungeeCord", new PluginMessageChannel(this));

// Init Message
TextComponent enabledMessage = Component.empty()
.color(NamedTextColor.GREEN)
.append(Component.text("\n\nZander Hub has been enabled.\n"))
// .append(Component.text("Running Version " + ZanderHubMain.class.getPackage().getImplementationVersion() + "\n"))
.append(Component.text("Running Version " + plugin.getDescription().getVersion() + "\n"))
.append(Component.text("Running Version " + plugin.getPluginMeta().getVersion() + "\n"))
.append(Component.text("GitHub Repository: https://github.com/ModularSoftAU/zander\n"))
.append(Component.text("Created by Modular Software\n\n", NamedTextColor.DARK_PURPLE));
getServer().sendMessage(enabledMessage);

// Event Registry
PluginManager pluginmanager = this.getServer().getPluginManager();
pluginmanager.registerEvents(new HubPlayerJoin(this), this);
pluginmanager.registerEvents(new HubPlayerLeave(this), this);
pluginmanager.registerEvents(new HubPlayerVoid(this), this);
pluginmanager.registerEvents(new HubBoosterPlate(this), this);
pluginmanager.registerEvents(new HubPlayerJoinChristmas(this), this);
Expand All @@ -53,8 +55,11 @@ public void onEnable() {

ConfigurationManager.getHubLocation();
saveConfig();

ConfigurationManager.setupMessage();
}

@Override
public void onDisable() {}
public void onDisable() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.modularsoft.zander.hub.configs;

import java.io.File;
import java.io.IOException;
import java.util.function.Consumer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.modularsoft.zander.hub.ZanderHubMain;

public class MessageConfig {
private final ZanderHubMain plugin;

// holds pre-deserialized legacy text
private TextComponent textCompJoinTemplate;
private TextComponent textCompLeaveTemplate;

public MessageConfig(ZanderHubMain plugin) {
this.plugin = plugin;
}

/// Configure the join/leave messages to be provided for the entire project.
/// Example string: "&7%p% joined the game" (%p% gets replaced by player name).
public void setupJoinLeave(String textLegacyJoin, String textLegacyLeave) {
int rc1 = parseToTemplate(textLegacyJoin, "&7%p% <default join message>", // * relied to be correct
(template) -> this.textCompJoinTemplate = template);
int rc2 = parseToTemplate(textLegacyLeave, "&7%p% <default leave message>", // * relied to be correct
(template) -> this.textCompLeaveTemplate = template);
if (rc1 == 0 && rc2 == 0) {
if (plugin.getServer().getPluginManager().getPlugin("PremiumVanish") != null) {
setPremiumVanish(textLegacyJoin, textLegacyLeave);
}
}
}

/// Retrieve the 'join' message for `playerName`.
public Component playerJoin(Component playerName) {
if (textCompJoinTemplate == null)
throw new IllegalStateException("Message not initialized, please run 'setupJoinLeave'");
return insertPlayerName(this.textCompJoinTemplate, playerName);
}

/// Retrieve the 'leave' message for `playerName`.
public Component playerLeave(Component playerName) {
if (textCompLeaveTemplate == null)
throw new IllegalStateException("Message not initialized, please run 'setupJoinLeave'");
return insertPlayerName(this.textCompLeaveTemplate, playerName);
}

/// Insert component `playerName` into `template`.
// * depends on 'parseToTemplate' who ensures `template` contains single '%p%'
private Component insertPlayerName(TextComponent template, Component playerName) {
return template.replaceText(builder -> builder
.match("%p%")
.replacement(playerName)); // * correctly retains any style
}

/// Parse `textLegacy` (+ verify placeholder %p%) into TextComponent template.
/// Returns 0 if successful (use custom text), 1 if failed (use default text).
private int parseToTemplate(String textLegacy, String textLegacyDefault, Consumer<TextComponent> setter) {
LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand();
try {
// verify the '%p%' exists exactly once
int placeholderIndex = textLegacy.indexOf("%p%");
if (placeholderIndex == -1 || placeholderIndex != textLegacy.lastIndexOf("%p%")) {
throw new IllegalArgumentException("Should contain a single '%p%' (placeholder player name)");
}
TextComponent template = serializer.deserialize(textLegacy);
setter.accept(template);
return 0;
} catch (Exception e) {
plugin.getLogger().warning(String.format("Invalid legacy string \"%s\"", textLegacy));
plugin.getLogger().warning(String.format("%s", e.getMessage()));

// * fall back to default
TextComponent template = serializer.deserialize(textLegacyDefault);
setter.accept(template);
return 1;
}
}

/// Set join/leave messages in PremiumVanish 'messages.yml' file.
private void setPremiumVanish(String textLegacyJoin, String textLegacyLeave) {
File pvYML = new File(plugin.getDataFolder().getParentFile(), "PremiumVanish/messages.yml");
if (pvYML.exists()) {
YamlConfiguration config = YamlConfiguration.loadConfiguration(pvYML);
config.set("Messages.DiscordSRVFakeJoin", textLegacyJoin);
config.set("Messages.DiscordSRVFakeQuit", textLegacyLeave);
config.set("Messages.ReappearMessage", textLegacyJoin);
config.set("Messages.VanishMessage", textLegacyLeave);
try {
config.save(pvYML);
} catch (IOException e) {
plugin.getLogger().warning("Failed to update PremiumVanish messages.yml");
}
}
}
}
Loading