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

Commit 146f43b

Browse files
Feature/block harvest (#174)
* Impl removedByPlayer and canHarvestBlock in IForgeBlock * Apply kitlith's suggestions * Fix ThreadLocal memory leakage * Apply suggestions * Remove unneeded dependency * Disable an unnecessary 1.15 patch * Move BlockEvent from events-world to extensions-block. This removes a unnecessary dependency. * Replace a redirect with injection * Apply suggestions * Merge remote-tracking branch 'origin/master' into feature/block-harvest Conflicts: patchwork-events-world/src/main/java/net/patchworkmc/impl/event/world/WorldEvents.java Co-authored-by: TheGlitch76 <glitchieproductionsofficial@gmail.com>
1 parent 72a45a3 commit 146f43b

24 files changed

Lines changed: 622 additions & 156 deletions

File tree

patchwork-events-world/src/main/java/net/patchworkmc/impl/event/world/WorldEvents.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@
2525
import javax.annotation.Nullable;
2626

2727
import net.minecraftforge.common.MinecraftForge;
28-
import net.minecraftforge.event.world.BlockEvent;
2928
import net.minecraftforge.event.world.ChunkEvent;
3029
import net.minecraftforge.event.world.ChunkWatchEvent;
3130
import net.minecraftforge.event.world.WorldEvent;
3231

33-
import net.minecraft.block.BlockState;
34-
import net.minecraft.entity.player.PlayerEntity;
35-
import net.minecraft.item.ItemStack;
36-
import net.minecraft.util.DefaultedList;
37-
import net.minecraft.world.World;
3832
import net.minecraft.entity.EntityCategory;
3933
import net.minecraft.server.network.ServerPlayerEntity;
4034
import net.minecraft.server.world.ServerWorld;
@@ -78,13 +72,6 @@ public static void onWorldSave(IWorld world) {
7872
MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(world));
7973
}
8074

81-
// TODO: Leaving this unfired is intentional. See: https://github.com/MinecraftForge/MinecraftForge/issues/5828
82-
public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player) {
83-
BlockEvent.HarvestDropsEvent event = new BlockEvent.HarvestDropsEvent(world, pos, state, fortune, dropChance, drops, player, silkTouch);
84-
MinecraftForge.EVENT_BUS.post(event);
85-
return event.getDropChance();
86-
}
87-
8875
public static void fireChunkWatch(boolean watch, ServerPlayerEntity entity, ChunkPos chunkpos, ServerWorld world) {
8976
if (watch) {
9077
MinecraftForge.EVENT_BUS.post(new ChunkWatchEvent.Watch(entity, chunkpos, world));

patchwork-events-world/src/main/java/net/patchworkmc/mixin/event/world/MixinServerPlayerInteractionManager.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

patchwork-events-world/src/main/resources/patchwork-events-world.mixins.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
"mixins": [
66
"MixinChunkGenerator",
77
"MixinMinecraftServer",
8-
"MixinServerPlayerInteractionManager",
98
"MixinServerWorld",
109
"MixinSpawnHelper",
11-
"MixinThreadedAnvilChunkStorage",
10+
"MixinThreadedAnvilChunkStorage"
1211
],
1312
"client": [
1413
"MixinClientWorld",

patchwork-extensions-block/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = getSubprojectVersion(project, "0.3.0")
44
dependencies {
55
implementation project(path: ':patchwork-api-base', configuration: 'dev')
66
implementation project(path: ':patchwork-enum-hacks', configuration: 'dev')
7+
implementation project(path: ':patchwork-extensions-item', configuration: 'dev')
78
implementation project(path: ':patchwork-tooltype', configuration: 'dev')
89
}
910

patchwork-extensions-block/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.annotation.Nullable;
2828

2929
import net.minecraftforge.common.IPlantable;
30+
import net.minecraftforge.common.ToolType;
3031

3132
import net.minecraft.block.BedBlock;
3233
import net.minecraft.block.Block;
@@ -82,10 +83,12 @@
8283
import net.fabricmc.api.EnvType;
8384
import net.fabricmc.api.Environment;
8485

86+
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;
87+
import net.patchworkmc.impl.extensions.block.PatchworkBlock;
8588
import net.patchworkmc.mixin.extensions.block.FireBlockAccessor;
8689
import net.patchworkmc.mixin.extensions.block.PlantBlockAccessor;
8790

88-
public interface IForgeBlock {
91+
public interface IForgeBlock extends PatchworkBlock {
8992
default Block getBlock() {
9093
return (Block) this;
9194
}
@@ -204,18 +207,18 @@ default BlockEntity createTileEntity(BlockState state, BlockView world) {
204207
return null;
205208
}
206209

207-
/* TODO IForgeBlock#canHarvestBlock indirectly requires ToolType (via ForgeHooks#canHarvestBlock)
210+
/* TODO IForgeBlock#canHarvestBlock indirectly requires ToolType (via ForgeHooks#canHarvestBlock) */
208211
/**
209212
* Determines if the player can harvest this block, obtaining it's drops when the block is destroyed.
210213
*
211214
* @param world The current world
212215
* @param pos The block's current position
213216
* @param player The player damaging the block
214217
* @return True to spawn the drops
215-
*
218+
*/
216219
default boolean canHarvestBlock(BlockState state, BlockView world, BlockPos pos, PlayerEntity player) {
217-
return ForgeHooks.canHarvestBlock(state, player, world, pos);
218-
}*/
220+
return BlockHarvestManager.canHarvestBlock(state, player, world, pos);
221+
}
219222

220223
// TODO Call locations: Patches: ServerPlayerInteractionManager*
221224
/**
@@ -240,7 +243,7 @@ default boolean canHarvestBlock(BlockState state, BlockView world, BlockPos pos,
240243
*/
241244
default boolean removedByPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, FluidState fluid) {
242245
getBlock().onBreak(world, pos, state, player);
243-
return world.removeBlock(pos, false);
246+
return world.setBlockState(pos, fluid.getBlockState(), world.isClient ? 11 : 3);
244247
}
245248

246249
// TODO Call locations: Patches: LivingEntity*, PlayerEntity*, Forge classes: ForgeEventFactory (called from LivingEntity patch)
@@ -645,6 +648,7 @@ default boolean isBeaconBase(BlockState state, CollisionView world, BlockPos pos
645648
// TODO Call locations: Forge classes: BreakEvent*
646649
/**
647650
* Gathers how much experience this block drops when broken.
651+
* TODO: there's no equivalent callback in Fabric API, so for now Fabric mods should always return 0 here.
648652
*
649653
* @param state The current state
650654
* @param world The world
@@ -778,12 +782,12 @@ default boolean getWeakChanges(BlockState state, CollisionView world, BlockPos p
778782
return false;
779783
}
780784

781-
/* TODO IForgeBlock#getHarvestTool needs ToolType
785+
/* TODO IForgeBlock#getHarvestTool needs ToolType */
782786
/**
783787
* Queries the class of tool required to harvest this block, if null is returned
784788
* we assume that anything can harvest this block.
785-
*
786-
ToolType getHarvestTool(BlockState state);*/
789+
*/
790+
ToolType getHarvestTool(BlockState state);
787791

788792
// TODO Call locations: Patches: PickaxeItem*, Forge classes: ForgeHooks*
789793
/**
@@ -793,18 +797,18 @@ default boolean getWeakChanges(BlockState state, CollisionView world, BlockPos p
793797
*/
794798
int getHarvestLevel(BlockState state);
795799

796-
/* TODO IForgeBlock#isToolEffective needs ToolType
800+
/* TODO IForgeBlock#isToolEffective needs ToolType */
797801
/**
798802
* Checks if the specified tool type is efficient on this block,
799803
* meaning that it digs at full speed.
800-
*
804+
*/
801805
default boolean isToolEffective(BlockState state, ToolType tool) {
802806
if (tool == ToolType.PICKAXE && (this.getBlock() == Blocks.REDSTONE_ORE || this.getBlock() == Blocks.REDSTONE_LAMP || this.getBlock() == Blocks.OBSIDIAN)) {
803807
return false;
804808
}
805809

806810
return tool == getHarvestTool(state);
807-
}*/
811+
}
808812

809813
// TODO Call locations: Forge classes: ForgeHooksClient
810814
/**

patchwork-extensions-block/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.annotation.Nullable;
2626

2727
import net.minecraftforge.common.IPlantable;
28+
import net.minecraftforge.common.ToolType;
2829

2930
import net.minecraft.block.Block;
3031
import net.minecraft.block.BlockEntityProvider;
@@ -149,18 +150,18 @@ default BlockEntity createTileEntity(BlockView world) {
149150
return patchwork$getForgeBlock().createTileEntity(getBlockState(), world);
150151
}
151152

152-
/* TODO IForgeBlockState#canHarvestBlock indirectly requires ToolType
153+
/* TODO IForgeBlockState#canHarvestBlock indirectly requires ToolType */
153154
/**
154155
* Determines if the player can harvest this block, obtaining it's drops when the block is destroyed.
155156
*
156157
* @param world The current world
157158
* @param pos The block's current position
158159
* @param player The player damaging the block
159160
* @return True to spawn the drops
160-
*
161+
*/
161162
default boolean canHarvestBlock(BlockView world, BlockPos pos, PlayerEntity player) {
162163
return patchwork$getForgeBlock().canHarvestBlock(getBlockState(), world, pos, player);
163-
}*/
164+
}
164165

165166
/**
166167
* Called when a player removes a block. This is responsible for
@@ -582,14 +583,14 @@ default boolean getWeakChanges(CollisionView world, BlockPos pos) {
582583
return patchwork$getForgeBlock().getWeakChanges(getBlockState(), world, pos);
583584
}
584585

585-
/* TODO IForgeBlockState#getHarvestTool needs ToolType
586+
/* TODO IForgeBlockState#getHarvestTool needs ToolType */
586587
/**
587588
* Queries the class of tool required to harvest this block, if null is returned
588589
* we assume that anything can harvest this block.
589-
*
590+
*/
590591
default ToolType getHarvestTool() {
591592
return patchwork$getForgeBlock().getHarvestTool(getBlockState());
592-
}*/
593+
}
593594

594595
default int getHarvestLevel() {
595596
return patchwork$getForgeBlock().getHarvestLevel(getBlockState());
@@ -605,13 +606,15 @@ default boolean isToolEffective(ToolType tool) {
605606
}*/
606607

607608
/**
609+
* TODO: do not bother implementing hooks, deprecated since 1.13
608610
* Can return IExtendedBlockState.
609611
*/
610612
default BlockState getExtendedState(BlockView world, BlockPos pos) {
611613
return patchwork$getForgeBlock().getExtendedState(getBlockState(), world, pos);
612614
}
613615

614616
/**
617+
* TODO: do not bother implementing hooks, deprecated since 1.15
615618
* Queries if this block should render in a given layer.
616619
* A custom {@link net.minecraft.client.render.model.BakedModel} can use {@link net.minecraftforge.client.MinecraftForgeClient#getRenderLayer()} to alter the model based on layer.
617620
*/

patchwork-events-world/src/main/java/net/minecraftforge/event/world/BlockEvent.java renamed to patchwork-extensions-block/src/main/java/net/minecraftforge/event/world/BlockEvent.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@
2121

2222
import java.util.List;
2323

24+
import net.minecraftforge.common.extensions.IForgeBlockState;
2425
import net.minecraftforge.eventbus.api.Event;
2526

2627
import net.minecraft.item.ItemStack;
2728
import net.minecraft.util.DefaultedList;
2829
import net.minecraft.block.BlockState;
30+
import net.minecraft.enchantment.EnchantmentHelper;
31+
import net.minecraft.enchantment.Enchantments;
2932
import net.minecraft.entity.player.PlayerEntity;
3033
import net.minecraft.util.math.BlockPos;
3134
import net.minecraft.world.IWorld;
3235
import net.minecraft.world.World;
3336

37+
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;
38+
3439
public class BlockEvent extends Event {
3540
private final IWorld world;
3641
private final BlockPos pos;
@@ -73,17 +78,14 @@ public BreakEvent(World world, BlockPos pos, BlockState state, PlayerEntity play
7378
this.player = player;
7479
this.exp = 0;
7580

76-
// TODO: BlockState#getExpDrop
77-
78-
/*
7981
// Handle empty block or player unable to break block scenario
80-
if (state == null || !ForgeHooks.canHarvestBlock(state, player, world, pos)) {
82+
if (state == null || !BlockHarvestManager.canHarvestBlock(state, player, world, pos)) {
8183
this.exp = 0;
8284
} else {
83-
int bonusLevel = EnchantmentHelper.getLevel(Enchantments.FORTUNE, player.getHeldItemMainhand());
84-
int silklevel = EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH, player.getHeldItemMainhand());
85-
this.exp = state.getExpDrop(world, pos, bonusLevel, silklevel);
86-
}*/
85+
int bonusLevel = EnchantmentHelper.getLevel(Enchantments.FORTUNE, player.getMainHandStack());
86+
int silklevel = EnchantmentHelper.getLevel(Enchantments.SILK_TOUCH, player.getMainHandStack());
87+
this.exp = ((IForgeBlockState) state).getExpDrop(world, pos, bonusLevel, silklevel);
88+
}
8789
}
8890

8991
public PlayerEntity getPlayer() {
@@ -123,6 +125,7 @@ public boolean isCancelable() {
123125
*
124126
* <p>{@link #isSilkTouching} is set if this is considered a silk touch harvesting operation, vs a normal harvesting operation. Act accordingly.</p>
125127
*/
128+
@Deprecated
126129
public static class HarvestDropsEvent extends BlockEvent {
127130
private final int fortuneLevel;
128131
private final DefaultedList<ItemStack> drops;

0 commit comments

Comments
 (0)