-
Notifications
You must be signed in to change notification settings - Fork 34
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
Add game packets #126
Open
haykam821
wants to merge
4
commits into
NucleoidMC:1.16
Choose a base branch
from
haykam821:game-packets
base: 1.16
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add game packets #126
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package xyz.nucleoid.plasmid.event; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.Identifier; | ||
import xyz.nucleoid.plasmid.Plasmid; | ||
import xyz.nucleoid.plasmid.game.ConfiguredGame; | ||
import xyz.nucleoid.plasmid.game.GameCloseReason; | ||
import xyz.nucleoid.plasmid.game.GameSpace; | ||
import xyz.nucleoid.plasmid.game.GameType; | ||
|
||
/** | ||
* A helper class for creating {@linkplain net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket custom payload packets} to send to clients. | ||
*/ | ||
public final class GamePackets { | ||
private GamePackets() { } | ||
|
||
public static CustomPayloadS2CPacket playerAdd(GameSpace gameSpace, ServerPlayerEntity player) { | ||
return createPacket("player_add", buf -> writeGameAndPlayerInfo(buf, gameSpace, player)); | ||
} | ||
|
||
public static CustomPayloadS2CPacket playerRemove(GameSpace gameSpace, ServerPlayerEntity player) { | ||
return createPacket("player_remove", buf -> writeGameAndPlayerInfo(buf, gameSpace, player)); | ||
} | ||
|
||
public static CustomPayloadS2CPacket gameClose(GameSpace gameSpace, GameCloseReason reason) { | ||
return createPacket("game_close", buf -> { | ||
writeGameInfo(buf, gameSpace); | ||
buf.writeString(reason == GameCloseReason.FINISHED ? "finished" : "canceled"); | ||
}); | ||
} | ||
|
||
private static void writeGameAndPlayerInfo(PacketByteBuf buf, GameSpace gameSpace, ServerPlayerEntity player) { | ||
writeGameInfo(buf, gameSpace); | ||
writePlayerInfo(buf, gameSpace, player); | ||
} | ||
|
||
private static void writeGameInfo(PacketByteBuf buf, GameSpace gameSpace) { | ||
ConfiguredGame<?> game = gameSpace.getGameConfig(); | ||
GameType<?> gameType = game.getType(); | ||
|
||
// Game type ID and name | ||
buf.writeIdentifier(gameType.getIdentifier()); | ||
buf.writeText(gameType.getName()); | ||
|
||
// Game ID and name | ||
buf.writeIdentifier(game.getSource()); | ||
buf.writeText(game.getNameText()); | ||
} | ||
|
||
private static void writePlayerInfo(PacketByteBuf buf, GameSpace gameSpace, ServerPlayerEntity player) { | ||
buf.writeInt(gameSpace.getPlayerCount()); | ||
buf.writeUuid(player.getUuid()); | ||
} | ||
|
||
private static CustomPayloadS2CPacket createPacket(String type, Consumer<PacketByteBuf> writer) { | ||
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); | ||
writer.accept(buf); | ||
|
||
return new CustomPayloadS2CPacket(createChannel(type), buf); | ||
} | ||
|
||
private static Identifier createChannel(String type) { | ||
return new Identifier(Plasmid.ID, "game/" + type); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, I'm not sure if this should be included in the packet. Relying on game ids is never a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific reason why packet consumers should not be able to have access to the game ID?
To be more specific, providing game type IDs (which I am not sure whether you are against as well) would allow packet consumers to distinguish between game types for icons, and providing game IDs would allow packet consumers to differentiate by individual game as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can’t and shouldn’t be relied on by anything. Games don’t have an ID- hence the rename to source. It’s just where the game was loaded from, and that might not even be present in the case of an anonymously created game.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Packet consumers would be required to handle anonymous games, then. If the identifier was removed, packet consumers would just end up relying on text, which is worse. After all, games can have no text as well (though, this just falls back rather than being
null
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should probably rather be passed all the game custom values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that custom value not just be the same as the game ID? In addition, would all anonymous games have that custom value as well? It would duplicate the existing game ID for not much benefit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No- it would provide whatever values are needed directly. Anonymous games are an example of where things would not have a game id, but really the motivation in this case is they can't be considered to be stable. A new game config can be added, or a config can be moved around- but then you have to update the map within the tracker to consider that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The game ID is the thing that this map would need, and for the problem the tracker aims to solve, there is no way around it; any attempts at providing a custom value replacement would just duplicate the ID.
I think packet consumers can handle the task of recognizing that the given game IDs are not stable and may not exist. The tracker would ignore games without IDs and treat games with changed IDs as entirely separate games, unless the user chooses to merge two game IDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is to not have the map in the first place, but rather the values within the map provided within the game. What do you imagine this map would be used to store? I suspect I am misunderstanding something and causing us to not get anywhere 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The map would store play counts for each game.