Skip to content

Commit

Permalink
Add util options
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jan 10, 2025
1 parent d8812ad commit 35877c6
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
import io.papermc.paper.event.player.AsyncChatEvent;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import net.kyori.adventure.text.Component;
Expand All @@ -20,6 +21,9 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
Expand All @@ -36,6 +40,7 @@
public class GlobalLinkServer extends JavaPlugin implements Listener {
public static Logger LOGGER;
public static LinkManager linkManager;
public static Config config;

public final static Component LINK_INSTRUCTIONS = Component.text("Run the ").color(NamedTextColor.AQUA)
.append(Component.text("`/link`", NamedTextColor.GREEN))
Expand All @@ -49,7 +54,7 @@ public class GlobalLinkServer extends JavaPlugin implements Listener {
public void onEnable() {
LOGGER = getLogger();

Config config = ConfigReader.readConfig(this);
config = ConfigReader.readConfig(this);

linkManager = new LinkManager(config);
CommandUtils commandUtils = new CommandUtils(linkManager);
Expand Down Expand Up @@ -128,11 +133,52 @@ public void onCommands(PlayerCommandSendEvent event) {

@EventHandler
public void onPlayerLoad(PlayerJoinEvent event) {
if (config.util().hideJoinLeaveMessages()) event.joinMessage(null);
Utils.processJoin(event.getPlayer());

if (config.util().hidePlayers()) {
Bukkit.getOnlinePlayers().forEach(player -> {
event.getPlayer().hidePlayer(this, player);
player.hidePlayer(this, event.getPlayer());
});
}

if (config.util().respawnOnJoin()) Utils.fakeRespawn(event.getPlayer());
}

@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
if (config.util().hideJoinLeaveMessages()) event.quitMessage(null);
Utils.processLeave(event.getPlayer());
}

@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
if (config.util().voidTeleport()
&& event.getEntity() instanceof Player player
&& event.getCause() == EntityDamageEvent.DamageCause.VOID) {
event.setCancelled(true);

Utils.fakeRespawn(player);
}
}

@EventHandler
public void onFoodLevelChange(FoodLevelChangeEvent event) {
if (config.util().preventHunger()) {
if (event.getFoodLevel() < event.getEntity().getFoodLevel()) {
event.setCancelled(true);
}
}
}

@EventHandler
public void onDeath(PlayerDeathEvent event) {
if (config.util().hideDeathMessages()) event.deathMessage(null);
}

@EventHandler
public void onPlayerChat(AsyncChatEvent event) {
if (config.util().disableChat()) event.setCancelled(true);
}
}
25 changes: 20 additions & 5 deletions src/main/java/org/geysermc/globallinkserver/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@
package org.geysermc.globallinkserver.config;

public record Config(
String hostname,
String username,
String password,
String database
) {}
Database database,
Util util
) {
public record Database(
String hostname,
String username,
String password,
String database
) {}

public record Util(
boolean hideJoinLeaveMessages,
boolean hideDeathMessages,
boolean hidePlayers,
boolean disableChat,
boolean voidTeleport,
boolean preventHunger,
boolean respawnOnJoin
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ public static Config readConfig(JavaPlugin plugin) {
var config = plugin.getConfig();
plugin.saveConfig();

return new Config(config.getString("hostname"),
config.getString("username"),
config.getString("password"),
config.getString("database"));
var database = config.getConfigurationSection("database");
var util = config.getConfigurationSection("util");

return new Config(new Config.Database(
database.getString("hostname"),
database.getString("username"),
database.getString("password"),
database.getString("database")
), new Config.Util(
util.getBoolean("hide-join-leave-messages"),
util.getBoolean("hide-death-messages"),
util.getBoolean("hide-players"),
util.getBoolean("disable-chat"),
util.getBoolean("void-teleport"),
util.getBoolean("prevent-hunger"),
util.getBoolean("respawn-on-join")
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class LinkManager {
public LinkManager(Config config) {
try {
Class.forName("org.mariadb.jdbc.Driver");
dataSource = new MariaDbPoolDataSource("jdbc:mariadb://" + config.hostname() + "/" + config.database()
+ "?user=" + config.username() + "&password=" + config.password() + "&minPoolSize=1&maxPoolSize=3");
dataSource = new MariaDbPoolDataSource("jdbc:mariadb://" + config.database().hostname() + "/" + config.database().database()
+ "?user=" + config.database().username() + "&password=" + config.database().password() + "&minPoolSize=1&maxPoolSize=3");
} catch (ClassNotFoundException exception) {
throw new RuntimeException("Cannot find required class to load the MariaDB database");
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/geysermc/globallinkserver/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
Expand Down Expand Up @@ -88,4 +90,10 @@ public static void sendCurrentLinkInfo(Player player) {
);
}
}

public static void fakeRespawn(Player player) {
player.teleport(player.getWorld().getSpawnLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
player.setFallDistance(0);
player.setHealth(player.getAttribute(Attribute.MAX_HEALTH).getValue());
}
}
18 changes: 14 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
hostname: 127.0.0.1
username: global_link
password: some_pass
database: global_link_dev
database:
hostname: 127.0.0.1
username: global_link
password: some_pass
database: global_link_dev

util:
hide-join-leave-messages: true
hide-death-messages: true
hide-players: true
disable-chat: true
void-teleport: true
prevent-hunger: true
respawn-on-join: true

0 comments on commit 35877c6

Please sign in to comment.