Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String → Identifier #238

Closed
wants to merge 13 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package nova.core.wrapper.mc.forge.v17.recipes;

import nova.core.item.Item;
import nova.core.recipes.crafting.SpecificItemIngredient;
import nova.internal.core.Game;

Expand All @@ -29,6 +28,6 @@
*/
public class MinecraftItemIngredient extends SpecificItemIngredient {
public MinecraftItemIngredient(net.minecraft.item.ItemStack itemStack) {
super(((Item) Game.natives().toNova(itemStack)).getID());
super(Game.natives().toNova(itemStack));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static Object getInternal(ItemIngredient ingredient) {
if (ingredient instanceof SpecificItemIngredient) {
return wrapSpecific((SpecificItemIngredient) ingredient);
} else if (ingredient instanceof OreItemIngredient) {
return ((OreItemIngredient) ingredient).getName();
return ((OreItemIngredient) ingredient).getID().asString();
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import nova.core.event.BlockEvent;
import nova.core.loader.Loadable;
import nova.core.nativewrapper.NativeConverter;
import nova.core.util.id.NamespacedStringIdentifier;
import nova.core.util.id.StringIdentifier;
import nova.core.wrapper.mc.forge.v17.launcher.NovaMinecraft;
import nova.core.wrapper.mc.forge.v17.util.ModCreativeTab;
import nova.core.wrapper.mc.forge.v17.wrapper.block.backward.BWBlock;
Expand Down Expand Up @@ -107,7 +109,7 @@ private void registerMinecraftToNOVA() {
BlockManager blockManager = Game.blocks();
net.minecraft.block.Block.blockRegistry.forEach(obj ->
blockManager.register(
new BlockFactory(net.minecraft.block.Block.blockRegistry.getNameForObject(obj).toString(),
new BlockFactory(new NamespacedStringIdentifier(net.minecraft.block.Block.blockRegistry.getNameForObject(obj)),
() -> new BWBlock((net.minecraft.block.Block) obj), evt -> {
})
)
Expand All @@ -118,7 +120,7 @@ private void registerNOVAToMinecraft() {
BlockManager blockManager = Game.blocks();

//Register air block
BlockFactory airBlock = new BlockFactory("air", () -> new BWBlock(Blocks.air) {
BlockFactory airBlock = new BlockFactory(new StringIdentifier("air"), () -> new BWBlock(Blocks.air) {
@Override
public boolean canReplace() {
return true;
Expand All @@ -136,7 +138,7 @@ private void registerNovaBlock(BlockFactory blockFactory) {
FWBlock blockWrapper = new FWBlock(blockFactory);
blockFactoryMap.put(blockFactory, blockWrapper);
NovaMinecraft.proxy.registerBlock(blockWrapper);
GameRegistry.registerBlock(blockWrapper, FWItemBlock.class, blockFactory.getID());
GameRegistry.registerBlock(blockWrapper, FWItemBlock.class, blockFactory.getID().asString()); // TODO?

if (blockWrapper.dummy.components.has(Category.class) && FMLCommonHandler.instance().getSide().isClient()) {
//Add into creative tab
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public FWBlock(BlockFactory factory) {
this.factory = factory;
this.dummy = factory.build();
this.blockClass = dummy.getClass();
this.setBlockName(dummy.getID());
this.setBlockName(dummy.getID().asString()); // TODO?

// Recalculate super constructor things after loading the block properly
this.opaque = isOpaqueCube();
Expand All @@ -102,6 +102,10 @@ public FWBlock(BlockFactory factory) {
}
}

public BlockFactory getFactory() {
return this.factory;
}

public Block getBlockInstance(net.minecraft.world.IBlockAccess access, Vector3D position) {
/**
* If this block has a TileEntity, forward the method into the Stateful
Expand Down Expand Up @@ -169,7 +173,7 @@ public boolean hasTileEntity(int metadata) {

@Override
public TileEntity createTileEntity(World world, int metadata) {
return FWTileLoader.loadTile(dummy.getID());
return FWTileLoader.loadTile(factory.getID());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,30 @@
import nova.core.network.Syncable;
import nova.core.retention.Data;
import nova.core.retention.Storable;
import nova.core.util.id.Identifier;
import nova.core.util.id.IdentifierRegistry;
import nova.core.util.id.StringIdentifier;
import nova.core.wrapper.mc.forge.v17.network.netty.MCNetworkManager;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.util.Objects;

/**
* A Minecraft TileEntity to Nova block wrapper
* @author Calclavia
*/
public class FWTile extends TileEntity {

private String blockID;
private Identifier blockID;
private Block block;
private Data cacheData = null;

public FWTile() {

}

public FWTile(String blockID) {
public FWTile(Identifier blockID) {
this.blockID = blockID;
}

Expand All @@ -57,6 +62,7 @@ public Block getBlock() {

public void setBlock(Block block) {
this.block = block;
this.blockID = block.getID();
}

@Override
Expand Down Expand Up @@ -108,7 +114,8 @@ public boolean canUpdate() {
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);

nbt.setString("novaID", blockID);
// If we have a block, store its id instead
nbt.setTag("novaID", Game.natives().toNative(Data.serialize(block != null ? block.getID() : blockID)));

if (block != null) {
if (block instanceof Storable) {
Expand All @@ -128,7 +135,7 @@ public void readFromNBT(NBTTagCompound nbt) {
* wait until the block is injected withPriority World and Position data using
* Future.
*/
blockID = nbt.getString("novaID");
blockID = ((Data)Game.natives().toNova(nbt)).getIdentifier("novaID");
cacheData = Game.natives().toNova(nbt.getCompoundTag("nova"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import net.minecraft.nbt.NBTTagCompound;
import nova.core.block.Block;
import nova.core.block.BlockFactory;
import nova.core.retention.Data;
import nova.core.util.id.Identifier;
import nova.core.util.id.StringIdentifier;
import nova.core.wrapper.mc.forge.v17.asm.lib.ComponentInjector;
import nova.internal.core.Game;

Expand All @@ -40,7 +43,7 @@ private FWTileLoader() {

public static FWTile loadTile(NBTTagCompound data) {
try {
String blockID = data.getString("novaID");
Identifier blockID = Data.unserialize(Game.natives().toNative(data.getTag("novaID")));
Block block = createBlock(blockID);
FWTile tile = injector.inject(block, new Class[0], new Object[0]);
tile.setBlock(block);
Expand All @@ -50,18 +53,18 @@ public static FWTile loadTile(NBTTagCompound data) {
}
}

public static FWTile loadTile(String blockID) {
public static FWTile loadTile(Identifier blockID) {
try {
Block block = createBlock(blockID);
FWTile tile = injector.inject(block, new Class[] { String.class }, new Object[] { blockID });
FWTile tile = injector.inject(block, new Class[] { Identifier.class }, new Object[] { blockID });
tile.setBlock(block);
return tile;
} catch (Exception e) {
throw new RuntimeException("Fatal error when trying to create a new NOVA tile.", e);
}
}

private static Block createBlock(String blockID) {
private static Block createBlock(Identifier blockID) {
Optional<BlockFactory> blockFactory = Game.blocks().get(blockID);
if (blockFactory.isPresent()) {
return blockFactory.get().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import nova.core.entity.EntityFactory;
import nova.core.item.Item;
import nova.core.sound.Sound;
import nova.core.util.id.Identifier;
import nova.core.util.id.StringIdentifier;
import nova.core.util.shape.Cuboid;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v17.launcher.NovaMinecraft;
Expand Down Expand Up @@ -144,12 +146,12 @@ public Optional<Entity> getEntity(String uniqueID) {
}

@Override
public String getID() {
return world().provider.getDimensionName();
public Identifier getID() {
return new StringIdentifier(world().provider.getDimensionName());
}

@Override
public void playSoundAtPosition(Vector3D position, Sound sound) {
world().playSound(position.getX(), position.getY(), position.getZ(), sound.getID(), sound.pitch, sound.volume, false);
world().playSound(position.getX(), position.getY(), position.getZ(), sound.getID().asString(), sound.pitch, sound.volume, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Data toNova(NBTTagCompound nbt) {
if (nbt != null) {
data.className = nbt.getString("class");
Set<String> keys = nbt.func_150296_c();
keys.forEach(k -> data.put(k, load(nbt, k)));
keys.stream().filter(k -> !k.endsWith(":isBoolean")).forEach(k -> data.put(k, load(nbt, k)));
}
return data;
}
Expand Down Expand Up @@ -94,10 +94,10 @@ public NBTTagCompound toNative(NBTTagCompound nbt, Data data) {
*/
public NBTTagCompound save(NBTTagCompound tag, String key, Object value) {
if (value instanceof Boolean) {
tag.setBoolean("isBoolean", true);
tag.setBoolean(key + ":isBoolean", true);
tag.setBoolean(key, (boolean) value);
} else if (value instanceof Byte) {
tag.setBoolean("isBoolean", false);
tag.setBoolean(key + ":isBoolean", false);
tag.setByte(key, (byte) value);
} else if (value instanceof Short) {
tag.setShort(key, (short) value);
Expand Down Expand Up @@ -142,7 +142,7 @@ public Object load(NBTTagCompound tag, String key) {
} else if (saveTag instanceof NBTTagShort) {
return tag.getShort(key);
} else if (saveTag instanceof NBTTagByte) {
if (tag.getBoolean("isBoolean")) {
if (tag.getBoolean(key + ":isBoolean")) {
return tag.getBoolean(key);
} else {
return tag.getByte(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import nova.core.entity.EntityFactory;
import nova.core.loader.Loadable;
import nova.core.nativewrapper.NativeConverter;
import nova.core.util.id.ClassIdentifier;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.backward.BWEntity;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.backward.BWEntityFX;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.FWEntity;
Expand Down Expand Up @@ -55,7 +56,7 @@ public Entity toNova(net.minecraft.entity.Entity mcEntity) {

//TODO: Make this BWRegistry non-lazy
//Lazy registry
String id = mcEntity.getClass().getName();
ClassIdentifier id = new ClassIdentifier(mcEntity.getClass()); // Minecraft uses Class as entity ID
Optional<EntityFactory> entityFactory = Game.entities().get(id);

if (entityFactory.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import nova.core.entity.Entity;
import nova.core.entity.component.Living;
import nova.core.entity.component.Player;
import nova.core.util.id.Identifier;
import nova.core.util.id.UUIDIdentifier;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.MCEntityTransform;
import nova.core.wrapper.mc.forge.v17.wrapper.inventory.BWInventory;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
Expand Down Expand Up @@ -88,8 +90,8 @@ public String getUsername() {
}

@Override
public String getID() {
return entity.getGameProfile().getId().toString();
public Identifier getID() {
return new UUIDIdentifier(entity.getGameProfile().getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import nova.core.entity.EntityFactory;
import nova.core.retention.Data;
import nova.core.retention.Storable;
import nova.core.util.id.IdentifierRegistry;
import nova.core.util.id.StringIdentifier;
import nova.core.util.shape.Cuboid;
import nova.core.wrapper.mc.forge.v17.wrapper.data.DataWrapper;
import nova.internal.core.Game;
Expand Down Expand Up @@ -64,7 +66,7 @@ protected void readEntityFromNBT(NBTTagCompound nbt) {
}
if (wrapped == null) {
//This entity was saved to disk.
setWrapped(Game.entities().get(nbt.getString("novaID")).get().build());
setWrapped(Game.entities().get(((Data)Game.natives().toNova(nbt)).getIdentifier("novaID")).get().build());
}
}

Expand All @@ -75,29 +77,43 @@ protected void writeEntityToNBT(NBTTagCompound nbt) {
((Storable) wrapped).save(data);
DataWrapper.instance().toNative(nbt, data);
}
nbt.setString("novaID", wrapped.getID());
Data identifierData = new Data();
IdentifierRegistry.instance().save(identifierData, wrapped.getID());
nbt.setTag("novaID", Game.natives().toNative(identifierData));
}

@Override
public void writeSpawnData(ByteBuf buffer) {
//Write the ID of the entity to client
String id = wrapped.getID();
char[] chars = id.toCharArray();
String type = IdentifierRegistry.instance().getName(wrapped.getID()).get().asString();
String id = wrapped.getID().asString();

char[] chars = type.toCharArray();
buffer.writeInt(chars.length);
for (char c : chars)
buffer.writeChar(c);

chars = id.toCharArray();
buffer.writeInt(chars.length);

for (char c : chars)
buffer.writeChar(c);
}

@Override
public void readSpawnData(ByteBuf buffer) {
//Load the client ID
String id = "";
String type = "";
int length = buffer.readInt();
for (int i = 0; i < length; i++)
type += buffer.readChar();

String id = "";
length = buffer.readInt();
for (int i = 0; i < length; i++)
id += buffer.readChar();

setWrapped(Game.entities().get(id).get().build());
setWrapped(Game.entities().get(IdentifierRegistry.instance().get(type)
.orElseThrow(IdentifierRegistry.noIdentifierClassFound(type)).load(id)).get().build());
}

public Entity getWrapped() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ public net.minecraft.item.ItemStack makeItemStack(int stackSize) {

@Override
public String toString() {
return getID();
return getID().asString(); // TODO?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import nova.core.item.Item;
import nova.core.item.ItemFactory;
import nova.core.retention.Data;
import nova.core.util.id.StringIdentifier;
import nova.internal.core.Game;

/**
Expand All @@ -37,7 +38,7 @@ public class BWItemFactory extends ItemFactory {
private final int meta;

public BWItemFactory(net.minecraft.item.Item item, int meta) {
super(net.minecraft.item.Item.itemRegistry.getNameForObject(item) + (item.getHasSubtypes() ? ":" + meta : ""), () -> new BWItem(item, meta, null));
super(new StringIdentifier(net.minecraft.item.Item.itemRegistry.getNameForObject(item) + (item.getHasSubtypes() ? ":" + meta : "")), () -> new BWItem(item, meta, null));

this.item = item;
this.meta = meta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class FWItem extends net.minecraft.item.Item implements ItemWrapperMethod

public FWItem(ItemFactory item) {
this.itemFactory = item;
setUnlocalizedName(item.getID());
setUnlocalizedName(item.getID().asString()); // TODO?
setMaxStackSize(item.build().getMaxCount());
}

Expand Down
Loading