Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel system & menu update #12

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/kasuga/lib/core/KasugaLibStacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import kasuga.lib.core.base.commands.ArgumentTypes.BaseArgument;
import kasuga.lib.core.base.commands.ArgumentTypes.BaseArgumentInfo;
import kasuga.lib.core.channel.ChannelNetworkManager;
import kasuga.lib.core.channel.network.NetworkManager;
import kasuga.lib.core.channel.network.address.NetworkAddressTypes;
import kasuga.lib.core.channel.packets.ChannelNetworkPacket;
import kasuga.lib.core.channel.test.ChannelTest;
Expand All @@ -19,6 +18,7 @@
import kasuga.lib.core.events.server.ServerStartingEvents;
import kasuga.lib.core.client.render.texture.old.SimpleTexture;
import kasuga.lib.core.javascript.JavascriptApi;
import kasuga.lib.core.menu.GuiMenuManager;
import kasuga.lib.core.menu.targets.TargetsClient;
import kasuga.lib.core.util.Envs;
import kasuga.lib.registrations.client.KeyBindingReg;
Expand Down Expand Up @@ -56,6 +56,9 @@ public class KasugaLibStacks {
public final JavascriptApi JAVASCRIPT = new JavascriptApi();

public Optional<GuiEngine> GUI = Optional.empty();

public static final GuiMenuManager MENU = new GuiMenuManager();

public static final SimpleRegistry REGISTRY = new SimpleRegistry(KasugaLib.MOD_ID, KasugaLib.EVENTS);
public static final ChannelNetworkManager CHANNEL = new ChannelNetworkManager();
public static HashSet<Minecraft> mcs = new HashSet<>();
Expand All @@ -70,6 +73,8 @@ public KasugaLibStacks(IEventBus bus) {
TEXTURES = new TextureRegistry(MOD_ID);
FONTS = new FontRegistry(MOD_ID);
BLOCK_RENDERERS = new HashMap<>();
MENU.init();

MinecraftForge.EVENT_BUS.addListener(ServerStartingEvents::serverStarting);
MinecraftForge.EVENT_BUS.addListener(ServerStartingEvents::serverAboutToStart);
MinecraftForge.EVENT_BUS.addListener(PacketEvent::onServerPayloadHandleEvent);
Expand All @@ -95,13 +100,14 @@ public KasugaLibStacks(IEventBus bus) {
bus.addListener(TextureRegistryEvent::onModelRegistry);
bus.addListener(ClientSetupEvent::onClientSetup);
MinecraftForge.EVENT_BUS.addListener(RenderTickEvent::onRenderTick);
MinecraftForge.EVENT_BUS.addListener(InteractionFovEvent::onComputedFov);
bus.addListener(GeometryEvent::registerGeometry);
bus.addListener(GeometryEvent::registerReloadListener);
bus.addListener(BothSetupEvent::RegisterKeyEvent);
GUI = Optional.of(new GuiEngine());
bus.addListener(AnimationModelRegistryEvent::registerAnimations);
if (Envs.isDevEnvironment()) KasugaLibClient.invoke();
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()-> TargetsClient::reigster);
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()-> TargetsClient::register);
}

MinecraftForge.EVENT_BUS.addListener(ServerResourceListener::onServerStarting);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/kasuga/lib/core/channel/NetworkSwitcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import kasuga.lib.core.channel.peer.Channel;
import kasuga.lib.core.channel.peer.ChannelPeer;
import kasuga.lib.core.channel.peer.ChannelReciever;
import kasuga.lib.core.channel.route.SimpleRouter;

import java.util.HashMap;

Expand All @@ -30,4 +31,13 @@ public void addPeer(ChannelPeer peer) {
direct_peers.put(peer.getAddress(), peer);
peer.setDistributor(this);
}

public void removePeer(ChannelPeer peer) {
direct_peers.remove(peer.getAddress());
peer.setDistributor(null);
}

public void setDefaultReciever(ChannelReciever reciever) {
this.defaultReciever = reciever;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ public void close() {
public void setHandler(ChannelHandler handler) {
this.handler = handler;
}

public Channel getChannel() {
return channel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package kasuga.lib.core.channel.peer;

import kasuga.lib.core.channel.address.ChannelPort;
import kasuga.lib.core.channel.address.Label;

import java.util.HashMap;
import java.util.function.Function;

public class FeaturedChannelPeer extends ChannelPeer {
public FeaturedChannelPeer(Label address) {
super(address);
}

HashMap<ChannelPort, Function<ChannelPeerSocketServer, ChannelHandler>> openedPorts = new HashMap<>();

public void openPort(ChannelPort port, Function<ChannelPeerSocketServer, ChannelHandler> handler){
openedPorts.put(port, handler);
}

public void closePort(ChannelPort port){
openedPorts.remove(port);
}

@Override
protected boolean onConnect(ChannelPeerSocketServer server) {
ChannelPort port = server.getChannel().destination().getPort();
if(openedPorts.containsKey(port)){
server.setHandler(openedPorts.get(port).apply(server));
return true;
}
return false;
}
}
12 changes: 12 additions & 0 deletions src/main/java/kasuga/lib/core/client/frontend/dom/DomContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,16 @@ public void removeEventListener(String eventName, JavascriptValue callback){
public void dispatchEvent(String eventName,Object event){
emitter.dispatchEvent(eventName,event);
}

@HostAccess.Export
public JavascriptValue getContextModule(String contextModuleName){
return
renderer.getContext().orElseThrow()
.getRuntimeContext()
.asValue(getContextModuleNative(contextModuleName));
}

public Object getContextModuleNative(String contextModuleName){
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@
import kasuga.lib.core.client.frontend.gui.nodes.GuiDomRoot;
import kasuga.lib.core.client.frontend.rendering.RenderContext;
import kasuga.lib.core.javascript.JavascriptContext;
import kasuga.lib.core.javascript.JavascriptThread;
import kasuga.lib.core.javascript.Tickable;
import kasuga.lib.core.util.Callback;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.locks.ReentrantLock;

@V8Convert()
public class GuiContext extends DomContext<GuiDomNode,GuiDomRoot> implements Tickable {

private final GuiInstance guiInstance;
LayoutEngine<?,GuiDomNode> layoutEngine;

GuiAttachTarget attachedTargets = new GuiAttachTarget();
ReentrantLock renderLock = new ReentrantLock();

public volatile boolean isRendering;

public GuiContext(DOMPriorityRegistry registry, ResourceLocation location) {
public GuiContext(GuiInstance guiInstance, DOMPriorityRegistry registry, ResourceLocation location) {
super(registry, location);
layoutEngine = LayoutEngines.YOGA;
this.guiInstance = guiInstance;
}

@Override
Expand Down Expand Up @@ -132,4 +131,9 @@ public void queueDuringRenderUnsafe(Runnable task){
context.enqueueAfterRenderTask(task);
}
}

@Override
public Object getContextModuleNative(String contextModuleName) {
return this.guiInstance.getModule(contextModuleName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class GuiInstance {

public Map<Object, SourceInfo> sourceInfos = new HashMap<>();

public Map<String, Object> contextObject = new HashMap<>();

public GuiInstance(ResourceLocation location){
this.location = location;
}
Expand Down Expand Up @@ -80,7 +82,7 @@ public void close(Object object){

public void openInternal(Object target){
if(this.context == null){
this.context = new GuiContext(KasugaLib.STACKS.GUI.orElseThrow(IllegalStateException::new).domRegistry, location);
this.context = new GuiContext(this, KasugaLib.STACKS.GUI.orElseThrow(IllegalStateException::new).domRegistry, location);
this.sourceInfos.forEach((source, info)->{
this.context.setSourceInfo(source, info);
});
Expand Down Expand Up @@ -141,4 +143,12 @@ public void afterRender(){
c.dispatchBeforeRenderTick();
});
}

public Object getModule(String contextModuleName) {
return contextObject.get(contextModuleName);
}

public void putContextObject(String contextModuleName, Object object){
contextObject.put(contextModuleName, object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public boolean mouseReleased(double pMouseX, double pMouseY, int pButton) {
});
return true;
}

@Override
public boolean isPauseScreen() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package kasuga.lib.core.client.interaction;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

public class GuiOperatingPerspectiveScreen extends Screen {
float fov = 1.0F;

public GuiOperatingPerspectiveScreen() {
super(Component.literal(""));
}

public void render(int mouseX, int mouseY, float partialTicks) {}


@Override
public boolean mouseScrolled(double pMouseX, double pMouseY, double pDelta) {
fov = Math.max(Math.min(1.0F, fov - (float) pDelta / 5.0F), 0.1F);
return true;
}

@Override
public boolean mouseDragged(double pMouseX, double pMouseY, int pButton, double pDragX, double pDragY) {
if(super.mouseDragged(pMouseX, pMouseY, pButton, pDragX, pDragY)){
return true;
}
Minecraft.getInstance().player.turn(pDragX, pDragY);
return true;
}

@Override
public boolean isPauseScreen() {
return false;
}

public float getFovModifier() {
return fov;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kasuga.lib.core.events.client;

import kasuga.lib.core.client.interaction.GuiOperatingPerspectiveScreen;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.ComputeFovModifierEvent;

public class InteractionFovEvent {
public static void onComputedFov(ComputeFovModifierEvent event){
if(Minecraft.getInstance().screen != null &&
Minecraft.getInstance().screen instanceof GuiOperatingPerspectiveScreen operating)
event.setNewFovModifier(operating.getFovModifier());
}
}
18 changes: 15 additions & 3 deletions src/main/java/kasuga/lib/core/javascript/CompoundTagWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public CompoundTagWrapper(CompoundTag tag) {
this.tag = tag;
}

public CompoundTagWrapper(){
this(new CompoundTag());
}

@HostAccess.Export
public String getType(String key) {
return switch (tag.get(key).getId()) {
Expand Down Expand Up @@ -81,8 +85,8 @@ public ListTag getList(String key, int pTagType) {
}

@HostAccess.Export
public CompoundTag getCompound(String key) {
return tag.getCompound(key);
public CompoundTagWrapper getCompound(String key) {
return new CompoundTagWrapper(tag.getCompound(key));
}

@HostAccess.Export
Expand Down Expand Up @@ -140,11 +144,15 @@ public void putList(String key, ListTag value) {
tag.put(key, value);
}

@HostAccess.Export
public void putCompound(String key, CompoundTag value) {
tag.put(key, value);
}

@HostAccess.Export
public void putCompound(String key, CompoundTagWrapper wrapper){
tag.put(key, wrapper.getNativeTag());
}

@HostAccess.Export
public void putIntArray(String key, int[] value) {
tag.putIntArray(key, value);
Expand All @@ -154,4 +162,8 @@ public void putIntArray(String key, int[] value) {
public void putLongArray(String key, long[] value) {
tag.putLongArray(key, value);
}

public CompoundTag getNativeTag() {
return tag;
}
}
Loading
Loading