Add BlockEntity support
This commit is contained in:
@ -1,15 +1,15 @@
|
||||
package tnze.github.com;
|
||||
package pers.tnze.gomc.gen;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.core.DefaultedRegistry;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.NbtIo;
|
||||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.nbt.*;
|
||||
import net.minecraft.server.Bootstrap;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.properties.EnumProperty;
|
||||
import net.minecraft.world.level.block.state.properties.Property;
|
||||
@ -18,9 +18,10 @@ import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class Main {
|
||||
public class GenBlocks {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("program start!");
|
||||
@ -40,6 +41,12 @@ public class Main {
|
||||
NbtIo.writeUnnamedTag(getBlockStates(), writer);
|
||||
}
|
||||
}
|
||||
try (FileOutputStream f = new FileOutputStream("block_entities.nbt")) {
|
||||
try (GZIPOutputStream g = new GZIPOutputStream(f)) {
|
||||
DataOutput writer = new DataOutputStream(g);
|
||||
NbtIo.writeUnnamedTag(genBlockEntities(), writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ListTag getBlocksWithMeta() throws Exception {
|
||||
@ -84,4 +91,21 @@ public class Main {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static ListTag genBlockEntities() {
|
||||
ListTag list = new ListTag();
|
||||
for (BlockEntityType blockEntity : BuiltInRegistries.BLOCK_ENTITY_TYPE) {
|
||||
ListTag validBlocksList = new ListTag();
|
||||
Set<Block> validBlocks = blockEntity.validBlocks;
|
||||
for (Block validBlock : validBlocks){
|
||||
validBlocksList.add(StringTag.valueOf(BuiltInRegistries.BLOCK.getKey(validBlock).toString()));
|
||||
}
|
||||
CompoundTag be = new CompoundTag();
|
||||
be.putString("Name", BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(blockEntity).toString());
|
||||
be.putString("ValidBlocks", BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(blockEntity).toString());
|
||||
|
||||
list.add(be);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
31
level/block/generator/blockentities/blockentities.go.tmpl
Normal file
31
level/block/generator/blockentities/blockentities.go.tmpl
Normal file
@ -0,0 +1,31 @@
|
||||
// Code generated by {{Generator}}; DO NOT EDIT.
|
||||
|
||||
package block
|
||||
|
||||
{{/* type (
|
||||
{{- range .}}
|
||||
{{.Name | ToGoTypeName}}Entity struct {}
|
||||
{{- end}}
|
||||
) */}}
|
||||
|
||||
var EntityList = [...]Entity{
|
||||
{{- range .}}
|
||||
{{.Name | ToGoTypeName}}Entity{},
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
{{- range .}}
|
||||
func ({{.Name | ToGoTypeName}}Entity) ID() string { return {{.Name | printf "%q"}} }
|
||||
{{- end}}
|
||||
|
||||
{{range .}}
|
||||
func ({{.Name | ToFuncReceiverName}} {{.Name | ToGoTypeName}}Entity) IsValidBlock(block Block) bool {
|
||||
{{if eq 1 (len .ValidBlocks)}}return block.ID() == {{index .ValidBlocks 0 | printf "%q"}}{{else}}switch block.ID() {
|
||||
case {{index .ValidBlocks 0 | printf "%q"}}{{range slice .ValidBlocks 1}},
|
||||
{{. | printf "%q"}}{{end}}:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}{{end}}
|
||||
}
|
||||
{{end}}
|
77
level/block/generator/blockentities/main.go
Normal file
77
level/block/generator/blockentities/main.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
_ "embed"
|
||||
"go/format"
|
||||
"log"
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"github.com/Tnze/go-mc/internal/generateutils"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
)
|
||||
|
||||
//go:embed blockentities.go.tmpl
|
||||
var tempSource string
|
||||
|
||||
var temp = template.Must(template.
|
||||
New("block_template").
|
||||
Funcs(template.FuncMap{
|
||||
"UpperTheFirst": generateutils.UpperTheFirst,
|
||||
"ToGoTypeName": generateutils.ToGoTypeName,
|
||||
"ToFuncReceiverName": generateutils.ToFuncReceiverName,
|
||||
"Generator": func() string { return "generator/blockentities/main.go" },
|
||||
}).
|
||||
Parse(tempSource),
|
||||
)
|
||||
|
||||
type BlockEntity struct {
|
||||
Name string
|
||||
ValidBlocks []string
|
||||
}
|
||||
|
||||
func main() {
|
||||
var states []BlockEntity
|
||||
readBlockEntities(&states)
|
||||
|
||||
// generate go source file
|
||||
genSourceFile(states)
|
||||
}
|
||||
|
||||
func readBlockEntities(states *[]BlockEntity) {
|
||||
f, err := os.Open("block_entities.nbt")
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
r, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
// parse the nbt format
|
||||
if _, err := nbt.NewDecoder(r).Decode(states); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func genSourceFile(states []BlockEntity) {
|
||||
var source bytes.Buffer
|
||||
if err := temp.Execute(&source, states); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
formattedSource, err := format.Source(source.Bytes())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("blockentities.go", formattedSource, 0o666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Print("Generated blockentities.go")
|
||||
}
|
Reference in New Issue
Block a user