Skip to content

Commit

Permalink
Add C2S ClientPendingRequestManager, ServerRequestHandler And S2C Ser…
Browse files Browse the repository at this point in the history
…verPendingRequestManager, ClientRequestHandler

Add AbstractPendingRequestManager And RequestAction
Fix AbstractCache
  • Loading branch information
FirstMegaGame4 committed Dec 2, 2023
1 parent 71e44c7 commit cb537c4
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class AbstractCache<K, V> extends HashMap<K, V> {
this.value = value;
}

abstract public boolean clientReserved();
public abstract boolean clientReserved();

public void debug() {
System.out.println((this.clientReserved() ? "Client " : "Local ") + "Cache {" + this.cache + "} :");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mmodding.mmodding_lib.library.network.request.c2s;

import com.mmodding.mmodding_lib.library.network.request.common.AbstractPendingRequestManager;
import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import com.mmodding.mmodding_lib.networking.MModdingPackets;
import net.minecraft.util.Identifier;
import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ClientPendingRequestManager<T extends NetworkSupport> extends AbstractPendingRequestManager<T> {

private static final Map<Identifier, ClientPendingRequestManager<?>> PENDING_REQUESTS = new HashMap<>();

public static Set<Identifier> getManagerKeys() {
return ClientPendingRequestManager.PENDING_REQUESTS.keySet();
}

public static boolean exists(Identifier identifier) {
return ClientPendingRequestManager.PENDING_REQUESTS.containsKey(identifier);
}

public static ClientPendingRequestManager<?> getManager(Identifier identifier) {
return ClientPendingRequestManager.PENDING_REQUESTS.get(identifier);
}

public ClientPendingRequestManager(Identifier handler) {
this(handler, 0);
}

public ClientPendingRequestManager(Identifier handler, int maximumEntryNumberForEachBuff) {
super(handler, maximumEntryNumberForEachBuff);
ClientPendingRequestManager.PENDING_REQUESTS.put(handler, this);
}

public void send() {
this.provideBufs().forEach(buf -> ClientPlayNetworking.send(MModdingPackets.C2S_REQUESTS, buf));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mmodding.mmodding_lib.library.network.request.c2s;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import com.mmodding.mmodding_lib.library.network.support.type.NetworkList;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

import java.util.HashMap;
import java.util.Map;

@FunctionalInterface
public interface ServerRequestHandler<T extends NetworkSupport> {

Map<Identifier, ServerRequestHandler<?>> HANDLERS = new HashMap<>();

@ApiStatus.NonExtendable
static <T extends NetworkSupport> ServerRequestHandler<T> create(Identifier identifier, ServerRequestHandler<T> handler) {
HANDLERS.put(identifier, handler);
return handler;
}

T respond(NetworkList arguments);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.mmodding.mmodding_lib.library.network.request.common;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import com.mmodding.mmodding_lib.library.network.support.type.NetworkList;
import com.mmodding.mmodding_lib.library.utils.BiHashMap;
import com.mmodding.mmodding_lib.library.utils.BiMap;
import com.mmodding.mmodding_lib.library.utils.MapUtils;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import org.quiltmc.qsl.networking.api.PacketByteBufs;

import java.util.*;
import java.util.function.BooleanSupplier;

public abstract class AbstractPendingRequestManager<T extends NetworkSupport> {

protected final Identifier handler;
protected final BiMap<UUID, NetworkList, RequestAction<T>> primaries;
protected final Map<UUID, NetworkList> pending;
protected final Map<UUID, RequestAction<T>> actions;
protected final int maximumEntryNumberForEachBuff;

protected AbstractPendingRequestManager(Identifier handler, int maximumEntryNumberForEachBuff) {
this.handler = handler;
this.primaries = new BiHashMap<>();
this.pending = new HashMap<>();
this.actions = new HashMap<>();
this.maximumEntryNumberForEachBuff = maximumEntryNumberForEachBuff;
}

public void primary(NetworkList arguments, RequestAction<T> action) {
UUID uuid = MapUtils.untilNotContainingKey(UUID::randomUUID, this.primaries, this.pending, this.actions);
this.primaries.put(uuid, arguments, action);
}

public void action(NetworkList arguments, RequestAction<T> action) {
UUID uuid = MapUtils.untilNotContainingKey(UUID::randomUUID, this.primaries, this.pending, this.actions);
this.pending.put(uuid, arguments);
this.actions.put(uuid, action);
}

public void primaryOrAction(NetworkList arguments, RequestAction<T> action, BooleanSupplier isAction) {
if (!isAction.getAsBoolean()) {
this.primary(arguments, action);
}
else {
this.action(arguments, action);
}
}

public void actionOrPrimary(NetworkList arguments, RequestAction<T> action, BooleanSupplier isPrimary) {
if (!isPrimary.getAsBoolean()) {
this.action(arguments, action);
}
else {
this.primary(arguments, action);
}
}

public Optional<RequestAction<T>> consume(UUID uuid) {
RequestAction<T> action;
if (this.primaries.containsKey(uuid)) {
action = this.primaries.getSecondValue(uuid);
}
else {
this.pending.remove(uuid);
action = MapUtils.consume(this.actions, uuid);
}
return Optional.ofNullable(action);
}

public void clear() {
this.pending.clear();
this.actions.clear();
}

protected List<PacketByteBuf> provideBufs() {
List<PacketByteBuf> bufs = new ArrayList<>();
Map<UUID, NetworkList> provided = new HashMap<>();
this.primaries.forEachFirst(provided::put);
provided.putAll(this.pending);
MapUtils.divide(provided, this.maximumEntryNumberForEachBuff).forEach(
map -> {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeIdentifier(this.handler);
buf.writeMap(map, PacketByteBuf::writeUuid, (current, args) -> args.writeComplete(current));
bufs.add(buf);
}
);
return bufs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mmodding.mmodding_lib.library.network.request.common;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import org.jetbrains.annotations.ApiStatus;

@FunctionalInterface
public interface RequestAction<T extends NetworkSupport> {

@ApiStatus.NonExtendable
@SuppressWarnings("unchecked")
default void cast(NetworkSupport received) {
this.end((T) received);
}

void end(T received);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.mmodding.mmodding_lib.library.network.request.s2c;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import com.mmodding.mmodding_lib.library.network.support.type.NetworkList;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

import java.util.HashMap;
import java.util.Map;

@FunctionalInterface
public interface ClientRequestHandler<T extends NetworkSupport> {

Map<Identifier, ClientRequestHandler<?>> HANDLERS = new HashMap<>();

@ApiStatus.NonExtendable
static <T extends NetworkSupport> ClientRequestHandler<T> create(Identifier identifier, ClientRequestHandler<T> handler) {
HANDLERS.put(identifier, handler);
return handler;
}

T respond(ClientPlayerEntity player, NetworkList arguments);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.mmodding.mmodding_lib.library.network.request.s2c;

import com.mmodding.mmodding_lib.library.network.request.common.AbstractPendingRequestManager;
import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import com.mmodding.mmodding_lib.networking.MModdingPackets;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.quiltmc.qsl.networking.api.ServerPlayNetworking;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ServerPendingRequestManager<T extends NetworkSupport> extends AbstractPendingRequestManager<T> {

private static final Map<Identifier, ServerPendingRequestManager<?>> PENDING_REQUESTS = new HashMap<>();

public static Set<Identifier> getManagerKeys() {
return ServerPendingRequestManager.PENDING_REQUESTS.keySet();
}

public static boolean exists(Identifier identifier) {
return ServerPendingRequestManager.PENDING_REQUESTS.containsKey(identifier);
}

public static ServerPendingRequestManager<?> getManager(Identifier identifier) {
return ServerPendingRequestManager.PENDING_REQUESTS.get(identifier);
}

public ServerPendingRequestManager(Identifier handler) {
this(handler, 0);
}

public ServerPendingRequestManager(Identifier handler, int maximumEntryNumberForEachBuff) {
super(handler, maximumEntryNumberForEachBuff);
ServerPendingRequestManager.PENDING_REQUESTS.put(handler, this);
}

public void send(ServerPlayerEntity player) {
this.provideBufs().forEach(buf -> ServerPlayNetworking.send(player, MModdingPackets.S2C_REQUESTS, buf));
}
}

0 comments on commit cb537c4

Please sign in to comment.