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

Commit 7c2f7f3

Browse files
committed
Implement ISTERs, fix block entities being invalid when breaking
1 parent 0186171 commit 7c2f7f3

File tree

17 files changed

+407
-70
lines changed

17 files changed

+407
-70
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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 net.patchworkmc.mixin.extensions.block;
21+
22+
import net.minecraftforge.common.extensions.IForgeBlockState;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
import org.spongepowered.asm.mixin.Overwrite;
25+
26+
import net.minecraft.block.AbstractBlock;
27+
import net.minecraft.block.BlockState;
28+
import net.minecraft.util.math.BlockPos;
29+
import net.minecraft.world.World;
30+
31+
@Mixin(AbstractBlock.class)
32+
public class MixinAbstractBlock {
33+
@Deprecated
34+
@Overwrite
35+
/**
36+
* @author glitch
37+
* @reason patch is a pain to write and i'm lazy
38+
*/
39+
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
40+
IForgeBlockState ext = (IForgeBlockState) state;
41+
IForgeBlockState newExt = (IForgeBlockState) newState;
42+
43+
if (ext.hasTileEntity() && (!state.isOf(newState.getBlock()) || !newExt.hasTileEntity())) {
44+
world.removeBlockEntity(pos);
45+
}
46+
}
47+
}

patchwork-extensions-block/src/main/java/net/patchworkmc/mixin/extensions/block/MixinChunkRegion.java

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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 net.patchworkmc.mixin.extensions.block;
21+
22+
import java.util.Map;
23+
24+
import net.minecraftforge.common.extensions.IForgeBlockState;
25+
import org.jetbrains.annotations.Nullable;
26+
import org.spongepowered.asm.mixin.Final;
27+
import org.spongepowered.asm.mixin.Mixin;
28+
import org.spongepowered.asm.mixin.Overwrite;
29+
import org.spongepowered.asm.mixin.Shadow;
30+
31+
import net.minecraft.block.Block;
32+
import net.minecraft.block.BlockEntityProvider;
33+
import net.minecraft.block.BlockState;
34+
import net.minecraft.block.entity.BlockEntity;
35+
import net.minecraft.util.math.BlockPos;
36+
import net.minecraft.world.Heightmap;
37+
import net.minecraft.world.World;
38+
import net.minecraft.world.chunk.ChunkSection;
39+
import net.minecraft.world.chunk.WorldChunk;
40+
41+
@Mixin(WorldChunk.class)
42+
public abstract class MixinWorldChunk {
43+
@Shadow
44+
@Final
45+
@Nullable
46+
public static ChunkSection EMPTY_SECTION;
47+
48+
@Shadow
49+
@Final
50+
private ChunkSection[] sections;
51+
52+
@Shadow
53+
@Final
54+
private Map<Heightmap.Type, Heightmap> heightmaps;
55+
56+
@Shadow
57+
@Final
58+
private World world;
59+
60+
@Shadow
61+
private volatile boolean shouldSave;
62+
63+
@Shadow
64+
@Nullable
65+
public abstract BlockEntity getBlockEntity(BlockPos pos, WorldChunk.CreationType creationType);
66+
67+
/**
68+
* @author glitch
69+
* @reason these injects are a pain to write and hasn't broken something yet :tm:
70+
*/
71+
@Overwrite
72+
@Nullable
73+
public BlockState setBlockState(BlockPos pos, BlockState state, boolean moved) {
74+
int i = pos.getX() & 15;
75+
int j = pos.getY();
76+
int k = pos.getZ() & 15;
77+
ChunkSection chunkSection = this.sections[j >> 4];
78+
79+
if (chunkSection == EMPTY_SECTION) {
80+
if (state.isAir()) {
81+
return null;
82+
}
83+
84+
chunkSection = new ChunkSection(j >> 4 << 4);
85+
this.sections[j >> 4] = chunkSection;
86+
}
87+
88+
boolean bl = chunkSection.isEmpty();
89+
BlockState blockState = chunkSection.setBlockState(i, j & 15, k, state);
90+
91+
if (blockState == state) {
92+
return null;
93+
} else {
94+
Block block = state.getBlock();
95+
Block block2 = blockState.getBlock();
96+
((Heightmap) this.heightmaps.get(Heightmap.Type.MOTION_BLOCKING)).trackUpdate(i, j, k, state);
97+
((Heightmap) this.heightmaps.get(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES)).trackUpdate(i, j, k, state);
98+
((Heightmap) this.heightmaps.get(Heightmap.Type.OCEAN_FLOOR)).trackUpdate(i, j, k, state);
99+
((Heightmap) this.heightmaps.get(Heightmap.Type.WORLD_SURFACE)).trackUpdate(i, j, k, state);
100+
boolean bl2 = chunkSection.isEmpty();
101+
102+
if (bl != bl2) {
103+
this.world.getChunkManager().getLightingProvider().setSectionStatus(pos, bl2);
104+
}
105+
106+
if (!this.world.isClient) {
107+
blockState.onStateReplaced(this.world, pos, state, moved);
108+
} else if (block2 != block || !((IForgeBlockState) state).hasTileEntity() && ((IForgeBlockState) blockState).hasTileEntity()) {
109+
this.world.removeBlockEntity(pos);
110+
}
111+
112+
if (!chunkSection.getBlockState(i, j & 15, k).isOf(block)) {
113+
return null;
114+
} else {
115+
BlockEntity blockEntity2;
116+
117+
if (block2 instanceof BlockEntityProvider) {
118+
blockEntity2 = this.getBlockEntity(pos, WorldChunk.CreationType.CHECK);
119+
120+
if (blockEntity2 != null) {
121+
blockEntity2.resetBlock();
122+
}
123+
}
124+
125+
if (!this.world.isClient) {
126+
state.onBlockAdded(this.world, pos, blockState, moved);
127+
}
128+
129+
if (block instanceof BlockEntityProvider) {
130+
blockEntity2 = this.getBlockEntity(pos, WorldChunk.CreationType.CHECK);
131+
132+
if (blockEntity2 == null) {
133+
blockEntity2 = ((BlockEntityProvider) block).createBlockEntity(this.world);
134+
this.world.setBlockEntity(pos, blockEntity2);
135+
} else {
136+
blockEntity2.resetBlock();
137+
}
138+
}
139+
140+
this.shouldSave = true;
141+
return blockState;
142+
}
143+
}
144+
}
145+
}
146+

patchwork-extensions-block/src/main/resources/patchwork-extensions-block.mixins.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"mixins": [
66
"MixinBlock",
77
"MixinBlockState",
8-
"MixinChunkRegion"
8+
"MixinAbstractBlock",
9+
"MixinWorldChunk"
910
],
1011
"injectors": {
1112
"defaultRequire": 1

patchwork-extensions-item/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,6 @@ default void onHorseArmorTick(ItemStack stack, World world, MobEntity horse) {
832832
* one.
833833
*/
834834
@Environment(EnvType.CLIENT)
835-
@Stubbed
836-
// TODO: needs call locations in ItemRenderer and BlockRenderManager
837835
BuiltinModelItemRenderer getItemStackTileEntityRenderer();
838836

839837
/**

patchwork-extensions-item/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ default ActionResult onItemUseFirst(ItemUsageContext context) {
130130
Item item = getStack().getItem();
131131
ActionResult enumactionresult = patchwork$getForgeItem().onItemUseFirst(getStack(), context);
132132

133-
if (entityplayer != null && enumactionresult == ActionResult.SUCCESS) {
133+
if (entityplayer != null && enumactionresult.isAccepted()) {
134134
entityplayer.incrementStat(Stats.USED.getOrCreateStat(item));
135135
}
136136

patchwork-extensions-item/src/main/java/net/patchworkmc/mixin/extensions/item/MixinItem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ private void onConstruct(Item.Settings settings, CallbackInfo info) {
6363

6464
toolClasses = Maps.newHashMap();
6565
toolClasses.putAll(extension.patchwork$toolTypes());
66+
// ISTER handled in client package
6667
}
6768

6869
@Inject(method = "isIn(Lnet/minecraft/item/ItemGroup;)Z", at = @At("HEAD"), cancellable = true)

patchwork-extensions-item/src/main/java/net/patchworkmc/mixin/extensions/item/MixinItemStack.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626

2727
@Mixin(ItemStack.class)
2828
public abstract class MixinItemStack implements IForgeItemStack {
29+
// TODO: useOnBlock special magick
2930
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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 net.patchworkmc.mixin.extensions.item;
21+
22+
import net.minecraftforge.common.extensions.IForgeItemStack;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.Inject;
26+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
27+
28+
import net.minecraft.item.ItemStack;
29+
import net.minecraft.item.ItemUsageContext;
30+
import net.minecraft.server.network.ServerPlayerEntity;
31+
import net.minecraft.server.network.ServerPlayerInteractionManager;
32+
import net.minecraft.util.ActionResult;
33+
import net.minecraft.util.Hand;
34+
import net.minecraft.util.hit.BlockHitResult;
35+
import net.minecraft.world.World;
36+
37+
@Mixin(ServerPlayerInteractionManager.class)
38+
public class MixinServerPlayerInteractionManager {
39+
@Inject(method = "interactBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;getMainHandStack()Lnet/minecraft/item/ItemStack;",
40+
ordinal = 0), cancellable = true)
41+
private void patchwork$fireOnItemFirstUse(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
42+
ItemUsageContext ctx = new ItemUsageContext(player, hand, hitResult);
43+
ActionResult result = ((IForgeItemStack) (Object) player.getStackInHand(hand)).onItemUseFirst(ctx);
44+
45+
if (result != ActionResult.PASS) {
46+
cir.setReturnValue(result);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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 net.patchworkmc.mixin.extensions.item.client;
21+
22+
import net.minecraftforge.common.extensions.IForgeItem;
23+
import org.objectweb.asm.Opcodes;
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Redirect;
27+
28+
import net.minecraft.block.BlockState;
29+
import net.minecraft.client.render.VertexConsumerProvider;
30+
import net.minecraft.client.render.block.BlockRenderManager;
31+
import net.minecraft.client.render.item.BuiltinModelItemRenderer;
32+
import net.minecraft.client.util.math.MatrixStack;
33+
34+
@Mixin(BlockRenderManager.class)
35+
public class MixinBlockRenderManager {
36+
@Redirect(method = "renderBlockAsEntity", at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/item/BuiltinModelItemRenderer;INSTANCE:Lnet/minecraft/client/render/item/BuiltinModelItemRenderer;",
37+
opcode = Opcodes.GETSTATIC, ordinal = 0))
38+
private BuiltinModelItemRenderer patchwork$useForgeISTER(BlockState state, MatrixStack matrices, VertexConsumerProvider vertexConsumer, int light, int overlay) {
39+
return ((IForgeItem) state.getBlock().asItem()).getItemStackTileEntityRenderer();
40+
}
41+
}

0 commit comments

Comments
 (0)