Skip to content

Commit

Permalink
🍃小优化
Browse files Browse the repository at this point in the history
* 优化项目结构
+ 新增浸液物品堆叠组件
  • Loading branch information
WinExp committed May 20, 2024
1 parent 4eb67fa commit 96b182e
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.github.winexp.battlegrounds.block.Blocks;
import com.github.winexp.battlegrounds.block.SmeltableBlockRegistry;
import com.github.winexp.battlegrounds.block.entity.BlockEntityType;
import com.github.winexp.battlegrounds.command.BattlegroundsCommand;
import com.github.winexp.battlegrounds.command.RandomTpCommand;
import com.github.winexp.battlegrounds.command.argument.PVPModeArgumentType;
import com.github.winexp.battlegrounds.component.DataComponentTypes;
import com.github.winexp.battlegrounds.config.ConfigUtil;
import com.github.winexp.battlegrounds.config.ServerRootConfig;
import com.github.winexp.battlegrounds.enchantment.Enchantments;
Expand Down Expand Up @@ -144,6 +146,10 @@ public void onInitialize() {
resourceManagerHelper.registerReloadListener(new DataPackResourceReloadListener());
// 注册方块
Blocks.bootstrap();
// 注册方块实体
BlockEntityType.bootstrap();
// 注册物品堆叠组件
DataComponentTypes.bootstrap();
// 注册物品
Items.bootstrap();
// 注册物品组
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ private static <T extends BlockEntity> net.minecraft.block.entity.BlockEntityTyp
Type<?> type = Util.getChoiceType(TypeReferences.BLOCK_ENTITY, id);
return Registry.register(Registries.BLOCK_ENTITY_TYPE, new Identifier("battlegrounds", id), builder.build(type));
}

public static void bootstrap() {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
package com.github.winexp.battlegrounds.component;

import net.minecraft.component.DataComponentType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

import java.util.function.UnaryOperator;

public class DataComponentTypes extends net.minecraft.component.DataComponentTypes {
public static final DataComponentType<SoakComponent> SOAK_DATA = register("soak_data", builder ->
builder.codec(SoakComponent.CODEC).packetCodec(SoakComponent.PACKET_CODEC).cache());

private static <T> DataComponentType<T> register(String id, UnaryOperator<DataComponentType.Builder<T>> builderOperator) {
return Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier("battlegrounds", id), builderOperator.apply(DataComponentType.builder()).build());
}

public static void bootstrap() {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
package com.github.winexp.battlegrounds.component;

public record SoakComponent() {
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.client.item.TooltipType;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffectUtil;
import net.minecraft.item.Item;
import net.minecraft.item.TooltipAppender;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.function.Consumer;
import java.util.function.Function;

public record SoakComponent(ObjectArrayList<StatusEffectInstance> immerseEffects, ObjectArrayList<StatusEffectInstance> leachEffects, boolean showInTooltip) implements TooltipAppender {
public static final Codec<SoakComponent> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.list(StatusEffectInstance.CODEC).xmap(ObjectArrayList::new, Function.identity()).fieldOf("immerse_effects").forGetter(SoakComponent::immerseEffects),
Codec.list(StatusEffectInstance.CODEC).xmap(ObjectArrayList::new, Function.identity()).fieldOf("leach_effects").forGetter(SoakComponent::leachEffects),
Codec.BOOL.optionalFieldOf("show_in_tooltip", true).forGetter(SoakComponent::showInTooltip)
).apply(instance, SoakComponent::new));
private static final PacketCodec<RegistryByteBuf, ObjectArrayList<StatusEffectInstance>> LIST_PACKET_CODEC = PacketCodecs.<RegistryByteBuf, StatusEffectInstance>toList().apply(StatusEffectInstance.PACKET_CODEC).xmap(ObjectArrayList::new, Function.identity());
public static final PacketCodec<RegistryByteBuf, SoakComponent> PACKET_CODEC = PacketCodec.tuple(LIST_PACKET_CODEC, SoakComponent::immerseEffects, LIST_PACKET_CODEC, SoakComponent::leachEffects, PacketCodecs.BOOL, SoakComponent::showInTooltip, SoakComponent::new);

@Override
public void appendTooltip(Item.TooltipContext context, Consumer<Text> tooltip, TooltipType type) {
if (!this.showInTooltip) return;
if (!this.immerseEffects.isEmpty()) tooltip.accept(Text.translatable("soak.battlegrounds.immerse.tooltip").formatted(Formatting.WHITE));
this.immerseEffects.forEach(effect -> addEffectTooltip(tooltip, context, effect));
if (!this.leachEffects.isEmpty()) tooltip.accept(Text.translatable("soak.battlegrounds.leach.tooltip").formatted(Formatting.WHITE));
this.leachEffects.forEach(effect -> addEffectTooltip(tooltip, context, effect));
}

private static void addEffectTooltip(Consumer<Text> appender, Item.TooltipContext context, StatusEffectInstance instance) {
appender.accept(Text.literal(" "));
appender.accept(Text.translatable(instance.getTranslationKey()).formatted(Formatting.GRAY)
.append(Text.translatable("enchantment.level." + instance.getAmplifier() + 1))
.append(Text.literal(": "))
.append(StatusEffectUtil.getDurationText(instance, 1.0F, context.getUpdateTickRate())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.winexp.battlegrounds.client.gui.screen.vote.VoteScreen;
import com.github.winexp.battlegrounds.event.ServerVoteEvents;
import com.github.winexp.battlegrounds.network.codec.ModPacketCodecs;
import com.github.winexp.battlegrounds.util.task.ScheduledTask;
import com.github.winexp.battlegrounds.util.task.TaskScheduler;
import com.github.winexp.battlegrounds.util.PlayerUtil;
Expand All @@ -26,7 +25,8 @@

public class VoteInstance {
public static final PacketCodec<ByteBuf, VoteInstance> PACKET_CODEC = new PacketCodec<>() {
private static final PacketCodec<ByteBuf, List<UUID>> UUID_LIST_PACKET_CODEC = PacketCodecs.<ByteBuf, UUID>toList().apply(ModPacketCodecs.UUID);
private static final PacketCodec<ByteBuf, UUID> UUID_PACKET_CODEC = PacketCodec.ofStatic(PacketByteBuf::writeUuid, PacketByteBuf::readUuid);
private static final PacketCodec<ByteBuf, List<UUID>> UUID_LIST_PACKET_CODEC = PacketCodecs.<ByteBuf, UUID>toList().apply(UUID_PACKET_CODEC);

@Override
public VoteInstance decode(ByteBuf buf) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.winexp.battlegrounds.mixin;

import com.github.winexp.battlegrounds.component.DataComponentTypes;
import com.github.winexp.battlegrounds.component.SoakComponent;
import net.minecraft.component.ComponentMapImpl;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemStack.class)
public abstract class soak_ItemStackMixin {
@Shadow
@Final
ComponentMapImpl components;

@Inject(method = "inventoryTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;inventoryTick(Lnet/minecraft/item/ItemStack;Lnet/minecraft/world/World;Lnet/minecraft/entity/Entity;IZ)V"))
private void inventoryTick(World world, Entity entity, int slot, boolean selected, CallbackInfo ci) {
if (selected && entity instanceof LivingEntity livingEntity && this.components.contains(DataComponentTypes.SOAK_DATA)) {
SoakComponent component = this.components.get(DataComponentTypes.SOAK_DATA);
assert component != null;
component.immerseEffects().forEach(effect -> livingEntity.addStatusEffect(new StatusEffectInstance(effect)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.winexp.battlegrounds.mixin;

import com.github.winexp.battlegrounds.component.DataComponentTypes;
import com.github.winexp.battlegrounds.component.SoakComponent;
import net.minecraft.component.ComponentMap;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerEntity.class)
public abstract class soak_PlayerEntityMixin {
@Shadow
public abstract float getAttackCooldownProgress(float baseTime);

@Unique
private float lastAttackCooldown;

@Inject(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;resetLastAttackedTicks()V"))
private void getAttackCooldown(Entity target, CallbackInfo ci) {
this.lastAttackCooldown = this.getAttackCooldownProgress(0.5F);
}

@Inject(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;damage(Lnet/minecraft/entity/damage/DamageSource;F)Z", shift = At.Shift.AFTER))
private void attack(Entity target, CallbackInfo ci) {
PlayerEntity player = (PlayerEntity) (Object) this;
ItemStack stack = player.getMainHandStack();
ComponentMap components = stack.getComponents();
if (this.lastAttackCooldown > 0.9F && target instanceof LivingEntity livingEntity && components.contains(DataComponentTypes.SOAK_DATA)) {
SoakComponent component = components.get(DataComponentTypes.SOAK_DATA);
assert component != null;
component.leachEffects().forEach(effect -> livingEntity.addStatusEffect(new StatusEffectInstance(effect)));
}
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/resources/assets/battlegrounds/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
"key.battlegrounds.vote_screen": "Voting Screen",
"key.categories.battlegrounds": "Battlegrounds",

"soak.battlegrounds.immerse.tooltip": "Immerse Effects:",
"soak.battlegrounds.leach.tooltip": "Leach Effects",
"subtitles.battlegrounds.entity.flash_bang.explode": "Flash Bang: Explode",
"subtitles.battlegrounds.entity.prop.generic.trigger": "Thrown Entity: Trigger",
"subtitles.battlegrounds.entity.prop.generic.rebound": "Thrown Entity: Rebound",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/battlegrounds/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
"key.battlegrounds.vote_screen": "打开投票界面",
"key.categories.battlegrounds": "Battlegrounds",

"soak.battlegrounds.immerse.tooltip": "浸药效果:",
"soak.battlegrounds.leach.tooltip": "浸毒效果:",
"subtitles.battlegrounds.entity.flash_bang.explode": "闪光弹:爆炸",
"subtitles.battlegrounds.entity.prop.generic.trigger": "投掷物:触发",
"subtitles.battlegrounds.entity.prop.generic.rebound": "投掷物:反弹",
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/battlegrounds.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"events_PlayerEntityMixin",
"events_PlayerManagerMixin",
"shapeType_RaycastContextMixin",
"shield_LivingEntityMixin"
"shield_LivingEntityMixin",
"soak_ItemStackMixin",
"soak_PlayerEntityMixin"
],
"client": [
"client.flashSound_AbstractSoundInstanceMixin",
Expand Down

0 comments on commit 96b182e

Please sign in to comment.