|
1 | 1 | package redart15.commandly.mixins.mixin; |
2 | 2 |
|
3 | | -import net.minecraft.core.block.Block; |
| 3 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 4 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
4 | 5 | import net.minecraft.core.block.BlockLogicGrass; |
5 | | -import net.minecraft.core.block.Blocks; |
6 | | -import net.minecraft.core.data.gamerule.GameRules; |
7 | 6 | import net.minecraft.core.world.World; |
8 | | -import net.minecraft.core.world.biome.Biome; |
9 | | -import net.minecraft.core.world.biome.Biomes; |
10 | 7 | import org.spongepowered.asm.mixin.Mixin; |
11 | 8 | import org.spongepowered.asm.mixin.injection.At; |
12 | | -import org.spongepowered.asm.mixin.injection.Inject; |
13 | | -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
14 | 9 | import redart15.commandly.CommandlyMod; |
15 | 10 |
|
16 | | -import java.util.Random; |
17 | 11 | @Mixin(value = BlockLogicGrass.class, remap = false) |
18 | 12 | public abstract class StopGrassSpreadingMixin{ |
19 | | - |
20 | | - @Inject(method = "updateTick", at = @At("HEAD"), cancellable = true) |
21 | | - public void updateTick(World world, int x, int y, int z, Random rand, CallbackInfo ci) { |
22 | | - |
23 | | - if (world.isClientSide) { |
24 | | - return; |
25 | | - } |
26 | | - |
27 | | - BlockLogicGrass self = (BlockLogicGrass)(Object)this; |
28 | | - |
29 | | - Block<?> dirt = self.dirt; |
30 | | - Block<?> block = self.block; |
31 | | - |
32 | | - if (revertToDirt(world, x, y, z, rand, dirt)) return; |
33 | | - if (world.getBlockLightValue(x, y + 1, z) < 9) { |
34 | | - return; |
35 | | - } |
36 | | - spreadGrass(world, x, y, z, rand, dirt, block); |
37 | | - spreadsFlowers(world, x, y, z, rand); |
38 | | - ci.cancel(); |
39 | | - } |
40 | | - |
41 | | - private void spreadGrass(World world, int x, int y, int z, Random rand, Block<?> dirt, Block<?> block) { |
42 | | - if(!world.getGameRuleValue(CommandlyMod.GRASS_SPREADING)) { |
43 | | - return; |
44 | | - } |
45 | | - for(int i = 0; i < 4; ++i) { |
46 | | - int x1 = x + rand.nextInt(3) - 1; |
47 | | - int y1 = y + rand.nextInt(5) - 3; |
48 | | - int z1 = z + rand.nextInt(3) - 1; |
49 | | - if (world.getBlockId(x1, y1, z1) == dirt.id() && world.getBlockLightValue(x1, y1 + 1, z1) >= 4 && Blocks.lightBlock[world.getBlockId(x1, y1 + 1, z1)] <= 2) { |
50 | | - world.setBlockWithNotify(x1, y1, z1, block.id()); |
51 | | - } |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - private boolean revertToDirt(World world, int x, int y, int z, Random rand, Block<?> dirt) { |
56 | | - if (world.getBlockLightValue(x, y + 1, z) < 4 && Blocks.lightBlock[world.getBlockId(x, y + 1, z)] > 2) { |
57 | | - if (rand.nextInt(4) == 0) { |
58 | | - world.setBlockWithNotify(x, y, z, dirt.id()); |
59 | | - } |
60 | | - return true; |
61 | | - } |
62 | | - return false; |
63 | | - } |
64 | | - |
65 | | - private void spreadsFlowers(World world, int x, int y, int z, Random rand) { |
66 | | - if (!((Boolean) world.getGameRuleValue(GameRules.DO_SEASONAL_GROWTH)) |
67 | | - || world.getBlockId(x, y + 1, z) != 0 |
68 | | - || world.getSeasonManager().getCurrentSeason() == null |
69 | | - || !world.getSeasonManager().getCurrentSeason().growFlowers |
70 | | - || rand.nextInt(256) != 0 |
71 | | - ) return; |
72 | | - int flowerID = determineFlower(world,x,y,z,rand); |
73 | | - world.setBlockWithNotify(x, y + 1, z, flowerID); |
74 | | - } |
75 | | - |
76 | | - private int determineFlower(World world, int x,int y, int z, Random rand){ |
77 | | - int r = rand.nextInt(400); |
78 | | - if (r < 26) { |
79 | | - return Blocks.FLOWER_RED.id(); |
80 | | - } |
81 | | - if (r < 41) { |
82 | | - return Blocks.FLOWER_YELLOW.id(); |
83 | | - } |
84 | | - if (r < 60) { |
85 | | - Biome biome = world.getBlockBiome(x, y + 1, z); |
86 | | - if (biome == Biomes.OVERWORLD_BIRCH_FOREST || biome == Biomes.OVERWORLD_SEASONAL_FOREST) { |
87 | | - return Blocks.FLOWER_PINK.id(); |
88 | | - } |
89 | | - if (biome == Biomes.OVERWORLD_MEADOW || biome == Biomes.OVERWORLD_BOREAL_FOREST || biome == Biomes.OVERWORLD_SHRUBLAND) { |
90 | | - return Blocks.FLOWER_PURPLE.id(); |
91 | | - } |
92 | | - if (biome == Biomes.OVERWORLD_FOREST || biome == Biomes.OVERWORLD_SWAMPLAND || biome == Biomes.OVERWORLD_RAINFOREST || biome == Biomes.OVERWORLD_CAATINGA) { |
93 | | - return Blocks.FLOWER_LIGHT_BLUE.id(); |
94 | | - } |
95 | | - } |
96 | | - if (rand.nextInt(8) == 0) { |
97 | | - return Blocks.TALLGRASS_FERN.id(); |
98 | | - } |
99 | | - return Blocks.TALLGRASS.id(); |
| 13 | + @WrapOperation( |
| 14 | + method = "updateTick", |
| 15 | + at = @At( |
| 16 | + value = "INVOKE", |
| 17 | + target = "Lnet/minecraft/core/world/World;getBlockLightValue(III)I", |
| 18 | + ordinal = 2 |
| 19 | + ) |
| 20 | + ) |
| 21 | + public int allowGrassUpdates(World instance, int x, int y, int z, Operation<Integer> original){ |
| 22 | + if(Boolean.TRUE.equals(instance.getGameRuleValue(CommandlyMod.GRASS_SPREADING))){ |
| 23 | + return original.call(instance, x, y, z); |
| 24 | + } |
| 25 | + return 0; |
100 | 26 | } |
101 | 27 | } |
0 commit comments