A idea to optimize the code generate

Started by cdc1234 on

Topic category: Feature requests and ideas for MCreator

Joined Dec 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
A idea to optimize the code generate

Original Issue: Input code Section · Issue #5 · cdc12345/SpigotLikeGenerator

 

In this issue, we could split the template code into three sections: Head, Body, Tail.

Like below:
```
<#head>
if (${input$entity} instanceof Player _player){
</#head>
_player.doSomething();
<#tail>
}
</#tail>
```

the head is a global variable.

when the same block append to the next

the system will check the head. If the head is same to the current head, It will ignore the head generate.

it will generate the code like:

```
//the first block generated
if (entity instanceof Player _player){
      _player.doSomething();
// the first block generated
// the second block generated
    _player.doSomething();
//the second block generated
//the tail
}
//the tail
```

the another block
```
<#head>
if (${input$entity} instanceof Zombie _zom){
</#head>
_zom.doSomething();
<#tail>
}
</#tail>
```

when the another block append to the above block. Its head is not equal the last head. So we will append the last tail to the head.

So it will generate like this:
```
//the first block generated
if (entity instanceof Player _player){
      _player.doSomething();
// the first block generated
// the second block generated
    _player.doSomething();
//the second block generated
//the tail
}
//the tail
//the another block generated
if (entity instanceof Zombie _zom){
_zom.doSomething();
//the tail
}
// the tail
```

the idea from the c language's precommand.

`#define helloworld helloworld`

 

advantage:

this featur will avoid generating the code like below:

```
   if (entity instanceof Player _player) {
               //skip
   }
   if (entity instanceof Player _player){
              //skip
   }
```