Skip to content

Commit

Permalink
Updated to 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Aug 26, 2023
1 parent c6ef7e0 commit f258861
Show file tree
Hide file tree
Showing 14 changed files with 418 additions and 420 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'fabric-loom' version '0.13-SNAPSHOT'
id 'maven-publish'
}

Expand Down
File renamed without changes.
12 changes: 7 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.18.2
yarn_mappings=1.18+build.1
loader_version=0.14.7
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.14.22

#Fabric api

# Mod Properties
mod_version = 1.0.0
mod_version = 1.1.0
maven_group = com.hashicraft
archives_base_name = fabric-mod-minecraft-api

# Dependencies
fabric_version=0.58.0+1.18.2
fabric_version=0.87.0+1.20.1
javalin_version=5.1.2
49 changes: 24 additions & 25 deletions src/main/java/com/hashicraft/minecraftapi/APIMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,45 @@
import com.hashicraft.minecraftapi.server.Server;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.LiteralText;
import net.minecraft.util.math.Vec2f;
import net.minecraft.text.Text;
import net.minecraft.util.math.Vec3d;
import static net.minecraft.server.command.CommandManager.*;

public class APIMod implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("modid");
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("modid");
public static final Server apiServer = new Server();

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.

LOGGER.info("Hello Fabric world!");
LOGGER.info("Hello Fabric world!");

// Register the postion command
CommandRegistrationCallback.EVENT.register((dispatcher, environment) ->
dispatcher.register(
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(
literal("position").executes(context -> {
Vec3d pos = context.getSource().getPosition();
String facing = context.getSource().getEntityOrThrow().getHorizontalFacing().toString();
context.getSource().sendFeedback(
new LiteralText(
String.format("Your position is: '%d %d %d', and you are facing: '%s'", Math.round(pos.getX()), Math.round(pos.getY()), Math.round(pos.getZ()), facing))
, false);
String facing = context.getSource().getEntityOrThrow().getHorizontalFacing().toString();

context.getSource().sendFeedback(() -> Text.of(
String.format("Your position is: '%d %d %d', and you are facing: '%s'", Math.round(pos.getX()),
Math.round(pos.getY()), Math.round(pos.getZ()), facing)),
false);

return 1;
}))
);
})));

ServerLifecycleEvents.SERVER_STARTED.register((MinecraftServer server) -> {
ServerLifecycleEvents.SERVER_STARTED.register((MinecraftServer server) -> {
// start the server
apiServer.start(server);
});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,52 @@
import io.javalin.openapi.OpenApiSecurity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;

public class BlockDELETE implements Handler {

private final Logger LOGGER = LoggerFactory.getLogger("server");
private final Logger LOGGER = LoggerFactory.getLogger("server");
private final ServerWorld world;

public BlockDELETE(ServerWorld world) {
this.world = world;
}

@OpenApi(
path = "/v1/block/{x}/{y}/{z}",
methods = HttpMethod.DELETE,
summary = "Delete a single block",
description = "Deletes a block at the given location, if the block does not exist, an status code 404 is returned",
operationId = "deleteSingleBlock",
tags = {"Block"},
security = {
@OpenApiSecurity(name = "ApiKeyAuth")
},
pathParams = {
@OpenApiParam(name = "x", example = "12", required = true),
@OpenApiParam(name = "y", example = "13", required = true),
@OpenApiParam(name = "z", example = "14", required = true)
},
responses = {
@OpenApi(path = "/v1/block/{x}/{y}/{z}", methods = HttpMethod.DELETE, summary = "Delete a single block", description = "Deletes a block at the given location, if the block does not exist, an status code 404 is returned", operationId = "deleteSingleBlock", tags = {
"Block" }, security = {
@OpenApiSecurity(name = "ApiKeyAuth")
}, pathParams = {
@OpenApiParam(name = "x", example = "12", required = true),
@OpenApiParam(name = "y", example = "13", required = true),
@OpenApiParam(name = "z", example = "14", required = true)
}, responses = {
@OpenApiResponse(status = "200", description = "Block created successfully"),
@OpenApiResponse(status = "404", description = "Block does not exist")
}
)
})
public void handle(Context ctx) throws Exception {
int x = Integer.parseInt(ctx.pathParam("x"));
int y = Integer.parseInt(ctx.pathParam("y"));
int z = Integer.parseInt(ctx.pathParam("z"));

LOGGER.info("Block DELETE called x:{}, y:{}, z:{}",x,y,z);
int x = Integer.parseInt(ctx.pathParam("x"));
int y = Integer.parseInt(ctx.pathParam("y"));
int z = Integer.parseInt(ctx.pathParam("z"));

BlockPos pos = new BlockPos(x,y,z);
String material = Util.getIdentifierAtPosition(world, pos);
LOGGER.info("Block DELETE called x:{}, y:{}, z:{}", x, y, z);

// if air no block
if (material.contains("minecraft:air") || material.isBlank()) {
LOGGER.info(String.format("404 block does not exist at x:{}, y:{} z:{}", x,y,z));
Vec3i pos = new Vec3i(x, y, z);
String material = Util.getIdentifierAtPosition(world, pos);

ctx.res().sendError(404, "Block not found");
return;
}
// if air no block
if (material.contains("minecraft:air") || material.isBlank()) {
LOGGER.info(String.format("404 block does not exist at x:{}, y:{} z:{}", x, y, z));

boolean didBreak = world.breakBlock(new BlockPos(x,y,z),false);
ctx.res().sendError(404, "Block not found");
return;
}

if (!didBreak) {
LOGGER.error("Unable to delete block {} at {},{},{} the minecraft server refused to perform this operation", material, x,y,z);
boolean didBreak = Util.BreakBlock(pos, world);
if (!didBreak) {
LOGGER.error("Unable to delete block {} at {},{},{} the minecraft server refused to perform this operation",
material, x, y, z);

ctx.res().sendError(500,"Unable to place block");
}
ctx.res().sendError(500, "Unable to place block");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;

public class BlockGET implements Handler {

Expand Down Expand Up @@ -55,12 +56,10 @@ public void handle(Context ctx) throws Exception {
int z = Integer.parseInt(ctx.pathParam("z"));

LOGGER.info("Blocks GET called x:{}, y:{}, z:{}",x,y,z);

BlockPos pos = new BlockPos(x,y,z);
BlockState state = world.getBlockState(pos);
Vec3i pos = new Vec3i(x, y, z);
BlockState state = Util.GetBlockState(pos, world);

String material = Util.getIdentifierAtPosition(world, pos);

if (material.contains("minecraft:air") || material.isBlank()) {
LOGGER.info(String.format("404 block does not exist at x:{}, y:{} z:{}", x,y,z));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hashicraft.minecraftapi.server.handlers.block;

import com.hashicraft.minecraftapi.server.models.Block;
import com.hashicraft.minecraftapi.server.util.Util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,14 +16,8 @@
import io.javalin.openapi.OpenApiRequestBody;
import io.javalin.openapi.OpenApiResponse;
import io.javalin.openapi.OpenApiSecurity;
import net.minecraft.block.BlockState;
import net.minecraft.block.enums.BlockHalf;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.property.Properties;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry;

public class BlockPOST implements Handler {

Expand Down Expand Up @@ -53,52 +48,8 @@ public void handle(Context ctx) throws Exception {

LOGGER.info("Block POST called x:{}, y:{}, z:{} type:{}",block.getX(),block.getY(),block.getZ(),block.getMaterial());

var item = Registry.BLOCK.get(new Identifier(block.getMaterial()));
if (item==null) {
LOGGER.error("500 Unable to create block, material '{}' does not exist", block.getMaterial());

ctx.res().sendError(500,"Unable to create block " + block.getMaterial() + " material does not exist");
return;
}

BlockState state = item.getDefaultState();

switch(block.getFacing()) {
case "north":
state = state.with(Properties.HORIZONTAL_FACING, Direction.NORTH);
break;
case "south":
state = state.with(Properties.HORIZONTAL_FACING, Direction.SOUTH);
break;
case "east":
state = state.with(Properties.HORIZONTAL_FACING, Direction.EAST);
break;
case "west":
state = state.with(Properties.HORIZONTAL_FACING, Direction.WEST);
break;
}

switch(block.getHalf()) {
case "top":
state = state.with(Properties.BLOCK_HALF, BlockHalf.TOP);
break;
case "bottom":
state = state.with(Properties.BLOCK_HALF, BlockHalf.BOTTOM);
break;
}

if (block.getRotation() > -1 ) {
state = state.with(Properties.ROTATION, block.getRotation());
}

BlockPos pos = new BlockPos(block.getX(), block.getY(), block.getZ());
boolean didSet = world.setBlockState(pos, state);

if (!didSet) {
LOGGER.error("500 Unable to place block {} at {},{},{}", block.getMaterial(), block.getX(), block.getY(), block.getZ());

ctx.res().sendError(500,"Unable to place block");
}
BlockPos pos = new BlockPos(block.getX(),block.getY(),block.getZ());
Util.SetBlock(pos, block, block.getRotation(), world);

String id = Base64.getEncoder().withoutPadding().encodeToString(String.format("%s/%s/%s", block.getX(), block.getY(),block.getZ()).getBytes());
block.setID(id);
Expand Down
Loading

0 comments on commit f258861

Please sign in to comment.