Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit bc00803

Browse files
committed
Deduplicate code for shearing entities into Shearables#shearEntity
1 parent 1cb8326 commit bc00803

3 files changed

Lines changed: 80 additions & 34 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package com.patchworkmc.impl.extensions.shearing;
21+
22+
import java.util.List;
23+
import java.util.Random;
24+
25+
import net.minecraftforge.common.IShearable;
26+
27+
import net.minecraft.enchantment.EnchantmentHelper;
28+
import net.minecraft.enchantment.Enchantments;
29+
import net.minecraft.entity.Entity;
30+
import net.minecraft.entity.ItemEntity;
31+
import net.minecraft.item.ItemStack;
32+
import net.minecraft.util.math.BlockPos;
33+
import net.minecraft.world.IWorld;
34+
35+
public class Shearables {
36+
public static void shearEntity(ItemStack stack, IWorld world, BlockPos pos, IShearable target) {
37+
if (!(target instanceof Entity)) {
38+
throw new IllegalArgumentException("Tried to call shearEntity on something that was not an entity!");
39+
}
40+
41+
Entity entity = (Entity) target;
42+
43+
List<ItemStack> drops = target.onSheared(stack, world, pos, EnchantmentHelper.getLevel(Enchantments.FORTUNE, stack));
44+
Random rand = world.getRandom();
45+
46+
for (ItemStack drop : drops) {
47+
ItemEntity item = entity.dropStack(drop, 1.0F);
48+
49+
if (item == null) {
50+
continue;
51+
}
52+
53+
float accelerationX = (rand.nextFloat() - rand.nextFloat()) * 0.1F;
54+
float accelerationY = rand.nextFloat() * 0.05F;
55+
float accelerationZ = (rand.nextFloat() - rand.nextFloat()) * 0.1F;
56+
57+
item.setVelocity(item.getVelocity().add(accelerationX, accelerationY, accelerationZ));
58+
}
59+
60+
if (stack.damage(1, world.getRandom(), null)) {
61+
stack.setCount(0);
62+
}
63+
}
64+
}

patchwork-extensions-shearing/src/main/java/com/patchworkmc/mixin/extensions/shearing/MixinDispenserBehavior.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,22 @@
2020
package com.patchworkmc.mixin.extensions.shearing;
2121

2222
import java.util.List;
23-
import java.util.Random;
2423

2524
import net.minecraftforge.common.IShearable;
2625
import org.spongepowered.asm.mixin.Mixin;
2726
import org.spongepowered.asm.mixin.Overwrite;
28-
import org.spongepowered.asm.mixin.Unique;
27+
import org.spongepowered.asm.mixin.Shadow;
2928

3029
import net.minecraft.block.DispenserBlock;
31-
import net.minecraft.enchantment.EnchantmentHelper;
32-
import net.minecraft.enchantment.Enchantments;
3330
import net.minecraft.entity.Entity;
34-
import net.minecraft.entity.ItemEntity;
3531
import net.minecraft.item.ItemStack;
3632
import net.minecraft.util.math.BlockPointer;
3733
import net.minecraft.util.math.BlockPos;
3834
import net.minecraft.util.math.Box;
3935
import net.minecraft.world.World;
4036

37+
import com.patchworkmc.impl.extensions.shearing.Shearables;
38+
4139
/**
4240
* Patches the inner class containing the logic for dispensers when using shears, to allow them to shear any
4341
* {@link IShearable} entity.
@@ -46,7 +44,7 @@
4644
*/
4745
@Mixin(targets = "net/minecraft/block/dispenser/DispenserBehavior$13")
4846
public class MixinDispenserBehavior {
49-
@Unique
47+
@Shadow
5048
protected boolean success;
5149

5250
/**
@@ -61,24 +59,20 @@ public ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
6159
if (!world.isClient()) {
6260
this.success = false;
6361
BlockPos pos = pointer.getBlockPos().offset(pointer.getBlockState().get(DispenserBlock.FACING));
64-
List<Entity> entities = world.getEntities(Entity.class, new Box(pos));
62+
List<Entity> entities = world.getEntities(Entity.class, new Box(pos),
63+
entity -> !entity.isSpectator() && entity instanceof IShearable
64+
);
6565

6666
for (Entity entity : entities) {
67-
if (entity instanceof IShearable) {
68-
IShearable target = (IShearable) entity;
67+
IShearable target = (IShearable) entity;
6968

70-
if (target.isShearable(stack, world, pos)) {
71-
List<ItemStack> drops = target.onSheared(stack, world, pos, EnchantmentHelper.getLevel(Enchantments.FORTUNE, stack));
72-
Random rand = new Random();
69+
if (!target.isShearable(stack, world, pos)) {
70+
continue;
71+
}
7372

74-
for (ItemStack drop : drops) {
75-
ItemEntity ent = entity.dropStack(drop, 1.0F);
76-
ent.setVelocity(ent.getVelocity().add((rand.nextFloat() - rand.nextFloat()) * 0.1F, rand.nextFloat() * 0.05F, (rand.nextFloat() - rand.nextFloat()) * 0.1F));
77-
}
73+
Shearables.shearEntity(stack, world, pos, target);
7874

79-
this.success = true;
80-
}
81-
}
75+
this.success = true;
8276
}
8377
}
8478

patchwork-extensions-shearing/src/main/java/com/patchworkmc/mixin/extensions/shearing/MixinShearsItem.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@
1919

2020
package com.patchworkmc.mixin.extensions.shearing;
2121

22-
import java.util.List;
23-
import java.util.Random;
24-
2522
import net.minecraftforge.common.IShearable;
2623
import org.spongepowered.asm.mixin.Mixin;
2724

28-
import net.minecraft.enchantment.EnchantmentHelper;
29-
import net.minecraft.enchantment.Enchantments;
30-
import net.minecraft.entity.ItemEntity;
3125
import net.minecraft.entity.LivingEntity;
3226
import net.minecraft.entity.player.PlayerEntity;
3327
import net.minecraft.item.Item;
@@ -36,6 +30,8 @@
3630
import net.minecraft.util.Hand;
3731
import net.minecraft.util.math.BlockPos;
3832

33+
import com.patchworkmc.impl.extensions.shearing.Shearables;
34+
3935
/**
4036
* Patch {@link ShearsItem} to allow it to shear any {@link IShearable}.
4137
*
@@ -58,15 +54,7 @@ public boolean useOnEntity(ItemStack stack, PlayerEntity playerIn, LivingEntity
5854
BlockPos pos = entity.getBlockPos();
5955

6056
if (target.isShearable(stack, entity.world, pos)) {
61-
List<ItemStack> drops = target.onSheared(stack, entity.world, pos, EnchantmentHelper.getLevel(Enchantments.FORTUNE, stack));
62-
Random rand = new Random();
63-
64-
for (ItemStack drop : drops) {
65-
ItemEntity ent = entity.dropStack(drop, 1.0F);
66-
ent.setVelocity(ent.getVelocity().add((rand.nextFloat() - rand.nextFloat()) * 0.1F, rand.nextFloat() * 0.05F, (rand.nextFloat() - rand.nextFloat()) * 0.1F));
67-
}
68-
69-
stack.damage(1, entity, e -> e.sendToolBreakStatus(hand));
57+
Shearables.shearEntity(stack, entity.world, pos, target);
7058
}
7159

7260
return true;

0 commit comments

Comments
 (0)