Skip to content

Commit

Permalink
Merge pull request #309 from NOVA-Team/native-converter/use-instance
Browse files Browse the repository at this point in the history
Use the `instance()` method for all `NativeConverter` implementations
  • Loading branch information
ExE-Boss committed Oct 21, 2017
2 parents 16e8f78 + d5cb883 commit baf9cd1
Show file tree
Hide file tree
Showing 39 changed files with 273 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
import nova.core.wrapper.mc.forge.v17.wrapper.block.forward.FWBlock;
import nova.core.wrapper.mc.forge.v17.wrapper.block.forward.FWTile;
import nova.core.wrapper.mc.forge.v17.wrapper.block.forward.FWTileRenderer;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.backward.BWEntityFX;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.FWEntity;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.FWEntityFX;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.FWEntityRenderer;
import nova.core.wrapper.mc.forge.v17.wrapper.item.FWItem;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.io.IOException;
Expand Down Expand Up @@ -156,7 +156,7 @@ public Entity spawnParticle(net.minecraft.world.World world, EntityFactory facto
if (build instanceof BWEntityFX) {
EntityFX entityFX = ((BWEntityFX) build).createEntityFX();
FMLClientHandler.instance().getClient().effectRenderer.addEffect(entityFX);
return Game.natives().toNova(entityFX);
return EntityConverter.instance().toNova(entityFX);
} else {
FWEntityFX bwEntityFX = new FWEntityFX(world, factory);
FMLClientHandler.instance().getClient().effectRenderer.addEffect(bwEntityFX);
Expand All @@ -174,7 +174,7 @@ public Entity spawnParticle(net.minecraft.world.World world, Entity entity) {
entityFX.posY = position.getY();
entityFX.posZ = position.getZ();
FMLClientHandler.instance().getClient().effectRenderer.addEffect(entityFX);
return Game.natives().toNova(entityFX);
return EntityConverter.instance().toNova(entityFX);
} else {
FWEntityFX bwEntityFX = new FWEntityFX(world, entity);
FMLClientHandler.instance().getClient().effectRenderer.addEffect(bwEntityFX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import nova.core.event.PlayerEvent;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.internal.core.Game;

/**
Expand All @@ -32,12 +33,12 @@
public class FMLEventHandler {
@SubscribeEvent
public void playerJoin(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent evt) {
Game.events().publish(new PlayerEvent.Join(Game.natives().toNova(evt.player)));
Game.events().publish(new PlayerEvent.Join(EntityConverter.instance().toNova(evt.player)));
}

@SubscribeEvent
public void playerLeave(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent evt) {
Game.events().publish(new PlayerEvent.Leave(Game.natives().toNova(evt.player)));
Game.events().publish(new PlayerEvent.Leave(EntityConverter.instance().toNova(evt.player)));
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import nova.core.event.PlayerEvent;
import nova.core.item.Item;
import nova.core.item.ItemDictionary;
import nova.core.wrapper.mc.forge.v17.wrapper.block.world.WorldConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.item.ItemConverter;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
Expand All @@ -38,12 +40,12 @@
public class ForgeEventHandler {
@SubscribeEvent
public void worldUnload(WorldEvent.Load evt) {
Game.events().publish(new nova.core.event.WorldEvent.Load(Game.natives().toNova(evt.world)));
Game.events().publish(new nova.core.event.WorldEvent.Load(WorldConverter.instance().toNova(evt.world)));
}

@SubscribeEvent
public void worldLoad(WorldEvent.Unload evt) {
Game.events().publish(new nova.core.event.WorldEvent.Unload(Game.natives().toNova(evt.world)));
Game.events().publish(new nova.core.event.WorldEvent.Unload(WorldConverter.instance().toNova(evt.world)));
}

@SubscribeEvent
Expand All @@ -58,20 +60,20 @@ public void onOreRegister(OreDictionary.OreRegisterEvent event) {

@SubscribeEvent
public void playerJoin(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent evt) {
Game.events().publish(new PlayerEvent.Join(Game.natives().toNova(evt.player)));
Game.events().publish(new PlayerEvent.Join(EntityConverter.instance().toNova(evt.player)));
}

@SubscribeEvent
public void playerLeave(cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent evt) {
Game.events().publish(new PlayerEvent.Leave(Game.natives().toNova(evt.player)));
Game.events().publish(new PlayerEvent.Leave(EntityConverter.instance().toNova(evt.player)));
}

@SubscribeEvent
public void playerInteractEvent(PlayerInteractEvent event) {
nova.core.event.PlayerEvent.Interact evt = new nova.core.event.PlayerEvent.Interact(
Game.natives().toNova(event.world),
WorldConverter.instance().toNova(event.world),
new Vector3D(event.x, event.y, event.z),
Game.natives().toNova(event.entityPlayer),
EntityConverter.instance().toNova(event.entityPlayer),
nova.core.event.PlayerEvent.Interact.Action.values()[event.action.ordinal()]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import nova.core.retention.Data;
import nova.core.retention.Storable;
import nova.core.util.registry.RetentionManager;
import nova.core.wrapper.mc.forge.v17.wrapper.data.DataConverter;
import nova.internal.core.Game;

import java.io.File;
Expand Down Expand Up @@ -59,13 +60,13 @@ public void saveAll() {
public void save(String filename, Storable storable) {
Data saveMap = new Data();
storable.save(saveMap);
saveFile(filename, Game.natives().toNative(saveMap));
saveFile(filename, DataConverter.instance().toNative(saveMap));
}

@Override
public void load(String filename, Storable storable) {
NBTTagCompound nbt = loadFile(filename);
storable.load(Game.natives().toNova(nbt));
storable.load(DataConverter.instance().toNova(nbt));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import nova.core.entity.Entity;
import nova.core.entity.component.Player;
import nova.core.network.handler.PacketHandler;
import nova.core.wrapper.mc.forge.v17.network.MCPacket;
import nova.core.wrapper.mc.forge.v17.network.netty.MCNetworkManager;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.internal.core.Game;

/**
Expand Down Expand Up @@ -65,7 +65,7 @@ public void handle(EntityPlayer player) {
MCNetworkManager network = (MCNetworkManager) Game.network();
PacketHandler<?> packetHandler = network.getPacketType(data.readInt());
int subId = data.readInt();
MCPacket packet = new MCPacket(data.slice(), ((Entity) Game.natives().toNova(player)).components.get(Player.class));
MCPacket packet = new MCPacket(data.slice(), EntityConverter.instance().toNova(player).components.get(Player.class));
//Set the ID of the packet
packet.setID(subId);
packetHandler.read(packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public EnumFacing toNative(Direction novaObj) {
case SOUTH: return EnumFacing.SOUTH;
case WEST: return EnumFacing.WEST;
case EAST: return EnumFacing.EAST;
default: return (EnumFacing) null;
default: return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import nova.core.block.component.LightEmitter;
import nova.core.component.misc.Collider;
import nova.core.component.renderer.StaticRenderer;
import nova.core.component.transform.BlockTransform;
import nova.core.item.ItemFactory;
import nova.core.render.model.CustomModel;
import nova.core.retention.Data;
Expand All @@ -47,6 +46,7 @@
import nova.core.wrapper.mc.forge.v17.wrapper.cuboid.CuboidConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.data.DataConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.item.ItemConverter;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

Expand Down Expand Up @@ -113,7 +113,7 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {

@Override
public ItemFactory getItemFactory() {
return Game.natives().toNova(new ItemStack(Item.getItemFromBlock(mcBlock)));
return ItemConverter.instance().toNova(new ItemStack(Item.getItemFromBlock(mcBlock))).getFactory();
}

public int xi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import nova.core.component.transform.BlockTransform;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v17.wrapper.block.world.WorldConverter;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.util.Optional;
Expand Down Expand Up @@ -61,8 +60,8 @@ public IBlockAccess blockAccess() {

@Override
public void setWorld(World world) {
net.minecraft.world.World oldWorld = Game.natives().toNative(this.world);
net.minecraft.world.World newWorld = Game.natives().toNative(world);
net.minecraft.world.World oldWorld = (net.minecraft.world.World) WorldConverter.instance().toNative(this.world);
net.minecraft.world.World newWorld = (net.minecraft.world.World) WorldConverter.instance().toNative(world);
Optional<TileEntity> tileEntity = Optional.ofNullable(oldWorld.getTileEntity((int) position.getX(), (int) position.getY(), (int) position.getZ()));
Optional<NBTTagCompound> nbt = Optional.empty();
if (tileEntity.isPresent()) {
Expand All @@ -82,7 +81,7 @@ public void setWorld(World world) {

@Override
public void setPosition(Vector3D position) {
net.minecraft.world.World world = Game.natives().toNative(this.world);
net.minecraft.world.World world = (net.minecraft.world.World) WorldConverter.instance().toNative(this.world);
Optional<TileEntity> tileEntity = Optional.ofNullable(world.getTileEntity((int) this.position.getX(), (int) this.position.getY(), (int) this.position.getZ()));
Optional<NBTTagCompound> nbt = Optional.empty();
if (tileEntity.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import nova.core.block.component.LightEmitter;
import nova.core.component.Updater;
import nova.core.component.misc.Collider;
import nova.core.component.renderer.DynamicRenderer;
import nova.core.component.renderer.Renderer;
import nova.core.component.renderer.StaticRenderer;
import nova.core.retention.Storable;
Expand All @@ -57,6 +56,10 @@
import nova.core.util.math.MatrixStack;
import nova.core.util.shape.Cuboid;
import nova.core.wrapper.mc.forge.v17.util.WrapperEvent;
import nova.core.wrapper.mc.forge.v17.wrapper.block.world.WorldConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.cuboid.CuboidConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.item.ItemConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.render.BWModel;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
Expand Down Expand Up @@ -149,7 +152,7 @@ public Block getBlockInstance(net.minecraft.world.IBlockAccess access, Vector3D
e.printStackTrace();
}
}
return getBlockInstance((nova.core.world.World) Game.natives().toNova(access), position);
return getBlockInstance(WorldConverter.instance().toNova(access), position);

}

Expand Down Expand Up @@ -193,18 +196,18 @@ public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metad
Block.DropEvent event = new Block.DropEvent(blockInstance);
blockInstance.events.publish(event);

return new ArrayList<>(
event.drops
.stream()
.map(item -> (ItemStack) Game.natives().toNative(item))
.collect(Collectors.toCollection(ArrayList::new))
);
return event.drops
.stream()
.map(ItemConverter.instance()::toNative)
.collect(Collectors.toCollection(ArrayList::new));
}

@Override
public boolean hasTileEntity(int metadata) {
// A block requires a TileEntity if it stores data or if it ticks.
return Storable.class.isAssignableFrom(blockClass) || Stateful.class.isAssignableFrom(blockClass) || Updater.class.isAssignableFrom(blockClass);
return Storable.class.isAssignableFrom(blockClass)
|| Stateful.class.isAssignableFrom(blockClass)
|| Updater.class.isAssignableFrom(blockClass);
}

@Override
Expand Down Expand Up @@ -261,7 +264,7 @@ public void onNeighborBlockChange(World world, int x, int y, int z, net.minecraf
@Override
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willHarvest) {
Block blockInstance = getBlockInstance(world, new Vector3D(x, y, z));
Block.RemoveEvent evt = new Block.RemoveEvent(Optional.of(Game.natives().toNova(player)));
Block.RemoveEvent evt = new Block.RemoveEvent(Optional.of(EntityConverter.instance().toNova(player)));
blockInstance.events.publish(evt);
if (evt.result) {
return super.removedByPlayer(world, player, x, y, z, willHarvest);
Expand All @@ -273,7 +276,7 @@ public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, i
public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) {
Block blockInstance = getBlockInstance(world, new Vector3D(x, y, z));
MovingObjectPosition mop = player.rayTrace(10, 1);
Block.LeftClickEvent evt = new Block.LeftClickEvent(Game.natives().toNova(player), Direction.fromOrdinal(mop.sideHit), new Vector3D(mop.hitVec.xCoord, mop.hitVec.yCoord, mop.hitVec.zCoord));
Block.LeftClickEvent evt = new Block.LeftClickEvent(EntityConverter.instance().toNova(player), Direction.fromOrdinal(mop.sideHit), new Vector3D(mop.hitVec.xCoord, mop.hitVec.yCoord, mop.hitVec.zCoord));
blockInstance.events.publish(evt);
}

Expand All @@ -285,15 +288,15 @@ public void registerBlockIcons(IIconRegister ir) {
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
Block blockInstance = getBlockInstance(world, new Vector3D(x, y, z));
Block.RightClickEvent evt = new Block.RightClickEvent(Game.natives().toNova(player), Direction.fromOrdinal(side), new Vector3D(hitX, hitY, hitZ));
Block.RightClickEvent evt = new Block.RightClickEvent(EntityConverter.instance().toNova(player), Direction.fromOrdinal(side), new Vector3D(hitX, hitY, hitZ));
blockInstance.events.publish(evt);
return evt.result;
}

@Override
public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {
Block blockInstance = getBlockInstance(world, new Vector3D(x, y, z));
blockInstance.components.getOp(Collider.class).ifPresent(collider -> blockInstance.events.publish(new Collider.CollideEvent(Game.natives().toNova(entity))));
blockInstance.components.getOp(Collider.class).ifPresent(collider -> blockInstance.events.publish(new Collider.CollideEvent(EntityConverter.instance().toNova(entity))));
}

@Override
Expand All @@ -312,24 +315,25 @@ public AxisAlignedBB getSelectedBoundingBoxFromPool(World world, int x, int y, i

if (blockInstance.components.has(Collider.class)) {
Cuboid cuboid = blockInstance.components.get(Collider.class).boundingBox.get();
return Game.natives().toNative(cuboid.add(new Vector3D(x, y, z)));
return CuboidConverter.instance().toNative(cuboid.add(new Vector3D(x, y, z)));
}
return super.getSelectedBoundingBoxFromPool(world, x, y, z);
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB aabb, List list, Entity entity) {
Block blockInstance = getBlockInstance(world, new Vector3D(x, y, z));
blockInstance.components.getOp(Collider.class).ifPresent(
collider -> {
Set<Cuboid> boxes = collider.occlusionBoxes.apply(Optional.ofNullable(entity != null ? Game.natives().toNova(entity) : null));
Set<Cuboid> boxes = collider.occlusionBoxes.apply(Optional.ofNullable(entity != null ? EntityConverter.instance().toNova(entity) : null));

list.addAll(
boxes
.stream()
.map(c -> c.add(new Vector3D(x, y, z)))
.filter(c -> c.intersects((Cuboid) Game.natives().toNova(aabb)))
.map(cuboid -> Game.natives().toNative(cuboid))
.filter(c -> c.intersects(CuboidConverter.instance().toNova(aabb)))
.map(CuboidConverter.instance()::toNative)
.collect(Collectors.toList())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@
import nova.core.util.shape.Cuboid;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v17.launcher.NovaMinecraft;
import nova.core.wrapper.mc.forge.v17.wrapper.block.BlockConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.block.backward.BWBlock;
import nova.core.wrapper.mc.forge.v17.wrapper.block.forward.FWBlock;
import nova.core.wrapper.mc.forge.v17.wrapper.block.forward.MCBlockTransform;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.backward.BWEntity;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.FWEntity;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.forward.MCEntityTransform;
import nova.core.wrapper.mc.forge.v17.wrapper.item.ItemConverter;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -93,7 +97,7 @@ public Optional<Block> getBlock(Vector3D position) {
@Override
public boolean setBlock(Vector3D position, BlockFactory blockFactory) {
//TODO: Implement object arguments
net.minecraft.block.Block mcBlock = Game.natives().toNative(blockFactory.build());
net.minecraft.block.Block mcBlock = BlockConverter.instance().toNative(blockFactory);
return world().setBlock((int) position.getX(), (int) position.getY(), (int) position.getZ(), mcBlock != null ? mcBlock : Blocks.air);
}

Expand All @@ -116,6 +120,7 @@ public Entity addClientEntity(EntityFactory factory) {
}

@Override
@SuppressWarnings("unchecked")
public Entity addClientEntity(Entity entity) {
return NovaMinecraft.proxy.spawnParticle(world(), entity);
}
Expand All @@ -128,23 +133,24 @@ public void removeEntity(Entity entity) {
}

@Override
@SuppressWarnings("unchecked")
public Set<Entity> getEntities(Cuboid bound) {
return (Set) world().getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(bound.min.getX(), bound.min.getY(), bound.min.getZ(), bound.max.getX(), bound.max.getY(), bound.max.getZ()))
return ((List<net.minecraft.entity.Entity>) world().getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(bound.min.getX(), bound.min.getY(), bound.min.getZ(), bound.max.getX(), bound.max.getY(), bound.max.getZ())))
.stream()
.map(mcEnt -> Game.natives().getNative(Entity.class, net.minecraft.entity.Entity.class).toNova((net.minecraft.entity.Entity) mcEnt))
.map(EntityConverter.instance()::toNova)
.collect(Collectors.toSet());
}

@Override
public Entity addEntity(Vector3D position, Item item) {
EntityItem entityItem = new EntityItem(world(), position.getX(), position.getY(), position.getZ(), Game.natives().toNative(item));
EntityItem entityItem = new EntityItem(world(), position.getX(), position.getY(), position.getZ(), ItemConverter.instance().toNative(item));
world().spawnEntityInWorld(entityItem);
return new BWEntity(entityItem);
}

@Override
public Optional<Entity> getEntity(String uniqueID) {
return Optional.ofNullable(Game.natives().toNova(world().getEntityByID(Integer.parseInt(uniqueID))));
return Optional.ofNullable(world().getEntityByID(Integer.parseInt(uniqueID))).map(EntityConverter.instance()::toNova);
}

@Override
Expand Down
Loading

0 comments on commit baf9cd1

Please sign in to comment.