generated from minecraft-cursed-legacy/Example-Mod
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `EntityType`s and `EntityTypeRegistry`. * Bump version number * Make gradlew executable * Fix checkstyle complaints * Add way to create EntityTypes through API * Move AccessorEntityRegistry into accessor package, rename, and put in correct json * Improve AccessorEntityRegistry names
- Loading branch information
Showing
15 changed files
with
206 additions
and
18 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
57 changes: 57 additions & 0 deletions
57
src/main/java/io/github/minecraftcursedlegacy/accessor/AccessorEntityRegistry.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,57 @@ | ||
package io.github.minecraftcursedlegacy.accessor; | ||
|
||
import net.minecraft.entity.Entity; | ||
import net.minecraft.entity.EntityRegistry; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
import org.spongepowered.asm.mixin.gen.Invoker; | ||
|
||
import java.util.Map; | ||
|
||
@Mixin(EntityRegistry.class) | ||
public interface AccessorEntityRegistry { | ||
@Accessor("ID_TO_CLASS") | ||
static Map<Integer, Class<? extends Entity>> getIdToClassMap() { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Accessor("ID_TO_CLASS") | ||
static void setIdToClassMap(Map<Integer, Class<? extends Entity>> value) { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Accessor("CLASS_TO_ID") | ||
static Map<Class<? extends Entity>, Integer> getClassToIdMap() { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Accessor("CLASS_TO_ID") | ||
static void setClassToIdMap(Map<Class<? extends Entity>, Integer> value) { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Accessor("CLASS_TO_STRING_ID") | ||
static Map<Class<? extends Entity>, String> getClassToStringIdMap() { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Accessor("CLASS_TO_STRING_ID") | ||
static void setClassToStringIdMap(Map<Class<? extends Entity>, String> value) { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Accessor("STRING_ID_TO_CLASS") | ||
static Map<String, Class<? extends Entity>> getStringIdToClassMap() { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Accessor("STRING_ID_TO_CLASS") | ||
static void setStringIdToClassMap(Map<String, Class<? extends Entity>> value) { | ||
throw new AssertionError("mixin"); | ||
} | ||
|
||
@Invoker | ||
static void callRegister(Class<? extends Entity> arg, String string, int i) { | ||
throw new AssertionError("mixin"); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/main/java/io/github/minecraftcursedlegacy/api/registry/EntityTypes.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,15 @@ | ||
package io.github.minecraftcursedlegacy.api.registry; | ||
|
||
import io.github.minecraftcursedlegacy.impl.registry.EntityType; | ||
import net.minecraft.entity.Entity; | ||
|
||
public class EntityTypes { | ||
/** | ||
* Use this to create EntityTypes for your own entities. | ||
* @param clazz the entity class. | ||
* @param id the identifier used in the EntityType registry. | ||
*/ | ||
public static EntityType createEntityType(Class<? extends Entity> clazz, Id id) { | ||
return new EntityType(clazz, id); | ||
} | ||
} |
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
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
36 changes: 36 additions & 0 deletions
36
src/main/java/io/github/minecraftcursedlegacy/impl/registry/EntityType.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,36 @@ | ||
package io.github.minecraftcursedlegacy.impl.registry; | ||
|
||
import io.github.minecraftcursedlegacy.api.registry.Id; | ||
import net.minecraft.entity.Entity; | ||
|
||
public class EntityType { | ||
private final Class<? extends Entity> clazz; | ||
private final String vanillaRegistryStringId; | ||
|
||
/** | ||
* Protected constructor used only for vanilla entities. | ||
* @param clazz the entity class. | ||
* @param vanillaRegistryStringId the vanilla name of the entity. | ||
*/ | ||
protected EntityType(Class<? extends Entity> clazz, String vanillaRegistryStringId) { | ||
this.clazz = clazz; | ||
this.vanillaRegistryStringId = vanillaRegistryStringId; | ||
} | ||
|
||
public EntityType(Class<? extends Entity> clazz, Id id) { | ||
this.clazz = clazz; | ||
this.vanillaRegistryStringId = id.toString(); | ||
} | ||
|
||
public Class<? extends Entity> getClazz() { | ||
return clazz; | ||
} | ||
|
||
public String getVanillaRegistryStringId() { | ||
return vanillaRegistryStringId; | ||
} | ||
|
||
public Id getId() { | ||
return new Id(vanillaRegistryStringId); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/io/github/minecraftcursedlegacy/impl/registry/EntityTypeRegistry.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,67 @@ | ||
package io.github.minecraftcursedlegacy.impl.registry; | ||
|
||
import io.github.minecraftcursedlegacy.accessor.AccessorEntityRegistry; | ||
import io.github.minecraftcursedlegacy.api.registry.Id; | ||
import io.github.minecraftcursedlegacy.api.registry.Registry; | ||
import net.minecraft.entity.Entity; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class EntityTypeRegistry extends Registry<EntityType> { | ||
private int currentId = 0; | ||
|
||
/** | ||
* Creates a new registry object. | ||
* | ||
* @param registryName the identifier for this registry. | ||
*/ | ||
public EntityTypeRegistry(Id registryName) { | ||
super(EntityType.class, registryName, null); | ||
|
||
// add vanilla entities | ||
AccessorEntityRegistry.getIdToClassMap().forEach((intId, clazz) -> { | ||
if (clazz != null) { | ||
String idPart = AccessorEntityRegistry.getClassToStringIdMap().get(clazz); | ||
|
||
EntityType type = new EntityType(clazz, idPart == null ? "entity" : idPart); | ||
if (idPart == null) { | ||
idPart = "entity"; | ||
} else { | ||
idPart = idPart.toLowerCase(); | ||
} | ||
|
||
this.byRegistryId.put(new Id(idPart), type); | ||
this.bySerialisedId.put(intId, type); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected int getNextSerialisedId() { | ||
Map<Integer, Class<? extends Entity>> idToClass = AccessorEntityRegistry.getIdToClassMap(); | ||
while (idToClass.containsKey(currentId)) { | ||
++currentId; | ||
} | ||
|
||
return currentId; | ||
} | ||
|
||
@Override | ||
protected int getStartSerialisedId() { | ||
return 1; //Maybe this could be changed to 0, not sure if vanilla would like an entity having 0 as an id. | ||
} | ||
|
||
@Override | ||
protected void beforeRemap() { | ||
AccessorEntityRegistry.setIdToClassMap(new HashMap<>()); | ||
AccessorEntityRegistry.setClassToIdMap(new HashMap<>()); | ||
AccessorEntityRegistry.setStringIdToClassMap(new HashMap<>()); | ||
AccessorEntityRegistry.setClassToStringIdMap(new HashMap<>()); | ||
} | ||
|
||
@Override | ||
protected void onRemap(EntityType remappedValue, int newSerialisedId) { | ||
AccessorEntityRegistry.callRegister(remappedValue.getClazz(), remappedValue.getVanillaRegistryStringId(), newSerialisedId); | ||
} | ||
} |
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