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

Commit 69bb40c

Browse files
authored
Revert "Impl removedByPlayer and canHarvestBlock in IForgeBlock (#126)"
This reverts commit 7d7f2b1.
1 parent 7d7f2b1 commit 69bb40c

File tree

22 files changed

+92
-635
lines changed

22 files changed

+92
-635
lines changed

patchwork-events-world/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ version = getSubprojectVersion(project, "0.3.0")
33

44
dependencies {
55
implementation project(path: ':patchwork-api-base', configuration: 'dev')
6-
implementation project(path: ':patchwork-extensions-block', configuration: 'dev')
76
}

patchwork-events-world/src/main/java/net/minecraftforge/event/world/BlockEvent.java

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

2222
import java.util.List;
2323

24-
import net.minecraftforge.common.extensions.IForgeBlockState;
2524
import net.minecraftforge.eventbus.api.Event;
2625

2726
import net.minecraft.item.ItemStack;
2827
import net.minecraft.util.DefaultedList;
2928
import net.minecraft.block.BlockState;
30-
import net.minecraft.enchantment.EnchantmentHelper;
31-
import net.minecraft.enchantment.Enchantments;
3229
import net.minecraft.entity.player.PlayerEntity;
3330
import net.minecraft.util.math.BlockPos;
3431
import net.minecraft.world.IWorld;
3532
import net.minecraft.world.World;
3633

37-
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;
38-
3934
public class BlockEvent extends Event {
4035
private final IWorld world;
4136
private final BlockPos pos;
@@ -78,14 +73,17 @@ public BreakEvent(World world, BlockPos pos, BlockState state, PlayerEntity play
7873
this.player = player;
7974
this.exp = 0;
8075

76+
// TODO: BlockState#getExpDrop
77+
78+
/*
8179
// Handle empty block or player unable to break block scenario
82-
if (state == null || !BlockHarvestManager.canHarvestBlock(state, player, world, pos)) {
80+
if (state == null || !ForgeHooks.canHarvestBlock(state, player, world, pos)) {
8381
this.exp = 0;
8482
} else {
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-
}
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+
}*/
8987
}
9088

9189
public PlayerEntity getPlayer() {

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

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,14 @@
2828
import net.minecraftforge.event.world.WorldEvent;
2929

3030
import net.minecraft.block.BlockState;
31-
import net.minecraft.block.entity.BlockEntity;
3231
import net.minecraft.entity.player.PlayerEntity;
3332
import net.minecraft.item.ItemStack;
3433
import net.minecraft.util.DefaultedList;
3534
import net.minecraft.world.World;
3635
import net.minecraft.entity.EntityCategory;
37-
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
38-
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
3936
import net.minecraft.server.network.ServerPlayerEntity;
4037
import net.minecraft.server.world.ServerWorld;
4138
import net.minecraft.util.math.BlockPos;
42-
import net.minecraft.world.EmptyBlockView;
43-
import net.minecraft.world.GameMode;
4439
import net.minecraft.util.math.ChunkPos;
4540
import net.minecraft.world.IWorld;
4641
import net.minecraft.world.biome.Biome;
@@ -77,60 +72,6 @@ public static void onWorldSave(IWorld world) {
7772
MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(world));
7873
}
7974

80-
/**
81-
* Called by Mixin and ForgeHooks.
82-
* @return experience dropped, -1 = block breaking is cancelled.
83-
*/
84-
public static int onBlockBreakEvent(World world, GameMode gameMode, ServerPlayerEntity player, BlockPos pos) {
85-
// Logic from tryHarvestBlock for pre-canceling the event
86-
boolean preCancelEvent = false;
87-
88-
ItemStack itemstack = player.getMainHandStack();
89-
90-
if (!itemstack.isEmpty() && !itemstack.getItem().canMine(world.getBlockState(pos), world, pos, player)) {
91-
preCancelEvent = true;
92-
}
93-
94-
// method_21701 => canMine
95-
// Isn't the function really canNotMine?
96-
97-
if (player.method_21701(world, pos, gameMode)) {
98-
preCancelEvent = true;
99-
}
100-
101-
// Tell client the block is gone immediately then process events
102-
if (world.getBlockEntity(pos) == null) {
103-
player.networkHandler.sendPacket(new BlockUpdateS2CPacket(EmptyBlockView.INSTANCE, pos));
104-
}
105-
106-
// Post the block break event
107-
BlockState state = world.getBlockState(pos);
108-
BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, state, player);
109-
event.setCanceled(preCancelEvent);
110-
MinecraftForge.EVENT_BUS.post(event);
111-
112-
// Handle if the event is canceled
113-
if (event.isCanceled()) {
114-
// Let the client know the block still exists
115-
player.networkHandler.sendPacket(new BlockUpdateS2CPacket(world, pos));
116-
117-
// Update any block entity data for this block
118-
BlockEntity entity = world.getBlockEntity(pos);
119-
120-
if (entity != null) {
121-
BlockEntityUpdateS2CPacket packet = entity.toUpdatePacket();
122-
123-
if (packet != null) {
124-
player.networkHandler.sendPacket(packet);
125-
}
126-
}
127-
128-
return -1; // Cancelled
129-
} else {
130-
return event.getExpToDrop();
131-
}
132-
}
133-
13475
// TODO: Leaving this unfired is intentional. See: https://github.com/MinecraftForge/MinecraftForge/issues/5828
13576
public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player) {
13677
BlockEvent.HarvestDropsEvent event = new BlockEvent.HarvestDropsEvent(world, pos, state, fortune, dropChance, drops, player, silkTouch);

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

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,28 @@
1919

2020
package net.patchworkmc.mixin.event.world;
2121

22+
import net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.event.world.BlockEvent;
2224
import org.spongepowered.asm.mixin.Mixin;
2325
import org.spongepowered.asm.mixin.Shadow;
2426
import org.spongepowered.asm.mixin.injection.At;
2527
import org.spongepowered.asm.mixin.injection.Inject;
2628
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2729

30+
import net.minecraft.block.BlockState;
31+
import net.minecraft.block.entity.BlockEntity;
32+
import net.minecraft.item.ItemStack;
33+
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
34+
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
2835
import net.minecraft.server.network.ServerPlayerEntity;
2936
import net.minecraft.server.network.ServerPlayerInteractionManager;
3037
import net.minecraft.server.world.ServerWorld;
3138
import net.minecraft.util.math.BlockPos;
39+
import net.minecraft.world.EmptyBlockView;
3240
import net.minecraft.world.GameMode;
3341

34-
import net.patchworkmc.impl.event.world.WorldEvents;
35-
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;
36-
3742
@Mixin(ServerPlayerInteractionManager.class)
38-
public abstract class MixinServerPlayerInteractionManager {
43+
public class MixinServerPlayerInteractionManager {
3944
@Shadow
4045
public ServerWorld world;
4146
@Shadow
@@ -45,17 +50,52 @@ public abstract class MixinServerPlayerInteractionManager {
4550

4651
@Inject(method = "tryBreakBlock", at = @At("HEAD"), cancellable = true)
4752
private void hookBreakBlock(BlockPos pos, CallbackInfoReturnable<Boolean> callback) {
48-
int exp = WorldEvents.onBlockBreakEvent(world, gameMode, player, pos);
53+
boolean preCancelEvent = false;
4954

50-
if (exp < 0) {
51-
callback.setReturnValue(false);
52-
} else {
53-
BlockHarvestManager.pushExpDropStack(exp);
55+
ItemStack itemstack = player.getMainHandStack();
56+
57+
if (!itemstack.isEmpty() && !itemstack.getItem().canMine(world.getBlockState(pos), world, pos, player)) {
58+
preCancelEvent = true;
5459
}
55-
}
5660

57-
@Inject(method = "tryBreakBlock", at = @At("RETURN"), cancellable = true)
58-
private void tryBreakBlock_return(BlockPos pos, CallbackInfoReturnable<Boolean> callback) {
59-
BlockHarvestManager.popExpDropStack();
61+
// method_21701 => canMine
62+
// Isn't the function really canNotMine?
63+
64+
if (player.method_21701(world, pos, gameMode)) {
65+
preCancelEvent = true;
66+
}
67+
68+
// Tell client the block is gone immediately then process events
69+
if (world.getBlockEntity(pos) == null) {
70+
player.networkHandler.sendPacket(new BlockUpdateS2CPacket(EmptyBlockView.INSTANCE, pos));
71+
}
72+
73+
// Post the block break event
74+
BlockState state = world.getBlockState(pos);
75+
BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, state, player);
76+
event.setCanceled(preCancelEvent);
77+
MinecraftForge.EVENT_BUS.post(event);
78+
79+
// Handle if the event is canceled
80+
if (event.isCanceled()) {
81+
// Let the client know the block still exists
82+
player.networkHandler.sendPacket(new BlockUpdateS2CPacket(world, pos));
83+
84+
// Update any block entity data for this block
85+
BlockEntity entity = world.getBlockEntity(pos);
86+
87+
if (entity != null) {
88+
BlockEntityUpdateS2CPacket packet = entity.toUpdatePacket();
89+
90+
if (packet != null) {
91+
player.networkHandler.sendPacket(packet);
92+
}
93+
}
94+
95+
callback.setReturnValue(false);
96+
} else if (event.getExpToDrop() != 0) {
97+
// TODO: Drop experience
98+
throw new UnsupportedOperationException("Cannot drop exp from a BreakEvent yet");
99+
}
60100
}
61101
}

patchwork-events-world/src/main/resources/fabric.mod.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"PatchworkMC"
1414
],
1515
"depends": {
16-
"patchwork-api-base": "*",
17-
"patchwork-extensions-block": "*"
16+
"patchwork-api-base": "*"
1817
},
1918
"mixins": [
2019
"patchwork-events-world.mixins.json"

patchwork-extensions-block/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ 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')
87
implementation project(path: ':patchwork-tooltype', configuration: 'dev')
98
}
109

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

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

2929
import net.minecraftforge.common.IPlantable;
30-
import net.minecraftforge.common.ToolType;
3130

3231
import net.minecraft.block.BedBlock;
3332
import net.minecraft.block.Block;
@@ -83,12 +82,10 @@
8382
import net.fabricmc.api.EnvType;
8483
import net.fabricmc.api.Environment;
8584

86-
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;
87-
import net.patchworkmc.impl.extensions.block.PatchworkBlock;
8885
import net.patchworkmc.mixin.extensions.block.FireBlockAccessor;
8986
import net.patchworkmc.mixin.extensions.block.PlantBlockAccessor;
9087

91-
public interface IForgeBlock extends PatchworkBlock {
88+
public interface IForgeBlock {
9289
default Block getBlock() {
9390
return (Block) this;
9491
}
@@ -207,18 +204,18 @@ default BlockEntity createTileEntity(BlockState state, BlockView world) {
207204
return null;
208205
}
209206

210-
/* TODO IForgeBlock#canHarvestBlock indirectly requires ToolType (via ForgeHooks#canHarvestBlock) */
207+
/* TODO IForgeBlock#canHarvestBlock indirectly requires ToolType (via ForgeHooks#canHarvestBlock)
211208
/**
212209
* Determines if the player can harvest this block, obtaining it's drops when the block is destroyed.
213210
*
214211
* @param world The current world
215212
* @param pos The block's current position
216213
* @param player The player damaging the block
217214
* @return True to spawn the drops
218-
*/
215+
*
219216
default boolean canHarvestBlock(BlockState state, BlockView world, BlockPos pos, PlayerEntity player) {
220-
return BlockHarvestManager.canHarvestBlock(state, player, world, pos);
221-
}
217+
return ForgeHooks.canHarvestBlock(state, player, world, pos);
218+
}*/
222219

223220
// TODO Call locations: Patches: ServerPlayerInteractionManager*
224221
/**
@@ -243,7 +240,7 @@ default boolean canHarvestBlock(BlockState state, BlockView world, BlockPos pos,
243240
*/
244241
default boolean removedByPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, FluidState fluid) {
245242
getBlock().onBreak(world, pos, state, player);
246-
return world.setBlockState(pos, fluid.getBlockState(), world.isClient ? 11 : 3);
243+
return world.removeBlock(pos, false);
247244
}
248245

249246
// TODO Call locations: Patches: LivingEntity*, PlayerEntity*, Forge classes: ForgeEventFactory (called from LivingEntity patch)
@@ -648,7 +645,6 @@ default boolean isBeaconBase(BlockState state, CollisionView world, BlockPos pos
648645
// TODO Call locations: Forge classes: BreakEvent*
649646
/**
650647
* 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.
652648
*
653649
* @param state The current state
654650
* @param world The world
@@ -782,12 +778,12 @@ default boolean getWeakChanges(BlockState state, CollisionView world, BlockPos p
782778
return false;
783779
}
784780

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

792788
// TODO Call locations: Patches: PickaxeItem*, Forge classes: ForgeHooks*
793789
/**
@@ -797,18 +793,18 @@ default boolean getWeakChanges(BlockState state, CollisionView world, BlockPos p
797793
*/
798794
int getHarvestLevel(BlockState state);
799795

800-
/* TODO IForgeBlock#isToolEffective needs ToolType */
796+
/* TODO IForgeBlock#isToolEffective needs ToolType
801797
/**
802798
* Checks if the specified tool type is efficient on this block,
803799
* meaning that it digs at full speed.
804-
*/
800+
*
805801
default boolean isToolEffective(BlockState state, ToolType tool) {
806802
if (tool == ToolType.PICKAXE && (this.getBlock() == Blocks.REDSTONE_ORE || this.getBlock() == Blocks.REDSTONE_LAMP || this.getBlock() == Blocks.OBSIDIAN)) {
807803
return false;
808804
}
809805
810806
return tool == getHarvestTool(state);
811-
}
807+
}*/
812808

813809
// TODO Call locations: Forge classes: ForgeHooksClient
814810
/**

0 commit comments

Comments
 (0)