Use the API & create exporter MC plugin

Getting a StackOverflowError at the moment
This commit is contained in:
Norbi Peti 2020-01-04 02:35:03 +01:00
parent cb9d16b61b
commit bdbc3ac184
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 149 additions and 139 deletions

View file

@ -1,5 +1,6 @@
using System; using System;
using DataLoader; using DataLoader;
using GamecraftModdingAPI.Blocks;
using RobocraftX.Blocks; using RobocraftX.Blocks;
using RobocraftX.Blocks.Ghost; using RobocraftX.Blocks.Ghost;
using RobocraftX.Blocks.Scaling; using RobocraftX.Blocks.Scaling;
@ -27,11 +28,9 @@ namespace GCMC
} }
public IEntitiesDB entitiesDB { get; set; } public IEntitiesDB entitiesDB { get; set; }
internal static BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine
private void ImportWorld(string name) 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) private void PlaceBlock(string args)
@ -47,8 +46,10 @@ namespace GCMC
float scaleX = float.Parse(s[7]); float scaleX = float.Parse(s[7]);
float scaleY = float.Parse(s[8]); float scaleY = float.Parse(s[8]);
float scaleZ = float.Parse(s[9]); float scaleZ = float.Parse(s[9]);
float rotX = float.Parse(s[10]);
uint playerId = 0; 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) catch (Exception e)
{ {
@ -57,116 +58,11 @@ namespace GCMC
} }
} }
/// <summary>
/// Places a block at the given position
/// </summary>
/// <param name="block">The block's type</param>
/// <param name="color">The block's color</param>
/// <param name="darkness">The block color's darkness - 0 is default color</param>
/// <param name="position">The block's position - default block size is 0.2</param>
/// <param name="scale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
/// <param name="scaleX">The block's non-uniform scale - less than 1 means <paramref name="scale"/> is used</param>
/// <param name="scaleY">The block's non-uniform scale - less than 1 means <paramref name="scale"/> is used</param>
/// <param name="scaleZ">The block's non-uniform scale - less than 1 means <paramref name="scale"/> is used</param>
/// <param name="playerId">The player who placed the block</param>
/// <exception cref="Exception"></exception>
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) public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility, in PlayerInput[] playerInputs)
{ {
return new JobHandle(); return new JobHandle();
} }
public string name { get; } = "Cube placer engine"; public string name { get; } = "Cube placer engine";
enum BlockColors
{
Default = byte.MaxValue,
White = 0,
Pink,
Purple,
Blue,
Aqua,
Green,
Lime,
Yellow,
Orange,
Red
}
} }
} }

View file

@ -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];
}
}
}

View file

@ -21,6 +21,9 @@
<Reference Include="DataLoader, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"> <Reference Include="DataLoader, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\ref\DataLoader.dll</HintPath> <HintPath>..\ref\DataLoader.dll</HintPath>
</Reference> </Reference>
<Reference Include="GamecraftModdingAPI, Version=0.1.1.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net48\GamecraftModdingAPI.dll</HintPath>
</Reference>
<Reference Include="IllusionPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> <Reference Include="IllusionPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>IllusionPlugin.dll</HintPath> <HintPath>IllusionPlugin.dll</HintPath>
</Reference> </Reference>

49
GCMCPlugin/pom.xml Normal file
View file

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.norbipeti</groupId>
<artifactId>GCMCPlugin</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.15-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
</project>

View file

@ -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;
}

View file

@ -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 <x1> <y1> <z1> <x2> <y2> <z2>");
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<Blocks>();
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;
}
}

View file

@ -0,0 +1,6 @@
name: GCMCPlugin
main: io.github.norbipeti.gcmc.PluginMain
version: '1.0'
commands:
export:
description: Exports an area of the world