Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianDev committed Dec 19, 2023
1 parent 034ce57 commit a330e9b
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.NotNull;

/**
* Represents information exchanged during the handshake.
*
Expand All @@ -19,28 +20,6 @@ public HandshakeInformation(@NotNull T value, @NotNull Function<T, String> facto
this.factory = factory;
}

public @NotNull T getValue() {
return value;
}

/**
* Returns a string representation of the current data.
* This will use the provided function to transform the object into string
* before sending it through the handshake packet.
*
* @return A string representation of the object.
*/
public String asString() {
return this.factory.apply(this.value);
}

@Override
public String toString() {
return "handshake_data[data:" + this.asString() + "]";
}

// Static Fields

/**
* Returns a new instance of HandshakeInformation with the provided information.
*
Expand Down Expand Up @@ -71,6 +50,7 @@ public static HandshakeInformation<Long> asLong(long information) {
return new HandshakeInformation<>(information, String::valueOf);
}

// Static Fields

/**
* Returns a new instance of HandshakeInformation with the provided value.
Expand All @@ -84,4 +64,24 @@ public static HandshakeInformation<Long> asLong(long information) {
public static <T> HandshakeInformation<T> of(@NotNull T value) {
return new HandshakeInformation<>(value, Object::toString);
}

public @NotNull T getValue() {
return value;
}

/**
* Returns a string representation of the current data.
* This will use the provided function to transform the object into string
* before sending it through the handshake packet.
*
* @return A string representation of the object.
*/
public String asString() {
return this.factory.apply(this.value);
}

@Override
public String toString() {
return "handshake_data[data:" + this.asString() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ public interface HandshakeDataInjectionEvent extends Event {
/**
* Injects the provided information into the handshake using the specified key.
*
* @param key The key associated with the information.
* @param information The information to be injected into the handshake.
* @param key The key associated with the information.
* @param information The information to be injected into the handshake.
*/
void inject(@NotNull String key, @NotNull HandshakeInformation<?> information);

/**
* Injects the provided information into the handshake using the specified module.
*
* @param module The module associated with the information.
* @param information The information to be injected into the handshake.
* @param module The module associated with the information.
* @param information The information to be injected into the handshake.
*/
void inject(@NotNull Module module, @NotNull HandshakeInformation<?> information);

/**
* Injects the provided information into the handshake packet associated with the given resource key.
*
* @param key The resource key that identifies the handshake packet.
* @param information The information to be injected into the handshake packet.
* @param key The resource key that identifies the handshake packet.
* @param information The information to be injected into the handshake packet.
*/
void inject(@NotNull ResourceKey key, @NotNull HandshakeInformation<?> information);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ public ResourceKey(@NotNull Module module, @NotNull String key) {
this.key = key;
}

public static ResourceKey fromText(@NotNull String text) {
Preconditions.checkArgument(text != null, "Text may not be null!");

if (!text.contains(":"))
throw new IllegalArgumentException("Text does not contain ':' separator!");

String[] split = text.split(":", 1);

String namespace = split[0];
String key = split[1];

if (!namespace.matches(REGEX) || !key.matches(REGEX))
throw new IllegalArgumentException("Text does not match with '" + REGEX + "'!");

return new ResourceKey(namespace, key);
}

/**
* Retrieves the namespace of the resource key.
* The namespace represents a unique identifier.
Expand Down Expand Up @@ -68,21 +85,4 @@ public boolean equals(Object obj) {
public int hashCode() {
return this.toString().hashCode();
}

public static ResourceKey fromText(@NotNull String text) {
Preconditions.checkArgument(text != null, "Text may not be null!");

if (!text.contains(":"))
throw new IllegalArgumentException("Text does not contain ':' separator!");

String[] split = text.split(":", 1);

String namespace = split[0];
String key = split[1];

if (!namespace.matches(REGEX) || !key.matches(REGEX))
throw new IllegalArgumentException("Text does not match with '" + REGEX + "'!");

return new ResourceKey(namespace, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import fr.atlasworld.network.api.networking.exception.packet.PacketExecutionException;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.networking.packet.PacketByteBuf;
import fr.atlasworld.network.api.registry.ResourceKey;
import fr.atlasworld.network.api.test.TestModule;
import org.jetbrains.annotations.NotNull;

public class MyCustomPacket implements NetworkPacket {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class Environment {
SYSTEM_EXECUTOR = new NioEventLoopGroup(AVAILABLE_LOGIC_PROCESSORS * 4);
}

public static void init() {} // Loads the class.
public static void init() {
} // Loads the class.

public static long usedMemory() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import org.slf4j.LoggerFactory;

public class LogUtils {
public static final Logger CODE_LINTING_LOGGER = LoggerFactory.getLogger("Code-Linting");
private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
private static final Logger LOGGER = LogUtils.getLogger();

public static final Logger CODE_LINTING_LOGGER = LoggerFactory.getLogger("Code-Linting");

public static Logger getLogger() {
return LoggerFactory.getLogger(STACK_WALKER.getCallerClass().getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.atlasworld.network.core.networking.handler;

import fr.atlasworld.network.api.concurrent.action.FutureAction;
import fr.atlasworld.network.api.networking.ConnectionSource;
import fr.atlasworld.network.api.networking.ConnectionState;
import fr.atlasworld.network.api.networking.exception.NetworkSynchronisationException;
import fr.atlasworld.network.api.networking.exception.NetworkingException;
Expand All @@ -14,10 +13,8 @@
import fr.atlasworld.network.api.security.exceptions.CryptographyException;
import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.services.database.Database;
import fr.atlasworld.network.core.AtlasNetwork;
import fr.atlasworld.network.core.networking.NetworkClient;
import fr.atlasworld.network.core.networking.codec.NetworkCodec;
import fr.atlasworld.network.core.networking.codec.exception.DecodeException;
import fr.atlasworld.network.core.networking.packet.PacketByteBufImpl;
import fr.atlasworld.network.core.networking.packet.SystemInfoPacket;
import fr.atlasworld.network.core.networking.security.authentication.AuthenticationProfile;
Expand All @@ -29,7 +26,6 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import io.netty.util.ReferenceCountUtil;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import fr.atlasworld.network.api.networking.exception.NetworkingException;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.networking.packet.PacketByteBuf;
import fr.atlasworld.network.api.registry.ResourceKey;
import fr.atlasworld.network.core.logging.LogUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import fr.atlasworld.network.api.networking.ConnectionSource;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.networking.packet.PacketByteBuf;
import fr.atlasworld.network.api.registry.ResourceKey;
import fr.atlasworld.network.api.server.AtlasNetworkServer;
import fr.atlasworld.network.api.util.NetworkVersion;
import fr.atlasworld.network.core.AtlasNetwork;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public WrappedPacket(@NotNull ResourceKey packet, @Nullable PacketByteBuf buffer
this.alloc = alloc;
}

public static WrappedPacket of(@NotNull ResourceKey key, @NotNull NetworkPacket packet, @NotNull ByteBufAllocator alloc) {
return new WrappedPacket(key, null, packet, alloc);
}

public static WrappedPacket of(@NotNull ResourceKey key, @NotNull PacketByteBuf packet, @NotNull ByteBufAllocator alloc) {
return new WrappedPacket(key, packet, null, alloc);
}

public @NotNull ResourceKey getPacket() {
return packet;
}
Expand Down Expand Up @@ -56,12 +64,4 @@ public PacketByteBuf build() throws PacketExecutionException {

return buf;
}

public static WrappedPacket of(@NotNull ResourceKey key, @NotNull NetworkPacket packet, @NotNull ByteBufAllocator alloc) {
return new WrappedPacket(key, null, packet, alloc);
}

public static WrappedPacket of(@NotNull ResourceKey key, @NotNull PacketByteBuf packet, @NotNull ByteBufAllocator alloc) {
return new WrappedPacket(key, packet, null, alloc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import fr.atlasworld.network.api.command.Command;
import fr.atlasworld.network.api.command.CommandSource;
import fr.atlasworld.network.api.networking.HandshakeInformation;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.registry.ResourceKey;
import fr.atlasworld.network.api.registry.SimpleRegistry;
import fr.atlasworld.network.core.registry.registries.PacketRegistry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.atlasworld.network.core.registry.registries;

import fr.atlasworld.network.api.networking.exception.packet.UnknownPacketException;
import fr.atlasworld.network.api.networking.packet.NetworkPacket;
import fr.atlasworld.network.api.registry.Registry;
import fr.atlasworld.network.api.registry.ResourceKey;
Expand Down

0 comments on commit a330e9b

Please sign in to comment.