Skip to content

Commit

Permalink
feat: added ChatModules
Browse files Browse the repository at this point in the history
chore: reorganized Roflan.java
style: moved Brand task to split file
  • Loading branch information
shockpast committed Jul 29, 2024
1 parent 22c34fd commit 14c182c
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 8 deletions.
11 changes: 9 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("java")
// id("io.papermc.paperweight.userdev") version "1.7.1"
id("io.papermc.paperweight.userdev") version "1.7.1"
id("xyz.jpenilla.run-paper") version "2.3.0"
}

Expand All @@ -17,12 +17,19 @@ repositories {
name = "sonatype"
url = "https://oss.sonatype.org/content/groups/public/"
}
maven {
url = "https://repo.dmulloy2.net/repository/public/"
}
}

dependencies {
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0")

compileOnly("org.projectlombok:lombok:1.18.30")
annotationProcessor("org.projectlombok:lombok:1.18.30")

// paperweight.paperDevBundle("1.21-R0.1-SNAPSHOT")
paperweight.paperDevBundle("1.21-R0.1-SNAPSHOT")
}

def targetJavaVersion = 21
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/me/shockpast/roflan/Roflan.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,43 @@

import me.shockpast.roflan.commands.*;
import me.shockpast.roflan.listeners.*;
import me.shockpast.roflan.runnables.BrandRunnable;

import org.bukkit.Server;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;

import lombok.Getter;

public final class Roflan extends JavaPlugin {
@Getter private static Roflan instance;

public ProtocolManager protocolManager;
public PluginManager pluginManager;
public SharedData data;

@Override
public void onEnable() {
saveResource("config.yml", false);
saveDefaultConfig();
Roflan.instance = this;

//
SharedData data = new SharedData();
// Accesible Fields that are shared between files.
protocolManager = ProtocolLibrary.getProtocolManager();
pluginManager = getServer().getPluginManager();
data = new SharedData();

//
Server server = getServer();
PluginManager pluginManager = server.getPluginManager();
BukkitScheduler scheduler = server.getScheduler();

//
saveDefaultConfig();

//
pluginManager.registerEvents(new ChatListener(this), this);
pluginManager.registerEvents(new PlayerListener(this, data), this);
pluginManager.registerEvents(new EntityListener(this), this);
pluginManager.registerEvents(new TweakListener(this), this);
Expand All @@ -29,5 +49,14 @@ public void onEnable() {
getCommand("report").setExecutor(new Report(data));
getCommand("item").setExecutor(new Item(this));
getCommand("tweak").setExecutor(new Tweak(this));

//
if (getConfig().getBoolean("features.custom_brand.enabled"))
scheduler.runTaskTimerAsynchronously(
this,
new BrandRunnable()::run,
getConfig().getLong("features.custom_brand.delay") * 20L,
getConfig().getLong("features.custom_brand.period") * 20L
);
}
}
42 changes: 42 additions & 0 deletions src/main/java/me/shockpast/roflan/listeners/ChatListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package me.shockpast.roflan.listeners;

import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;

public class ChatListener implements Listener {
private final FileConfiguration config;

public ChatListener(JavaPlugin plugin) {
this.config = plugin.getConfig();
}

@EventHandler
public void chatModuleEvent(AsyncChatEvent event) {
Player player = event.getPlayer();
if (player == null)
return;

Component message = event.message();

// FROM: i'm here :loc:
// TO: i'm here [10, 67, -103]
if (config.getBoolean("features.chat_modules.location")) {
Location location = player.getLocation();
String text = "[%d, %d, %d]".formatted((int)location.getX(), (int)location.getY(), (int)location.getZ());

message = message.replaceText(TextReplacementConfig.builder()
.matchLiteral(":loc:")
.replacement(text).once().build());
}

event.message(message);
}
}
66 changes: 66 additions & 0 deletions src/main/java/me/shockpast/roflan/runnables/BrandRunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package me.shockpast.roflan.runnables;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.injector.netty.WirePacket;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import me.shockpast.roflan.Roflan;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

// much love to https://github.com/LoreSchaeffer/CustomF3Brand
// i took his implementation
public class BrandRunnable {
private final MiniMessage miniMessage = MiniMessage.miniMessage();
private final ProtocolManager protocolManager;
private final FileConfiguration config;
private final List<String> names;
private Integer index = 0;

public BrandRunnable() {
this.protocolManager = Roflan.getInstance().protocolManager;
this.config = Roflan.getInstance().getConfig();

this.names = config.getStringList("features.custom_brand.names");
}

public void run() {
if (names.size() > 1 && index++ == names.size() - 1)
index = 0;

String name = names.get(index);

for (Player player : Bukkit.getOnlinePlayers()) {
try {
Class<?> dataSerializer = Class.forName("net.minecraft.network.PacketDataSerializer");
Constructor<?> dataConstructor = dataSerializer.getConstructor(ByteBuf.class);
ByteBuf buf = (ByteBuf)dataConstructor.newInstance(Unpooled.buffer());

Method writeString = dataSerializer.getDeclaredMethod("a", String.class);
writeString.invoke(buf, "minecraft:brand");
writeString.invoke(buf, LegacyComponentSerializer.legacySection()
.serialize(miniMessage.deserialize(name)));

byte[] data = new byte[buf.readableBytes()];
for (int i = 0; i < data.length; i++) data[i] = buf.getByte(i);

WirePacket packet = new WirePacket(PacketType.Play.Server.CUSTOM_PAYLOAD, data);
protocolManager.sendWirePacket(player, packet);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
20 changes: 19 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@ tweaks:
# players in creative will open containers
# regardless of it's block/lock state
creativeOpenContainerForcibly: false
# simply allows to clip inside of blocks
# while being in creative
creativeNoClip: false

# customization for some commands
commands:
item:
# defaults to 1, takes {n} amount of levels
# upon rename, set to -1 to don't take it all
renameCost: 1
renameCost: 1

# things are not done, or shouldn't be used
features:
custom_brand:
enabled: false
delay: 0 # long (in seconds, internally <delay * 20L>)
period: 3 # long (in seconds, internally <delay * 20L>)
names: # list of strings that will appear in brand
- line 1
- line 2
- line 3
chat_modules:
# FROM: i'm here :loc:
# TO: i'm here [10, 67, -103]
location: true
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: RoflanUtils
version: '1.0-SNAPSHOT'
main: me.shockpast.roflan.Roflan
api-version: '1.21'
depend: [ ProtocolLib ] # required only for BrandRunnable
load: STARTUP
commands:
vanish:
Expand Down

0 comments on commit 14c182c

Please sign in to comment.