-
Notifications
You must be signed in to change notification settings - Fork 9
Add the rendershape command #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haykam821
wants to merge
1
commit into
NucleoidMC:1.16
Choose a base branch
from
haykam821:rendershape-command
base: 1.16
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| package xyz.nucleoid.spleef; | ||
|
|
||
| import net.fabricmc.api.ModInitializer; | ||
| import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; | ||
| import net.minecraft.command.argument.ArgumentTypes; | ||
| import net.minecraft.command.argument.serialize.ConstantArgumentSerializer; | ||
| import net.minecraft.util.Identifier; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import xyz.nucleoid.plasmid.game.GameType; | ||
| import xyz.nucleoid.spleef.command.MapShapeRendererArgumentType; | ||
| import xyz.nucleoid.spleef.command.RenderShapeCommand; | ||
| import xyz.nucleoid.spleef.game.SpleefConfig; | ||
| import xyz.nucleoid.spleef.game.SpleefWaiting; | ||
| import xyz.nucleoid.spleef.game.map.shape.renderer.*; | ||
|
|
@@ -24,5 +30,10 @@ public void onInitialize() { | |
| MapShapeRenderer.REGISTRY.register(new Identifier(Spleef.ID, "circle"), CircleShapeRenderer.CODEC); | ||
| MapShapeRenderer.REGISTRY.register(new Identifier(Spleef.ID, "square"), SquareShapeRenderer.CODEC); | ||
| MapShapeRenderer.REGISTRY.register(new Identifier(Spleef.ID, "pattern"), PatternShapeRenderer.CODEC); | ||
|
|
||
| ArgumentTypes.register("spleef:map_shape_renderer", MapShapeRendererArgumentType.class, new ConstantArgumentSerializer<MapShapeRendererArgumentType>(MapShapeRendererArgumentType::mapShapeRenderer)); | ||
| CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably only register this in dev environment |
||
| RenderShapeCommand.register(dispatcher); | ||
| }); | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/main/java/xyz/nucleoid/spleef/command/MapShapeRendererArgumentType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package xyz.nucleoid.spleef.command; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
||
| import com.mojang.brigadier.StringReader; | ||
| import com.mojang.brigadier.arguments.ArgumentType; | ||
| import com.mojang.brigadier.context.CommandContext; | ||
| import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
| import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; | ||
| import com.mojang.serialization.DataResult; | ||
| import com.mojang.serialization.DataResult.PartialResult; | ||
|
|
||
| import net.minecraft.command.argument.NbtTagArgumentType; | ||
| import net.minecraft.nbt.NbtOps; | ||
| import net.minecraft.nbt.Tag; | ||
| import net.minecraft.server.command.ServerCommandSource; | ||
| import net.minecraft.text.LiteralText; | ||
| import xyz.nucleoid.spleef.game.map.shape.renderer.MapShapeRenderer; | ||
|
|
||
| public class MapShapeRendererArgumentType implements ArgumentType<MapShapeRenderer> { | ||
| private static final Collection<String> EXAMPLES = Arrays.asList("{type='spleef:circle',radius=16}"); | ||
|
|
||
| public static MapShapeRendererArgumentType mapShapeRenderer() { | ||
| return new MapShapeRendererArgumentType(); | ||
| } | ||
|
|
||
| public static MapShapeRenderer getMapShapeRenderer(CommandContext<ServerCommandSource> context, String name) { | ||
| return context.getArgument(name, MapShapeRenderer.class); | ||
| } | ||
|
|
||
| @Override | ||
| public MapShapeRenderer parse(StringReader reader) throws CommandSyntaxException { | ||
| Tag tag = NbtTagArgumentType.nbtTag().parse(reader); | ||
|
|
||
| DataResult<MapShapeRenderer> result = MapShapeRenderer.REGISTRY_CODEC.codec().parse(NbtOps.INSTANCE, tag); | ||
| if (result.error().isPresent()) { | ||
| PartialResult<MapShapeRenderer> partial = result.error().get(); | ||
| throw new SimpleCommandExceptionType(new LiteralText(partial.message())).create(); | ||
| } | ||
| return result.result().get(); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> getExamples() { | ||
| return EXAMPLES; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
src/main/java/xyz/nucleoid/spleef/command/RenderShapeCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package xyz.nucleoid.spleef.command; | ||
|
|
||
| import com.mojang.brigadier.CommandDispatcher; | ||
| import com.mojang.brigadier.context.CommandContext; | ||
|
|
||
| import net.minecraft.block.Blocks; | ||
| import net.minecraft.server.command.CommandManager; | ||
| import net.minecraft.server.command.ServerCommandSource; | ||
| import net.minecraft.util.math.Vec3d; | ||
| import net.minecraft.world.gen.stateprovider.BlockStateProvider; | ||
| import net.minecraft.world.gen.stateprovider.SimpleBlockStateProvider; | ||
| import xyz.nucleoid.spleef.game.map.shape.ShapeCanvas; | ||
| import xyz.nucleoid.spleef.game.map.shape.ShapePlacer; | ||
| import xyz.nucleoid.spleef.game.map.shape.SpleefShape; | ||
| import xyz.nucleoid.spleef.game.map.shape.WorldShapePlacer; | ||
| import xyz.nucleoid.spleef.game.map.shape.renderer.MapShapeRenderer; | ||
|
|
||
| public class RenderShapeCommand { | ||
| private static final BlockStateProvider FILL_PROVIDER = new SimpleBlockStateProvider(Blocks.WHITE_CONCRETE.getDefaultState()); | ||
| private static final BlockStateProvider OUTLINE_PROVIDER = new SimpleBlockStateProvider(Blocks.BLACK_CONCRETE.getDefaultState()); | ||
|
|
||
| public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { | ||
| dispatcher.register(CommandManager | ||
| .literal("rendershape") | ||
| .then(CommandManager.argument("shape", MapShapeRendererArgumentType.mapShapeRenderer()) | ||
| .executes(RenderShapeCommand::execute))); | ||
| } | ||
|
|
||
| private static int execute(CommandContext<ServerCommandSource> context) { | ||
|
|
||
| ShapeCanvas canvas = new ShapeCanvas(); | ||
|
|
||
| MapShapeRenderer renderer = MapShapeRendererArgumentType.getMapShapeRenderer(context, "shape"); | ||
| renderer.renderTo(canvas); | ||
|
|
||
| SpleefShape shape = canvas.render(); | ||
|
|
||
| Vec3d pos = context.getSource().getPosition(); | ||
| int x = (int) pos.getX(); | ||
| int y = (int) pos.getY() - 1; | ||
| int z = (int) pos.getZ(); | ||
|
|
||
| ShapePlacer fill = new WorldShapePlacer(x, z, context.getSource().getWorld(), FILL_PROVIDER); | ||
| fill.fill(shape, y, y); | ||
|
|
||
| ShapePlacer outline = new WorldShapePlacer(x, z, context.getSource().getWorld(), OUTLINE_PROVIDER); | ||
| outline.outline(shape, y, y); | ||
|
|
||
| return 1; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/xyz/nucleoid/spleef/game/map/shape/CustomOffsetShapePlacer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package xyz.nucleoid.spleef.game.map.shape; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| import net.minecraft.world.gen.stateprovider.BlockStateProvider; | ||
|
|
||
| public abstract class CustomOffsetShapePlacer extends ShapePlacer { | ||
| private final int offsetX; | ||
| private final int offsetZ; | ||
|
|
||
| public CustomOffsetShapePlacer(int offsetX, int offsetZ, BlockStateProvider provider, Random random) { | ||
| super(provider, random); | ||
|
|
||
| this.offsetX = offsetX; | ||
| this.offsetZ = offsetZ; | ||
| } | ||
|
|
||
| @Override | ||
| public int getOffsetX() { | ||
| return this.offsetX; | ||
| } | ||
|
|
||
| @Override | ||
| public int getOffsetZ() { | ||
| return this.offsetZ; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/main/java/xyz/nucleoid/spleef/game/map/shape/MapShapePlacer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package xyz.nucleoid.spleef.game.map.shape; | ||
|
|
||
| import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; | ||
| import net.minecraft.block.BlockState; | ||
| import net.minecraft.util.math.BlockPos; | ||
| import net.minecraft.world.gen.stateprovider.BlockStateProvider; | ||
| import xyz.nucleoid.plasmid.map.template.MapTemplate; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.Set; | ||
|
|
||
| public final class MapShapePlacer extends ShapePlacer { | ||
| private final MapTemplate template; | ||
| private final Set<BlockState> usedStates = new ReferenceOpenHashSet<>(); | ||
|
|
||
| public MapShapePlacer(MapTemplate template, BlockStateProvider provider, Random random) { | ||
| super(provider, random); | ||
| this.template = template; | ||
| } | ||
|
|
||
| @Override | ||
| public void set(BlockPos pos) { | ||
| BlockState state = this.provider.getBlockState(this.random, pos); | ||
| this.usedStates.add(state); | ||
|
|
||
| this.template.setBlockState(pos, state); | ||
| } | ||
|
|
||
| public Set<BlockState> getUsedStates() { | ||
| return this.usedStates; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/xyz/nucleoid/spleef/game/map/shape/WorldShapePlacer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package xyz.nucleoid.spleef.game.map.shape; | ||
|
|
||
| import net.minecraft.block.BlockState; | ||
| import net.minecraft.util.math.BlockPos; | ||
| import net.minecraft.world.World; | ||
| import net.minecraft.world.gen.stateprovider.BlockStateProvider; | ||
|
|
||
| public final class WorldShapePlacer extends CustomOffsetShapePlacer { | ||
| private final World world; | ||
|
|
||
| public WorldShapePlacer(int offsetX, int offsetZ, World world, BlockStateProvider provider) { | ||
| super(offsetX, offsetZ, provider, world.getRandom()); | ||
| this.world = world; | ||
| } | ||
|
|
||
| @Override | ||
| public void set(BlockPos pos) { | ||
| BlockState state = this.provider.getBlockState(this.random, pos); | ||
| this.world.setBlockState(pos, state); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registering argument types breaks compatibility with vanilla clients I am fairly sure