Skip to content

Commit

Permalink
Add EntityTypes and EntityTypeRegistry. (#8)
Browse files Browse the repository at this point in the history
* 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
williambl authored May 18, 2020
1 parent d79252e commit f5aa118
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ org.gradle.jvmargs=-Xmx1G
loader_version=392aab7

# Mod Properties
mod_version = 0.3.1
mod_version = 0.4.0
maven_group = io.github.minecraftcursedlegacy.api
archives_base_name = cursed-legacy-api
Empty file modified gradlew
100644 → 100755
Empty file.
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");
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.github.minecraftcursedlegacy.api.event;

import java.util.function.Consumer;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.level.biome.Biome;

import java.util.function.Consumer;

/**
* Callback for biome placement. Add a hook for this in the {@link net.fabricmc.api.ModInitializer init} stage, as the biomes are calculated in postinit.
*
* <p>Upon return:<ul>
* <p>Upon return:
* <ul>
* <li> SUCCESS succeeds in altering the biome, and sets the latest biome set via the biome setter. If no biome has been set, then the behaviour defaults to FAIL
* <li> PASS falls back to further event processing. If all events PASS, then the behaviour defaults to SUCCESS.
* <li> FAIL falls back to vanilla biome placement.
* </ul>
*/
@FunctionalInterface
public interface BiomePlacementCallback {
Expand All @@ -25,6 +27,7 @@ public interface BiomePlacementCallback {
return result;
}
}

return ActionResult.SUCCESS;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ public void send(byte[] data, ServerPlayer player) {
public void send(byte[] data, Minecraft mc) {
mc.method_2145().sendPacket(new PluginMessagePacket(getChannelIdentifier().toString(), data));
}

}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.minecraftcursedlegacy.api.registry;

import io.github.minecraftcursedlegacy.impl.registry.EntityType;
import io.github.minecraftcursedlegacy.impl.registry.RegistryImpl;
import net.minecraft.item.ItemType;
import net.minecraft.tile.Tile;
Expand All @@ -16,4 +17,9 @@ public final class Registries {
* Registry for Tiles.
*/
public static final Registry<Tile> TILE = RegistryImpl.TILE;

/**
* Registry for Entity types.
*/
public static final Registry<EntityType> ENTITY_TYPE = RegistryImpl.ENTITY_TYPE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ private VanillaChecker() {
}

/**
* Checks if a player is using a Vanilla Client
* Checks if a player is using a Vanilla Client.
*/
public static boolean isVanilla(Player player) {
return VanillaCheckerImpl.playermap.get(player.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.fabricmc.api.ModInitializer;

public class NetworkingImpl implements ModInitializer {

@Override
public void onInitialize() {
AccessorAbstractPacket.register(250, true, true, PluginMessagePacket.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.minecraftcursedlegacy.impl.networking;

import net.minecraft.network.PacketHandler;
import net.minecraft.packet.AbstractPacket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import net.minecraft.network.PacketHandler;
import net.minecraft.packet.AbstractPacket;

//Similar to Plugin Message but supports
public class PluginMessagePacket extends AbstractPacket {
public String channel;
Expand All @@ -29,6 +29,7 @@ public void read(DataInputStream dataInputStream) {
for (int i = 0; i < channellength; i++) {
buffer.append(dataInputStream.readChar());
}

channel = buffer.toString();

int datalength = dataInputStream.readInt();
Expand Down Expand Up @@ -60,5 +61,4 @@ public void handle(PacketHandler arg) {
public int length() {
return 2 /* short length */ + channel.length() + 2 /* short length */ + data.length;
}

}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.github.minecraftcursedlegacy.impl.registry;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

import io.github.minecraftcursedlegacy.accessor.AccessorEntityRegistry;
import io.github.minecraftcursedlegacy.api.registry.Id;
import io.github.minecraftcursedlegacy.api.registry.Registry;
import io.github.minecraftcursedlegacy.impl.Hacks;
Expand All @@ -12,6 +9,10 @@
import net.minecraft.item.TileItem;
import net.minecraft.tile.Tile;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public class RegistryImpl implements ModInitializer {
private static int currentItemtypeId = Tile.BY_ID.length;
private static int currentTileId = 1;
Expand Down Expand Up @@ -103,10 +104,14 @@ public void onInitialize() {

public static final Registry<ItemType> ITEM_TYPE;
public static final Registry<Tile> TILE;
public static final Registry<EntityType> ENTITY_TYPE;

static {
//noinspection ResultOfMethodCallIgnored
Tile.BED.hashCode(); // make sure tiles are initialised
AccessorEntityRegistry.getIdToClassMap(); // make sure entities are initialised
ITEM_TYPE = new ItemTypeRegistry(new Id("api:item_type"));
TILE = new TileRegistry(new Id("api:tile"));
ENTITY_TYPE = new EntityTypeRegistry(new Id("api:entity_type"));
}
}
3 changes: 2 additions & 1 deletion src/main/resources/api.accessors.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"AccessorShapedRecipe",
"AccessorShapelessRecipe",
"AccessorTileItem",
"AccessorAbstractPacket"
"AccessorAbstractPacket",
"AccessorEntityRegistry"
],
"client": [
],
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/api.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"MixinHandshakeC2SClient"
],
"server": [
"MixinPendingConnection",
"MixinClass_11"
"MixinClass_11",
"MixinPendingConnection"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit f5aa118

Please sign in to comment.