From bdbc3ac184f7b7fce721743abfa7424e5b1f1309 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 4 Jan 2020 02:35:03 +0100 Subject: [PATCH] Use the API & create exporter MC plugin Getting a StackOverflowError at the moment --- GCMC/CubePlacerEngine.cs | 112 +----------------- GCMC/FactoryObtainerPatch.cs | 31 ----- GCMC/GCMC.csproj | 3 + GCMCPlugin/pom.xml | 49 ++++++++ .../java/io/github/norbipeti/gcmc/Blocks.java | 14 +++ .../io/github/norbipeti/gcmc/PluginMain.java | 73 ++++++++++++ GCMCPlugin/src/main/resources/plugin.yml | 6 + 7 files changed, 149 insertions(+), 139 deletions(-) delete mode 100644 GCMC/FactoryObtainerPatch.cs create mode 100644 GCMCPlugin/pom.xml create mode 100644 GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/Blocks.java create mode 100644 GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/PluginMain.java create mode 100644 GCMCPlugin/src/main/resources/plugin.yml diff --git a/GCMC/CubePlacerEngine.cs b/GCMC/CubePlacerEngine.cs index 36aca7f..e5a6953 100644 --- a/GCMC/CubePlacerEngine.cs +++ b/GCMC/CubePlacerEngine.cs @@ -1,5 +1,6 @@ using System; using DataLoader; +using GamecraftModdingAPI.Blocks; using RobocraftX.Blocks; using RobocraftX.Blocks.Ghost; using RobocraftX.Blocks.Scaling; @@ -27,11 +28,9 @@ namespace GCMC } public IEntitiesDB entitiesDB { get; set; } - internal static BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine private void ImportWorld(string name) { - PlaceBlock(0, BlockColors.Default, 0, new float3(0, 0, 0), 5, 1, 1, 1, 0); } private void PlaceBlock(string args) @@ -47,8 +46,10 @@ namespace GCMC float scaleX = float.Parse(s[7]); float scaleY = float.Parse(s[8]); float scaleZ = float.Parse(s[9]); + float rotX = float.Parse(s[10]); uint playerId = 0; - PlaceBlock(block, (BlockColors) color, darkness, new float3(x, y, z), scale, scaleX, scaleY, scaleZ, playerId); + Placement.PlaceBlock((BlockIDs) block, new float3(x, y, z), new quaternion(rotX, 0, 0, 1), + (BlockColors) color, darkness, scale, new float3(scaleX, scaleY, scaleZ), playerId); } catch (Exception e) { @@ -57,116 +58,11 @@ namespace GCMC } } - /// - /// Places a block at the given position - /// - /// The block's type - /// The block's color - /// The block color's darkness - 0 is default color - /// The block's position - default block size is 0.2 - /// The block's uniform scale - default scale is 1 (with 0.2 width) - /// The block's non-uniform scale - less than 1 means is used - /// The block's non-uniform scale - less than 1 means is used - /// The block's non-uniform scale - less than 1 means is used - /// The player who placed the block - /// - private void PlaceBlock(ushort block, BlockColors color, byte darkness, float3 position, int scale, float scaleX, float scaleY, float scaleZ, uint playerId) - { - try - { - if (darkness > 9) throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)"); - BuildBlock(block, (byte)color, position, scale, scaleX, scaleY, scaleZ).Init(new BlockPlacementInfoStruct() - { - loadedFromDisk = false, - placedBy = playerId - }); - } - catch (Exception e) - { - Console.WriteLine(e); - Log.Error(e.Message); - } - } - - private EntityStructInitializer BuildBlock(ushort block, byte color, float3 position, int scale, float scaleX, float scaleY, float scaleZ) - { - if (_blockEntityFactory == null) - throw new Exception("The factory is null."); - if (scale == 0) - throw new Exception("Scale needs to be at least 1"); - if (Math.Abs(scaleX) < 1) scaleX = scale; - if (Math.Abs(scaleY) < 1) scaleY = scale; - if (Math.Abs(scaleZ) < 1) scaleZ = scale; - //RobocraftX.CR.MachineEditing.PlaceBlockEngine - ScalingEntityStruct scaling = new ScalingEntityStruct {scale = new float3(scaleX, scaleY, scaleZ)}; - RotationEntityStruct rotation = new RotationEntityStruct {rotation = quaternion.identity}; - GridRotationStruct gridRotation = new GridRotationStruct - {position = float3.zero, rotation = quaternion.identity}; - CubeCategoryStruct category = new CubeCategoryStruct - {category = CubeCategory.General, type = CubeType.Block}; - uint dbid = block; - DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid}; - uint num = PrefabsID.DBIDMAP[dbid]; - GFXPrefabEntityStructGO gfx = new GFXPrefabEntityStructGO {prefabID = num}; - BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct - { - blockPlacementHeight = scale, blockPlacementWidth = scale, desiredScaleFactor = scale, snapGridScale = scale, - unitSnapOffset = 0, isUsingUnitSize = true - }; - EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color}; - EGID egid2; - switch (category.category) - { - case CubeCategory.SpawnPoint: - case CubeCategory.BuildingSpawnPoint: - egid2 = MachineEditingGroups.NewSpawnPointBlockID; - break; - default: - egid2 = MachineEditingGroups.NewBlockID; - break; - } - - int cubeId = PrefabsID.GenerateDBID((ushort) category.category, block); - EntityStructInitializer structInitializer = _blockEntityFactory.Build(egid2, (uint) cubeId); //The ghost block index is only used for triggers - if (colour.indexInPalette != byte.MaxValue) - structInitializer.Init(new ColourParameterEntityStruct - { - indexInPalette = colour.indexInPalette - }); - structInitializer.Init(new GFXPrefabEntityStructGPUI(gfx.prefabID)); - structInitializer.Init(new PhysicsPrefabEntityStruct(gfx.prefabID)); - structInitializer.Init(dbEntity); - structInitializer.Init(new PositionEntityStruct {position = position}); - structInitializer.Init(rotation); - structInitializer.Init(scaling); - structInitializer.Init(gridRotation); - structInitializer.Init(new UniformBlockScaleEntityStruct - { - scaleFactor = placementScale.desiredScaleFactor - }); - return structInitializer; - } - public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility, in PlayerInput[] playerInputs) { return new JobHandle(); } public string name { get; } = "Cube placer engine"; - - enum BlockColors - { - Default = byte.MaxValue, - White = 0, - Pink, - Purple, - Blue, - Aqua, - Green, - Lime, - Yellow, - Orange, - Red - } } } \ No newline at end of file diff --git a/GCMC/FactoryObtainerPatch.cs b/GCMC/FactoryObtainerPatch.cs deleted file mode 100644 index aec6b75..0000000 --- a/GCMC/FactoryObtainerPatch.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Reflection; -using DataLoader; -using Harmony; -using JetBrains.Annotations; -using RobocraftX.Blocks; -using RobocraftX.Blocks.GUI; -using RobocraftX.Common; -using RobocraftX.CR.MachineEditing; -using RobocraftX.StateSync; -using Svelto.ECS; -using Unity.Entities; -using UnityEngine; - -namespace GCMC -{ - [HarmonyPatch] - [UsedImplicitly] - public class FactoryObtainerPatch - { - static void Postfix(BlockEntityFactory blockEntityFactory) - { - CubePlacerEngine._blockEntityFactory = blockEntityFactory; - Debug.Log("Block entity factory injected."); - } - - static MethodBase TargetMethod(HarmonyInstance instance) - { - return typeof(PlaceBlockEngine).GetConstructors()[0]; - } - } -} \ No newline at end of file diff --git a/GCMC/GCMC.csproj b/GCMC/GCMC.csproj index 85e464d..9a0dabb 100644 --- a/GCMC/GCMC.csproj +++ b/GCMC/GCMC.csproj @@ -21,6 +21,9 @@ ..\ref\DataLoader.dll + + ..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net48\GamecraftModdingAPI.dll + IllusionPlugin.dll diff --git a/GCMCPlugin/pom.xml b/GCMCPlugin/pom.xml new file mode 100644 index 0000000..b41c99a --- /dev/null +++ b/GCMCPlugin/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + com.github.norbipeti + GCMCPlugin + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + org.spigotmc + spigot-api + 1.15-R0.1-SNAPSHOT + provided + + + org.projectlombok + lombok + 1.18.10 + + + com.google.code.gson + gson + 2.8.5 + + + + \ No newline at end of file diff --git a/GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/Blocks.java b/GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/Blocks.java new file mode 100644 index 0000000..fa8a3e4 --- /dev/null +++ b/GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/Blocks.java @@ -0,0 +1,14 @@ +package io.github.norbipeti.gcmc; + +import lombok.AllArgsConstructor; +import lombok.Data; +import org.bukkit.Location; +import org.bukkit.Material; + +@Data +@AllArgsConstructor +public class Blocks { + private Location start; + private Location end; + private Material material; +} diff --git a/GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/PluginMain.java b/GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/PluginMain.java new file mode 100644 index 0000000..33a8bb8 --- /dev/null +++ b/GCMCPlugin/src/main/java/io/github/norbipeti/gcmc/PluginMain.java @@ -0,0 +1,73 @@ +package io.github.norbipeti.gcmc; + +import com.google.common.io.Files; +import com.google.gson.Gson; +import lombok.val; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; + +public class PluginMain extends JavaPlugin { + @Override + public void onEnable() { + + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length < 6) { + sender.sendMessage("§cUsage: /export "); + return true; + } + int[] xyz = new int[6]; + for (int i = 0; i < args.length; i++) + xyz[i] = Integer.parseInt(args[i]); + for (int i = 0; i < 3; i++) { + if (xyz[i] >= xyz[i + 3]) { + int tmp = xyz[i]; + xyz[i] = xyz[i + 3]; + xyz[i + 3] = tmp; + } + } + World world = sender instanceof Player ? ((Player) sender).getWorld() : Bukkit.getWorlds().get(0); + val list = new ArrayList(); + for (int y = xyz[1]; y < xyz[4]; y++) { + Blocks blocks = new Blocks(null, null, null); + for (int x = xyz[0]; x < xyz[3]; x++) { + for (int z = xyz[2]; z < xyz[5]; z++) { + Block block = world.getBlockAt(x, y, z); + Material mat = block.getType(); + if (blocks.getMaterial() != mat) { + if (blocks.getStart() != null) + list.add(blocks); + blocks.setMaterial(mat); + blocks.setStart(new Location(null, x, y, z)); + blocks.setEnd(blocks.getStart()); + } else + blocks.setEnd(new Location(null, x, y, z)); + } + } + list.add(blocks); + } + Gson gson = new Gson(); + try { + Files.write(gson.toJson(list), new File("result.txt"), StandardCharsets.UTF_8); + sender.sendMessage("§bSuccess!"); + } catch (IOException e) { + e.printStackTrace(); + sender.sendMessage("§cAn error occurred."); + } + return true; + } +} diff --git a/GCMCPlugin/src/main/resources/plugin.yml b/GCMCPlugin/src/main/resources/plugin.yml new file mode 100644 index 0000000..1230320 --- /dev/null +++ b/GCMCPlugin/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ +name: GCMCPlugin +main: io.github.norbipeti.gcmc.PluginMain +version: '1.0' +commands: + export: + description: Exports an area of the world \ No newline at end of file