preparations for networking

Kleine Sicherungskopie vom funktionierenden networking part bevor ich wieder alles zerstöre
This commit is contained in:
Paula 2024-04-28 18:36:59 +02:00
parent 987461a790
commit 05a176f3c1
7 changed files with 75 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package modchest.networking;
import modchest.REServerMod;
import modchest.networking.packet.gridRenderRequestS2CPacket;
import modchest.networking.packet.respawnRequestS2CPacket;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.util.Identifier;
@ -9,7 +10,10 @@ public class modNetworkingClient { //Identifier werden eingeführt
public static final Identifier request_respawn = new Identifier(REServerMod.MOD_ID, "request_respawn"); //alle Identifier muessen leider IMMER auf Client und Server (doppelt) eingefuehrt werden
public static final Identifier start_sleeping_call_buttons = new Identifier(REServerMod.MOD_ID, "start_sleeping_call_buttons");
public static final Identifier death_multi_respawn_buttons = new Identifier(REServerMod.MOD_ID, "death_call_respawn_buttons");
public static final Identifier grid_block_networking = new Identifier(REServerMod.MOD_ID, "grid_block_networking");
public static void registerC2SPackets() { //Identifier fuer packets werden registriert (Identifier die der Server aufruft um beim CLient was auszufuehren)
ClientPlayNetworking.registerGlobalReceiver(request_respawn, respawnRequestS2CPacket::receive); //was der Client dann machen soll steht in der receive Methode
ClientPlayNetworking.registerGlobalReceiver(grid_block_networking, gridRenderRequestS2CPacket::receive);
}
}

View File

@ -0,0 +1,20 @@
package modchest.networking.packet;
import modchest.REServerMod;
import modchest.event.useBlockCallback;
import modchest.networking.modNetworkingClient;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
public class gridRenderRequestS2CPacket {
public static void receive(MinecraftClient minecraftClient, ClientPlayNetworkHandler clientPlayNetworkHandler, PacketByteBuf buffer, PacketSender PacketSender){
//clientPlayNetworkHandler.getWorld();
//modNetworkingClient.grid_block_networking.;
//PacketByteBuf buf =
}
}

View File

@ -1,12 +1,18 @@
package modchest.block.custom;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.block.Block;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.network.PacketByteBuf;
public class gridBlock extends Block{
public static final String ServerPlayNetworking = null;
public gridBlock(Settings settings) {
super(settings);
}
@ -21,4 +27,15 @@ public class gridBlock extends Block{
public boolean isSideInvisible(BlockState state, BlockState stateFrom, Direction direction) {
return super.isSideInvisible(state, stateFrom, direction) || (state == stateFrom);
}
public static PacketByteBuf writeBuffer(BlockHitResult hitResult){
BlockPos gridBlockPos = hitResult.getBlockPos();
PacketByteBuf gridBlockBuffer = PacketByteBufs.create();
gridBlockBuffer.writeInt(1);
gridBlockBuffer.writeBlockHitResult(hitResult);
gridBlockBuffer.writeBlockPos(gridBlockPos);
return gridBlockBuffer;
}
}

View File

@ -24,7 +24,7 @@ public class modBlocks {
modItemGroup.modchest);
grid_block = registerBlock("grid_block",
new gridBlock(FabricBlockSettings.of(Material.WOOD).strength(1.0f).requiresTool()),
new gridBlock(FabricBlockSettings.of(Material.WOOD).luminance(1).strength(1.0f).requiresTool()),
modItemGroup.modchest);
}

View File

@ -25,6 +25,13 @@ public class useBlockCallback implements UseBlockCallback{
Block BlockInHand = getBlockFromItem(player.getMainHandStack().getItem());
BlockState StateInHand = BlockInHand.getDefaultState();
BlockPos gridBlockPos = hitResult.getBlockPos();
/*PlayerPublicKey playerPublicKey = player.getPublicKey();
GameProfile gameProfile = player.getGameProfile();
MinecraftServer server = player.getServer();
ServerWorld serverWorld = server.getOverworld();
ServerPlayerEntity serverPlayerEntity = new ServerPlayerEntity(server, serverWorld, gameProfile, playerPublicKey);
ServerPlayerEntity sp = ;*/
if (world.getBlockState(hitResult.getBlockPos()).getBlock().toString().equals("Block{modchest:grid_block}") && player.isSpectator()==false) {
@ -34,8 +41,13 @@ public class useBlockCallback implements UseBlockCallback{
player.sendMessage(Text.of("gridBlock benutzt"));
world.setBlockState(gridBlockPos, StateInHand); //tauscht die Textur des "benutzten" gridBlocks mit der des Blocks in der Hand
player.getMainHandStack().decrement(1);
//sp.sendMessage(Text.of("das ist der serverplayer"));
player.jump();
//player.onLanding(player.playSoundIfNotSilent());
List<Position> gridBlockList = new LinkedList<Position>();
gridBlockList.add(hitResult.getPos());
//gridBlockS2CPacket.sendPacket(ServerPlayerEntity PlayerEntity, BlockHitResult hitResult);
}
} else {
return ActionResult.PASS;

View File

@ -10,6 +10,7 @@ public class modNetworkingServer { //Identifier werden eingeführt
public static final Identifier request_respawn = new Identifier(REServerMod.MOD_ID, "request_respawn"); //alle Identifier muessen leider IMMER auf Client und Server (doppelt) eingefuehrt werden
public static final Identifier start_sleeping_call_buttons = new Identifier(REServerMod.MOD_ID, "start_sleeping_call_buttons");
public static final Identifier death_multi_respawn_buttons = new Identifier(REServerMod.MOD_ID, "death_call_respawn_buttons");
public static final Identifier grid_block_networking = new Identifier(REServerMod.MOD_ID, "grid_block_networking");
public static void registerS2CPackets() { //Identifier fuer packets werden registriert (Identifier die der Client aufruft um beim Server was auszufuehren)
ServerPlayNetworking.registerGlobalReceiver(start_sleeping_call_buttons, setNewRespawnsC2SPacket::receive); //was der Server dann machen soll steht in der receive Methode

View File

@ -0,0 +1,20 @@
package modchest.networking.packet;
import modchest.block.custom.gridBlock;
import modchest.networking.modNetworkingServer;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.hit.BlockHitResult;
public class gridBlockS2CPacket {
public void sendPacket(ServerPlayerEntity serverPlayerEntity, BlockHitResult hitResult){
PacketByteBuf gridBuffer = gridBlock.writeBuffer(hitResult);
ServerPlayNetworking.send(serverPlayerEntity, modNetworkingServer.grid_block_networking, gridBuffer);
//The player in this method is the player the packet will be sent to. The channel name is the Identifier you decided on earlier
//to identify your packet. The PacketByteBuf is what stores the data for the packet.
}
}