Skip to content

Commit

Permalink
Add setfire command
Browse files Browse the repository at this point in the history
  • Loading branch information
BrokenSwing committed Mar 19, 2019
1 parent 04db76b commit 7fd7163
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/dev/mff/modtutorial/ModTutorial.java
@@ -1,12 +1,15 @@
package dev.mff.modtutorial;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import dev.mff.modtutorial.commands.SetFireCommand;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLDedicatedServerSetupEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(ModTutorial.MOD_ID)
public class ModTutorial {
Expand All @@ -22,6 +25,13 @@ public ModTutorial() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::serverSetup);

MinecraftForge.EVENT_BUS.addListener(this::serverStartingEvent);
}

private void serverStartingEvent(FMLServerStartingEvent event)
{
SetFireCommand.register(event.getCommandDispatcher());
}

private void setup(final FMLCommonSetupEvent event) {
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/dev/mff/modtutorial/commands/SetFireCommand.java
@@ -0,0 +1,95 @@
package dev.mff.modtutorial.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.block.state.IBlockState;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.command.arguments.EntityArgument;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;

import java.util.Collection;

public class SetFireCommand
{

public static void register(CommandDispatcher<CommandSource> dispatcher)
{
dispatcher.register(
LiteralArgumentBuilder.<CommandSource>literal("setfire")
.requires(src -> src.hasPermissionLevel(2))
.then(
Commands.literal("entities")
.then(
Commands.argument("targets", EntityArgument.multipleEntities())
.executes(ctx -> setFireEntities(ctx.getSource(), EntityArgument.getEntitiesAllowingNone(ctx, "targets"), 5))
.then(
Commands.argument("duration", IntegerArgumentType.integer(0))
.executes(ctx -> setFireEntities(ctx.getSource(), EntityArgument.getEntitiesAllowingNone(ctx, "targets"), IntegerArgumentType.getInteger(ctx, "duration")))
)
)
)
.then(
Commands.literal("blocks")
.then(
Commands.argument("radius", IntegerArgumentType.integer(0))
.executes(ctx -> setFireBlocks(ctx.getSource(), IntegerArgumentType.getInteger(ctx, "radius")))
)
)
);
}

/**
* Met en feu les entités données pendant la durée donnée. Indique aussi au joueur que les entités ont été mises en
* feu.
* @param src La source de la commande
* @param targets Les entités à mettre en feu
* @param duration La durée du feu
* @return le nombre d'entités mises en feu
*/
private static int setFireEntities(CommandSource src, Collection<? extends Entity> targets, int duration)
{
targets.forEach(e -> e.setFire(duration));

src.sendFeedback(new TextComponentString(targets.size() + " entities burnt !"), false);

return targets.size();
}

/**
* Met les blocs autour de la source en feu
* @param src La source de la commande
* @param radius Le rayon d'action
* @return le nombre de blocs mis en feu
*/
private static int setFireBlocks(CommandSource src, int radius)
{
Vec3d srcPos = src.getPos();
World world = src.getWorld();
int count = 0;
for(int x = -radius; x < radius; x++)
{
for(int z = -radius; z < radius; z++)
{
BlockPos pos = new BlockPos(srcPos.x + x, srcPos.y, srcPos.z + z);
IBlockState state = world.getBlockState(pos);
if(state.getBlock() == Blocks.AIR)
{
world.setBlockState(pos, Blocks.FIRE.getDefaultState());
count++;
}
}
}

src.sendFeedback(new TextComponentString(count + " blocks set to fire !"), false);

return count;
}

}

0 comments on commit 7fd7163

Please sign in to comment.