Skip to content

Commit

Permalink
优化钢剪刀事件Mixin,哞菇仍添加失败
Browse files Browse the repository at this point in the history
  • Loading branch information
YipKei committed Jun 27, 2024
1 parent a71d189 commit 08e8a3e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 150 deletions.
15 changes: 0 additions & 15 deletions src/main/java/com/yipkei/vanilladdition/mixin/ExampleMixin.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/com/yipkei/vanilladdition/mixin/ItemMixin.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package com.yipkei.vanilladdition.mixin;

import com.yipkei.vanilladdition.util.ModTags;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.SuspiciousStewEffectsComponent;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.Shearable;
import net.minecraft.entity.passive.CowEntity;
import net.minecraft.entity.passive.MooshroomEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsage;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.tag.ItemTags;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Optional;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Deprecated
@Mixin(MooshroomEntity.class)
Expand All @@ -34,74 +26,87 @@ public MooshroomEntityMixin(EntityType<? extends CowEntity> entityType, World wo
super(entityType, world);
}

@Shadow private SuspiciousStewEffectsComponent stewEffects;
@Shadow
public abstract boolean isShearable();

@Shadow public abstract MooshroomEntity.Type getVariant();
@Shadow public abstract void sheared(SoundCategory shearedSoundCategory);

@Shadow protected abstract Optional<SuspiciousStewEffectsComponent> getStewEffectFrom(ItemStack flower) ;

/**
* @author YipKei
* @reason The original method cannot accept custom Shears.
*/
@Overwrite
public ActionResult interactMob(PlayerEntity player, Hand hand) {
@Inject(method = "interactMob", at = @At("HEAD"), cancellable = true)
private void checkSteelShears(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> ci){
ItemStack itemStack = player.getStackInHand(hand);
if (itemStack.isOf(Items.BOWL) && !this.isBaby()) {
boolean bl = false;
ItemStack itemStack2;
if (this.stewEffects != null) {
bl = true;
itemStack2 = new ItemStack(Items.SUSPICIOUS_STEW);
itemStack2.set(DataComponentTypes.SUSPICIOUS_STEW_EFFECTS, this.stewEffects);
this.stewEffects = null;
} else {
itemStack2 = new ItemStack(Items.MUSHROOM_STEW);
}

ItemStack itemStack3 = ItemUsage.exchangeStack(itemStack, player, itemStack2, false);
player.setStackInHand(hand, itemStack3);
SoundEvent soundEvent;
if (bl) {
soundEvent = SoundEvents.ENTITY_MOOSHROOM_SUSPICIOUS_MILK;
} else {
soundEvent = SoundEvents.ENTITY_MOOSHROOM_MILK;
}

this.playSound(soundEvent, 1.0F, 1.0F);
return ActionResult.success(this.getWorld().isClient);
} else if (itemStack.isIn(ModTags.Items.SHEARS) && this.isShearable()) {
if (itemStack.isIn(ModTags.Items.SHEARS) && this.isShearable()){
this.sheared(SoundCategory.PLAYERS);
this.emitGameEvent(GameEvent.SHEAR, player);
if (!this.getWorld().isClient) {
itemStack.damage(1, player, getSlotForHand(hand));
}

return ActionResult.success(this.getWorld().isClient);
} else if (this.getVariant() == MooshroomEntity.Type.BROWN && itemStack.isIn(ItemTags.SMALL_FLOWERS)) {
if (this.stewEffects != null) {
for(int i = 0; i < 2; ++i) {
this.getWorld().addParticle(ParticleTypes.SMOKE, this.getX() + this.random.nextDouble() / 2.0, this.getBodyY(0.5), this.getZ() + this.random.nextDouble() / 2.0, 0.0, this.random.nextDouble() / 5.0, 0.0);
}
} else {
Optional<SuspiciousStewEffectsComponent> optional = this.getStewEffectFrom(itemStack);
if (optional.isEmpty()) {
return ActionResult.PASS;
}

itemStack.decrementUnlessCreative(1, player);

for(int j = 0; j < 4; ++j) {
this.getWorld().addParticle(ParticleTypes.EFFECT, this.getX() + this.random.nextDouble() / 2.0, this.getBodyY(0.5), this.getZ() + this.random.nextDouble() / 2.0, 0.0, this.random.nextDouble() / 5.0, 0.0);
}

this.stewEffects = (SuspiciousStewEffectsComponent)optional.get();
this.playSound(SoundEvents.ENTITY_MOOSHROOM_EAT, 2.0F, 1.0F);
}

return ActionResult.success(this.getWorld().isClient);
} else {
return super.interactMob(player, hand);
ci.setReturnValue(ActionResult.success(this.getWorld().isClient));
}
}

// /**
// * @author YipKei
// * @reason The original method cannot accept custom Shears.
// */
// @Overwrite
// public ActionResult interactMob(PlayerEntity player, Hand hand) {
// ItemStack itemStack = player.getStackInHand(hand);
// if (itemStack.isOf(Items.BOWL) && !this.isBaby()) {
// boolean bl = false;
// ItemStack itemStack2;
// if (this.stewEffects != null) {
// bl = true;
// itemStack2 = new ItemStack(Items.SUSPICIOUS_STEW);
// itemStack2.set(DataComponentTypes.SUSPICIOUS_STEW_EFFECTS, this.stewEffects);
// this.stewEffects = null;
// } else {
// itemStack2 = new ItemStack(Items.MUSHROOM_STEW);
// }
//
// ItemStack itemStack3 = ItemUsage.exchangeStack(itemStack, player, itemStack2, false);
// player.setStackInHand(hand, itemStack3);
// SoundEvent soundEvent;
// if (bl) {
// soundEvent = SoundEvents.ENTITY_MOOSHROOM_SUSPICIOUS_MILK;
// } else {
// soundEvent = SoundEvents.ENTITY_MOOSHROOM_MILK;
// }
//
// this.playSound(soundEvent, 1.0F, 1.0F);
// return ActionResult.success(this.getWorld().isClient);
// } else if (itemStack.isIn(ModTags.Items.SHEARS) && this.isShearable()) {
// this.sheared(SoundCategory.PLAYERS);
// this.emitGameEvent(GameEvent.SHEAR, player);
// if (!this.getWorld().isClient) {
// itemStack.damage(1, player, getSlotForHand(hand));
// }
//
// return ActionResult.success(this.getWorld().isClient);
// } else if (this.getVariant() == MooshroomEntity.Type.BROWN && itemStack.isIn(ItemTags.SMALL_FLOWERS)) {
// if (this.stewEffects != null) {
// for(int i = 0; i < 2; ++i) {
// this.getWorld().addParticle(ParticleTypes.SMOKE, this.getX() + this.random.nextDouble() / 2.0, this.getBodyY(0.5), this.getZ() + this.random.nextDouble() / 2.0, 0.0, this.random.nextDouble() / 5.0, 0.0);
// }
// } else {
// Optional<SuspiciousStewEffectsComponent> optional = this.getStewEffectFrom(itemStack);
// if (optional.isEmpty()) {
// return ActionResult.PASS;
// }
//
// itemStack.decrementUnlessCreative(1, player);
//
// for(int j = 0; j < 4; ++j) {
// this.getWorld().addParticle(ParticleTypes.EFFECT, this.getX() + this.random.nextDouble() / 2.0, this.getBodyY(0.5), this.getZ() + this.random.nextDouble() / 2.0, 0.0, this.random.nextDouble() / 5.0, 0.0);
// }
//
// this.stewEffects = (SuspiciousStewEffectsComponent)optional.get();
// this.playSound(SoundEvents.ENTITY_MOOSHROOM_EAT, 2.0F, 1.0F);
// }
//
// return ActionResult.success(this.getWorld().isClient);
// } else {
// return super.interactMob(player, hand);
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,58 @@
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PumpkinBlock.class)
public abstract class PumpkinBlockMixin extends Block {
public PumpkinBlockMixin(Settings settings) {
super(settings);
}

/**
* @author YipKei
* @reason The original method cannot accept custom Shears.
*/
@Overwrite
public ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!stack.isIn(ModTags.Items.SHEARS)) {
return super.onUseWithItem(stack, state, world, pos, player, hand, hit);
@Inject(method = "onUseWithItem",at = @At("HEAD"), cancellable = true)
private void checkSteelShears(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ItemActionResult> ci){
if (stack.isIn(ModTags.Items.SHEARS)){
if (world.isClient){
ci.setReturnValue(ItemActionResult.success(world.isClient));
}
Direction direction = hit.getSide();
Direction direction2 = direction.getAxis() == Direction.Axis.Y ? player.getHorizontalFacing().getOpposite() : direction;
world.playSound(null, pos, SoundEvents.BLOCK_PUMPKIN_CARVE, SoundCategory.BLOCKS, 1.0f, 1.0f);
world.setBlockState(pos, Blocks.CARVED_PUMPKIN.getDefaultState().with(CarvedPumpkinBlock.FACING, direction2), Block.NOTIFY_ALL_AND_REDRAW);
ItemEntity itemEntity = new ItemEntity(world, (double)pos.getX() + 0.5 + (double)direction2.getOffsetX() * 0.65, (double)pos.getY() + 0.1, (double)pos.getZ() + 0.5 + (double)direction2.getOffsetZ() * 0.65, new ItemStack(Items.PUMPKIN_SEEDS, 4));
itemEntity.setVelocity(0.05 * (double)direction2.getOffsetX() + world.random.nextDouble() * 0.02, 0.05, 0.05 * (double)direction2.getOffsetZ() + world.random.nextDouble() * 0.02);
world.spawnEntity(itemEntity);
stack.damage(1, player, LivingEntity.getSlotForHand(hand));
world.emitGameEvent(player, GameEvent.SHEAR, pos);
player.incrementStat(Stats.USED.getOrCreateStat(Items.SHEARS));
ci.setReturnValue(ItemActionResult.success(world.isClient));
}
if (world.isClient) {
return ItemActionResult.success(world.isClient);
}
Direction direction = hit.getSide();
Direction direction2 = direction.getAxis() == Direction.Axis.Y ? player.getHorizontalFacing().getOpposite() : direction;
world.playSound(null, pos, SoundEvents.BLOCK_PUMPKIN_CARVE, SoundCategory.BLOCKS, 1.0f, 1.0f);
world.setBlockState(pos, Blocks.CARVED_PUMPKIN.getDefaultState().with(CarvedPumpkinBlock.FACING, direction2), Block.NOTIFY_ALL_AND_REDRAW);
ItemEntity itemEntity = new ItemEntity(world, (double)pos.getX() + 0.5 + (double)direction2.getOffsetX() * 0.65, (double)pos.getY() + 0.1, (double)pos.getZ() + 0.5 + (double)direction2.getOffsetZ() * 0.65, new ItemStack(Items.PUMPKIN_SEEDS, 4));
itemEntity.setVelocity(0.05 * (double)direction2.getOffsetX() + world.random.nextDouble() * 0.02, 0.05, 0.05 * (double)direction2.getOffsetZ() + world.random.nextDouble() * 0.02);
world.spawnEntity(itemEntity);
stack.damage(1, player, LivingEntity.getSlotForHand(hand));
world.emitGameEvent(player, GameEvent.SHEAR, pos);
player.incrementStat(Stats.USED.getOrCreateStat(Items.SHEARS));
return ItemActionResult.success(world.isClient);
}

// /**
// * @author YipKei
// * @reason The original method cannot accept custom Shears.
// */
// @Overwrite
// public ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
// if (!stack.isIn(ModTags.Items.SHEARS)) {
// return super.onUseWithItem(stack, state, world, pos, player, hand, hit);
// }
// if (world.isClient) {
// return ItemActionResult.success(world.isClient);
// }
// Direction direction = hit.getSide();
// Direction direction2 = direction.getAxis() == Direction.Axis.Y ? player.getHorizontalFacing().getOpposite() : direction;
// world.playSound(null, pos, SoundEvents.BLOCK_PUMPKIN_CARVE, SoundCategory.BLOCKS, 1.0f, 1.0f);
// world.setBlockState(pos, Blocks.CARVED_PUMPKIN.getDefaultState().with(CarvedPumpkinBlock.FACING, direction2), Block.NOTIFY_ALL_AND_REDRAW);
// ItemEntity itemEntity = new ItemEntity(world, (double)pos.getX() + 0.5 + (double)direction2.getOffsetX() * 0.65, (double)pos.getY() + 0.1, (double)pos.getZ() + 0.5 + (double)direction2.getOffsetZ() * 0.65, new ItemStack(Items.PUMPKIN_SEEDS, 4));
// itemEntity.setVelocity(0.05 * (double)direction2.getOffsetX() + world.random.nextDouble() * 0.02, 0.05, 0.05 * (double)direction2.getOffsetZ() + world.random.nextDouble() * 0.02);
// world.spawnEntity(itemEntity);
// stack.damage(1, player, LivingEntity.getSlotForHand(hand));
// world.emitGameEvent(player, GameEvent.SHEAR, pos);
// player.incrementStat(Stats.USED.getOrCreateStat(Items.SHEARS));
// return ItemActionResult.success(world.isClient);
// }
}
49 changes: 35 additions & 14 deletions src/main/java/com/yipkei/vanilladdition/mixin/SheepEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,56 @@
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
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.CallbackInfoReturnable;

@Mixin(SheepEntity.class)
public abstract class SheepEntityMixin extends AnimalEntity implements Shearable {


@Shadow public abstract boolean isShearable();

@Shadow public abstract void sheared(SoundCategory shearedSoundCategory);

protected SheepEntityMixin(EntityType<? extends AnimalEntity> entityType, World world) {
super(entityType, world);
}


/**
* @author YipKei
* @reason The original method cannot accept custom Shears.
*/
@Overwrite
public ActionResult interactMob(PlayerEntity player, Hand hand){

@Inject(method = "interactMob", at = @At( value = "HEAD" ), cancellable = true)
private void checkSteelShears(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> ci){
ItemStack itemStack = player.getStackInHand(hand);
if (itemStack.isIn(ModTags.Items.SHEARS)) {
if (!this.getWorld().isClient && this.isShearable()) {
if (itemStack.isIn(ModTags.Items.SHEARS)){
if (!this.getWorld().isClient && this.isShearable()){
this.sheared(SoundCategory.PLAYERS);
this.emitGameEvent(GameEvent.SHEAR, player);
itemStack.damage(1, player, getSlotForHand(hand));
return ActionResult.SUCCESS;
} else {
return ActionResult.CONSUME;
ci.setReturnValue(ActionResult.SUCCESS);
}
} else {
return super.interactMob(player, hand);
}
}

// /**
// * @author YipKei
// * @reason The original method cannot accept custom Shears.
// */
// @Overwrite
// public ActionResult interactMob(PlayerEntity player, Hand hand){
// ItemStack itemStack = player.getStackInHand(hand);
// if (itemStack.isIn(ModTags.Items.SHEARS)) {
// if (!this.getWorld().isClient && this.isShearable()) {
// this.sheared(SoundCategory.PLAYERS);
// this.emitGameEvent(GameEvent.SHEAR, player);
// itemStack.damage(1, player, getSlotForHand(hand));
// return ActionResult.SUCCESS;
// } else {
// return ActionResult.CONSUME;
// }
// } else {
// return super.interactMob(player, hand);
// }
// }
}
1 change: 0 additions & 1 deletion src/main/resources/vanilla-addition.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"compatibilityLevel": "JAVA_21",
"mixins": [
"EnderDragonFightMixin",
"ExampleMixin",
"PumpkinBlockMixin",
"SheepEntityMixin",
"SpawnHelperMixin"
Expand Down

0 comments on commit 08e8a3e

Please sign in to comment.