-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: reorganized Roflan.java style: moved Brand task to split file
- Loading branch information
Showing
6 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/me/shockpast/roflan/listeners/ChatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
src/main/java/me/shockpast/roflan/runnables/BrandRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters