Skip to content

Commit

Permalink
Implement Common-Networking by Mysticdrew
Browse files Browse the repository at this point in the history
Various other changes and fixes
  • Loading branch information
GirafiStudios committed Dec 5, 2023
1 parent 2c06ce5 commit 0c2ed5b
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 131 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ subprojects {
name = "Forge Config API Port mod"
url = "https://raw.githubusercontent.com/Fuzss/modresources/main/maven/"
}
maven {
name = "Common Networking by Mysticdrew"
url = "https://jm.gserv.me/repository/maven-public/"
}
}

tasks.withType(JavaCompile).configureEach {
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ dependencies {
compileOnly group: 'org.spongepowered', name: 'mixin', version: '0.8.5'
implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1'
implementation "fuzs.forgeconfigapiport:forgeconfigapiport-fabric:${forge_config_api_port}"
implementation 'mysticdrew:common-networking-common:1.0.4-1.20.2'
}
10 changes: 9 additions & 1 deletion common/src/main/java/com/girafi/ping/PingCommon.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.girafi.ping;

import com.girafi.ping.network.packet.ClientSendPing;
import com.girafi.ping.network.packet.ServerBroadcastPing;
import com.girafi.ping.util.TempConfig;
import commonnetwork.api.Network;

import java.nio.file.Path;

public class PingCommon {
private static TempConfig config;

public static void setConfigFolder (Path configPath) {
public static void loadCommon(Path configPath) {
config = TempConfig.load(configPath.resolve(Constants.MOD_ID + ".json").toFile());


Network.registerPacket(ClientSendPing.CHANNEL, ClientSendPing.class, ClientSendPing::encode, ClientSendPing::decode, ClientSendPing::handle)
.registerPacket(ServerBroadcastPing.CHANNEL, ServerBroadcastPing.class, ServerBroadcastPing::encode, ServerBroadcastPing::decode, ServerBroadcastPing::handle);
}

public static TempConfig config() {
return config;
}

}
22 changes: 15 additions & 7 deletions common/src/main/java/com/girafi/ping/client/ClientHandlerBase.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.girafi.ping.client;

import com.girafi.ping.PingCommon;
import com.girafi.ping.data.PingType;
import com.girafi.ping.data.PingWrapper;
import com.girafi.ping.network.packet.ClientSendPing;
import commonnetwork.api.Dispatcher;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;

import javax.annotation.Nullable;
import java.awt.*;
import java.util.Objects;

public class ClientHandlerBase {
public static final ClientHandlerBase INSTANCE = new ClientHandlerBase();

@Nullable
public static ShaderInstance rendertypePing;

Expand All @@ -23,11 +28,14 @@ public static BlockHitResult raytrace(Player player, double distance) {
return (BlockHitResult) player.pick(distance, eyeHeight, false);
}

public void sendPing(BlockHitResult raytrace, int color, PingType type) {
System.out.println("Send Ping common");
public static void sendPing(BlockHitResult raytrace, int color, PingType type) {
Dispatcher.sendToServer(new ClientSendPing(new PingWrapper(raytrace.getBlockPos(), color, type)));
}

public void sendPing(PingType type) {
System.out.println("Send Ping common PingType");
public static void sendPing(PingType type) {
BlockHitResult raytraceBlock = raytrace(Minecraft.getInstance().player, 50);
if (raytraceBlock.getType() == HitResult.Type.BLOCK) {
sendPing(raytraceBlock, new Color(PingCommon.config().pingColorRed, PingCommon.config().pingColorGreen, PingCommon.config().pingColorBlue).getRGB(), type);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
package com.girafi.ping.client;

import com.girafi.ping.Constants;
import com.girafi.ping.client.gui.PingSelectGui;
import com.girafi.ping.data.PingType;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.TickEvent;
import org.lwjgl.glfw.GLFW;

@Mod.EventBusSubscriber(modid = Constants.MOD_ID, value = Dist.CLIENT)
public class KeyHandler {
public class KeyHelper {

@SubscribeEvent
public static void onClientTick(TickEvent.ClientTickEvent event) {
public static void onTick() {
PingHandlerHelper.pingTimer();

if (event.phase == TickEvent.Phase.END) {
Minecraft mc = Minecraft.getInstance();
if ((mc.level == null || mc.isPaused()) && PingSelectGui.active) {
PingSelectGui.deactivate();
}
return;
}

Minecraft mc = Minecraft.getInstance();

if (mc.level == null) {
return;
}

long handle = Minecraft.getInstance().getWindow().getWindow();
int keycode = PingKeybinds.KEY_BINDING.getKey().getValue();
int keycode = PingKeybinds.KEY_BINDING.getDefaultKey().getValue();
if (keycode >= 0) {
boolean keyPressed = (PingKeybinds.KEY_BINDING.matchesMouse(keycode) ? GLFW.glfwGetMouseButton(handle, keycode) == 1 : InputConstants.isKeyDown(handle, keycode));

Expand All @@ -56,17 +41,17 @@ public static void onClientTick(TickEvent.ClientTickEvent event) {
}

if (canSendQuickPing(PingKeybinds.PING_ALERT)) {
ClientHandler.INSTANCE.sendPing(PingType.ALERT);
ClientHandlerBase.sendPing(PingType.ALERT);
} else if (canSendQuickPing(PingKeybinds.PING_MINE)) {
ClientHandler.INSTANCE.sendPing(PingType.MINE);
ClientHandlerBase.sendPing(PingType.MINE);
} else if (canSendQuickPing(PingKeybinds.PING_LOOK)) {
ClientHandler.INSTANCE.sendPing(PingType.LOOK);
ClientHandlerBase.sendPing(PingType.LOOK);
} else if (canSendQuickPing(PingKeybinds.PING_GOTO)) {
ClientHandler.INSTANCE.sendPing(PingType.GOTO);
ClientHandlerBase.sendPing(PingType.GOTO);
}
}

private static boolean canSendQuickPing(KeyMapping keyBinding) {
return keyBinding.isDown(); //Will continuously be triggered when key is held down due to vanilla issue.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
import com.mojang.math.Axis;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -37,11 +34,10 @@
import java.util.List;

public class PingHandlerHelper {
public static final PingHandlerHelper INSTANCE = new PingHandlerHelper();
public static final ResourceLocation TEXTURE = new ResourceLocation(Constants.MOD_ID, "textures/ping.png");
private static final List<PingWrapper> ACTIVE_PINGS = new ArrayList<>();

public void onPingPacket(ServerBroadcastPing packet) {
public static void onPingPacket(ServerBroadcastPing packet) {
Minecraft mc = Minecraft.getInstance();
if (mc.player != null && Mth.sqrt((float) mc.player.distanceToSqr(packet.ping.pos.getX(), packet.ping.pos.getY(), packet.ping.pos.getZ())) <= PingCommon.config().pingAcceptDistance) {
if (PingCommon.config().playPingSound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static RenderType getPingIcon(ResourceLocation location) {
public static Object createRenderType(String name, VertexFormat vertexFormat, VertexFormat.Mode mode, int i, boolean b, boolean b1, RenderType.CompositeState compositeState) {
Object compositeRenderType = null;
try {
Method m = RenderType.class.getDeclaredMethod("create");
Method m = RenderType.class.getDeclaredMethod("create", String.class, VertexFormat.class, VertexFormat.Mode.class, Integer.TYPE, Boolean.TYPE, Boolean.TYPE, RenderType.CompositeState.class);
m.setAccessible(true);
compositeRenderType = m.invoke(null, name, vertexFormat, mode, i, b, b1, compositeState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
mouseY >= (drawY - ITEM_SIZE * 0.5D) && mouseY <= (drawY + ITEM_SIZE * 0.5D);

if (mouseIn) {
System.out.println("Mouse click");
ClientHandlerBase.INSTANCE.sendPing(type);
ClientHandlerBase.sendPing(type);
PingKeybinds.Helper.ignoreNextRelease = true;
return true;
}
Expand All @@ -84,7 +83,7 @@ public void renderBackground(@Nonnull GuiGraphics guiGraphics, int i, int i1, fl
int halfHeight = (ITEM_SIZE + ITEM_PADDING) / 2;
int backgroundX = minecraft.getWindow().getGuiScaledWidth() / 2 - halfWidth;
int backgroundY = minecraft.getWindow().getGuiScaledHeight() / 4 - halfHeight;
guiGraphics.fillGradient(backgroundX, backgroundY, this.width / 2 + halfWidth, this.height / 3 + (halfHeight / 2), -1072689136, -804253680);
guiGraphics.fillGradient(backgroundX, backgroundY, this.width / 2 + halfWidth, ((this.height / 4) + (halfHeight * 2)) - 10, -1072689136, -804253680);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
package com.girafi.ping.network.packet;

import com.girafi.ping.Constants;
import com.girafi.ping.data.PingWrapper;
import commonnetwork.api.Dispatcher;
import commonnetwork.networking.data.PacketContext;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;

/**
* Sent from the Client, handled on the Server
*/
public class ClientSendPing {
private final PingWrapper ping;
public static final ResourceLocation CHANNEL = new ResourceLocation(Constants.MOD_ID, "client_send_ping");
private PingWrapper ping;

public ClientSendPing() {}

public ClientSendPing(PingWrapper ping) {
this.ping = ping;
}

public PingWrapper getPing() {
return ping;
}

public static void encode(ClientSendPing pingPacket, FriendlyByteBuf buf) {
pingPacket.ping.writeToBuffer(buf);
}

public static ClientSendPing receive(FriendlyByteBuf buf) {
public static ClientSendPing decode(FriendlyByteBuf buf) {
return new ClientSendPing(PingWrapper.readFromBuffer(buf));
}

public PingWrapper getPing() {
return ping;
}

public static void handle(PacketContext<ClientSendPing> ctx) {
ServerPlayer playerMP = ctx.sender();
if (playerMP != null) {
for (Player player : playerMP.level().players()) {
if (player instanceof ServerPlayer) {
Dispatcher.sendToServer(new ServerBroadcastPing(ctx.message().getPing()));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.girafi.ping.network.packet;

import com.girafi.ping.Constants;
import com.girafi.ping.client.PingHandlerHelper;
import com.girafi.ping.data.PingWrapper;
import commonnetwork.networking.data.PacketContext;
import commonnetwork.networking.data.Side;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;

/**
* Sent from the Server, handled on the Client
*/
public class ServerBroadcastPing {
public static final ResourceLocation CHANNEL = new ResourceLocation(Constants.MOD_ID, "server_broadcast_ping");
public PingWrapper ping;

public ServerBroadcastPing() {}

public ServerBroadcastPing(PingWrapper ping) {
this.ping = ping;
}
Expand All @@ -17,7 +25,13 @@ public static void encode(ServerBroadcastPing pingPacket, FriendlyByteBuf buf) {
pingPacket.ping.writeToBuffer(buf);
}

public static ServerBroadcastPing receive(FriendlyByteBuf buf) {
public static ServerBroadcastPing decode(FriendlyByteBuf buf) {
return new ServerBroadcastPing(PingWrapper.readFromBuffer(buf));
}

public static void handle(PacketContext<ServerBroadcastPing> ctx) {
if (ctx.side() == Side.SERVER) {
PingHandlerHelper.onPingPacket(ctx.message());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//package com.girafi.ping.util;
//
//import dmillerw.ping.Ping;
//import net.minecraft.resources.ResourceLocation;
//import net.minecraft.sounds.SoundEvent;
//import net.minecraftforge.registries.DeferredRegister;
//import net.minecraftforge.registries.ForgeRegistries;
//import net.minecraftforge.registries.RegistryObject;
//
//public class PingSounds {
// public static final DeferredRegister<SoundEvent> SOUND_DEFERRED = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, Ping.MOD_ID);
Expand Down
5 changes: 5 additions & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ dependencies {
implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1'
implementation project(":common")
modImplementation "fuzs.forgeconfigapiport:forgeconfigapiport-fabric:${forge_config_api_port}"
implementation 'mysticdrew:common-networking-fabric:1.0.4-1.20.2'
}

remapJar {
archiveClassifier.set("fabric")
}

loom {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.girafi.ping;

import fuzs.forgeconfigapiport.api.config.v3.ForgeConfigRegistry;
import net.fabricmc.api.ModInitializer;
import net.neoforged.fml.config.ModConfig;

public class Waddles implements ModInitializer {
public class Ping implements ModInitializer {

@Override
public void onInitialize() {
Expand Down
2 changes: 1 addition & 1 deletion fabric/src/main/resources/waddles.fabric.mixins.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"required": true,
"minVersion": "0.8",
"package": "com.girafi.waddles.mixin",
"package": "com.girafi.ping.mixin",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_17",
"mixins": [
Expand Down
12 changes: 12 additions & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ minecraft {

sourceSets.main.resources.srcDir 'src/generated/resources'

jarJar.enable()
dependencies {
minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
implementation fg.deobf('mysticdrew:common-networking-forge:1.0.4-1.20.2')
compileOnly project(":common")

jarJar group: 'mysticdrew', name: 'common-networking-neoforge', version: '[1.0.1, 5.0.0)'
}

reobf {
jarJar {}
}

tasks.withType(JavaCompile).configureEach {
Expand All @@ -75,6 +83,10 @@ tasks.named("sourcesJar", Jar) {
from(project(":common").sourceSets.main.allSource)
}

tasks.jarJar.configure {
archiveClassifier.set('forge')
}

processResources {
from project(":common").sourceSets.main.resources
}
Expand Down
1 change: 1 addition & 0 deletions forge/src/main/java/com/girafi/ping/Ping.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public Ping() {
}

public void setupCommon(final FMLCommonSetupEvent event) {

}

public void setupClient(final FMLClientSetupEvent event) {
Expand Down

0 comments on commit 0c2ed5b

Please sign in to comment.