Skip to content

Commit

Permalink
修复生长工具对史莱姆缩小时没有粒子效果的问题
Browse files Browse the repository at this point in the history
修复按钮在对焦时不能用键盘上的Enter、Space进行操作的问题
修复没有使用 TextBridge 的小问题
  • Loading branch information
SolidBlock-cn committed Jul 3, 2023
1 parent 004e4bc commit 95d2206
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 51 deletions.
47 changes: 31 additions & 16 deletions src/main/java/pers/solid/mishang/uc/item/CarryingToolItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public static boolean hasHoldingEntity(@NotNull ItemStack stack) {
@Contract(pure = true)
public static @Nullable Block getHoldingBlock(@NotNull ItemStack stack) {
final NbtCompound holdingBlockStateNbt = stack.getSubNbt("holdingBlockState");
if (holdingBlockStateNbt == null) return null;
if (holdingBlockStateNbt == null)
return null;
final Identifier identifier = Identifier.tryParse(holdingBlockStateNbt.getString("Name"));
return Registries.BLOCK.get(identifier);
}
Expand All @@ -95,7 +96,8 @@ public static boolean hasHoldingEntity(@NotNull ItemStack stack) {
if (holdingBlockStateNbt != null) {
try {
return NbtHelper.toBlockState(world.createCommandRegistryWrapper(RegistryKeys.BLOCK), holdingBlockStateNbt);
} catch (Exception e) {
} catch (
Exception e) {
return null;
}
} else {
Expand Down Expand Up @@ -162,14 +164,15 @@ public static void setHoldingBlockState(@NotNull ItemStack stack, @Nullable Bloc

private static MutableText getEntityName(@NotNull ItemStack stack) {
final NbtCompound nbt = stack.getNbt();
if (nbt == null) return TextBridge.empty();
if (nbt == null)
return TextBridge.empty();
if (nbt.contains("holdingEntityName", NbtElement.STRING_TYPE)) {
return Text.Serializer.fromJson(nbt.getString("holdingEntityName"));
} else if (nbt.contains("holdingEntityType", NbtElement.STRING_TYPE)) {
final Identifier holdingEntityType = Identifier.tryParse(nbt.getString("holdingEntityType"));
return Registries.ENTITY_TYPE.containsId(holdingEntityType) ? Registries.ENTITY_TYPE.get(holdingEntityType).getName().copy() : TextBridge.literal(String.valueOf(holdingEntityType));
} else {
return Text.empty();
return TextBridge.empty();
}
}

Expand Down Expand Up @@ -226,7 +229,8 @@ public ActionResult useOnBlock(ItemStack stack, PlayerEntity player, World world
} else if (hasHoldingEntity(stack)) {
if (world instanceof ServerWorld serverWorld) {
final Entity entity = createHoldingEntity(stack, serverWorld, player);
if (entity == null) return ActionResult.PASS;
if (entity == null)
return ActionResult.PASS;
final Vec3d pos = blockHitResult.getPos();
entity.updatePosition(pos.x, pos.y, pos.z);
final boolean spawnEntity = world.spawnEntity(entity);
Expand Down Expand Up @@ -272,7 +276,8 @@ private boolean hasAccess(PlayerEntity player, World world, boolean warn) {

@Override
public ActionResult beginAttackBlock(ItemStack stack, PlayerEntity player, World world, Hand hand, BlockPos pos, Direction direction, boolean fluidIncluded) {
if (!hasAccess(player, world, true)) return ActionResult.PASS;
if (!hasAccess(player, world, true))
return ActionResult.PASS;
final Block alreadyHolding = getHoldingBlock(stack);
if (alreadyHolding != null && !player.isCreative()) {
if (!world.isClient) {
Expand All @@ -284,7 +289,8 @@ public ActionResult beginAttackBlock(ItemStack stack, PlayerEntity player, World
}
final boolean alreadyHoldingEntity = hasHoldingEntity(stack);
if (alreadyHoldingEntity && !player.isCreative()) {
if (world.isClient) return ActionResult.CONSUME;
if (world.isClient)
return ActionResult.CONSUME;
else {
player.sendMessage(TextBridge.translatable("item.mishanguc.carrying_tool.message.no_picking", getEntityName(stack)).formatted(Formatting.RED), true);
return ActionResult.FAIL;
Expand Down Expand Up @@ -331,7 +337,8 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
if (holdingBlockState.getBlock() instanceof OperatorBlock && !user.hasPermissionLevel(2)) {
return TypedActionResult.fail(stack);
}
if (world.isClient) return TypedActionResult.success(use.getValue());
if (world.isClient)
return TypedActionResult.success(use.getValue());
final FallingBlockEntity fallingBlockEntity = new FallingBlockEntity(EntityType.FALLING_BLOCK, world);
NbtCompound nbt = new NbtCompound();
nbt.put("BlockState", stack.getSubNbt("holdingBlockState"));
Expand All @@ -356,7 +363,8 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
} else if (hasHoldingEntity(stack)) {
if (world instanceof ServerWorld serverWorld) {
final Entity entity = createHoldingEntity(stack, serverWorld, user);
if (entity == null) return use;
if (entity == null)
return use;
final Vec3d pos = user.getPos();
entity.updatePositionAndAngles(pos.x, pos.y, pos.z, user.getYaw(), user.getPitch());
entity.setVelocity(Vec3d.fromPolar(user.getPitch(), user.getYaw()).multiply(2).add(user.getVelocity()));
Expand All @@ -382,7 +390,8 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han

@Override
public @NotNull ActionResult attackEntityCallback(PlayerEntity player, World world, Hand hand, Entity entity, @Nullable EntityHitResult hitResult) {
if (!hasAccess(player, world, true) || player.isSpectator()) return ActionResult.PASS;
if (!hasAccess(player, world, true) || player.isSpectator())
return ActionResult.PASS;
final ItemStack stack = player.getStackInHand(hand);
if (entity instanceof PlayerEntity) {
if (world.isClient) {
Expand All @@ -392,13 +401,15 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
return ActionResult.FAIL;
}
} else if (hasHoldingEntity(stack) && !player.isCreative()) {
if (world.isClient) return ActionResult.SUCCESS;
if (world.isClient)
return ActionResult.SUCCESS;
else {
player.sendMessage(TextBridge.translatable("item.mishanguc.carrying_tool.message.no_picking", getEntityName(stack)).formatted(Formatting.RED), true);
return ActionResult.FAIL;
}
} else if (hasHoldingBlockState(stack) && !player.isCreative()) {
if (world.isClient) return ActionResult.SUCCESS;
if (world.isClient)
return ActionResult.SUCCESS;
else {
player.sendMessage(TextBridge.translatable("item.mishanguc.carrying_tool.message.no_picking", Optional.ofNullable(getHoldingBlock(stack)).map(Block::getName).orElse(TextBridge.empty())).formatted(Formatting.RED), true);
return ActionResult.FAIL;
Expand Down Expand Up @@ -441,7 +452,8 @@ public void writeItemModel(RuntimeResourcePack pack) {
@Environment(EnvType.CLIENT)
@Override
public boolean renderBlockOutline(PlayerEntity player, ItemStack itemStack, WorldRenderContext worldRenderContext, WorldRenderContext.BlockOutlineContext blockOutlineContext, Hand hand) {
if (!hasAccess(player, worldRenderContext.world(), true)) return true;
if (!hasAccess(player, worldRenderContext.world(), true))
return true;
final MinecraftClient client = MinecraftClient.getInstance();
final VertexConsumerProvider consumers = worldRenderContext.consumers();
if (consumers == null) {
Expand Down Expand Up @@ -487,12 +499,14 @@ public boolean renderBlockOutline(PlayerEntity player, ItemStack itemStack, Worl
@Override
public void renderBeforeOutline(WorldRenderContext context, HitResult hitResult, ClientPlayerEntity player, Hand hand) {
// 只在使用主手且有权限时持有此物品时进行渲染。
if (hand != Hand.MAIN_HAND || player.isSpectator() || !hasAccess(player, context.world(), true)) return;
if (hand != Hand.MAIN_HAND || player.isSpectator() || !hasAccess(player, context.world(), true))
return;
final ItemStack stack = player.getMainHandStack();
final NbtCompound nbt = stack.getNbt();
final VertexConsumerProvider consumers = context.consumers();
final MatrixStack matrices = context.matrixStack();
if (consumers == null) return;
if (consumers == null)
return;
final VertexConsumer vertexConsumer = consumers.getBuffer(RenderLayer.getLines());
final Vec3d cameraPos = context.camera().getPos();
if (hitResult.getType() == HitResult.Type.BLOCK && hasHoldingEntity(stack) && nbt != null) {
Expand All @@ -501,7 +515,8 @@ public void renderBeforeOutline(WorldRenderContext context, HitResult hitResult,
final Vec3d pos = hitResult.getPos();
WorldRendererInvoker.drawCuboidShapeOutline(matrices, vertexConsumer, VoxelShapes.cuboid(pos.x - width / 2, pos.y, pos.z - width / 2, pos.x + width / 2, pos.y + height, pos.z + width / 2), -cameraPos.x, -cameraPos.y, -cameraPos.z, 0, 1, 1, 0.8f);
}
if (!player.isCreative() && (hasHoldingBlockState(stack) || hasHoldingEntity(stack))) return;
if (!player.isCreative() && (hasHoldingBlockState(stack) || hasHoldingEntity(stack)))
return;
if (hitResult instanceof EntityHitResult entityHitResult) {
final Entity entity = entityHitResult.getEntity();
WorldRendererInvoker.drawCuboidShapeOutline(matrices, vertexConsumer, VoxelShapes.cuboid(entity.getBoundingBox()), -cameraPos.x, -cameraPos.y, -cameraPos.z, 1.0f, 0f, 0f, 0.8f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public static int apply(World world, Vec3d center, boolean isPositive) {
final int prevSize = slimeEntity.getSize();
if (isPositive) {
slimeEntity.setSize(Math.min(prevSize * 2, Math.max(prevSize, 16)), false);
createParticle(world, entity.getPos(), isPositive);
} else {
slimeEntity.setSize(prevSize / 2, false);
}
createParticle(world, entity.getPos(), isPositive);
damage += 1;
} else if (entity instanceof MobEntity mobEntity) {
if (mobEntity.isBaby() == isPositive) {
Expand Down
Loading

0 comments on commit 95d2206

Please sign in to comment.