33import java .io .IOException ;
44import java .util .List ;
55
6+ import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
67import net .fabricmc .fabric .api .networking .v1 .PacketByteBufs ;
8+ import net .fabricmc .fabric .api .networking .v1 .PacketSender ;
79import net .fabricmc .fabric .api .networking .v1 .ServerPlayNetworking ;
10+ import net .minecraft .block .entity .BlockEntity ;
811import net .minecraft .network .PacketByteBuf ;
12+ import net .minecraft .server .MinecraftServer ;
13+ import net .minecraft .server .network .ServerPlayNetworkHandler ;
914import net .minecraft .server .network .ServerPlayerEntity ;
1015import net .minecraft .util .hit .BlockHitResult ;
16+ import net .minecraft .util .math .BlockPos ;
17+ import net .minecraft .util .math .Direction ;
1118import org .apache .logging .log4j .LogManager ;
1219import org .apache .logging .log4j .Logger ;
13- import org .dimdev .dimdoors .DimensionalDoorsInitializer ;
1420import org .dimdev .dimdoors .block .entity .RiftBlockEntity ;
1521
1622import net .minecraft .client .item .TooltipContext ;
2733import net .minecraft .world .World ;
2834
2935import net .fabricmc .api .Environment ;
36+ import org .dimdev .dimdoors .network .c2s .HitBlockS2CPacket ;
3037import org .dimdev .dimdoors .network .s2c .PlayerInventorySlotUpdateS2CPacket ;
3138import org .dimdev .dimdoors .rift .targets .IdMarker ;
3239import org .dimdev .dimdoors .util .EntityUtils ;
@@ -49,18 +56,14 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand h
4956 HitResult hit = player .raycast (RaycastHelper .REACH_DISTANCE , 0 , false );
5057
5158 if (world .isClient ) {
52- if (!RaycastHelper .hitsRift (hit , world )) {
53- EntityUtils .chat (player , new TranslatableText ("tools.rift_miss" ));
54- RiftBlockEntity .showRiftCoreUntil = System .currentTimeMillis () + DimensionalDoorsInitializer .CONFIG .getGraphicsConfig ().highlightRiftCoreFor ;
55- }
56- return new TypedActionResult <>(ActionResult .FAIL , stack );
59+ return TypedActionResult .fail (stack );
5760 } else {
5861 Counter counter = Counter .get (stack );
5962
6063 if (RaycastHelper .hitsRift (hit , world )) {
6164 RiftBlockEntity rift = (RiftBlockEntity ) world .getBlockEntity (((BlockHitResult ) hit ).getBlockPos ());
6265
63- if (rift .getDestination () instanceof IdMarker ) {
66+ if (rift .getDestination () instanceof IdMarker && (( IdMarker ) rift . getDestination ()). getId () >= 0 ) {
6467 EntityUtils .chat (player , Text .of ("Id: " + ((IdMarker ) rift .getDestination ()).getId ()));
6568 } else {
6669 int id = counter .increment ();
@@ -70,21 +73,63 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand h
7073 rift .setDestination (new IdMarker (id ));
7174 }
7275
73- return new TypedActionResult <>( ActionResult . SUCCESS , stack );
76+ return TypedActionResult . success ( stack );
7477 } else {
75- if (player .isSneaking ()) {
76- counter .set (-1 );
77- sync (stack , player , hand );
78- EntityUtils .chat (player , Text .of ("Counter has been reset." ));
79- } else {
80- EntityUtils .chat (player , Text .of ("Current Count: " + counter .count ()));
78+ EntityUtils .chat (player , Text .of ("Current Count: " + counter .count ()));
79+ }
80+ }
81+
82+ return TypedActionResult .success (stack );
83+ }
84+
85+ public static ActionResult onAttackBlockCallback (PlayerEntity player , World world , Hand hand , BlockPos pos , Direction direction ) {
86+ if (world .isClient && player .isSneaking () && player .getStackInHand (hand ).getItem () instanceof RiftConfigurationToolItem ) {
87+ if (Counter .get (player .getStackInHand (hand )).count () != -1 || world .getBlockEntity (pos ) instanceof RiftBlockEntity ) {
88+ HitBlockS2CPacket packet = new HitBlockS2CPacket (hand , pos , direction );
89+ try {
90+ PacketByteBuf buf = PacketByteBufs .create ();
91+ packet .write (buf );
92+ ClientPlayNetworking .send (HitBlockS2CPacket .ID , buf );
93+ } catch (IOException e ) {
94+ LOGGER .error (e );
95+ return ActionResult .FAIL ;
8196 }
8297 }
98+ return ActionResult .SUCCESS ;
8399 }
100+ return ActionResult .PASS ;
101+ }
84102
85- return new TypedActionResult <>(ActionResult .SUCCESS , stack );
103+ public static void receiveHitBlock (MinecraftServer server , ServerPlayerEntity player , ServerPlayNetworkHandler networkHandler , PacketByteBuf buf , PacketSender sender ) {
104+ HitBlockS2CPacket packet = new HitBlockS2CPacket ();
105+ try {
106+ packet .read (buf );
107+ server .execute (() -> serverThreadReceiveHitBlock (player , packet .getHand (), packet .getPos ()));
108+ } catch (IOException e ) {
109+ LOGGER .error (e );
110+ }
111+ }
112+
113+ private static void serverThreadReceiveHitBlock (ServerPlayerEntity player , Hand hand , BlockPos pos ) {
114+ ItemStack stack = player .getStackInHand (hand );
115+ if (player .isSneaking () && stack .getItem () instanceof RiftConfigurationToolItem ) {
116+ BlockEntity blockEntity = player .getServerWorld ().getBlockEntity (pos );
117+ if (blockEntity instanceof RiftBlockEntity ) {
118+ RiftBlockEntity rift = (RiftBlockEntity ) blockEntity ;
119+ if (!(rift .getDestination () instanceof IdMarker ) || ((IdMarker ) rift .getDestination ()).getId () != -1 ) {
120+ rift .setDestination (new IdMarker (-1 ));
121+ EntityUtils .chat (player , Text .of ("Rift stripped of data and set to invalid id: -1" ));
122+ }
123+ } else if (Counter .get (stack ).count () != -1 ){
124+ Counter .get (stack ).set (-1 );
125+ ((RiftConfigurationToolItem ) stack .getItem ()).sync (stack , player , hand );
126+
127+ EntityUtils .chat (player , Text .of ("Counter has been reset." ));
128+ }
129+ }
86130 }
87131
132+
88133 @ Override
89134 @ Environment (CLIENT )
90135 public void appendTooltip (ItemStack itemStack , World world , List <Text > list , TooltipContext tooltipContext ) {
0 commit comments