Skip to content

Commit 53b45f8

Browse files
committed
feat: javadocing
1 parent 54462a2 commit 53b45f8

11 files changed

Lines changed: 150 additions & 34 deletions

File tree

common/src/client/java/io/github/jamalam360/jamlib/api/events/client/ClientPlayLifecycleEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.jamalam360.jamlib.api.events.client;
22

3-
import io.github.jamalam360.jamlib.events.Event;
3+
import io.github.jamalam360.jamlib.api.events.Event;
44
import net.minecraft.client.Minecraft;
55

66
/**

common/src/main/java/io/github/jamalam360/jamlib/events/CancellableEvent.java renamed to common/src/main/java/io/github/jamalam360/jamlib/api/events/CancellableEvent.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
package io.github.jamalam360.jamlib.events;
1+
package io.github.jamalam360.jamlib.api.events;
22

33
import java.util.function.Function;
44

5+
/**
6+
* A {@link Event} that can be canceled.
7+
* @param <L> The listener type.
8+
* @param <R> The return type.
9+
*/
510
public class CancellableEvent<L, R> extends Event<L> {
11+
/**
12+
* Invokes all listeners for this event.
13+
* @param invoker The listener invoker.
14+
* @return The result of the first listener to return a non-passing {@link EventResult}, or a passing {@link EventResult} if all listeners pass.
15+
*/
616
public EventResult<R> invokeCancellable(Function<L, EventResult<R>> invoker) {
717
for (int priority : this.getListeners().keySet()) {
818
for (L listener : this.getListeners().get(priority)) {

common/src/main/java/io/github/jamalam360/jamlib/events/Event.java renamed to common/src/main/java/io/github/jamalam360/jamlib/api/events/Event.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
package io.github.jamalam360.jamlib.events;
1+
package io.github.jamalam360.jamlib.api.events;
22

33
import com.google.common.collect.Ordering;
44
import com.google.common.collect.TreeMultimap;
55

66
import java.util.function.Consumer;
77

8+
/**
9+
* A listenable event.
10+
* @param <L> The listener type.
11+
*/
812
public class Event<L> {
913
public static final int DEFAULT_PRIORITY = 1000;
1014
private final TreeMultimap<Integer, L> listeners;
@@ -13,14 +17,27 @@ public Event() {
1317
this.listeners = TreeMultimap.create(Ordering.natural().reverse(), Ordering.arbitrary());
1418
}
1519

20+
/**
21+
* Listens for this event, using the default priority of 1000.
22+
* @param listener The listener.
23+
*/
1624
public void listen(L listener) {
1725
this.listen(DEFAULT_PRIORITY, listener);
1826
}
1927

28+
/**
29+
* Listens for this event.
30+
* @param priority The priority of the listener - higher numbers are executed first.
31+
* @param listener The listener.
32+
*/
2033
public void listen(int priority, L listener) {
2134
this.listeners.put(priority, listener);
2235
}
2336

37+
/**
38+
* Invokes all listeners for this event.
39+
* @param invoker The listener invoker.
40+
*/
2441
public void invoke(Consumer<L> invoker) {
2542
for (int priority : this.listeners.keySet()) {
2643
for (L listener : this.listeners.get(priority)) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.jamalam360.jamlib.api.events;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
/**
6+
* The result of a {@link CancellableEvent}. Either passes or returns a value.
7+
* @param <T> The returned value type.
8+
*/
9+
public class EventResult<T> {
10+
private final boolean cancelled;
11+
@Nullable
12+
private final T result;
13+
14+
private EventResult(boolean cancelled, @Nullable T result) {
15+
this.cancelled = cancelled;
16+
this.result = result;
17+
}
18+
19+
/**
20+
* Create a passing {@link EventResult}.
21+
* @return A passing {@link EventResult}.
22+
*/
23+
public static <T> EventResult<T> pass() {
24+
return new EventResult<>(false, null);
25+
}
26+
27+
/**
28+
* Create a cancelling {@link EventResult} with a result.
29+
* @param result The result.
30+
* @return A cancelling {@link EventResult} with the given result.
31+
*/
32+
public static <T> EventResult<T> cancel(T result) {
33+
return new EventResult<>(true, result);
34+
}
35+
36+
/**
37+
* @return Whether this {@link EventResult} indicates a cancellation.
38+
*/
39+
public boolean wasCancelled() {
40+
return this.cancelled;
41+
}
42+
43+
/**
44+
* @return The result of this {@link EventResult}, or null if it was not canceled.
45+
*/
46+
@Nullable
47+
public T getResult() {
48+
return this.result;
49+
}
50+
}

common/src/main/java/io/github/jamalam360/jamlib/api/network/Network.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@
88

99
import java.util.*;
1010

11+
/**
12+
* A platform-agnostic packet sending system.
13+
*/
1114
public class Network {
1215
private static final Map<PayloadType<?>, NetworkPayloadType<?>> types = new HashMap<>();
1316
private static final Map<PayloadType<?>, NetworkPayloadHandler<?>> clientBoundHandlers = new HashMap<>();
1417
private static final Map<PayloadType<?>, NetworkPayloadHandler<?>> serverBoundHandlers = new HashMap<>();
1518

19+
/**
20+
* Registers a handler for a payload type.
21+
* @param direction The direction the handler should be registered for.
22+
* @param id The ID of the payload the handler handles.
23+
* @param handler The handler.
24+
*/
1625
public static <T> void registerHandler(Direction direction, PayloadType<T> id, NetworkPayloadHandler<T> handler) {
1726
Map<PayloadType<?>, NetworkPayloadHandler<?>> handlers = switch (direction) {
1827
case CLIENT_BOUND -> clientBoundHandlers;
@@ -26,6 +35,10 @@ public static <T> void registerHandler(Direction direction, PayloadType<T> id, N
2635
handlers.put(id, handler);
2736
}
2837

38+
/**
39+
* @param id The ID of the payload type.
40+
* @param type The type of the payload.
41+
*/
2942
public static <T> void registerPayloadType(PayloadType<T> id, NetworkPayloadType<T> type) {
3043
if (types.containsKey(id)) {
3144
throw new IllegalArgumentException("A payload type with the id " + id + " is already registered");
@@ -35,6 +48,11 @@ public static <T> void registerPayloadType(PayloadType<T> id, NetworkPayloadType
3548
JamLib.LOGGER.info("Registered network payload type {}", id.id());
3649
}
3750

51+
/**
52+
* Sends a payload to the server.
53+
* @param id The ID of the payload type.
54+
* @param payload The payload.
55+
*/
3856
@SuppressWarnings("unchecked")
3957
public static <T> void sendToServer(PayloadType<T> id, T payload) {
4058
if (!types.containsKey(id)) {
@@ -45,6 +63,11 @@ public static <T> void sendToServer(PayloadType<T> id, T payload) {
4563
PlatformNetwork.sendToServer(id, type.getSerializer(), payload);
4664
}
4765

66+
/**
67+
* Sends a payload to the client.
68+
* @param id The ID of the payload type.
69+
* @param payload The payload.
70+
*/
4871
@SuppressWarnings("unchecked")
4972
public static <T> void sendToClient(ServerPlayer player, PayloadType<T> id, T payload) {
5073
if (!types.containsKey(id)) {

common/src/main/java/io/github/jamalam360/jamlib/api/network/NetworkContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
import net.minecraft.world.entity.player.Player;
44

5+
/**
6+
* Context passed to a {@link NetworkPayloadHandler}.
7+
* @param player The player who received or sent the packet.
8+
*/
59
public record NetworkContext(Player player) {
610
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package io.github.jamalam360.jamlib.api.network;
22

3+
/**
4+
* A handler for network payloads.
5+
*/
36
@FunctionalInterface
47
public interface NetworkPayloadHandler<T> {
8+
/**
9+
* Handle an incoming network payload.
10+
* @param ctx The network context.
11+
* @param payload The payload.
12+
*/
513
void handle(NetworkContext ctx, T payload);
614
}

common/src/main/java/io/github/jamalam360/jamlib/api/network/NetworkPayloadType.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,45 @@
22

33
import net.minecraft.network.RegistryFriendlyByteBuf;
44

5+
/**
6+
* A payload that can be sent over the network.
7+
* @param <T> The type of the payload, usually a record.
8+
*/
59
public interface NetworkPayloadType<T> {
10+
/**
11+
* @return A serializer instance for the payload type.
12+
*/
613
Serializer<T> getSerializer();
14+
15+
/**
16+
* @return A deserializer instance for the payload type.
17+
*/
718
Deserializer<T> getDeserializer();
819

20+
/**
21+
* A serializer for a payload.
22+
*/
923
@FunctionalInterface
1024
interface Serializer<T> {
25+
26+
/**
27+
* Serializes a payload to a buffer.
28+
* @param object The payload to serialize.
29+
* @param buf The buffer to write to.
30+
*/
1131
void serialize(T object, RegistryFriendlyByteBuf buf);
1232
}
1333

34+
/**
35+
* A deserializer for a payload.
36+
*/
1437
@FunctionalInterface
1538
interface Deserializer<T> {
39+
/**
40+
* Deserializes a payload from a buffer.
41+
* @param buf The buffer to read from.
42+
* @return The deserialized payload.
43+
*/
1644
T deserialize(RegistryFriendlyByteBuf buf);
1745
}
1846
}

common/src/main/java/io/github/jamalam360/jamlib/api/network/PayloadType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
import net.minecraft.resources.Identifier;
44

5+
/**
6+
* Uniquely identifies a {@link NetworkPayloadType}.
7+
* @param id A unique identifier for the payload type.
8+
*/
59
public record PayloadType<T>(Identifier id) {
610
}

common/src/main/java/io/github/jamalam360/jamlib/api/network/StreamCodecNetworkPayloadType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import net.minecraft.network.RegistryFriendlyByteBuf;
44
import net.minecraft.network.codec.StreamCodec;
55

6+
/**
7+
* A {@link NetworkPayloadType} that uses a {@link StreamCodec} to encode and decode the payload.
8+
*/
69
public interface StreamCodecNetworkPayloadType<T> extends NetworkPayloadType<T> {
710
StreamCodec<RegistryFriendlyByteBuf, T> getStreamCodec();
811

0 commit comments

Comments
 (0)