-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 优化项目结构 + 新增浸液物品堆叠组件
- Loading branch information
Showing
11 changed files
with
150 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/github/winexp/battlegrounds/component/DataComponentTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
43 changes: 42 additions & 1 deletion
43
src/main/java/com/github/winexp/battlegrounds/component/SoakComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/github/winexp/battlegrounds/mixin/soak_ItemStackMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/winexp/battlegrounds/mixin/soak_PlayerEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} |
11 changes: 0 additions & 11 deletions
11
src/main/java/com/github/winexp/battlegrounds/network/codec/ModPacketCodecs.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters