-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
ff44a3e
commit d33cef1
Showing
6 changed files
with
51 additions
and
42 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
46 changes: 22 additions & 24 deletions
46
src/main/java/team/dovecotmc/glasses/common/network/handler/NetworkHandler.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 |
---|---|---|
@@ -1,33 +1,31 @@ | ||
package team.dovecotmc.glasses.common.network.handler; | ||
|
||
import net.minecraft.server.level.ServerPlayer; | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.fml.common.Mod; | ||
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent; | ||
import net.neoforged.neoforge.network.registration.IPayloadRegistrar; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.neoforged.neoforge.network.NetworkRegistry; | ||
import net.neoforged.neoforge.network.simple.SimpleChannel; | ||
import team.dovecotmc.glasses.Glasses; | ||
import team.dovecotmc.glasses.common.item.base.GlassesItem; | ||
import team.dovecotmc.glasses.common.network.msg.GlassesUseMessage; | ||
import team.dovecotmc.glasses.util.common.CommonUtilities; | ||
|
||
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) | ||
public class NetworkHandler { | ||
@SubscribeEvent | ||
public static void registerPayload(RegisterPayloadHandlerEvent event) { | ||
final IPayloadRegistrar registrar = event.registrar(Glasses.MODID); | ||
registrar.play(GlassesUseMessage.ID, GlassesUseMessage::new, h -> h | ||
.server((d, ctx) -> ctx.workHandler().submitAsync(() -> ctx.player().ifPresent(p -> { | ||
if (!(p instanceof ServerPlayer player)) return; | ||
CommonUtilities.getMatchedWearingItem(player, CommonUtilities.GLASSES).ifPresent(m -> { | ||
if (m.getItem() instanceof GlassesItem g | ||
&& (g.getProperties().packetAction() != null | ||
|| g.getProperties().canUse())) | ||
g.onReceivePacket(player, m); | ||
}); | ||
})).exceptionally(e -> { | ||
Glasses.LOGGER.error("Error occurred while handling 'use' message for glasses", e); | ||
return null; | ||
})) | ||
public static SimpleChannel INSTANCE; | ||
public static final String VERSION = "1"; | ||
private static int id = 0; | ||
|
||
public static int nextId() { | ||
return id++; | ||
} | ||
|
||
public static void registerMessage() { | ||
INSTANCE = NetworkRegistry.newSimpleChannel( | ||
new ResourceLocation(Glasses.MODID, "use"), | ||
() -> VERSION, | ||
VERSION::equals, | ||
VERSION::equals | ||
); | ||
INSTANCE.messageBuilder(GlassesUseMessage.class, nextId()) | ||
.encoder(GlassesUseMessage::toBytes) | ||
.decoder(GlassesUseMessage::new) | ||
.consumerMainThread(GlassesUseMessage::handler) | ||
.add(); | ||
} | ||
} |
33 changes: 21 additions & 12 deletions
33
src/main/java/team/dovecotmc/glasses/common/network/msg/GlassesUseMessage.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 |
---|---|---|
@@ -1,25 +1,34 @@ | ||
package team.dovecotmc.glasses.common.network.msg; | ||
|
||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload; | ||
import net.minecraft.resources.ResourceLocation; | ||
import team.dovecotmc.glasses.Glasses; | ||
|
||
public class GlassesUseMessage implements CustomPacketPayload { | ||
public static final ResourceLocation ID = new ResourceLocation(Glasses.MODID, "use"); | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.neoforged.neoforge.network.NetworkEvent; | ||
import team.dovecotmc.glasses.common.item.base.GlassesItem; | ||
import team.dovecotmc.glasses.util.common.CommonUtilities; | ||
|
||
public class GlassesUseMessage { | ||
public GlassesUseMessage(FriendlyByteBuf buf) { | ||
} | ||
|
||
public GlassesUseMessage() { | ||
} | ||
|
||
@Override | ||
public void write(FriendlyByteBuf pBuffer) { | ||
public void toBytes(FriendlyByteBuf buf) { | ||
|
||
} | ||
|
||
@Override | ||
public ResourceLocation id() { | ||
return ID; | ||
public void handler(NetworkEvent.Context ctx) { | ||
ctx.enqueueWork(() -> { | ||
final ServerPlayer player = ctx.getSender(); | ||
if (player == null) return; | ||
CommonUtilities.getMatchedWearingItem(player, CommonUtilities.GLASSES).ifPresent(m -> { | ||
if (m.getItem() instanceof GlassesItem g | ||
&& (g.getProperties().packetAction() != null | ||
|| g.getProperties().canUse())) | ||
g.onReceivePacket(player, m); | ||
}); | ||
}); | ||
ctx.setPacketHandled(true); | ||
} | ||
} | ||
|
||
} |