Skip to content

Commit

Permalink
Add support to the future 0.1.9 mod version.
Browse files Browse the repository at this point in the history
Code clean-up.
Add reset (since protocol 3) and check commands.
  • Loading branch information
SuperNeon4ik committed Apr 22, 2023
1 parent 218dc85 commit dfad3f0
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 131 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.superneon4ik</groupId>
<artifactId>NoxesiumUtils</artifactId>
<version>1.0</version>
<version>1.1</version>
<packaging>jar</packaging>
<url>https://github.com/SuperNeon4ik/NoxesiumUtils</url>

Expand Down Expand Up @@ -75,6 +75,11 @@
<artifactId>commandapi-shade</artifactId>
<version>8.8.0</version>
</dependency>
<dependency>
<groupId>dev.jorel</groupId>
<artifactId>commandapi-annotations</artifactId>
<version>8.8.0</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down
229 changes: 141 additions & 88 deletions src/main/java/me/superneon4ik/noxesiumutils/NoxesiumUtils.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
public class NoxesiumBukkitListener implements Listener {
@EventHandler
public void on(PlayerQuitEvent event) {
// Remove offline player from the noxesium player list
// Remove offline player from the cached values
NoxesiumUtils.getNoxesiumPlayers().remove(event.getPlayer().getUniqueId());
NoxesiumUtils.getNoxesiumClientSettings().remove(event.getPlayer().getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import io.netty.buffer.Unpooled;
import me.superneon4ik.noxesiumutils.NoxesiumUtils;
import me.superneon4ik.noxesiumutils.modules.FriendlyByteBuf;
import me.superneon4ik.noxesiumutils.modules.NoxesiumServerRuleBuilder;
import me.superneon4ik.noxesiumutils.objects.PlayerClientSettings;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class NoxesiumMessageListener implements PluginMessageListener {
@Override
public void onPluginMessageReceived(String channel, @NotNull Player player, byte @NotNull [] message) {
if (channel.equals(NoxesiumUtils.NOXESIUM_CLIENT_INFORMATION_CHANNEL)) {
if (channel.equals(NoxesiumUtils.NOXESIUM_LEGACY_CLIENT_INFORMATION_CHANNEL) || channel.equals(NoxesiumUtils.NOXESIUM_V1_CLIENT_INFORMATION_CHANNEL)) {
// Get player's noxesium protocol version.
FriendlyByteBuf incBuf = new FriendlyByteBuf(Unpooled.copiedBuffer(message));
int protocolVersion = incBuf.readInt();

int protocolVersion;
if (channel.equals(NoxesiumUtils.NOXESIUM_LEGACY_CLIENT_INFORMATION_CHANNEL)) protocolVersion = incBuf.readInt();
else protocolVersion = incBuf.readVarInt();

NoxesiumUtils.getPlugin().getLogger().info(player.getName() + " has Noxesium installed. (ProtocolVersion: " + protocolVersion + ")");
NoxesiumUtils.getNoxesiumPlayers().put(player.getUniqueId(), protocolVersion);

Expand All @@ -26,63 +29,68 @@ public void onPluginMessageReceived(String channel, @NotNull Player player, byte
new BukkitRunnable() {
@Override
public void run() {
// Create buffers
FriendlyByteBuf valuesBuffer = new FriendlyByteBuf(Unpooled.buffer());
List<Integer> modifiedRules = new ArrayList<>();
// Create builder
var builder = new NoxesiumServerRuleBuilder(protocolVersion);

if (protocolVersion >= 1) {
// Tridents
if (NoxesiumUtils.getPlugin().getConfig().contains("defaults.disableAutoSpinAttack")) {
modifiedRules.add(0);
valuesBuffer.writeInt(0);
valuesBuffer.writeBoolean(NoxesiumUtils.getPlugin().getConfig().getBoolean("defaults.disableAutoSpinAttack", false));
builder.add(0, NoxesiumUtils.getPlugin().getConfig().getBoolean("defaults.disableAutoSpinAttack", false));
}
// Global Can Place On
if (NoxesiumUtils.getPlugin().getConfig().contains("defaults.globalCanPlaceOn")) {
var blocks = NoxesiumUtils.getPlugin().getConfig().getStringList("defaults.globalCanPlaceOn");
modifiedRules.add(1);
valuesBuffer.writeInt(1);
valuesBuffer.writeVarInt(blocks.size());
for (String string : blocks) {
valuesBuffer.writeUtf(string);
}
builder.add(1, blocks);
}
// Global Can Destroy
if (NoxesiumUtils.getPlugin().getConfig().contains("defaults.globalCanDestroy")) {
var blocks = NoxesiumUtils.getPlugin().getConfig().getStringList("defaults.globalCanDestroy");
modifiedRules.add(2);
valuesBuffer.writeInt(2);
valuesBuffer.writeVarInt(blocks.size());
for (String string : blocks) {
valuesBuffer.writeUtf(string);
}
builder.add(2, blocks);
}
}
if (protocolVersion >= 2) {
// Held Item Name Offset
if (NoxesiumUtils.getPlugin().getConfig().contains("defaults.heldItemNameOffset")) {
modifiedRules.add(3);
valuesBuffer.writeInt(3);
valuesBuffer.writeVarInt(NoxesiumUtils.getPlugin().getConfig().getInt("defaults.heldItemNameOffset", 0));
builder.add(3, NoxesiumUtils.getPlugin().getConfig().getInt("defaults.heldItemNameOffset", 0));
}
// Camera Locked
if (NoxesiumUtils.getPlugin().getConfig().contains("defaults.cameraLocked")) {
modifiedRules.add(4);
valuesBuffer.writeInt(4);
valuesBuffer.writeBoolean(NoxesiumUtils.getPlugin().getConfig().getBoolean("defaults.cameraLocked", false));
builder.add(4, NoxesiumUtils.getPlugin().getConfig().getBoolean("defaults.cameraLocked", false));
}
}

// Build the final byte buffer
FriendlyByteBuf finalBuffer = new FriendlyByteBuf(Unpooled.buffer());
finalBuffer.writeVarIntArray(modifiedRules);
finalBuffer.writeInt(modifiedRules.size());
finalBuffer.writeBytes(valuesBuffer.array());
// Send packet
player.sendPluginMessage(NoxesiumUtils.getPlugin(), NoxesiumUtils.NOXESIUM_SERVER_RULE_CHANNEL, finalBuffer.array());
NoxesiumUtils.sendServerRulesPacket(player, builder.build());
}
}.runTaskLater(NoxesiumUtils.getPlugin(), 5);
}
}
else if (channel.equals(NoxesiumUtils.NOXESIUM_LEGACY_CLIENT_SETTINGS_CHANNEL) || channel.equals(NoxesiumUtils.NOXESIUM_V1_CLIENT_SETTINGS_CHANNEL)) {
FriendlyByteBuf incBuf = new FriendlyByteBuf(Unpooled.copiedBuffer(message));
int protocolVersion = NoxesiumUtils.getPlayerProtocolVersion(player.getUniqueId());

if (protocolVersion >= 3) {
int guiScale = incBuf.readVarInt();
double internalGuiScale = incBuf.readDouble();
int scaledWidth = incBuf.readVarInt();
int scaledHeight = incBuf.readVarInt();
boolean enforceUnicode = incBuf.readBoolean();
boolean touchscreenMode = incBuf.readBoolean();
double notificationDisplayTime = incBuf.readDouble();

var playerClientSettings = new PlayerClientSettings(
guiScale, internalGuiScale, scaledWidth, scaledHeight, enforceUnicode, touchscreenMode, notificationDisplayTime
);
NoxesiumUtils.getNoxesiumClientSettings().put(player.getUniqueId(), playerClientSettings);
}
else if (protocolVersion >= 1) {
int guiScale = incBuf.readVarInt();
boolean enforceUnicode = incBuf.readBoolean();
var playerClientSettings = new PlayerClientSettings(
guiScale, null, null, null, enforceUnicode, null, null
);
NoxesiumUtils.getNoxesiumClientSettings().put(player.getUniqueId(), playerClientSettings);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package me.superneon4ik.noxesiumutils.modules;

import io.netty.buffer.Unpooled;
import lombok.Getter;
import org.bukkit.Bukkit;

import java.util.ArrayList;
import java.util.List;

public class NoxesiumServerRuleBuilder {
private final FriendlyByteBuf valuesBuffer = new FriendlyByteBuf(Unpooled.buffer());
private final List<Integer> modifiedRules = new ArrayList<>();
@Getter private final int protocolVersion;

public NoxesiumServerRuleBuilder(int protocolVersion) {
this.protocolVersion = protocolVersion;
}

public NoxesiumServerRuleBuilder add(int index, boolean value) {
modifiedRules.add(index);
if (protocolVersion >= 3) valuesBuffer.writeVarInt(index);
else if (protocolVersion >= 1) valuesBuffer.writeInt(index);
valuesBuffer.writeBoolean(value);
return this;
}

public NoxesiumServerRuleBuilder add(int index, int value) {
modifiedRules.add(index);
if (protocolVersion >= 3) valuesBuffer.writeVarInt(index);
else if (protocolVersion >= 1) valuesBuffer.writeInt(index);
valuesBuffer.writeVarInt(value);
return this;
}

/**
* Add UTF List packet.
* @param index Packet index.
* @param values UTF Strings.
* @return Builder.
*/
public NoxesiumServerRuleBuilder add(int index, List<String> values) {
return add(index, values.toArray(new String[0]));
}

/**
* Add UTF List packet.
* @param index Packet index.
* @param values UTF Strings.
* @return Builder.
*/
public NoxesiumServerRuleBuilder add(int index, String... values) {
modifiedRules.add(index);
if (protocolVersion >= 3) valuesBuffer.writeVarInt(index);
else if (protocolVersion >= 1) valuesBuffer.writeInt(index);
valuesBuffer.writeVarInt(values.length);
for (String string : values) {
valuesBuffer.writeUtf(string);
}
return this;
}

/**
* Builds the final byte array.
* @return Packet bytes.
*/
public byte[] build() {
FriendlyByteBuf finalBuffer = new FriendlyByteBuf(Unpooled.buffer());
finalBuffer.writeVarIntArray(modifiedRules);
if (protocolVersion >= 3) finalBuffer.writeVarInt(modifiedRules.size());
else if (protocolVersion >= 1) finalBuffer.writeInt(modifiedRules.size());
finalBuffer.writeBytes(valuesBuffer.array());
return finalBuffer.array();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.superneon4ik.noxesiumutils.objects;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Getter
@AllArgsConstructor
public class PlayerClientSettings {
@NotNull Integer guiScale;
@Nullable Double internalGuiScale;
@Nullable Integer scaledWidth;
@Nullable Integer scaledHeight;
@NotNull Boolean enforceUnicode;
@Nullable Boolean touchscreenMode;
@Nullable Double notificationDisplayTime;

@Override
public String toString() {
return "PlayerClientSettings{" +
"guiScale=" + guiScale +
", internalGuiScale=" + internalGuiScale +
", scaledWidth=" + scaledWidth +
", scaledHeight=" + scaledHeight +
", enforceUnicode=" + enforceUnicode +
", touchscreenMode=" + touchscreenMode +
", notificationDisplayTime=" + notificationDisplayTime +
'}';
}
}
12 changes: 6 additions & 6 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# https://github.com/SuperNeon4ik/noxesium
# https://github.com/Noxcrew/noxesium
sendDefaultsOnJoin: false
defaults:
# comment out the line if you don't want to send that rule.
disableAutoSpinAttack: false # since protocol 1
globalCanPlaceOn: [] # unsupported. since protocol 1
globalCanDestroy: [] # unsupported. since protocol 1
heldItemNameOffset: 0 # unsupported. since protocol 2
cameraLocked: false # since protocol 2
# disableAutoSpinAttack: false # since protocol 1
# globalCanPlaceOn: [] # unsupported. since protocol 1
# globalCanDestroy: [] # unsupported. since protocol 1
# heldItemNameOffset: 0 # unsupported. since protocol 2
# cameraLocked: false # since protocol 2

0 comments on commit dfad3f0

Please sign in to comment.