|
| 1 | +package com.adccadc.rust.mixin.entity.iromGolem; |
| 2 | + |
| 3 | +import com.adccadc.rust.Rust; |
| 4 | +import com.adccadc.rust.item.Moditems; |
| 5 | +import com.adccadc.rust.manager.RustManager; |
| 6 | +import com.adccadc.rust.proxy.IronGolemEntityProxy; |
| 7 | +import net.fabricmc.api.EnvType; |
| 8 | +import net.fabricmc.api.Environment; |
| 9 | +import net.fabricmc.fabric.api.event.player.UseEntityCallback; |
| 10 | +import net.minecraft.block.Blocks; |
| 11 | +import net.minecraft.entity.EntityType; |
| 12 | +import net.minecraft.entity.ItemEntity; |
| 13 | +import net.minecraft.entity.passive.GolemEntity; |
| 14 | +import net.minecraft.entity.passive.IronGolemEntity; |
| 15 | +import net.minecraft.entity.player.PlayerEntity; |
| 16 | +import net.minecraft.item.*; |
| 17 | +import net.minecraft.particle.ParticleTypes; |
| 18 | +import net.minecraft.registry.tag.ItemTags; |
| 19 | +import net.minecraft.registry.tag.TagKey; |
| 20 | +import net.minecraft.server.world.ServerWorld; |
| 21 | +import net.minecraft.sound.SoundEvents; |
| 22 | +import net.minecraft.storage.ReadView; |
| 23 | +import net.minecraft.storage.WriteView; |
| 24 | +import net.minecraft.text.Text; |
| 25 | +import net.minecraft.util.ActionResult; |
| 26 | +import net.minecraft.util.Hand; |
| 27 | +import net.minecraft.world.World; |
| 28 | +import org.spongepowered.asm.mixin.Mixin; |
| 29 | +import org.spongepowered.asm.mixin.Unique; |
| 30 | +import org.spongepowered.asm.mixin.injection.At; |
| 31 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 32 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 33 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 34 | + |
| 35 | +@Mixin(IronGolemEntity.class) |
| 36 | +public abstract class IronGolemEntityMixin extends GolemEntity implements IronGolemEntityProxy |
| 37 | +{ |
| 38 | + // 锈蚀控制器 |
| 39 | + @Unique |
| 40 | + public RustManager rust = new RustManager(new ItemStack(Moditems.IRON_RUST, 2), 3); |
| 41 | + |
| 42 | + public IronGolemEntityMixin(EntityType<? extends IronGolemEntity> entityType, World world) { |
| 43 | + super(entityType, world); |
| 44 | + } |
| 45 | + |
| 46 | + @Unique |
| 47 | + public IronGolemEntityProxy proxy(){ |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + @Unique |
| 52 | + public RustManager getRust(){ |
| 53 | + return this.rust; |
| 54 | + } |
| 55 | + |
| 56 | + @Inject(method = "writeCustomData", |
| 57 | + /*at = @At(value = "INVOKE", |
| 58 | + target = "Lnet/minecraft/iromGolem/mob/Angerable;writeAngerToData(Lnet/minecraft/world/World;Lnet/minecraft/storage/WriteView;)V", |
| 59 | + shift = At.Shift.BEFORE |
| 60 | + )*/ |
| 61 | + at = @At("HEAD") |
| 62 | + ) |
| 63 | + private void writeCustomDataWithRust(WriteView view, CallbackInfo info){ |
| 64 | + view.putInt("RustLevel", this.rust.getState().level); |
| 65 | + view.putBoolean("IsWaxed", this.rust.getState().isWaxed); |
| 66 | + } |
| 67 | + |
| 68 | + @Inject(method = "readCustomData", |
| 69 | + /*at = @At(value = "INVOKE", |
| 70 | + target = "Lnet/minecraft/iromGolem/mob/Angerable;readAngerFromData(Lnet/minecraft/world/World;Lnet/minecraft/storage/WriteView;)V", |
| 71 | + shift = At.Shift.BEFORE |
| 72 | + )*/ |
| 73 | + at = @At("HEAD") |
| 74 | + ) |
| 75 | + private void readCustomDataWithRust(ReadView view, CallbackInfo info){ |
| 76 | + this.rust.setLevel(view.getInt("RustLevel", 0)); |
| 77 | + this.rust.setWaxed(view.getBoolean("IsWaxed", false)); |
| 78 | + } |
| 79 | + |
| 80 | + @Inject( |
| 81 | + method = "interactMob", |
| 82 | + at = @At("HEAD"), |
| 83 | + cancellable = true |
| 84 | + ) |
| 85 | + protected void interactMobWithRustActions(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir){ |
| 86 | + ItemStack itemStack = player.getStackInHand(hand); |
| 87 | + // 是否为水桶 |
| 88 | + if (itemStack.isOf(Items.WATER_BUCKET)){ |
| 89 | + // 处理物品 |
| 90 | + if(!player.isCreative()){ |
| 91 | + // 将水桶换成空桶 |
| 92 | + ItemStack newItemStack = ItemUsage.exchangeStack(itemStack, player, Items.BUCKET.getDefaultStack()); |
| 93 | + player.setStackInHand(hand, newItemStack); |
| 94 | + } |
| 95 | + // 尝试执行一次锈蚀 |
| 96 | + if(!(this.rust.tryRusted())){ |
| 97 | + // 生锈失败 |
| 98 | + cir.setReturnValue(ActionResult.FAIL); |
| 99 | + }else{ |
| 100 | + cir.setReturnValue(ActionResult.SUCCESS); |
| 101 | + } |
| 102 | + // 物品是否含有斧头标签 |
| 103 | + } else if (itemStack.isIn(ItemTags.AXES)){ |
| 104 | + // 执行一次除锈 |
| 105 | + ItemStack dropStack; |
| 106 | + if (((dropStack = this.rust.tryDerusted()) == null)){ |
| 107 | + // 除锈失败 |
| 108 | + cir.setReturnValue(ActionResult.FAIL); |
| 109 | + } else { |
| 110 | + World world = this.getWorld(); |
| 111 | + if (world instanceof ServerWorld serverWorld) { |
| 112 | + // 生成掉落物 |
| 113 | + ItemEntity drop = this.dropStack(serverWorld, dropStack, 1.5F); |
| 114 | + // 播放音效 |
| 115 | + this.playSound(SoundEvents.ITEM_AXE_WAX_OFF, 1.0F, 1.0F); |
| 116 | + // 播放粒子 |
| 117 | + serverWorld.spawnParticles( |
| 118 | + ParticleTypes.WAX_OFF, |
| 119 | + this.getX(), |
| 120 | + this.getY() + 1.0F, |
| 121 | + this.getZ(), |
| 122 | + 35, |
| 123 | + 0.4F, |
| 124 | + 0.8F, |
| 125 | + 0.4F, |
| 126 | + 0.1F |
| 127 | + ); |
| 128 | + // 处理物品 |
| 129 | + itemStack.damage(1, player); |
| 130 | + cir.setReturnValue(ActionResult.SUCCESS); |
| 131 | + } |
| 132 | + } |
| 133 | + // 物品是否为蜜脾 |
| 134 | + } else if (itemStack.isOf(Items.HONEYCOMB) && itemStack.getCount() >= 2) { |
| 135 | + // 尝试上蜡 |
| 136 | + if (!this.rust.tryWaxed()){ |
| 137 | + // 上蜡失败 |
| 138 | + cir.setReturnValue(ActionResult.FAIL); |
| 139 | + } else { |
| 140 | + World world = this.getWorld(); |
| 141 | + if(world instanceof ServerWorld serverWorld){ |
| 142 | + // 播放音效 |
| 143 | + this.playSound(SoundEvents.ITEM_HONEYCOMB_WAX_ON, 1.0F, 1.0F); |
| 144 | + // 播放粒子 |
| 145 | + serverWorld.spawnParticles( |
| 146 | + ParticleTypes.WAX_ON, |
| 147 | + this.getX(), |
| 148 | + this.getY() + 1.0F, |
| 149 | + this.getZ(), |
| 150 | + 35, |
| 151 | + 0.4F, |
| 152 | + 0.8F, |
| 153 | + 0.4F, |
| 154 | + 0.1F |
| 155 | + ); |
| 156 | + // 处理物品 |
| 157 | + itemStack.decrementUnlessCreative(2, player); |
| 158 | + cir.setReturnValue(ActionResult.SUCCESS); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + // 在名称前添加锈蚀状态 |
| 166 | + protected Text getDefaultName(){ |
| 167 | + return Text.of(this.rust.getName() + this.getType().getName().getString()); |
| 168 | + } |
| 169 | +} |
0 commit comments