This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Features: add settings for server brand
- Loading branch information
Showing
11 changed files
with
188 additions
and
1 deletion.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package net.flectone.testing; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
public class PacketSerializer { | ||
|
||
private final byte[] result; | ||
|
||
public PacketSerializer(String string) { | ||
ByteBuf buf = Unpooled.buffer(); | ||
writeString(string, buf); | ||
result = buf.array(); | ||
buf.release(); | ||
} | ||
|
||
private void writeString(String s, ByteBuf buf) { | ||
byte[] b = s.getBytes(StandardCharsets.UTF_8); | ||
writeVarInt(b.length, buf); | ||
buf.writeBytes(b); | ||
} | ||
|
||
private void writeVarInt(int value, ByteBuf output) { | ||
do { | ||
int part = value & 0x7F; | ||
value >>>= 7; | ||
if (value != 0) { | ||
part |= 0x80; | ||
} | ||
output.writeByte(part); | ||
} while (value != 0); | ||
} | ||
|
||
public byte[] toArray() { | ||
return result; | ||
} | ||
|
||
} |
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,79 @@ | ||
package net.flectone.testing; | ||
|
||
import net.flectone.Main; | ||
import net.flectone.managers.FileManager; | ||
import net.flectone.utils.ObjectUtil; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.Set; | ||
|
||
public class ServerBrand { | ||
|
||
private static ServerBrand instance; | ||
private static Field playerChannelsField; | ||
private String channel; | ||
|
||
public ServerBrand() { | ||
instance = this; | ||
|
||
try { | ||
Class.forName("org.bukkit.entity.Dolphin"); | ||
channel = "minecraft:brand"; | ||
} catch (ClassNotFoundException ignored) { | ||
channel = "MC|Brand"; | ||
} | ||
|
||
try { | ||
Method registerMethod = Main.getInstance().getServer().getMessenger().getClass().getDeclaredMethod("addToOutgoing", Plugin.class, String.class); | ||
registerMethod.setAccessible(true); | ||
registerMethod.invoke(Main.getInstance().getServer().getMessenger(), Main.getInstance(), channel); | ||
} catch (ReflectiveOperationException e) { | ||
throw new RuntimeException("Error while attempting to register plugin message channel", e); | ||
} | ||
|
||
updateEveryBrand(); | ||
} | ||
|
||
public static ServerBrand getInstance() { | ||
return instance; | ||
} | ||
|
||
public void setBrand(Player player) { | ||
if (playerChannelsField == null) { | ||
try { | ||
playerChannelsField = player.getClass().getDeclaredField("channels"); | ||
playerChannelsField.setAccessible(true); | ||
} catch (ReflectiveOperationException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
try { | ||
Set<String> channels = (Set<String>) playerChannelsField.get(player); | ||
channels.add(channel); | ||
} catch (ReflectiveOperationException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
updateBrand(player); | ||
} | ||
|
||
public void updateBrand(Player player) { | ||
String brandString = FileManager.locale.getStringList("server.brand.message").get(0); | ||
updateBrand(player, brandString); | ||
} | ||
|
||
public void updateBrand(Player player, String brandString) { | ||
player.sendPluginMessage(Main.getInstance(), channel, | ||
new PacketSerializer(ObjectUtil.formatString(brandString, player) + ChatColor.RESET).toArray()); | ||
} | ||
|
||
public void updateEveryBrand() { | ||
Bukkit.getOnlinePlayers().forEach(this::updateBrand); | ||
} | ||
} |
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,30 @@ | ||
package net.flectone.tickers; | ||
|
||
import net.flectone.managers.FileManager; | ||
import net.flectone.misc.runnables.FBukkitRunnable; | ||
import net.flectone.testing.ServerBrand; | ||
import org.bukkit.Bukkit; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ServerBrandTicker extends FBukkitRunnable { | ||
|
||
private static final List<String> brands = new ArrayList<>(); | ||
private static int index = 0; | ||
|
||
public ServerBrandTicker() { | ||
super.period = FileManager.config.getInt("server.brand.update.rate"); | ||
|
||
brands.clear(); | ||
brands.addAll(FileManager.locale.getStringList("server.brand.message")); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
int nextIndex = index++ % brands.size(); | ||
|
||
Bukkit.getOnlinePlayers().parallelStream().forEach(player -> | ||
ServerBrand.getInstance().updateBrand(player, brands.get(nextIndex))); | ||
} | ||
} |
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
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