-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add C2S ClientPendingRequestManager, ServerRequestHandler And S2C Ser…
…verPendingRequestManager, ClientRequestHandler Add AbstractPendingRequestManager And RequestAction Fix AbstractCache
- Loading branch information
1 parent
71e44c7
commit cb537c4
Showing
7 changed files
with
239 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
41 changes: 41 additions & 0 deletions
41
...va/com/mmodding/mmodding_lib/library/network/request/c2s/ClientPendingRequestManager.java
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,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)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...main/java/com/mmodding/mmodding_lib/library/network/request/c2s/ServerRequestHandler.java
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,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); | ||
} |
92 changes: 92 additions & 0 deletions
92
...m/mmodding/mmodding_lib/library/network/request/common/AbstractPendingRequestManager.java
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,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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/mmodding/mmodding_lib/library/network/request/common/RequestAction.java
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,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); | ||
} |
24 changes: 24 additions & 0 deletions
24
...main/java/com/mmodding/mmodding_lib/library/network/request/s2c/ClientRequestHandler.java
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,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); | ||
} |
42 changes: 42 additions & 0 deletions
42
...va/com/mmodding/mmodding_lib/library/network/request/s2c/ServerPendingRequestManager.java
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,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)); | ||
} | ||
} |