forked from Mumfrey/WorldEditCUI
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
363 additions
and
35 deletions.
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
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
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
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
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
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
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
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
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
87 changes: 87 additions & 0 deletions
87
...i-protocol-common/src/main/java/org/enginehub/worldeditcui/protocol/CUIPacketHandler.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,87 @@ | ||
/* | ||
* Copyright (c) 2011-2024 WorldEditCUI team and contributors | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* This Source Code may also be made available under the following | ||
* Secondary Licenses when the conditions for such availability set forth | ||
* in the Eclipse Public License, v. 2.0 are satisfied: | ||
* GNU General Public License version 3 or later | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.enginehub.worldeditcui.protocol; | ||
|
||
import com.mojang.logging.LogUtils; | ||
import net.minecraft.world.entity.player.Player; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.ServiceConfigurationError; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* Utilities for receiving CUI packets. | ||
*/ | ||
public interface CUIPacketHandler { | ||
/** | ||
* Get a handler instance to register with. | ||
* | ||
* @return the handler instance | ||
*/ | ||
static CUIPacketHandler instance() { | ||
return CUIPacketHandlers0.HANDLER_IMPL; | ||
} | ||
|
||
/** | ||
* Contextual info for the packet. | ||
* | ||
* @param player the player receiving the packet | ||
* @param workExecutor a work executor for the client/server main thread | ||
*/ | ||
record PacketContext(Player player, Executor workExecutor) {} | ||
|
||
/** | ||
* Register a handler that will receive packets on the logical client. | ||
* | ||
* @param clientbound the clientbound handler | ||
*/ | ||
void registerClientboundHandler(final BiConsumer<CUIPacket, PacketContext> clientbound); | ||
/** | ||
* Register a handler that will receive packets on the logical server. | ||
* | ||
* @param serverbound the serverbound handler | ||
*/ | ||
void registerServerboundHandler(final BiConsumer<CUIPacket, PacketContext> serverbound); | ||
} | ||
class CUIPacketHandlers0 { | ||
private static final Logger LOGGER = LogUtils.getLogger(); | ||
private static final ServiceLoader<CUIPacketHandler> HANDLER_DISCOVERY = ServiceLoader.load(CUIPacketHandler.class); | ||
public static final CUIPacketHandler HANDLER_IMPL; | ||
|
||
static { | ||
final Set<String> knownProviders = new HashSet<>(); | ||
try { | ||
final List<CUIPacketHandler> handlers = HANDLER_DISCOVERY.stream() | ||
.peek(prov -> knownProviders.add(prov.type().getCanonicalName())) | ||
.map(ServiceLoader.Provider::get) | ||
.toList(); | ||
|
||
if (handlers.isEmpty()) { | ||
throw new IllegalStateException("No CUI protocol providers available"); | ||
} else { | ||
HANDLER_IMPL = handlers.getFirst(); | ||
} | ||
} catch (final ServiceConfigurationError ex) { | ||
LOGGER.error("Failed to discover a CUI protocol handler, from known providers {}:", knownProviders, ex); | ||
throw new IllegalStateException("Failed to configure CUI protocol handlers", ex); | ||
} | ||
} | ||
} | ||
|
59 changes: 59 additions & 0 deletions
59
...abric/src/main/java/org/enginehub/worldeditcui/fabric/network/FabricCUIPacketHandler.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,59 @@ | ||
/* | ||
* Copyright (c) 2011-2024 WorldEditCUI team and contributors | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* This Source Code may also be made available under the following | ||
* Secondary Licenses when the conditions for such availability set forth | ||
* in the Eclipse Public License, v. 2.0 are satisfied: | ||
* GNU General Public License version 3 or later | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.enginehub.worldeditcui.fabric.network; | ||
|
||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; | ||
import org.enginehub.worldeditcui.protocol.CUIPacket; | ||
import org.enginehub.worldeditcui.protocol.CUIPacketHandler; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.BiConsumer; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class FabricCUIPacketHandler implements CUIPacketHandler { | ||
private static final Set<BiConsumer<CUIPacket, PacketContext>> CLIENTBOUND_HANDLERS = ConcurrentHashMap.newKeySet(); | ||
private static final Set<BiConsumer<CUIPacket, PacketContext>> SERVERBOUND_HANDLERS = ConcurrentHashMap.newKeySet(); | ||
|
||
static void register() { | ||
ServerPlayNetworking.registerGlobalReceiver(CUIPacket.TYPE, (pkt, ctx) -> { | ||
final PacketContext cuiCtx = new PacketContext(ctx.player(), ctx.player().getServer()); | ||
for (BiConsumer<CUIPacket, PacketContext> handler : SERVERBOUND_HANDLERS) { | ||
handler.accept(pkt, cuiCtx); | ||
} | ||
}); | ||
} | ||
|
||
static void registerClient() { | ||
ClientPlayNetworking.registerGlobalReceiver(CUIPacket.TYPE, (pkt, ctx) -> { | ||
final PacketContext cuiCtx = new PacketContext(ctx.player(), ctx.client()); | ||
for (BiConsumer<CUIPacket, PacketContext> handler : SERVERBOUND_HANDLERS) { | ||
handler.accept(pkt, cuiCtx); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void registerClientboundHandler(BiConsumer<CUIPacket, PacketContext> clientbound) { | ||
CLIENTBOUND_HANDLERS.add(requireNonNull(clientbound, "clientbound")); | ||
} | ||
|
||
@Override | ||
public void registerServerboundHandler(BiConsumer<CUIPacket, PacketContext> serverbound) { | ||
SERVERBOUND_HANDLERS.add(requireNonNull(serverbound, "clientbound")); | ||
} | ||
} |
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
Oops, something went wrong.