Defining blocks in an array

Forum intended for those developing plugins for or integration with MineTweaker. (Help with scripts is for Scripting Help)

Defining blocks in an array

Postby jakimfett » Sat Jan 03, 2015 10:38 pm

Oi. I've got a new-ish mod that I'm working on that I'd like to integrate with Minetweaker.

Specifically, the mod uses a 3d array to store a "schematic". The intent is that modpack creators could use Minetweaker to make new schematics, save them, and then my schematicLoader will iterate through all scripts in the folder, and load each of them. The schematic holds a block with metadata, and needs an x/y/z. So for a text data format, it could be something like "block:meta:x:y:z", or even "y{block:meta:x:z, block:meta:x:z}".

Don't know if this is possible without doing some legwork on my end, but I'm interested in figuring it out and implementing it in my mod (despite not having worked with Minetweaker before).

I spoke with hilburn, and he said I should post here about it, and see if I could get some help with it.
I develop Minechem and AlchemyPlusPlus.
http://jakimfett.com/
User avatar
jakimfett
 
Posts: 4
Joined: Sat Jan 03, 2015 10:24 pm

Re: Defining blocks in an array

Postby Stan » Sun Jan 04, 2015 3:00 pm

This is easy to accomplish by registering your own bracket handler. (see MineTweakerAPI.registerBracketHandler, IBracketHandler, ItemBracketHandler and OreBracketHandler for API and examples) You can then create your own custom syntax using brackets (for instance <schematicBlock:x:y:z:blockname:meta> or <sb:x:y:z:blockname:meta>). The bracket handler can resolve the value to your own schematic block class.

After that, you can register your own integration class which provides functions such as addSchematic accepting an array of your blocks class instances as well as any other parameters that would be needed to register a new schematic in your mod.

A script could then look like this:

: [Select all] [/] [] ()
  1. MySchematics.addSchematic("SchematicName", [
  2.   <sb:-1:0:-1:minecraft:stone>,
  3.   <sb:-1:0:0:minecraft:stone>,
  4.   <sb:-1:0:1:minecraft:stone>,
  5.   <sb:0:0:-1:minecraft:stone>,
  6.   <sb:0:0:1:minecraft:stone>,
  7.   <sb:1:0:-1:minecraft:stone>,
  8.   <sb:1:0:0:minecraft:stone>,
  9.   <sb:1:0:1:minecraft:stone>
  10. ]);
  11.  
GeSHi © Codebox Plus


If you need, I can provide you the necessary code, since not much is needed. Which syntax you use for the bracket handler is completely free as long as there's a way to detect that the content of the angled brackets is intended for your mod.
User avatar
Stan
Site Admin
 
Posts: 258
Joined: Fri Jul 11, 2014 9:04 pm

Re: Defining blocks in an array

Postby jakimfett » Wed Jan 07, 2015 6:10 am

Stan wrote:A script could then look like this:

: [Select all] [/] [] ()
  1. MySchematics.addSchematic("SchematicName", [
  2.   <sb:-1:0:-1:minecraft:stone>,
  3.   <sb:-1:0:0:minecraft:stone>,
  4.   <sb:-1:0:1:minecraft:stone>,
  5.   <sb:0:0:-1:minecraft:stone>,
  6.   <sb:0:0:1:minecraft:stone>,
  7.   <sb:1:0:-1:minecraft:stone>,
  8.   <sb:1:0:0:minecraft:stone>,
  9.   <sb:1:0:1:minecraft:stone>
  10. ]);
  11.  
GeSHi © Codebox Plus


So, if I'm understanding correctly, this script would call the "addSchematic(String schematicName, String[] data)" function in my MySchematics class...and in the addSchematic class could then iterate through the data array, adding blocks to the schematic object using normal java functions?
I develop Minechem and AlchemyPlusPlus.
http://jakimfett.com/
User avatar
jakimfett
 
Posts: 4
Joined: Sat Jan 03, 2015 10:24 pm

Re: Defining blocks in an array

Postby Stan » Wed Jan 07, 2015 12:13 pm

That's not the complete story. You'd need something to process the bracket syntax you want to use for the schematic blocks. If that bracket handler generates values of type, say, SchematicBlock, then your addSchematic method would receive arguments of the type (String, SchematicBlock[]).

The bracket handler would be the most annoying part, so I made one up for you (untested, may contain some errors):

: [Select all] [/] [] ()
  1. public class SchematicBlockBracketHandler implements IBracketHandler {
  2.     public static SchematicBlock getBlock(int x, int y, int z, IItemStack item) {
  3.         return new SchematicBlock(x, y, z, item);
  4.     }
  5.    
  6.     private final IJavaMethod method;
  7.     private final ItemBracketHandler itemHandler;
  8.    
  9.     public SchematicBlockBracketHandler() {
  10.         itemHandler = new ItemBracketHandler();
  11.         method = MineTweakerAPI.getJavaMethod(
  12.                 SchematicBlockBracketHandler.class,
  13.                 "getBlock",
  14.                 int.class, int.class, int.class, IItemStack.class);
  15.     }
  16.  
  17.     @Override
  18.     public IZenSymbol resolve(IEnvironmentGlobal environment, List<Token> tokens) {
  19.         if (tokens.size() < 8)
  20.             return null;
  21.        
  22.         if (!tokens.get(0).getValue().equals("sb"))
  23.             return null;
  24.            
  25.         if (tokens.get(1).getType() != ZenTokener.T_COLON
  26.             || tokens.get(2).getType() != ZenTokener.T_INTVALUE
  27.             || tokens.get(3).getType() != ZenTokener.T_COLON
  28.             || tokens.get(4).getType() != ZenTokener.T_INTVALUE
  29.             || tokens.get(5).getType() != ZenTokener.T_COLON
  30.             || tokens.get(6).getType() != ZenTokener.T_INTVALUE
  31.             || tokens.get(7).getType() != ZenTokener.T_COLON)
  32.             return null;
  33.        
  34.         int x = Integer.parseInt(tokens.get(2).getValue());
  35.         int y = Integer.parseInt(tokens.get(4).getValue());
  36.         int z = Integer.parseInt(tokens.get(6).getValue());
  37.         IZenSymbol itemStack = itemHandler.resolve(environment, tokens.subList(8, tokens.size()));
  38.         return new SchematicBlockSymbol(environment, x, y, z, itemStack);
  39.     }
  40.    
  41.     private class ItemReferenceSymbol implements IZenSymbol {
  42.         private final IEnvironmentGlobal environment;
  43.         private final int x;
  44.         private final int y;
  45.         private final int z;
  46.         private final IZenSymbol itemStack;
  47.        
  48.         public ItemReferenceSymbol(IEnvironmentGlobal environment, int x, int y, int z, IZenSymbol itemStack) {
  49.             this.environment = environment;
  50.             this.x = x;
  51.             this.y = y;
  52.             this.z = z;
  53.             this.itemStack = itemStack;
  54.         }
  55.        
  56.         @Override
  57.         public IPartialExpression instance(ZenPosition position) {
  58.             return new ExpressionCallStatic(
  59.                     position,
  60.                     environment,
  61.                     method,
  62.                     new ExpressionInt(position, x, ZenType.INT),
  63.                     new ExpressionInt(position, y, ZenType.INT),
  64.                     new ExpressionInt(position, z, ZenType.INT),
  65.                     itemStack.instance(position).eval());
  66.         }
  67.     }
  68. }
  69.  
GeSHi © Codebox Plus


Sorry if this is a bit cumbersome, next release will have easier ways of doing things like this.

Besides the bracket syntax handler, all you'd need is an integration class, which would be the easy part;

: [Select all] [/] [] ()
  1. @ZenClass("mods.mymod.Schematics")
  2. class Schematics {
  3.     @ZenMethod
  4.     public static void addSchematic(String schematicName, SchematicBlock[] blocks) { ... register your schematic ... }
  5. }
  6.  
GeSHi © Codebox Plus


Don't forget to register both classes using the corresponding register methods in MineTweakerAPI!
User avatar
Stan
Site Admin
 
Posts: 258
Joined: Fri Jul 11, 2014 9:04 pm

Re: Defining blocks in an array

Postby jakimfett » Wed Jan 14, 2015 2:29 am

Sorry for the lag in poking this, I'm working on mods during my free evenings and weekends...which keep getting full of responsibilities.

Couple of questions for you.

What's the purpose of the getBlock() method? Getting the block at the x/y/z inside the specific schematic?

What does resolve() need to return? What's SchematicBlockSymbol supposed to be?

And, finally, the purpose of the addSchematic() class is to register a schematic...so wouldn't I just be able to use my existing schematic adding mechanic (https://github.com/jakimfett/Projektor/ ... c.java#L38)?
I develop Minechem and AlchemyPlusPlus.
http://jakimfett.com/
User avatar
jakimfett
 
Posts: 4
Joined: Sat Jan 03, 2015 10:24 pm


Return to Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron