diff --git a/src/main/java/io/singularitynet/Client/ClientStateMachine.java b/src/main/java/io/singularitynet/Client/ClientStateMachine.java index eaaa2f7..5d7f95a 100755 --- a/src/main/java/io/singularitynet/Client/ClientStateMachine.java +++ b/src/main/java/io/singularitynet/Client/ClientStateMachine.java @@ -25,6 +25,7 @@ import io.singularitynet.MissionHandlerInterfaces.IVideoProducer; import io.singularitynet.MissionHandlerInterfaces.IWantToQuit; import io.singularitynet.MissionHandlerInterfaces.IWorldGenerator; +import io.singularitynet.MissionHandlers.DrawImplementation; import io.singularitynet.MissionHandlers.MissionBehaviour; import io.singularitynet.MissionHandlers.MultidimensionalReward; import io.singularitynet.Server.VereyaModServer; @@ -1084,6 +1085,11 @@ protected void execute() if (MinecraftClient.getInstance().getServer() != null) { MinecraftClient.getInstance().getServer().setOnlineMode(false); } + // draw blocks and entities from DrawingDecorator section + List worldDecorator = ClientStateMachine.this.currentMissionInit.getMission().getServerSection().getServerHandlers().getWorldDecorators(); + if (!worldDecorator.isEmpty()){ + DrawImplementation.draw(worldDecorator); + } } else { diff --git a/src/main/java/io/singularitynet/MissionHandlers/DrawImplementation.java b/src/main/java/io/singularitynet/MissionHandlers/DrawImplementation.java new file mode 100644 index 0000000..b879698 --- /dev/null +++ b/src/main/java/io/singularitynet/MissionHandlers/DrawImplementation.java @@ -0,0 +1,127 @@ +package io.singularitynet.MissionHandlers; + +import io.singularitynet.projectmalmo.*; +import jakarta.xml.bind.JAXBElement; +import net.minecraft.client.MinecraftClient; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import org.apache.logging.log4j.LogManager; + +import java.util.List; + +public class DrawImplementation extends DrawingDecorator { + public static void draw(List worldDecorator) { + //get command manager and command source + CommandManager commandManager = null; + ServerCommandSource commandSource= null; + try { + MinecraftClient client = MinecraftClient.getInstance(); + commandManager = client.getServer().getCommandManager(); + commandSource = client.getServer().getCommandSource(); + } catch (Exception e) { + LogManager.getLogger().error("Failed to get command manager and command source"); + LogManager.getLogger().error(e); + } + try { + for (Object obj : worldDecorator){ + List> drawObjects = null; + if (obj instanceof DrawingDecorator){ + drawObjects = ((DrawingDecorator)obj).getDrawObjectType(); + } + for (JAXBElement drawObject : drawObjects){ + switch (drawObject.getValue()) { + case DrawBlock db -> drawBlock(db, commandManager, commandSource); + case DrawCuboid dc -> drawCuboid(dc, commandManager, commandSource); + case DrawItem di -> drawItem(di, commandManager, commandSource); + case DrawLine dl -> drawLine(dl, commandManager, commandSource); +// case DrawSphere ds -> drawSphere(ds, commandManager, commandSource); + default -> LogManager.getLogger().warn("The type " + drawObject.getClass().getSimpleName() + " is not supported"); + } + } + } + } catch (Exception e) { + LogManager.getLogger().error("Failed to draw objects"); + LogManager.getLogger().error(e); + } + } + + public static void drawBlock(DrawBlock val, CommandManager commandManager, ServerCommandSource commandSource){ + try { + String command = "/setblock " + val.getX() + " " + + val.getY() + " " + + val.getZ() + " " + + val.getType().value(); + commandManager.executeWithPrefix(commandSource, command); + } catch (Exception e) { + LogManager.getLogger().error("Failed to draw the block"); + LogManager.getLogger().error(e); + } + } + + public static void drawCuboid(DrawCuboid val, CommandManager commandManager, ServerCommandSource commandSource){ + try { + String command = "/fill " + val.getX1() + " " + + val.getY1() + " " + + val.getZ1() + " " + + val.getX2() + " " + + val.getY2() + " " + + val.getZ2() + " " + + val.getType().value(); + commandManager.executeWithPrefix(commandSource, command); + } catch (Exception e) { + LogManager.getLogger().error("Failed to draw the cuboid"); + LogManager.getLogger().error(e); + } + } + + public static void drawItem(DrawItem val, CommandManager commandManager, ServerCommandSource commandSource){ + try { + String command = "/summon item " + val.getX() + " " + + val.getY() + " " + + val.getZ() + " " + + "{Item:{id:" + val.getType() + ",Count:1}}"; + commandManager.executeWithPrefix(commandSource, command); + } catch (Exception e) { + LogManager.getLogger().error("Failed to draw the item"); + LogManager.getLogger().error(e); + } + } + + public static void drawLine(DrawLine val, CommandManager commandManager, ServerCommandSource commandSource){ + try { + int dx = val.getX2() - val.getX1(); + int dy = val.getY2() - val.getY1(); + int dz = val.getZ2() - val.getZ1(); + + int steps = Math.max(Math.abs(dx), Math.max(Math.abs(dy), Math.abs(dz))); + + double xInc = (double)dx / steps; + double yInc = (double)dy / steps; + double zInc = (double)dz / steps; + + for (int i = 0; i <= steps; i++){ + int x = (int)Math.round(val.getX1() + i * xInc); + int y = (int)Math.round(val.getY1() + i * yInc); + int z = (int)Math.round(val.getZ1() + i * zInc); + setBlock(x, y, z, val.getType().value(), commandManager, commandSource); + } + + } catch (Exception e) { + LogManager.getLogger().error("Failed to draw the line"); + LogManager.getLogger().error(e); + } + } + + protected static void setBlock(int x, int y, int z, String type, CommandManager commandManager, ServerCommandSource commandSource){ + try { + String command = "/setblock " + x + " " + + y + " " + + z + " " + + type; + commandManager.executeWithPrefix(commandSource, command); + } catch (Exception e) { + LogManager.getLogger().error("Failed to set the block"); + LogManager.getLogger().error(e); + } + } +} \ No newline at end of file