-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new data points for entities
- Loading branch information
1 parent
47dbd17
commit 0a029fe
Showing
10 changed files
with
1,734 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...rator/src/main/java/com/soulfiremc/generator/generators/NamedEntityDataJavaGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.generator.generators; | ||
|
||
import com.soulfiremc.generator.util.FieldGenerationHelper; | ||
import com.soulfiremc.generator.util.GeneratorConstants; | ||
import com.soulfiremc.generator.util.MCHelper; | ||
import com.soulfiremc.generator.util.ResourceHelper; | ||
import lombok.SneakyThrows; | ||
import net.minecraft.network.syncher.EntityDataAccessor; | ||
import net.minecraft.world.entity.EntityType; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Stream; | ||
|
||
public class NamedEntityDataJavaGenerator implements IDataGenerator { | ||
@Override | ||
public String getDataName() { | ||
return "java/NamedEntityData.java"; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public String generateDataJson() { | ||
var base = ResourceHelper.getResourceAsString("/templates/NamedEntityData.java"); | ||
return base.replace( | ||
GeneratorConstants.VALUES_REPLACE, | ||
String.join("\n ", | ||
FieldGenerationHelper.mapFields(EntityType.class, EntityType.class) | ||
.flatMap(f -> { | ||
var defaultEntity = MCHelper.createEntity(f.value()); | ||
Class<?> clazz = defaultEntity.getClass(); | ||
Stream<String> fields = Stream.empty(); | ||
|
||
do { | ||
fields = Stream.concat(fields, generateFields(clazz)); | ||
} while ((clazz = clazz.getSuperclass()) != null); | ||
|
||
return fields; | ||
}) | ||
.distinct() | ||
.sorted() | ||
.toArray(String[]::new))); | ||
} | ||
|
||
@SneakyThrows | ||
private Stream<String> generateFields(Class<?> clazz) { | ||
var className = FieldGenerationHelper.toSnakeCase(clazz.getSimpleName()); | ||
|
||
List<String> fields = new ArrayList<>(); | ||
for (var field : clazz.getDeclaredFields()) { | ||
if (!EntityDataAccessor.class.isAssignableFrom(field.getType())) { | ||
continue; | ||
} | ||
|
||
var dataName = field.getName(); | ||
var fieldPrefix = "DATA_"; | ||
if (dataName.startsWith(fieldPrefix)) { | ||
dataName = dataName.substring(fieldPrefix.length()); | ||
} | ||
|
||
field.setAccessible(true); | ||
var dataValue = (EntityDataAccessor<?>) field.get(null); | ||
fields.add("public static final NamedEntityData %s = register(\"%s\", %d, \"%s\");".formatted( | ||
className + "__" + dataName, | ||
dataName.toLowerCase(Locale.ROOT), | ||
dataValue.id(), | ||
className.toLowerCase(Locale.ROOT) | ||
)); | ||
} | ||
|
||
return fields.stream(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
data-generator/src/main/resources/templates/NamedEntityData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.data; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@SuppressWarnings("unused") | ||
public record NamedEntityData(String key, int networkId, String entityClass) { | ||
public static final List<NamedEntityData> VALUES = new ArrayList<>(); | ||
|
||
//@formatter:off | ||
// VALUES REPLACE | ||
//@formatter:on | ||
|
||
public static NamedEntityData register(String key, int networkId, String entityClass) { | ||
var instance = new NamedEntityData(key, networkId, entityClass); | ||
VALUES.add(instance); | ||
return instance; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof NamedEntityData other)) { | ||
return false; | ||
} | ||
return key.equals(other.key) && entityClass.equals(other.entityClass); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return key.hashCode() + entityClass.hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.