Skip to content

Commit

Permalink
Hotfix
Browse files Browse the repository at this point in the history
* 修复鲁伯特之泪无法减少耐久的 Bug
  • Loading branch information
WinExp committed May 2, 2024
1 parent 6fc70d0 commit ddb2350
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.winexp.battlegrounds.util.raycast.BlockRaycastResult;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.item.ItemStack;
Expand All @@ -36,7 +37,7 @@ public class RupertsTearItem extends ToolItem implements EnchantRestrict {
public static final int MAX_DISTANCE = 100;
public static final int MAX_COOLDOWN = 30 * 20;
public static final int MIN_COOLDOWN = 3 * 20;
public static final int FAILED_COOLDOWN = 20;
public static final int FAILED_COOLDOWN = 30;

public RupertsTearItem(ToolMaterial toolMaterial, Settings settings) {
super(toolMaterial, settings);
Expand Down Expand Up @@ -71,6 +72,12 @@ public boolean isEnchantable(ItemStack stack) {
return false;
}

public static void damageStack(ServerPlayerEntity player, Hand hand) {
ItemStack stack = player.getStackInHand(hand);
EquipmentSlot slot = hand == Hand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND;
stack.damage(1, player, slot);
}

public static void teleport(ServerPlayerEntity player, Vec3d teleportPos, double distance) {
ServerWorld world = player.getServerWorld();
Vec3d pos = player.getPos();
Expand Down Expand Up @@ -106,7 +113,7 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
double y = BlockUtil.getBlockMaxY(world, pos);
Box boundingBox = BlockUtil.getCollisionShape(world, pos).getBoundingBox();
Vec3d tpPos = boundingBox.getCenter().add(Vec3d.of(pos)).withAxis(Direction.Axis.Y, y);
RupertsTearTeleportPayloadC2S packet = new RupertsTearTeleportPayloadC2S(stack, tpPos);
RupertsTearTeleportPayloadC2S packet = new RupertsTearTeleportPayloadC2S(hand, tpPos);
ClientPlayNetworking.send(packet);
}
return TypedActionResult.success(stack, world.isClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import com.github.winexp.battlegrounds.network.payload.s2c.play.vote.*;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;

Expand Down Expand Up @@ -68,12 +68,14 @@ private static void onVote(VotePayloadC2S packet, ServerPlayNetworking.Context c

private static void onRupertsTearTeleport(RupertsTearTeleportPayloadC2S packet, ServerPlayNetworking.Context context) {
ServerPlayerEntity player = context.player();
ItemStack stack = packet.itemStack();
Hand hand = packet.hand();
ItemStack stack = player.getStackInHand(hand);
Vec3d teleportPos = packet.teleportPos();
double distance = Math.floor(teleportPos.distanceTo(player.getEyePos()));
if (player.getInventory().contains(stack) && distance <= RupertsTearItem.MAX_DISTANCE) {
if (distance <= RupertsTearItem.MAX_DISTANCE) {
if (!stack.isOf(Items.RUPERTS_TEAR) || stack.getDamage() >= stack.getMaxDamage()) return;
player.server.execute(() -> RupertsTearItem.teleport(player, teleportPos, distance));
stack.damage(1, player, EquipmentSlot.MAINHAND);
RupertsTearItem.damageStack(player, hand);
} else {
player.getItemCooldownManager().set(Items.RUPERTS_TEAR, RupertsTearItem.FAILED_COOLDOWN);
player.sendMessage(Text.translatable("item.battlegrounds.ruperts_tear.use_failed"), true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.github.winexp.battlegrounds.network.payload.c2s.play;

import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;

public record RupertsTearTeleportPayloadC2S(ItemStack itemStack, Vec3d teleportPos) implements CustomPayload {
public record RupertsTearTeleportPayloadC2S(Hand hand, Vec3d teleportPos) implements CustomPayload {
public static final Id<RupertsTearTeleportPayloadC2S> ID = CustomPayload.id("battlegrounds:play/ruperts_tear_teleport");
public static final PacketCodec<RegistryByteBuf, RupertsTearTeleportPayloadC2S> PACKET_CODEC = CustomPayload.codecOf(RupertsTearTeleportPayloadC2S::write, RupertsTearTeleportPayloadC2S::new);

public RupertsTearTeleportPayloadC2S(RegistryByteBuf buf) {
this(ItemStack.PACKET_CODEC.decode(buf), buf.readVec3d());
this(Hand.valueOf(buf.readString()), buf.readVec3d());
}

public void write(RegistryByteBuf buf) {
ItemStack.PACKET_CODEC.encode(buf, this.itemStack);
buf.writeString(hand.name());
buf.writeVec3d(this.teleportPos);
}

Expand Down

0 comments on commit ddb2350

Please sign in to comment.