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
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ loader_version=0.15.6-bta.7

# Other Mods
mod_menu_version=3.0.0
halplibe_version=5.2.4
halplibe_version=5.3.1

# Mod
mod_version=1.0.0
mod_group=turniplabs
mod_name=examplemod
mod_group=redart15
mod_name=commandly
25 changes: 25 additions & 0 deletions src/main/java/redart15/commandly/CommandlyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package redart15.commandly;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import redart15.commandly.command.CommadlyCommands;
import turniplabs.halplibe.util.ClientStartEntrypoint;

@Environment(EnvType.CLIENT)
public class CommandlyClient implements ClientModInitializer, ClientStartEntrypoint {
@Override
public void onInitializeClient() {
CommadlyCommands.registerClientCommands();
}

@Override
public void beforeClientStart() {

}

@Override
public void afterClientStart() {

}
}
43 changes: 43 additions & 0 deletions src/main/java/redart15/commandly/CommandlyMod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package redart15.commandly;

import net.fabricmc.api.ModInitializer;
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.veincapitator.OreGroups;
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 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));
@Override
public void onInitialize() {
OreGroups.init();
LOGGER.info("Commandly initialized");
}

@Override
public void beforeGameStart() {

}

@Override
public void afterGameStart() {

}

@Override
public void onRecipesReady() {

}

@Override
public void initNamespaces() {

}
}
14 changes: 14 additions & 0 deletions src/main/java/redart15/commandly/CommandlyServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package redart15.commandly;

import net.fabricmc.api.DedicatedServerModInitializer;
import redart15.commandly.command.CommadlyCommands;

import static redart15.commandly.CommandlyMod.LOGGER;

public class CommandlyServer implements DedicatedServerModInitializer {
@Override
public void onInitializeServer() {
CommadlyCommands.registerServerCommands();
LOGGER.info("Commandly server initialized.");
}
}
20 changes: 20 additions & 0 deletions src/main/java/redart15/commandly/command/CommadlyCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package redart15.commandly.command;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.core.net.command.CommandManager;

public class CommadlyCommands {


@Environment(EnvType.CLIENT)
public static void registerClientCommands() {
CommandManager.registerCommand(new CommandGrow());
}

@Environment(EnvType.SERVER)
public static void registerServerCommands() {
CommandManager.registerServerCommand(new CommandGrow());
}

}
101 changes: 101 additions & 0 deletions src/main/java/redart15/commandly/command/CommandGrow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package redart15.commandly.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.ArgumentBuilderLiteral;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.core.block.Block;
import net.minecraft.core.block.BlockLogic;
import net.minecraft.core.entity.player.Player;
import net.minecraft.core.item.IBonemealable;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.item.Items;
import net.minecraft.core.net.command.CommandManager;
import net.minecraft.core.net.command.CommandSource;
import net.minecraft.core.util.helper.Side;
import net.minecraft.core.world.World;
import net.minecraft.core.world.chunk.*;
import redart15.commandly.util.Point;

import java.util.*;

@SuppressWarnings("ALL") //cause this drives me nuts
public class CommandGrow implements CommandManager.CommandRegistry {
private static int worldHeight = 256;
private static byte radius = 9;

public void register(CommandDispatcher<CommandSource> dispatcher) {
dispatcher.register((ArgumentBuilderLiteral) ArgumentBuilderLiteral.literal("grow")
.requires(c -> ((CommandSource) c).hasAdmin()).executes(c -> {
return growCommand(c);
}));
}

private int growCommand(CommandContext<Object> c) {
CommandSource source = (CommandSource) c.getSource();
Player player = source.getSender();
World world = source.getWorld();
Set<ChunkCoordinate> loaded = CommandGrow.getLoadedChunks(world, player.chunkCoordX, player.chunkCoordY, player.chunkCoordZ);
List<Point> bonemealTargets = getAllBonemeableBlocks(world, loaded);
grow(world, bonemealTargets);
return 1;
}

public void grow(World world, List<Point> targets){
Random rand = new Random();
for(Point p : targets){
int x = p.getIntX();
int y = p.getIntY();
int z = p.getIntZ();
Block<?> block = world.getBlock(x,y,z);
if(block == null) continue;
BlockLogic logic = block.getLogic();
if (block.getLogic() instanceof IBonemealable) {
((IBonemealable)logic).onBonemealUsed(new ItemStack(Items.DYE), (Player) null, world, x,y,z, Side.TOP, 0,0);
}
}
}

public List<Point> getAllBonemeableBlocks(World world,Set<ChunkCoordinate> loaded) {
List<Point> bonemeableBlocks = new ArrayList<Point>();
for(ChunkCoordinate coords: loaded){
bonemeableBlocks.addAll(getBonemeableBlocks(world, coords));
}
return bonemeableBlocks;
}

public List<Point> getBonemeableBlocks(World world,ChunkCoordinate chunkCoordinate) {
List<Point> bonemealableBlock = new ArrayList<Point>();
int ix = chunkCoordinate.x * 16;
int iz = chunkCoordinate.z * 16;
for (int y = 0; y <= worldHeight; y++) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
int curX = ix + x;
int curZ = iz + z;
Block<?> block = world.getBlock(curX, y, curZ);
if (block == null) continue;
if (block.id() == 0) continue;
if (block.getLogic() instanceof IBonemealable) {
bonemealableBlock.add(new Point(curX, y, curZ));
}
}
}
}
return bonemealableBlock;
}

public static Set<ChunkCoordinate> getLoadedChunks(World world,int playerChunkX, int playerChunkY, int playerChunkZ) {
Set<ChunkCoordinate> loadedChunks = new HashSet<ChunkCoordinate>();
for (int x = -radius; x <= radius; ++x) {
for (int z = -radius; z <= radius; ++z) {
int chunkCoordX = x + playerChunkX;
int chunkCoordZ = z + playerChunkZ;
if (world.isChunkLoaded(chunkCoordX, chunkCoordZ)) {
loadedChunks.add(new ChunkCoordinate(chunkCoordX, chunkCoordZ));
}
}
}
return loadedChunks;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package redart15.commandly.mixin;

import net.minecraft.core.block.Block;
import net.minecraft.core.data.tag.Tag;
import net.minecraft.core.entity.player.Player;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.item.material.ToolMaterial;
import net.minecraft.core.item.tool.ItemTool;
import net.minecraft.core.item.tool.ItemToolPickaxe;
import net.minecraft.core.util.helper.Side;
import net.minecraft.core.world.World;
import org.spongepowered.asm.mixin.Mixin;
import redart15.commandly.CommandlyMod;
import redart15.commandly.veincapitator.VeinMining;

@Mixin(value = ItemToolPickaxe.class, remap = false)
public class ItemToolPickaxeMixinVeinMining extends ItemTool {

protected ItemToolPickaxeMixinVeinMining(String name, String namespaceId, int id, int damageDealt, ToolMaterial toolMaterial, Tag<Block<?>> tagEffectiveAgainst) {
super(name, namespaceId, id, damageDealt, toolMaterial, tagEffectiveAgainst);
}

@Override
public boolean beforeDestroyBlock(World world, ItemStack itemStack, int blockId, int x, int y, int z, Side side, Player player) {
if (world.getGameRuleValue(CommandlyMod.VEIN_MINING)
&& !player.isSneaking()
&& !world.isClientSide
) {
return VeinMining.veinMining(world, itemStack, x, y, z, player).mine(blockId, side);
}
return true;
}
}
101 changes: 101 additions & 0 deletions src/main/java/redart15/commandly/mixin/StopGrassSpreadingMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package redart15.commandly.mixin;

import net.minecraft.core.block.Block;
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();
}
}
20 changes: 20 additions & 0 deletions src/main/java/redart15/commandly/mixin/StopMossSpreading.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package redart15.commandly.mixin;

import net.minecraft.core.block.BlockLogicMoss;
import net.minecraft.core.world.World;
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.CallbackInfoReturnable;
import redart15.commandly.CommandlyMod;

@Mixin(value = BlockLogicMoss.class, remap = false)
public 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);
}
}
}
Loading