networking bugfixed

Das networking funktioniert jetzt ohne Probleme mit der richtigen ServerPlayerEntity
This commit is contained in:
Paula 2024-05-18 17:23:23 +02:00
parent 8db9e9cf06
commit 74e05e6c7e
5 changed files with 58 additions and 23 deletions

View File

@ -1,13 +1,21 @@
package modchest.networking.packet;
import static net.minecraft.block.Block.getBlockFromItem;
/*import modchest.rendering.gridBlockRender;
import modchest.rendering.gridBlockTexture;*/
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.block.Block;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.block.BlockModelRenderer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
public class gridRenderRequestS2CPacket extends BlockModelRenderer {
@ -19,21 +27,19 @@ public class gridRenderRequestS2CPacket extends BlockModelRenderer {
public static void receive(MinecraftClient minecraftClient, ClientPlayNetworkHandler clientPlayNetworkHandler,
PacketByteBuf buffer, PacketSender PacketSender) {
// clientPlayNetworkHandler.getWorld();
// modNetworkingClient.grid_block_networking.;
// PacketByteBuf buf =
/*NbtCompound nbtgrid = buffer.readNbt();
NbtCompound nbtgrid = buffer.readNbt();
int[] positionarray = nbtgrid.getIntArray("BlockPosition");
Integer h = positionarray[0];
Integer i = positionarray[1];
Integer j = positionarray[2];*/
Integer j = positionarray[2];
BlockPos gridBlockPos = new BlockPos(h, i, j);
// minecraftClient.getBakedModelManager().getBlockModels();
// minecraftClient.getSpriteAtlas(gridBlockTexture.getBlockInHandId(null));
//BlockRenderView world = minecraftClient.world;
//BlockPos gridBlockPos = new BlockPos(h, i, j);
/*@Nullable
BlockState gridBlockState = getStateFromRawId(FORCE_STATE);
MatrixStack matrix = new MatrixStack();
@ -41,7 +47,7 @@ public class gridRenderRequestS2CPacket extends BlockModelRenderer {
Random randomGrid = new Random();
long seed = 7756495;*/
/*PlayerEntity playerEntity = minecraftClient.player;
PlayerEntity playerEntity = minecraftClient.player;
Block BlockInHand = getBlockFromItem(playerEntity.getMainHandStack().getItem());
String inHandData = BlockInHand.getDefaultState().toString();
playerEntity.sendMessage(Text.of(inHandData));

View File

@ -1,6 +1,16 @@
package modchest.rendering;
/*public class gridBlockRender extends BlockModelRenderer{
import net.minecraft.block.BlockState;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.block.BlockModelRenderer;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockRenderView;
public class gridBlockRender extends BlockModelRenderer{
public gridBlockRender(BlockColors colors) {
super(colors);
@ -11,4 +21,4 @@ package modchest.rendering;
MatrixStack matrices, VertexConsumer vertexConsumer, boolean cull, Random random, long seed, int overlay) {
}
}*/
}

View File

@ -1,11 +1,21 @@
package modchest.rendering;
//public class gridBlockTexture extends TextureManager{
import net.minecraft.client.texture.AbstractTexture;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
//public gridBlockTexture(ResourceManager resourceManager) {
// super(resourceManager);
public class gridBlockTexture extends TextureManager{
public gridBlockTexture(ResourceManager resourceManager) {
super(resourceManager);
//TODO Auto-generated constructor stub
//}
}
@Override
public AbstractTexture getTexture(Identifier id) {
return super.getTexture(id);
}
/*public static BlockColors getBlockColors(Block block, BlockRenderView world, BlockPos pos, BlockColors blockColors) {
BlockState state = block.getDefaultState();
@ -14,11 +24,6 @@ package modchest.rendering;
return colors;
}*/
/*@Override
public AbstractTexture getTexture(Identifier id) {
return super.getTexture(id);
}*/
/*public static Overlay getOverlayGrid(){
MinecraftClient minecraftClient = MinecraftClient.getInstance();
PlayerEntity player = minecraftClient.player;
@ -30,6 +35,6 @@ package modchest.rendering;
//stateInHand.getRenderingSeed
//}
}

View File

@ -3,10 +3,15 @@ package modchest.event;
import static net.minecraft.block.Block.getBlockFromItem;
import modchest.block.custom.gridBlock;
import modchest.networking.modNetworkingServer;
import modchest.networking.packet.gridBlockS2CPacket;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
@ -21,8 +26,8 @@ public class useBlockCallback implements UseBlockCallback {
Block BlockInHand = getBlockFromItem(player.getMainHandStack().getItem());
BlockState StateInHand = BlockInHand.getDefaultState();
BlockPos gridBlockPos = hitResult.getBlockPos();
//PlayerPublicKey playerPublicKey = player.getPublicKey();
//ServerPlayerEntity serverPlayer = ((ServerPlayerEntity) player);
// PlayerPublicKey playerPublicKey = player.getPublicKey();
if (world.getBlockState(hitResult.getBlockPos()).getBlock().toString().equals("Block{modchest:grid_block}")
&& player.isSpectator() == false) {
if (BlockInHand instanceof gridBlock) {
@ -34,7 +39,13 @@ public class useBlockCallback implements UseBlockCallback {
player.getMainHandStack().decrement(1);
player.jump();
// player.onLanding(player.playSoundIfNotSilent());
//gridBlockS2CPacket.sendPacket(serverPlayer, hitResult);
if (player instanceof ServerPlayerEntity) { // nötig, um sicherzustellen, dass eine ServerPlayerEntity
// fürs networking übergeben wird und nicht eine
// PlayerEntity, da sonst Absturz
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
ServerPlayNetworking.send(serverPlayer, modNetworkingServer.grid_block_networking,
gridBlock.writeBuffer(hitResult));
}
}
} else {
return ActionResult.PASS;

View File

@ -1,10 +1,13 @@
package modchest.networking.packet;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
public class gridBlockS2CPacket {
public static void sendPacket(ServerPlayerEntity serverPlayerEntity, BlockHitResult hitResult) {
//PacketByteBuf gridBuffer = gridBlock.writeBuffer(hitResult);
//ServerPlayNetworking.send(serverPlayerEntity, modNetworkingServer.grid_block_networking, gridBuffer);