Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/main/java/redart15/commandly/CommandlyMod.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,65 @@
package redart15.commandly;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
import net.minecraft.core.data.gamerule.GameRuleBoolean;
import net.minecraft.core.data.gamerule.GameRules;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redart15.commandly.api.CommandlyPlugin;
import redart15.commandly.veincapitator.OreGroups;
import redart15.commandly.veincapitator.PickAxeRegister;
import turniplabs.halplibe.util.GameStartEntrypoint;
import turniplabs.halplibe.util.RecipeEntrypoint;

public class CommandlyMod implements ModInitializer, RecipeEntrypoint, GameStartEntrypoint {
public static final String MOD_ID = "commandly";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final int MASK = 7;
public static final int MAX_BLOCK_COUNT = 256 * 16 * 16 * 25;
public static GameRuleBoolean MOSS_SPREADING = GameRules.register(new GameRuleBoolean("doMossSpreading", true));
public static GameRuleBoolean GRASS_SPREADING = GameRules.register(new GameRuleBoolean("doGrassSpreading", true));
public static GameRuleBoolean VEIN_MINING = GameRules.register(new GameRuleBoolean("veinmining", false));
private static final int MASK = 7;
private static final int MAX_BLOCK_COUNT = 256 * 16 * 16 * 25;
public static final GameRuleBoolean MOSS_SPREADING = GameRules.register(new GameRuleBoolean("doMossSpreading", true));
public static final GameRuleBoolean GRASS_SPREADING = GameRules.register(new GameRuleBoolean("doGrassSpreading", true));
public static final GameRuleBoolean VEINMINING = GameRules.register(new GameRuleBoolean("veinmining", false));

@Override
public void onInitialize() {
OreGroups.init();
LOGGER.info("Commandly initialized");
// no need
}

@Override
public void beforeGameStart() {

// no need
}

@Override
public void afterGameStart() {
LOGGER.info("Loading implementation");
OreGroups.init();
PickAxeRegister.init();
FabricLoader.getInstance()
.getEntrypointContainers("commandly", CommandlyPlugin.class)
.forEach(CommandlyMod::initialize);

}

private static void initialize(EntrypointContainer<CommandlyPlugin> plugin) {
CommandlyPlugin entrypoint = plugin.getEntrypoint();
entrypoint.registerOreGroups(OreGroups.getInstance());
entrypoint.registerPickaxe(PickAxeRegister.getInstance());
}

public static int getMask(){return MASK;}
public static int getMaxBlockCount(){return MAX_BLOCK_COUNT;}

@Override
public void onRecipesReady() {

// no need
}

@Override
public void initNamespaces() {

// no need
}
}
9 changes: 9 additions & 0 deletions src/main/java/redart15/commandly/api/CommandlyPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package redart15.commandly.api;

import redart15.commandly.veincapitator.OreGroups;
import redart15.commandly.veincapitator.PickAxeRegister;

public interface CommandlyPlugin {
void registerOreGroups(OreGroups registry);
void registerPickaxe(PickAxeRegister register);
}
11 changes: 5 additions & 6 deletions src/main/java/redart15/commandly/command/CommandProtect.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.function.Predicate;

import static redart15.commandly.command.CommandlyCommands.ReturnValues.*;
import static redart15.commandly.CommandlyMod.MASK;

@SuppressWarnings("ALL") //cause this drives me nuts
public class CommandProtect implements CommandManager.CommandRegistry {
Expand Down Expand Up @@ -98,7 +97,7 @@ private static int chunks_protect(CommandSource source, World world, Predicate<B
Block<?> block = world.getBlock(fx + x, y, fz + z);
if (block == null) continue;
if (treecapitator.test(block) || veinmining.test(block)) {
world.setBlockMetadata(fx + x, y, fz + z, (1 << MASK));
world.setBlockMetadata(fx + x, y, fz + z, (1 << CommandlyMod.getMask()));
count_protected++;
}
}
Expand Down Expand Up @@ -157,7 +156,7 @@ private static int radius_protect(
throw new RuntimeException(e);
}

if ((int) Math.floor(1.25 * Math.PI * Math.pow(radius, 3)) > CommandlyMod.MAX_BLOCK_COUNT) {
if ((int) Math.floor(1.25 * Math.PI * Math.pow(radius, 3)) > CommandlyMod.getMaxBlockCount()) {
source.sendTranslatableMessage("commadly.protected.tolarge");
return code(CANNOT);
}
Expand All @@ -169,7 +168,7 @@ private static int radius_protect(
Block<?> block = world.getBlock(fx + x, fy + y, fz + z);
if (block == null) continue;
if (treecapitator.test(block) || veinmining.test(block)) {
world.setBlockMetadata(fx + x, fy + y, fz + z, (1 << MASK));
world.setBlockMetadata(fx + x, fy + y, fz + z, (1 << CommandlyMod.getMask()));
count_protected++;
}
}
Expand Down Expand Up @@ -229,7 +228,7 @@ public static int point_protect(
} catch (CommandSyntaxException e) {
throw new RuntimeException(e);
}
if (Math.abs(fx - sx) * Math.abs(fy - sy) * Math.abs(fz - sz) > CommandlyMod.MAX_BLOCK_COUNT) {
if (Math.abs(fx - sx) * Math.abs(fy - sy) * Math.abs(fz - sz) > CommandlyMod.getMaxBlockCount()) {
source.sendTranslatableMessage("commadly.protected.toolarge");
return code(CANNOT);
}
Expand All @@ -241,7 +240,7 @@ public static int point_protect(
Block<?> block = world.getBlock(x, y, z);
if (block == null) continue;
if (treecapitator.test(block) || veinmining.test(block)) {
world.setBlockMetadata(x, y, z, (1 << MASK));
world.setBlockMetadata(x, y, z, (1 << CommandlyMod.getMask()));
count_protected++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected ItemToolPickaxeMixinVeinMining(String name, String namespaceId, int id

@Override
public boolean beforeDestroyBlock(World world, ItemStack itemStack, int blockId, int x, int y, int z, Side side, Player player) {
if (!world.isClientSide && world.getGameRuleValue(CommandlyMod.VEIN_MINING) && !player.isSneaking()) {
if (!world.isClientSide && world.getGameRuleValue(CommandlyMod.VEINMINING) && !player.isSneaking()) {
return !VeinMining.veinMining(world, itemStack, x, y, z, player).mine(blockId, side);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,101 +1,27 @@
package redart15.commandly.mixins.mixin;

import net.minecraft.core.block.Block;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.core.block.BlockLogicGrass;
import net.minecraft.core.block.Blocks;
import net.minecraft.core.data.gamerule.GameRules;
import net.minecraft.core.world.World;
import net.minecraft.core.world.biome.Biome;
import net.minecraft.core.world.biome.Biomes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import redart15.commandly.CommandlyMod;

import java.util.Random;
@Mixin(value = BlockLogicGrass.class, remap = false)
public abstract class StopGrassSpreadingMixin{

@Inject(method = "updateTick", at = @At("HEAD"), cancellable = true)
public void updateTick(World world, int x, int y, int z, Random rand, CallbackInfo ci) {

if (world.isClientSide) {
return;
}

BlockLogicGrass self = (BlockLogicGrass)(Object)this;

Block<?> dirt = self.dirt;
Block<?> block = self.block;

if (revertToDirt(world, x, y, z, rand, dirt)) return;
if (world.getBlockLightValue(x, y + 1, z) < 9) {
return;
}
spreadGrass(world, x, y, z, rand, dirt, block);
spreadsFlowers(world, x, y, z, rand);
ci.cancel();
}

private void spreadGrass(World world, int x, int y, int z, Random rand, Block<?> dirt, Block<?> block) {
if(!world.getGameRuleValue(CommandlyMod.GRASS_SPREADING)) {
return;
}
for(int i = 0; i < 4; ++i) {
int x1 = x + rand.nextInt(3) - 1;
int y1 = y + rand.nextInt(5) - 3;
int z1 = z + rand.nextInt(3) - 1;
if (world.getBlockId(x1, y1, z1) == dirt.id() && world.getBlockLightValue(x1, y1 + 1, z1) >= 4 && Blocks.lightBlock[world.getBlockId(x1, y1 + 1, z1)] <= 2) {
world.setBlockWithNotify(x1, y1, z1, block.id());
}
}
}

private boolean revertToDirt(World world, int x, int y, int z, Random rand, Block<?> dirt) {
if (world.getBlockLightValue(x, y + 1, z) < 4 && Blocks.lightBlock[world.getBlockId(x, y + 1, z)] > 2) {
if (rand.nextInt(4) == 0) {
world.setBlockWithNotify(x, y, z, dirt.id());
}
return true;
}
return false;
}

private void spreadsFlowers(World world, int x, int y, int z, Random rand) {
if (!((Boolean) world.getGameRuleValue(GameRules.DO_SEASONAL_GROWTH))
|| world.getBlockId(x, y + 1, z) != 0
|| world.getSeasonManager().getCurrentSeason() == null
|| !world.getSeasonManager().getCurrentSeason().growFlowers
|| rand.nextInt(256) != 0
) return;
int flowerID = determineFlower(world,x,y,z,rand);
world.setBlockWithNotify(x, y + 1, z, flowerID);
}

private int determineFlower(World world, int x,int y, int z, Random rand){
int r = rand.nextInt(400);
if (r < 26) {
return Blocks.FLOWER_RED.id();
}
if (r < 41) {
return Blocks.FLOWER_YELLOW.id();
}
if (r < 60) {
Biome biome = world.getBlockBiome(x, y + 1, z);
if (biome == Biomes.OVERWORLD_BIRCH_FOREST || biome == Biomes.OVERWORLD_SEASONAL_FOREST) {
return Blocks.FLOWER_PINK.id();
}
if (biome == Biomes.OVERWORLD_MEADOW || biome == Biomes.OVERWORLD_BOREAL_FOREST || biome == Biomes.OVERWORLD_SHRUBLAND) {
return Blocks.FLOWER_PURPLE.id();
}
if (biome == Biomes.OVERWORLD_FOREST || biome == Biomes.OVERWORLD_SWAMPLAND || biome == Biomes.OVERWORLD_RAINFOREST || biome == Biomes.OVERWORLD_CAATINGA) {
return Blocks.FLOWER_LIGHT_BLUE.id();
}
}
if (rand.nextInt(8) == 0) {
return Blocks.TALLGRASS_FERN.id();
}
return Blocks.TALLGRASS.id();
@WrapOperation(
method = "updateTick",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/core/world/World;getBlockLightValue(III)I",
ordinal = 2
)
)
public int allowGrassUpdates(World instance, int x, int y, int z, Operation<Integer> original){
if(Boolean.TRUE.equals(instance.getGameRuleValue(CommandlyMod.GRASS_SPREADING))){
return original.call(instance, x, y, z);
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package redart15.commandly.mixins.mixin;

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

@Inject(method = "canMossSpread(Lnet/minecraft/core/world/World;III)Z", at=@At("HEAD"), cancellable = true)
public void allowedMossSpread(World world, int x, int y, int z, CallbackInfoReturnable<Boolean> cir){
if(!world.isClientSide && !world.getGameRuleValue(CommandlyMod.MOSS_SPREADING)){
cir.setReturnValue(false);
@ModifyReturnValue(method = "canMossSpread", at = @At("RETURN"))
public boolean mossSpread(boolean original, World world){
if(Boolean.TRUE.equals(world.getGameRuleValue(CommandlyMod.MOSS_SPREADING))){
return original;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import redart15.commandly.CommandlyMod;

import static redart15.commandly.CommandlyMod.MASK;

@Mixin(value = TreecapitatorHelper.class, remap = false)
public abstract class SmartTreeCapitator {
Expand All @@ -30,7 +30,7 @@ public boolean isSmartLog(
TreecapitatorHelper asThis = (TreecapitatorHelper) (Object) this;
ChunkPosition p = asThis.basePosition;
int metadata = asThis.world.getBlockMetadata(p.x, p.y, p.z);
return block != null && !(block.getLogic() instanceof IPaintable) && (metadata >> MASK) == 0 && original.call(instance, block);
return block != null && !(block.getLogic() instanceof IPaintable) && (metadata >> CommandlyMod.getMask()) == 0 && original.call(instance, block);
}


Expand All @@ -52,6 +52,6 @@ private int getBlockID_addLogsAroundBlock(World instance, int x, int y, int z, O

@Unique
private static boolean isSmartTreecapitator(@NotNull BlockLogic logic, int metadata) {
return logic instanceof IPaintable || (metadata >> MASK) == 1;
return logic instanceof IPaintable || (metadata >> CommandlyMod.getMask()) == 1;
}
}
Loading
Loading