Skip to content

Commit

Permalink
cameramode, and draw commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gnembon committed May 10, 2019
1 parent bddad16 commit 6970864
Show file tree
Hide file tree
Showing 8 changed files with 976 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/main/java/carpet/CarpetServer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package carpet;

//import carpet.commands.CameraModeCommand;
import carpet.commands.CameraModeCommand;
//import carpet.commands.CarpetCommand;
//import carpet.commands.CounterCommand;
//import carpet.commands.DistanceCommand;
//import carpet.commands.DrawCommand;
import carpet.commands.DrawCommand;
//import carpet.commands.ScriptCommand;
//import carpet.commands.InfoCommand;
//import carpet.commands.LogCommand;
Expand Down Expand Up @@ -56,11 +56,11 @@ public static void registerCarpetCommands(CommandDispatcher<ServerCommandSource>
//LogCommand.register(dispatcher);
//SpawnCommand.register(dispatcher);
//PlayerCommand.register(dispatcher);
//CameraModeCommand.register(dispatcher);
CameraModeCommand.register(dispatcher);
//InfoCommand.register(dispatcher);
//DistanceCommand.register(dispatcher);
//PerimeterInfoCommand.register(dispatcher);
//DrawCommand.register(dispatcher);
DrawCommand.register(dispatcher);
//ScriptCommand.register(dispatcher);

TestCommand.register(dispatcher);
Expand Down
736 changes: 736 additions & 0 deletions src/main/java/carpet/CarpetSettings.java

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions src/main/java/carpet/commands/CameraModeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package carpet.commands;

import carpet.CarpetSettings;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.command.arguments.EntityArgumentType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.world.GameMode;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

public class CameraModeCommand
{
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> camera = literal("c").
requires((player) -> CarpetSettings.getBool("commandCameramode")).
executes((c) -> cameraMode(c.getSource(), c.getSource().getPlayer())).
then(argument("player", EntityArgumentType.player()).
executes( (c) -> cameraMode(c.getSource(), EntityArgumentType.getPlayer(c, "player"))));

LiteralArgumentBuilder<ServerCommandSource> survival = literal("s").
requires((player) -> CarpetSettings.getBool("commandCameramode")).
executes((c) -> survivalMode(
c.getSource(),
c.getSource().getPlayer())).
then(argument("player", EntityArgumentType.player()).
executes( (c) -> survivalMode(c.getSource(), EntityArgumentType.getPlayer(c, "player"))));

dispatcher.register(camera);
dispatcher.register(survival);
}
private static boolean iCanHasPermissions(ServerCommandSource source, PlayerEntity player)
{
try
{
return source.hasPermissionLevel(2) || source.getPlayer() == player;
}
catch (CommandSyntaxException e)
{
return true; // shoudn't happen because server has all permissions anyways
}
}
private static int cameraMode(ServerCommandSource source, PlayerEntity player)
{
if (!(iCanHasPermissions(source, player))) return 0;
player.setGameMode(GameMode.SPECTATOR);
player.addPotionEffect(new StatusEffectInstance(StatusEffects.NIGHT_VISION, 999999, 0, false, false));
player.addPotionEffect(new StatusEffectInstance(StatusEffects.CONDUIT_POWER, 999999, 0, false, false));
return 1;
}
private static int survivalMode(ServerCommandSource source, PlayerEntity player)
{
if (!(iCanHasPermissions(source, player))) return 0;
player.setGameMode(GameMode.SURVIVAL);
player.removePotionEffect(StatusEffects.NIGHT_VISION);
player.removePotionEffect(StatusEffects.CONDUIT_POWER);
return 1;
}

}
165 changes: 165 additions & 0 deletions src/main/java/carpet/commands/DrawCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package carpet.commands;

import carpet.CarpetSettings;
import carpet.utils.Messenger;
import com.google.common.collect.Lists;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.block.Block;
import net.minecraft.block.pattern.CachedBlockPosition;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.command.arguments.BlockPosArgumentType;
import net.minecraft.command.arguments.BlockPredicateArgumentType;
import net.minecraft.command.arguments.BlockStateArgument;
import net.minecraft.command.arguments.BlockStateArgumentType;
import net.minecraft.inventory.Inventory;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.server.world.ServerWorld;

import java.util.List;
import java.util.function.Predicate;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

public class DrawCommand
{
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> command = literal("draw").
requires((player) -> CarpetSettings.getBool("commandDraw")).
then(literal("sphere").
then(argument("center",BlockPosArgumentType.create()).
then(argument("radius",IntegerArgumentType.integer(1)).
then(argument("block",BlockStateArgumentType.create()).
executes((c)-> drawCircle(
c.getSource(),
BlockPosArgumentType.getBlockPos(c, "center"),
IntegerArgumentType.getInteger(c, "radius"),
BlockStateArgumentType.getBlockState (c,"block"),
null
)
).
then(literal("replace").
then(argument("filter",BlockPredicateArgumentType.create())
.executes((c)-> drawCircle(
c.getSource(),
BlockPosArgumentType.getBlockPos(c, "center"),
IntegerArgumentType.getInteger(c, "radius"),
BlockStateArgumentType.getBlockState(c,"block"),
BlockPredicateArgumentType.getBlockPredicate(c,
"filter")
))))))));
dispatcher.register(command);
}


private static int drawCircle(ServerCommandSource source, BlockPos pos, int radius, BlockStateArgument block,
Predicate<CachedBlockPosition> replacement)
{
return drawCircle(source, pos, (double)radius, (double)radius, (double)radius, block, replacement, false);
}
private static int drawCircle(ServerCommandSource source, BlockPos pos, double radiusX, double radiusY, double radiusZ,
BlockStateArgument block, Predicate<CachedBlockPosition> replacement, boolean solid)
{
int affected = 0;
ServerWorld world = source.getWorld();

radiusX += 0.5;
radiusY += 0.5;
radiusZ += 0.5;

final double invRadiusX = 1 / radiusX;
final double invRadiusY = 1 / radiusY;
final double invRadiusZ = 1 / radiusZ;

final int ceilRadiusX = (int) Math.ceil(radiusX);
final int ceilRadiusY = (int) Math.ceil(radiusY);
final int ceilRadiusZ = (int) Math.ceil(radiusZ);

BlockPos.Mutable mbpos = new BlockPos.Mutable(pos);
List<BlockPos> list = Lists.newArrayList();

double nextXn = 0;

forX: for (int x = 0; x <= ceilRadiusX; ++x) {
final double xn = nextXn;
nextXn = (x + 1) * invRadiusX;
double nextYn = 0;
forY: for (int y = 0; y <= ceilRadiusY; ++y) {
final double yn = nextYn;
nextYn = (y + 1) * invRadiusY;
double nextZn = 0;
forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {
final double zn = nextZn;
nextZn = (z + 1) * invRadiusZ;

double distanceSq = lengthSq(xn, yn, zn);
if (distanceSq > 1) {
if (z == 0) {
if (y == 0) {
break forX;
}
break forY;
}
break forZ;
}

if (!solid) {
if (lengthSq(nextXn, yn, zn) <= 1 && lengthSq(xn, nextYn, zn) <= 1 && lengthSq(xn, yn, nextZn) <= 1) {
continue;
}
}

for (int xmod = -1; xmod < 2; xmod+= 2)
{
for (int ymod = -1; ymod < 2; ymod += 2)
{
for (int zmod = -1; zmod < 2; zmod += 2)
{
mbpos.set(pos.getX()+xmod*x, pos.getY()+ymod*y, pos.getZ()+zmod*z);
if (replacement == null || replacement.test(
new CachedBlockPosition( world, mbpos, true)))
{
BlockEntity tileentity = world.getBlockEntity(mbpos);
if (tileentity instanceof Inventory)
{
((Inventory)tileentity).clear();
}

if (block.setBlockState(
world,
mbpos,
2 | (CarpetSettings.getBool("fillUpdates") ?0:1024)
))
{
list.add(mbpos.toImmutable());
++affected;
}
}
}
}
}
}
}
}
if (CarpetSettings.getBool("fillUpdates"))
{

for (BlockPos blockpos1 : list)
{
Block blokc = world.getBlockState(blockpos1).getBlock();
world.updateNeighbors(blockpos1, blokc); // or always version????
}
}
Messenger.m(source, "gi Filled "+affected+" blocks");

return 1;
}
private static double lengthSq(double x, double y, double z)
{
return (x * x) + (y * y) + (z * z);
}
}
4 changes: 2 additions & 2 deletions src/main/java/carpet/utils/Messenger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import net.minecraft.ChatFormat;
import net.minecraft.network.chat.BaseComponent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.chat.ClickEvent;// text.event.ClickEvent;
import net.minecraft.network.chat.HoverEvent;// text.event.HoverEvent;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.HoverEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down
14 changes: 0 additions & 14 deletions src/main/java/net/fabricmc/example/ExampleMod.java

This file was deleted.

15 changes: 0 additions & 15 deletions src/main/java/net/fabricmc/example/mixin/ExampleMixin.java

This file was deleted.

7 changes: 3 additions & 4 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
"id": "modid",
"version": "${version}",

"name": "Example Mod",
"name": "Carpet Mod in Fabric",
"description": "This is an example description! Tell everyone what your mod is about!",
"authors": [
"Me!"
],
"contact": {
"homepage": "https://fabricmc.net/",
"sources": "https://github.com/FabricMC/fabric-example-mod"
"sources": "https://github.com/gnembon/fabric-carpet"
},

"license": "CC0-1.0",
"license": "GNU LGPL V3",
"icon": "assets/modid/icon.png",

"environment": "*",
"entrypoints": {
"main": [
"net.fabricmc.example.ExampleMod"
]
},
"mixins": [
Expand Down

0 comments on commit 6970864

Please sign in to comment.