Skip to content

Commit 067b6da

Browse files
committed
simplified some mixin and added comments
1 parent 062ddb2 commit 067b6da

File tree

3 files changed

+30
-100
lines changed

3 files changed

+30
-100
lines changed

src/main/java/redart15/commandly/CommandlyMod.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class CommandlyMod implements ModInitializer, RecipeEntrypoint, GameStart
1414
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
1515
public static final int MASK = 7;
1616
public static final int MAX_BLOCK_COUNT = 256 * 16 * 16 * 25;
17-
public static GameRuleBoolean MOSS_SPREADING = GameRules.register(new GameRuleBoolean("doMossSpreading", true));
18-
public static GameRuleBoolean GRASS_SPREADING = GameRules.register(new GameRuleBoolean("doGrassSpreading", true));
19-
public static GameRuleBoolean VEIN_MINING = GameRules.register(new GameRuleBoolean("veinmining", false));
17+
public static final GameRuleBoolean MOSS_SPREADING = GameRules.register(new GameRuleBoolean("doMossSpreading", true));
18+
public static final GameRuleBoolean GRASS_SPREADING = GameRules.register(new GameRuleBoolean("doGrassSpreading", true));
19+
public static final GameRuleBoolean VEIN_MINING = GameRules.register(new GameRuleBoolean("veinmining", false));
20+
2021
@Override
2122
public void onInitialize() {
2223
OreGroups.init();
@@ -25,21 +26,21 @@ public void onInitialize() {
2526

2627
@Override
2728
public void beforeGameStart() {
28-
29+
// no need
2930
}
3031

3132
@Override
3233
public void afterGameStart() {
33-
34+
// no need
3435
}
3536

3637
@Override
3738
public void onRecipesReady() {
38-
39+
// no need
3940
}
4041

4142
@Override
4243
public void initNamespaces() {
43-
44+
// no need
4445
}
4546
}
Lines changed: 15 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,27 @@
11
package redart15.commandly.mixins.mixin;
22

3-
import net.minecraft.core.block.Block;
3+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
45
import net.minecraft.core.block.BlockLogicGrass;
5-
import net.minecraft.core.block.Blocks;
6-
import net.minecraft.core.data.gamerule.GameRules;
76
import net.minecraft.core.world.World;
8-
import net.minecraft.core.world.biome.Biome;
9-
import net.minecraft.core.world.biome.Biomes;
107
import org.spongepowered.asm.mixin.Mixin;
118
import org.spongepowered.asm.mixin.injection.At;
12-
import org.spongepowered.asm.mixin.injection.Inject;
13-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
149
import redart15.commandly.CommandlyMod;
1510

16-
import java.util.Random;
1711
@Mixin(value = BlockLogicGrass.class, remap = false)
1812
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;
10026
}
10127
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package redart15.commandly.mixins.mixin;
22

3+
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
4+
import com.llamalad7.mixinextras.sugar.Local;
35
import net.minecraft.core.block.BlockLogicMoss;
46
import net.minecraft.core.world.World;
57
import org.spongepowered.asm.mixin.Mixin;
@@ -11,10 +13,11 @@
1113
@Mixin(value = BlockLogicMoss.class, remap = false)
1214
public abstract class StopMossSpreading {
1315

14-
@Inject(method = "canMossSpread(Lnet/minecraft/core/world/World;III)Z", at=@At("HEAD"), cancellable = true)
15-
public void allowedMossSpread(World world, int x, int y, int z, CallbackInfoReturnable<Boolean> cir){
16-
if(!world.isClientSide && !world.getGameRuleValue(CommandlyMod.MOSS_SPREADING)){
17-
cir.setReturnValue(false);
16+
@ModifyReturnValue(method = "canMossSpread", at = @At("RETURN"))
17+
public boolean mossSpread(boolean original, World world){
18+
if(Boolean.TRUE.equals(world.getGameRuleValue(CommandlyMod.MOSS_SPREADING))){
19+
return original;
1820
}
21+
return false;
1922
}
2023
}

0 commit comments

Comments
 (0)