Skip to content

Commit

Permalink
Merge branch 'gui'
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeBather committed Dec 2, 2024
2 parents 20d13ef + b38f945 commit 599c8b9
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/main/java/kasuga/lib/core/KasugaLibStacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public KasugaLibStacks(IEventBus bus) {

MinecraftForge.EVENT_BUS.addListener(PlayLogEvent::playerLogout);
MinecraftForge.EVENT_BUS.addListener(PlayLogEvent::playerLogin);
MinecraftForge.EVENT_BUS.addListener(ClientConnection::onClientConnect);
MinecraftForge.EVENT_BUS.addListener(ClientConnection::onClientDisconnect);

bus.addListener(ModelRegistryEvent::registerAdditionalModels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ protected void createRuntime(NodePackage nodePackage){
case CLIENT:
createRuntimeForEntryType(nodePackage, nodePackage.minecraft.commonEntries());
createRuntimeForEntryType(nodePackage, nodePackage.minecraft.clientEntries());
break;
case SERVER:
createRuntimeForEntryType(nodePackage, nodePackage.minecraft.commonEntries());
createRuntimeForEntryType(nodePackage, nodePackage.minecraft.serverEntries());
break;
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/kasuga/lib/core/channel/ChannelNetworkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ChannelNetworkManager {
public HashMap<ServerPlayer, NetworkServerManager> SERVER = new HashMap<>();
public SimpleRouter CLIENT_ROUTER = new SimpleRouter();
public NetworkManager CLIENT = new NetworkManager(CLIENT_ROUTER);
private boolean clientConnectionExisted = false;

public NetworkManager server(ServerPlayer player){
return SERVER.computeIfAbsent(player, k -> new NetworkServerManager(SERVER_ROUTER, player));
Expand All @@ -41,4 +42,16 @@ public void reloadClient(){
public NetworkManager client(){
return CLIENT;
}

public void disableClient() {
this.clientConnectionExisted = false;
}

public void enableClient() {
this.clientConnectionExisted = true;
}

public boolean clientIsEnabled() {
return clientConnectionExisted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class NetworkDuplexer {
Expand Down Expand Up @@ -66,7 +67,7 @@ public void onExistedConnectionEstablished(long networkId, NetworkChannelSocket
}

public void close(){
ArrayList<Channel> currentChannels = new ArrayList<>(channels.values());
List<Channel> currentChannels = List.copyOf(channels.values());
for(Channel channel : currentChannels){
channel.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public void sendClose(NetworkChannelSocket socket) {
}

protected void sendNetworkConnecitonPacket(Channel channel, long networkId) {
if(!KasugaLib.STACKS.CHANNEL.clientIsEnabled()){
channel.close();
return;
}
KasugaLib.STACKS.CHANNEL.packet.channelReg.sendToServer(new C2SChannelConnectionPacket(
channel.source(),
channel.destination(),
Expand All @@ -106,12 +110,16 @@ protected void sendNetworkConnecitonPacket(Channel channel, long networkId) {
}

protected void sendMessagePacket(long networkId, CompoundTag message, boolean isOwn){
if(!KasugaLib.STACKS.CHANNEL.clientIsEnabled())
return;
KasugaLib.STACKS.CHANNEL.packet.channelReg.sendToServer(
new C2SChannelMessagePacket(networkId, message, isOwn)
);
}

protected void sendStatePacket(long networkId, ChannelStatus state, boolean isOwn){
if(!KasugaLib.STACKS.CHANNEL.clientIsEnabled())
return;
KasugaLib.STACKS.CHANNEL.packet.channelReg.sendToServer(
new C2SChannelStateChangePacket(networkId, state, isOwn)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;

public class ClientConnection {

public static void onClientConnect(ClientPlayerNetworkEvent.LoggingIn loggingIn){
KasugaLib.STACKS.CHANNEL.enableClient();
}
public static void onClientDisconnect(ClientPlayerNetworkEvent.LoggingOut loggingOut){
KasugaLib.STACKS.CHANNEL.disableClient();
KasugaLib.STACKS.CHANNEL.reloadClient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
public class ServerStartingEvents {

@SubscribeEvent
public static void serverStarting(ServerStartingEvent event) {
MinecraftServer server = event.getServer();
CloseableResourceManager manager = server.getServerResources().resourceManager();
KasugaLib.STACKS.JAVASCRIPT.setupServer();
ServerAddon.init(event.getServer());
}
public static void serverStarting(ServerStartingEvent event) {}

@SubscribeEvent
public static void serverAboutToStart(ServerAboutToStartEvent event) {
Expand All @@ -30,9 +25,4 @@ public static void serverAboutToStart(ServerAboutToStartEvent event) {
Start.printLogo();
}
}

public static void serverStopping(ServerStoppingEvent event) {
ServerAddon.unload();
KasugaLib.STACKS.JAVASCRIPT.destoryServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.FutureTask;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -37,9 +38,10 @@ public void close(){
effect.close();
}

Set<Tickable> tickables = new HashSet<>();
Set<Tickable> tickables = new CopyOnWriteArraySet<>();

public void tick(){
List<Tickable> tickables = List.copyOf(this.tickables); // @TODO: Improve performance
tickables.forEach(Tickable::tick);
this.context.tick();
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/kasuga/lib/core/menu/GuiMenuManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,12 @@ public void clientTick(){
menu.clientTick();
}
}

public void reset(){
List<GuiMenu> menus = List.copyOf(tickables);
for(GuiMenu menu : menus){
menu.close();
}
tickables.clear();
}
}
30 changes: 28 additions & 2 deletions src/main/java/kasuga/lib/core/menu/base/GuiMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class GuiMenu {
Expand Down Expand Up @@ -124,6 +126,10 @@ private ChannelHandler createClientHandler() {
@Override
public void onChannelEstabilished(ChannelHandle channel) {
connectionFailure = false;
if(!isDifferentiated){
channel.close();
return;
}
setChannelHandle(channel);
createGuiInstance();
}
Expand All @@ -139,6 +145,10 @@ public void onChannelClose(ChannelHandle channel) {

@Override
public void onChannelMessage(ChannelHandle channel, CompoundTag payload) {
if(!isDifferentiated){
channel.close();
return;
}
clientForwardMessageToGuiInstance(payload);
}
};
Expand All @@ -148,6 +158,10 @@ private ChannelHandler createServerHandler(Channel channel){
return new ChannelHandler() {
@Override
public void onChannelEstabilished(ChannelHandle handle) {
if(!isDifferentiated || !isServer){
channel.close();
return;
}
handles.put(channel, handle);
onInit(channel, handle);
}
Expand Down Expand Up @@ -186,14 +200,24 @@ public void onMesssage(Channel channel, ChannelHandle handle, CompoundTag payloa
public void close(){
KasugaLib.STACKS.MENU.removeMenuTickInstance(this);
if(isDifferentiated){
isDifferentiated = false;
isServer = false;
if(isServer){
for(ChannelHandle handle : handles.values()){
List<ChannelHandle> handles$value = List.copyOf(handles.values());
for(ChannelHandle handle : handles$value){
handle.close();
}
GuiMenuNetworking.getServerSwitcher().removePeer(peer);
}else{
connectionFailure = false;
reconnection = 20;
if(isGuiInstanceCreated){
closeGuiInstance();
}
if(handle != null){
handle.close();
}
GuiMenuNetworking.getClientSwitcher().removePeer(peer);
}
}
}
Expand Down Expand Up @@ -224,16 +248,18 @@ public UUID getServerId() {
return serverId;
}


int reconnection = 0;
public void clientTick(){
if(isDifferentiated && !isServer){
if(connectionFailure){
if(--reconnection > 0){
return;
}
reconnection = 20;
connectionFailure = false;
peer.createSocket(remoteInfo, this.createClientHandler());
} else {
reconnection = 20;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/kasuga/lib/core/menu/locator/GuiMenuHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void enableClient(MenuLocator locator) {
}

public void enable(LevelAccessor level) {
if(isEnabled)
return;
if(level.isClientSide()) {
enableClient(locator);
} else {
Expand Down

0 comments on commit 599c8b9

Please sign in to comment.