Skip to content

Commit

Permalink
Merge pull request #301 from NOVA-Team/enhancement/block-coords-long
Browse files Browse the repository at this point in the history
Change the Block coordinate methods (x(), y(), z()) return type to long
  • Loading branch information
ExE-Boss authored Apr 25, 2017
2 parents a33b6ee + 5a2e2f3 commit 1c34713
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@
import nova.core.util.shape.Cuboid;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v17.util.WrapperEvent;
import nova.core.wrapper.mc.forge.v17.wrapper.block.world.BWWorld;
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.data.DataConverter;
import nova.core.wrapper.mc.forge.v17.wrapper.entity.EntityConverter;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -67,37 +70,43 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
this.mcBlock = block;
components.add(new BWBlockTransform(this, world, pos));
components.add(new BlockProperty.Opacity().setOpacity(mcBlock.getMaterial().isOpaque() ? 1 : 0));
if (mcBlock.isReplaceable(getMcBlockAccess(), x(), y(), z()))
if (mcBlock.isReplaceable(blockAccess(), xi(), yi(), zi()))
components.add(BlockProperty.Replaceable.instance());

BlockProperty.BlockSound blockSound = components.add(new BlockProperty.BlockSound());
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", mcBlock.stepSound.func_150496_b()));
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK, new Sound("", mcBlock.stepSound.getBreakSound()));
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK, new Sound("", mcBlock.stepSound.getStepResourcePath()));

components.add(new LightEmitter()).setEmittedLevel(() -> mcBlock.getLightValue(getMcBlockAccess(), x(), y(), z()) / 15.0);
components.add(new LightEmitter()).setEmittedLevel(() -> mcBlock.getLightValue(blockAccess(), xi(), yi(), zi()) / 15.0);
components.add(new Collider(this))
.setBoundingBox(() -> new Cuboid(mcBlock.getBlockBoundsMinX(), mcBlock.getBlockBoundsMinY(), mcBlock.getBlockBoundsMinZ(), mcBlock.getBlockBoundsMaxX(), mcBlock.getBlockBoundsMaxY(), mcBlock.getBlockBoundsMaxZ()))
.setOcclusionBoxes(entity -> {
List<AxisAlignedBB> aabbs = new ArrayList<>();
mcBlock.addCollisionBoxesToList(
Game.natives().toNative(world()),
(int) position().getX(),
(int) position().getY(),
(int) position().getZ(),
Game.natives().toNative(entity.isPresent() ? entity.get().components.get(Collider.class).boundingBox.get() : Cuboid.ONE.add(pos)),
aabbs,
entity.isPresent() ? Game.natives().toNative(entity.get()) : null
);

if (blockAccess() instanceof net.minecraft.world.World)
mcBlock.addCollisionBoxesToList(
(net.minecraft.world.World) blockAccess(),
(int) position().getX(),
(int) position().getY(),
(int) position().getZ(),
CuboidConverter.instance().toNative(entity
.flatMap(e -> e.components.getOp(Collider.class))
.map(c -> c.boundingBox.get())
.orElseGet(() -> Cuboid.ONE.add(pos))),
aabbs,
entity.map(EntityConverter.instance()::toNative).orElse(null)
);
return aabbs.stream()
.map(aabb -> (Cuboid) Game.natives().toNova(aabb))
.map(CuboidConverter.instance()::toNova)
.map(cuboid -> cuboid.subtract(pos))
.collect(Collectors.toSet());
}).setSelectionBoxes(entity -> {
final AxisAlignedBB bb = mcBlock.getSelectedBoundingBoxFromPool(((net.minecraft.world.World) blockAccess()), xi(), yi(), zi());
Cuboid cuboid = CuboidConverter.instance().toNova(bb);
return Collections.singleton(cuboid.subtract(position()));
});
//TODO: Set selection bounds
components.add(new StaticRenderer())
.onRender(model -> model.addChild(new CustomModel(self -> RenderBlocks.getInstance().renderStandardBlock(mcBlock, x(), y(), z()))));
.onRender(model -> model.addChild(new CustomModel(self -> RenderBlocks.getInstance().renderStandardBlock(mcBlock, xi(), yi(), zi()))));
WrapperEvent.BWBlockCreate event = new WrapperEvent.BWBlockCreate(world, pos, this, mcBlock);
Game.events().publish(event);
}
Expand All @@ -107,37 +116,49 @@ public ItemFactory getItemFactory() {
return Game.natives().toNova(new ItemStack(Item.getItemFromBlock(mcBlock)));
}

public IBlockAccess getMcBlockAccess() {
public int xi() {
return (int) x();
}

public int yi() {
return (int) y();
}

public int zi() {
return (int) z();
}

public net.minecraft.block.Block block() {
return mcBlock;
}

public IBlockAccess blockAccess() {
return WorldConverter.instance().toNative(world());
}

public int getMetadata() {
return getMcBlockAccess().getBlockMetadata(x(), y(), z());
return blockAccess().getBlockMetadata(xi(), yi(), zi());
}

public Optional<TileEntity> getTileEntity() {
return Optional.ofNullable(getTileEntityImpl());
}

private TileEntity getTileEntityImpl() {
if (mcTileEntity == null && mcBlock.hasTileEntity(getMetadata())) {
mcTileEntity = getMcBlockAccess().getTileEntity(x(), y(), z());
mcTileEntity = blockAccess().getTileEntity(xi(), yi(), zi());
}
return mcTileEntity;
return Optional.ofNullable(mcTileEntity);
}

@Override
public boolean canReplace() {
return mcBlock.canPlaceBlockAt((net.minecraft.world.World) getMcBlockAccess(), x(), y(), z());
return mcBlock.canPlaceBlockAt((net.minecraft.world.World) blockAccess(), xi(), yi(), zi());
}

@Override
public boolean shouldDisplacePlacement() {
if (mcBlock == Blocks.snow_layer && (getMcBlockAccess().getBlockMetadata(x(), y(), z()) & 7) < 1) {
if (mcBlock == Blocks.snow_layer && (blockAccess().getBlockMetadata(xi(), yi(), zi()) & 7) < 1) {
return false;
}

if (mcBlock == Blocks.vine || mcBlock == Blocks.tallgrass || mcBlock == Blocks.deadbush || mcBlock.isReplaceable(getMcBlockAccess(), x(), y(), z())) {
if (mcBlock == Blocks.vine || mcBlock == Blocks.tallgrass || mcBlock == Blocks.deadbush || mcBlock.isReplaceable(blockAccess(), xi(), yi(), zi())) {
return false;
}
return super.shouldDisplacePlacement();
Expand All @@ -146,23 +167,17 @@ public boolean shouldDisplacePlacement() {
@Override
public void save(Data data) {
Storable.super.save(data);

TileEntity tileEntity = getTileEntityImpl();
if (tileEntity != null) {
getTileEntity().ifPresent(tile -> {
NBTTagCompound nbt = new NBTTagCompound();
tileEntity.writeToNBT(nbt);
data.putAll(Game.natives().toNova(nbt));
}
tile.writeToNBT(nbt);
data.putAll(DataConverter.instance().toNova(nbt));
});
}

@Override
public void load(Data data) {
Storable.super.load(data);

TileEntity tileEntity = getTileEntityImpl();
if (tileEntity != null) {
tileEntity.writeToNBT(Game.natives().toNative(data));
}
getTileEntity().ifPresent(tile -> tile.readFromNBT(DataConverter.instance().toNative(data)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@
import nova.core.util.shape.Cuboid;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v18.util.WrapperEvent;
import nova.core.wrapper.mc.forge.v18.wrapper.VectorConverter;
import nova.core.wrapper.mc.forge.v18.wrapper.block.world.WorldConverter;
import nova.core.wrapper.mc.forge.v18.wrapper.cuboid.CuboidConverter;
import nova.core.wrapper.mc.forge.v18.wrapper.data.DataConverter;
import nova.core.wrapper.mc.forge.v18.wrapper.entity.EntityConverter;
import nova.core.wrapper.mc.forge.v18.wrapper.render.backward.BWBakedModel;
import nova.internal.core.Game;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -69,34 +74,40 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
this.mcBlock = block;
components.add(new BWBlockTransform(this, world, pos));
components.add(new BlockProperty.Opacity().setOpacity(mcBlock.getMaterial().blocksLight() ? 1 : 0));
if (mcBlock.isReplaceable((net.minecraft.world.World) getMcBlockAccess(), new BlockPos(x(), y(), z())))
if (mcBlock.isReplaceable((net.minecraft.world.World) blockAccess(), new BlockPos(x(), y(), z())))
components.add(BlockProperty.Replaceable.instance());

BlockProperty.BlockSound blockSound = components.add(new BlockProperty.BlockSound());
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", mcBlock.stepSound.getPlaceSound()));
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK, new Sound("", mcBlock.stepSound.getBreakSound()));
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK, new Sound("", mcBlock.stepSound.getStepSound()));

components.add(new LightEmitter()).setEmittedLevel(() -> mcBlock.getLightValue(getMcBlockAccess(), new BlockPos(x(), y(), z())) / 15.0);
components.add(new LightEmitter()).setEmittedLevel(() -> mcBlock.getLightValue(blockAccess(), new BlockPos(x(), y(), z())) / 15.0);
components.add(new Collider(this))
.setBoundingBox(() -> new Cuboid(mcBlock.getBlockBoundsMinX(), mcBlock.getBlockBoundsMinY(), mcBlock.getBlockBoundsMinZ(), mcBlock.getBlockBoundsMaxX(), mcBlock.getBlockBoundsMaxY(), mcBlock.getBlockBoundsMaxZ()))
.setOcclusionBoxes(entity -> {
List<AxisAlignedBB> aabbs = new ArrayList<>();
mcBlock.addCollisionBoxesToList(
Game.natives().toNative(world()),
new BlockPos(x(), y(), z()),
blockState(),
Game.natives().toNative(entity.isPresent() ? entity.get().components.get(Collider.class).boundingBox.get() : Cuboid.ONE.add(pos)),
aabbs,
entity.isPresent() ? Game.natives().toNative(entity.get()) : null
);

if (blockAccess() instanceof net.minecraft.world.World)
mcBlock.addCollisionBoxesToList(
(net.minecraft.world.World) blockAccess(),
blockPos(),
blockState(),
CuboidConverter.instance().toNative(entity
.flatMap(e -> e.components.getOp(Collider.class))
.map(c -> c.boundingBox.get())
.orElseGet(() -> Cuboid.ONE.add(pos))),
aabbs,
entity.map(EntityConverter.instance()::toNative).orElse(null)
);
return aabbs.stream()
.map(aabb -> (Cuboid) Game.natives().toNova(aabb))
.map(CuboidConverter.instance()::toNova)
.map(cuboid -> cuboid.subtract(pos))
.collect(Collectors.toSet());
}).setSelectionBoxes(entity -> {
final AxisAlignedBB bb = mcBlock.getSelectedBoundingBox(((net.minecraft.world.World) blockAccess()), blockPos());
Cuboid cuboid = CuboidConverter.instance().toNova(bb);
return Collections.singleton(cuboid.subtract(position()));
});
//TODO: Set selection bounds
components.add(new StaticRenderer())
.onRender(model -> {
switch (block.getRenderType()) {
Expand All @@ -114,7 +125,7 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
case 3:
// model rendering
model.addChild(new BWBakedModel(Minecraft.getMinecraft().getBlockRendererDispatcher()
.getModelFromBlockState(blockState(), getMcBlockAccess(), new BlockPos(x(), y(), z())), DefaultVertexFormats.BLOCK));
.getModelFromBlockState(blockState(), blockAccess(), blockPos()), DefaultVertexFormats.BLOCK));
break;
}
});
Expand All @@ -129,28 +140,32 @@ public ItemFactory getItemFactory() {
return Game.natives().toNova(new ItemStack(Item.getItemFromBlock(mcBlock)));
}

public IBlockAccess getMcBlockAccess() {
public net.minecraft.block.Block block() {
return mcBlock;
}

public BlockPos blockPos() {
return VectorConverter.instance().toNative(position());
}

public IBlockAccess blockAccess() {
return WorldConverter.instance().toNative(world());
}

public IBlockState blockState() {
return getMcBlockAccess().getBlockState(new BlockPos(x(), y(), z()));
return blockAccess().getBlockState(new BlockPos(x(), y(), z()));
}

public Optional<TileEntity> getTileEntity() {
return Optional.ofNullable(getTileEntityImpl());
}

private TileEntity getTileEntityImpl() {
if (mcTileEntity == null && mcBlock.hasTileEntity(blockState())) {
mcTileEntity = getMcBlockAccess().getTileEntity(new BlockPos(x(), y(), z()));
mcTileEntity = blockAccess().getTileEntity(blockPos());
}
return mcTileEntity;
return Optional.ofNullable(mcTileEntity);
}

@Override
public boolean canReplace() {
return mcBlock.canPlaceBlockAt((net.minecraft.world.World) getMcBlockAccess(), new BlockPos(x(), y(), z()));
return mcBlock.canPlaceBlockAt((net.minecraft.world.World) blockAccess(), new BlockPos(x(), y(), z()));
}

@Override
Expand All @@ -168,23 +183,17 @@ public boolean shouldDisplacePlacement() {
@Override
public void save(Data data) {
Storable.super.save(data);

TileEntity tileEntity = getTileEntityImpl();
if (tileEntity != null) {
getTileEntity().ifPresent(tile -> {
NBTTagCompound nbt = new NBTTagCompound();
tileEntity.writeToNBT(nbt);
data.putAll(Game.natives().toNova(nbt));
}
tile.writeToNBT(nbt);
data.putAll(DataConverter.instance().toNova(nbt));
});
}

@Override
public void load(Data data) {
Storable.super.load(data);

TileEntity tileEntity = getTileEntityImpl();
if (tileEntity != null) {
tileEntity.writeToNBT(Game.natives().toNative(data));
}
getTileEntity().ifPresent(tile -> tile.readFromNBT(DataConverter.instance().toNative(data)));
}

@Override
Expand Down
Loading

0 comments on commit 1c34713

Please sign in to comment.