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

Commit ec71381

Browse files
committed
Merge branch 'feature/networking' of https://github.com/PatchworkMC/patchwork-api into feature/networking
2 parents 3c1c524 + d98cdd3 commit ec71381

14 files changed

Lines changed: 626 additions & 1 deletion

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
def ENV = System.getenv()
1313

1414
class Globals {
15-
static def baseVersion = "0.1.1"
15+
static def baseVersion = "0.1.2"
1616
static def mcVersion = "1.14.4"
1717
static def yarnVersion = "+build.15"
1818
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
archivesBaseName = "patchwork-extensions-shearing"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
}
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+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.mixin.extensions.shearing;
21+
22+
import java.util.List;
23+
24+
import net.minecraftforge.common.IShearable;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.injection.At;
27+
import org.spongepowered.asm.mixin.injection.Inject;
28+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
29+
30+
import net.minecraft.block.DispenserBlock;
31+
import net.minecraft.block.dispenser.FallibleItemDispenserBehavior;
32+
import net.minecraft.entity.Entity;
33+
import net.minecraft.entity.EntityType;
34+
import net.minecraft.item.ItemStack;
35+
import net.minecraft.util.math.BlockPointer;
36+
import net.minecraft.util.math.BlockPos;
37+
import net.minecraft.util.math.Box;
38+
import net.minecraft.world.World;
39+
40+
import com.patchworkmc.impl.extensions.shearing.Shearables;
41+
42+
/**
43+
* Patches the inner class containing the logic for dispensers when using shears, to allow them to shear any
44+
* {@link IShearable} entity.
45+
*
46+
* @author SuperCoder79
47+
*/
48+
@Mixin(targets = "net/minecraft/block/dispenser/DispenserBehavior$13")
49+
public class MixinDispenserBehavior extends FallibleItemDispenserBehavior {
50+
private static final String DISPENSE_SILENTLY = "net/minecraft/block/dispenser/DispenserBehavior$13.dispenseSilently(Lnet/minecraft/util/math/BlockPointer;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;";
51+
52+
/**
53+
* Shears non-sheep entities that implement {@link IShearable}. The vanilla code will handle sheep entities.
54+
*
55+
* @author SuperCoder79
56+
*/
57+
@Inject(method = DISPENSE_SILENTLY, at = @At("RETURN"))
58+
private void onDispenseSilently(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> callback) {
59+
World world = pointer.getWorld();
60+
61+
if (!world.isClient()) {
62+
BlockPos pos = pointer.getBlockPos().offset(pointer.getBlockState().get(DispenserBlock.FACING));
63+
List<Entity> entities = world.getEntities(Entity.class, new Box(pos),
64+
entity -> !entity.isSpectator() && entity instanceof IShearable && entity.getType() != EntityType.SHEEP
65+
);
66+
67+
for (Entity entity : entities) {
68+
IShearable target = (IShearable) entity;
69+
70+
if (!target.isShearable(stack, world, pos)) {
71+
continue;
72+
}
73+
74+
Shearables.shearEntity(stack, world, pos, target);
75+
76+
this.success = true;
77+
}
78+
}
79+
}
80+
81+
@Inject(method = DISPENSE_SILENTLY, at = @At(value = "INVOKE", target = "net/minecraft/entity/passive/SheepEntity.dropItems()V"))
82+
private void assertThatThisIsTheShearingDispenserAction(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> callback) {
83+
// Make sure that the anonymous class numbers didn't move around by checking for a SheepEntity reference.
84+
}
85+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.mixin.extensions.shearing;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
import net.minecraftforge.common.IShearable;
26+
import org.spongepowered.asm.mixin.Mixin;
27+
import org.spongepowered.asm.mixin.Shadow;
28+
29+
import net.minecraft.block.Block;
30+
import net.minecraft.entity.EntityType;
31+
import net.minecraft.entity.passive.CowEntity;
32+
import net.minecraft.entity.passive.MooshroomEntity;
33+
import net.minecraft.item.ItemStack;
34+
import net.minecraft.particle.ParticleTypes;
35+
import net.minecraft.sound.SoundEvents;
36+
import net.minecraft.util.math.BlockPos;
37+
import net.minecraft.world.IWorld;
38+
import net.minecraft.world.ViewableWorld;
39+
import net.minecraft.world.World;
40+
41+
/**
42+
* Patches {@link MooshroomEntity} to allow using {@link IShearable} for shearing the mooshroom when it is an adult, dropping mushrooms.
43+
*
44+
* @author SuperCoder79
45+
*/
46+
@Mixin(MooshroomEntity.class)
47+
public abstract class MixinMooshroomEntity extends CowEntity implements IShearable {
48+
@Shadow
49+
public abstract MooshroomEntity.Type getMooshroomType();
50+
51+
public MixinMooshroomEntity(EntityType<? extends CowEntity> entityType, World world) {
52+
super(entityType, world);
53+
}
54+
55+
@Override
56+
public boolean isShearable(ItemStack item, ViewableWorld world, BlockPos pos) {
57+
return this.getBreedingAge() >= 0;
58+
}
59+
60+
@Override
61+
public List<ItemStack> onSheared(ItemStack item, IWorld world, BlockPos pos, int fortune) {
62+
List<ItemStack> drops = new ArrayList<>();
63+
this.world.addParticle(ParticleTypes.EXPLOSION, this.x, this.y + (double) (this.getHeight() / 2.0F), this.z, 0.0D, 0.0D, 0.0D);
64+
65+
if (!this.world.isClient) {
66+
this.remove();
67+
68+
CowEntity cow = EntityType.COW.create(this.world);
69+
cow.setPositionAndAngles(this.x, this.y, this.z, this.yaw, this.pitch);
70+
cow.setHealth(this.getHealth());
71+
cow.field_6283 = this.field_6283;
72+
73+
if (this.hasCustomName()) {
74+
cow.setCustomName(this.getCustomName());
75+
}
76+
77+
this.world.spawnEntity(cow);
78+
Block mushroom = this.getMooshroomType().getMushroomState().getBlock();
79+
80+
// TODO: Fixes forge bug where shearing brown mooshrooms always drop red mushrooms (Fixed in 1.15)
81+
for (int i = 0; i < 5; ++i) {
82+
drops.add(new ItemStack(mushroom));
83+
}
84+
85+
this.playSound(SoundEvents.ENTITY_MOOSHROOM_SHEAR, 1.0F, 1.0F);
86+
}
87+
88+
return drops;
89+
}
90+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.mixin.extensions.shearing;
21+
22+
import net.minecraftforge.common.IShearable;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
25+
import net.minecraft.block.CobwebBlock;
26+
import net.minecraft.block.DeadBushBlock;
27+
import net.minecraft.block.FernBlock;
28+
import net.minecraft.block.LeavesBlock;
29+
import net.minecraft.block.ReplaceableTallPlantBlock;
30+
import net.minecraft.block.SeagrassBlock;
31+
import net.minecraft.block.VineBlock;
32+
33+
/**
34+
* Patches blocks to implement {@link IShearable}.
35+
*
36+
* @author SuperCoder79
37+
*/
38+
@Mixin({SeagrassBlock.class, VineBlock.class, LeavesBlock.class, DeadBushBlock.class, FernBlock.class, ReplaceableTallPlantBlock.class, CobwebBlock.class})
39+
public class MixinShearableBlock implements IShearable {
40+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.mixin.extensions.shearing;
21+
22+
import net.minecraftforge.common.IShearable;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
25+
import net.minecraft.entity.EntityType;
26+
import net.minecraft.entity.LivingEntity;
27+
import net.minecraft.entity.player.PlayerEntity;
28+
import net.minecraft.item.Item;
29+
import net.minecraft.item.ItemStack;
30+
import net.minecraft.item.Items;
31+
import net.minecraft.item.ShearsItem;
32+
import net.minecraft.util.Hand;
33+
import net.minecraft.util.math.BlockPos;
34+
35+
import com.patchworkmc.impl.extensions.shearing.Shearables;
36+
37+
/**
38+
* Patch {@link ShearsItem} to allow it to shear any {@link IShearable}.
39+
*
40+
* @author SuperCoder79
41+
*/
42+
@Mixin(ShearsItem.class)
43+
public class MixinShearsItem extends Item {
44+
public MixinShearsItem(Settings settings) {
45+
super(settings);
46+
}
47+
48+
@Override
49+
public boolean useOnEntity(ItemStack stack, PlayerEntity playerIn, LivingEntity entity, Hand hand) {
50+
if (entity.world.isClient) {
51+
return false;
52+
}
53+
54+
// Avoid duplicating vanilla interactions
55+
if (this == Items.SHEARS) {
56+
EntityType<?> type = entity.getType();
57+
58+
if (type == EntityType.MOOSHROOM || type == EntityType.SHEEP || type == EntityType.SNOW_GOLEM) {
59+
return false;
60+
}
61+
}
62+
63+
if (entity instanceof IShearable) {
64+
IShearable target = (IShearable) entity;
65+
BlockPos pos = entity.getBlockPos();
66+
67+
if (target.isShearable(stack, entity.world, pos)) {
68+
Shearables.shearEntity(stack, entity.world, pos, target);
69+
}
70+
71+
return true;
72+
}
73+
74+
return false;
75+
}
76+
}

0 commit comments

Comments
 (0)