Skip to content

Commit

Permalink
Merge pull request #310 from NOVA-Team/components/block-property
Browse files Browse the repository at this point in the history
Finish BlockProperty implementation
  • Loading branch information
ExE-Boss committed Oct 23, 2017
2 parents baf9cd1 + d5b2d15 commit 6ee0f33
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ private void registerNOVAToMinecraft() {
public boolean canReplace() {
return true;
}
}, evt -> {
});
}, evt -> {});

blockManager.register(airBlock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ public BWBlock(net.minecraft.block.Block block) {
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(blockAccess(), xi(), yi(), zi()))
components.add(BlockProperty.Replaceable.instance());
components.add(new BlockProperty.Opacity()).setOpacity(() -> mcBlock.getMaterial().isOpaque() ? 1 : 0);
BlockProperty.Replaceable replaceable = components.add(new BlockProperty.Replaceable());
if (block != Blocks.air) {
replaceable.setReplaceable(() -> mcBlock.canPlaceBlockAt((net.minecraft.world.World) blockAccess(), xi(), yi(), zi()));
}

BlockProperty.BlockSound blockSound = components.add(new BlockProperty.BlockSound());
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", mcBlock.stepSound.func_150496_b()));
Expand Down Expand Up @@ -147,11 +149,6 @@ public Optional<TileEntity> getTileEntity() {
return Optional.ofNullable(mcTileEntity);
}

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

@Override
public boolean shouldDisplacePlacement() {
if (mcBlock == Blocks.snow_layer && (blockAccess().getBlockMetadata(xi(), yi(), zi()) & 7) < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,34 @@ public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
@Override
public float getExplosionResistance(Entity expEntity, World world, int x, int y, int z, double explosionX, double p_explosionresistance, double explosionY) {
// TODO: Maybe do something withPriority these parameters.
return (float) getBlockInstance(world, new Vector3D(x, y, z)).getResistance() * 30;

// This number was calculated from the blast resistance of Stone,
// which requires exactly one cubic meter of TNT to get blown up.
//
// 1. During construction, the setResistance method is called
// on minecraft:stone with a value of 10.
//
// 2. The setResistance method multiplies that by 3 and assigns
// the result to the blockResistance instance variable.
//
// 3. Finally, the getExplosionResistance method divides the
// blockResistance instance variable by 5 and returns the result.
//
// From this we see that minecraft:stone’s final blast resistance is 6.

return (float) getBlockInstance(world, new Vector3D(x, y, z)).getResistance() * 6;
}

@Override
public float getBlockHardness(World world, int x, int y, int z) {
return (float) getBlockInstance(world, new Vector3D(x, y, z)).getHardness() * 2;
}

@Override
public boolean isReplaceable(IBlockAccess access, int x, int y, int z) {
return getBlockInstance(access, new Vector3D(x, y, z))
.components.getOp(BlockProperty.Replaceable.class)
.filter(BlockProperty.Replaceable::isReplaceable)
.isPresent();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
* Copyright (c) 2016 NOVA, All rights reserved.
* This library is free software, licensed under GNU Lesser General Public License version 3
*
* This file is part of NOVA.
*
* NOVA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NOVA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.wrapper.mc.forge.v17.wrapper.block.forward;

import net.minecraft.block.Block;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
* Copyright (c) 2016 NOVA, All rights reserved.
* This library is free software, licensed under GNU Lesser General Public License version 3
*
* This file is part of NOVA.
*
* NOVA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NOVA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.wrapper.mc.forge.v17.wrapper.block.forward;

import net.minecraft.block.material.MapColor;
Expand All @@ -17,20 +37,26 @@ public class ProxyMaterial extends Material {
* Construct a new proxy material.
* @param color The map color.
* @param opacity The Opacity to use.
* @param replaceable If this block is replaceable.
*/
public ProxyMaterial(MapColor color, Optional<BlockProperty.Opacity> opacity, Optional<BlockProperty.Replaceable> replaceable) {
super(color);
this.opacity = opacity;
this.replaceable = replaceable;
}

@Override
public boolean getCanBlockGrass() {
return opacity.isPresent() ? opacity.get().isOpaque() : super.isOpaque();
}

@Override
public boolean isOpaque() {
return opacity.isPresent() ? opacity.get().opacity == 1 : super.isOpaque();
return opacity.isPresent() ? opacity.get().isOpaque() : super.isOpaque();
}

@Override
public boolean isReplaceable() {
return replaceable.isPresent() || super.isReplaceable();
return replaceable.map(BlockProperty.Replaceable::isReplaceable).orElseGet(super::isReplaceable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ private void registerNOVAToMinecraft() {
public boolean canReplace() {
return true;
}
}, evt -> {
});
}, evt -> {});

blockManager.register(airBlock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public BWBlock(net.minecraft.block.Block block) {
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) blockAccess(), new BlockPos(x(), y(), z())))
components.add(BlockProperty.Replaceable.instance());
components.add(new BlockProperty.Opacity()).setOpacity(() -> mcBlock.getMaterial().isOpaque() ? 1 : 0);
BlockProperty.Replaceable replaceable = components.add(new BlockProperty.Replaceable());
if (block != Blocks.air) {
replaceable.setReplaceable(() -> mcBlock.canPlaceBlockAt((net.minecraft.world.World) blockAccess(), blockPos()));
}

BlockProperty.BlockSound blockSound = components.add(new BlockProperty.BlockSound());
blockSound.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", mcBlock.stepSound.getPlaceSound()));
Expand Down Expand Up @@ -164,18 +166,13 @@ public Optional<TileEntity> getTileEntity() {
return Optional.ofNullable(mcTileEntity);
}

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

@Override
public boolean shouldDisplacePlacement() {
if (mcBlock == Blocks.snow_layer && ((int) blockState().getValue(BlockSnow.LAYERS) < 1)) {
return false;
}

if (mcBlock == Blocks.vine || mcBlock == Blocks.tallgrass || mcBlock == Blocks.deadbush || mcBlock.isReplaceable((net.minecraft.world.World) WorldConverter.instance().toNative(world()), new BlockPos(x(), y(), z()))) {
if (mcBlock == Blocks.vine || mcBlock == Blocks.tallgrass || mcBlock == Blocks.deadbush || mcBlock.isReplaceable((net.minecraft.world.World) blockAccess(), blockPos())) {
return false;
}
return super.shouldDisplacePlacement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void onBlockHarvested(World world, BlockPos pos, IBlockState state, Entit
// hack is needed because the player sets the block to air *before*
// getting the drops. woo good logic from mojang.
if (!player.capabilities.isCreativeMode) {
harvestedBlocks.put(new BlockPosition(world, pos.getX(), pos.getY(), pos.getZ()), getBlockInstance(world, new Vector3D(pos.getX(), pos.getY(), pos.getZ())));
harvestedBlocks.put(new BlockPosition(world, pos.getX(), pos.getY(), pos.getZ()), getBlockInstance(world, VectorConverter.instance().toNova(pos)));
}
}

Expand All @@ -176,7 +176,7 @@ public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState st
if (harvestedBlocks.containsKey(position)) {
blockInstance = harvestedBlocks.remove(position);
} else {
blockInstance = getBlockInstance(world, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
blockInstance = getBlockInstance(world, VectorConverter.instance().toNova(pos));
}

Block.DropEvent event = new Block.DropEvent(blockInstance);
Expand Down Expand Up @@ -260,7 +260,7 @@ public void onEntityCollidedWithBlock(World world, BlockPos pos, Entity entity)

@Override
public void setBlockBoundsBasedOnState(IBlockAccess access, BlockPos pos) {
Block blockInstance = getBlockInstance(access, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
Block blockInstance = getBlockInstance(access, VectorConverter.instance().toNova(pos));
if (blockInstance.components.has(Collider.class)) {
Cuboid cuboid = blockInstance.components.get(Collider.class).boundingBox.get();
setBlockBounds((float) cuboid.min.getX(), (float) cuboid.min.getY(), (float) cuboid.min.getZ(), (float) cuboid.max.getX(), (float) cuboid.max.getY(), (float) cuboid.max.getZ());
Expand All @@ -269,7 +269,7 @@ public void setBlockBoundsBasedOnState(IBlockAccess access, BlockPos pos) {

@Override
public AxisAlignedBB getSelectedBoundingBox(World world, BlockPos pos) {
Block blockInstance = getBlockInstance(world, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
Block blockInstance = getBlockInstance(world, VectorConverter.instance().toNova(pos));

if (blockInstance.components.has(Collider.class)) {
Cuboid cuboid = blockInstance.components.get(Collider.class).boundingBox.get();
Expand All @@ -281,7 +281,7 @@ public AxisAlignedBB getSelectedBoundingBox(World world, BlockPos pos) {
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void addCollisionBoxesToList(World world, BlockPos pos, IBlockState state, AxisAlignedBB mask, List list, Entity entity) {
Block blockInstance = getBlockInstance(world, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
Block blockInstance = getBlockInstance(world, VectorConverter.instance().toNova(pos));
blockInstance.components.getOp(Collider.class).ifPresent(
collider -> {
Set<Cuboid> boxes = collider.occlusionBoxes.apply(Optional.ofNullable(entity != null ? EntityConverter.instance().toNova(entity) : null));
Expand Down Expand Up @@ -332,7 +332,7 @@ public boolean isFullCube() {

@Override
public int getLightValue(IBlockAccess access, BlockPos pos) {
Block blockInstance = getBlockInstance(access, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
Block blockInstance = getBlockInstance(access, VectorConverter.instance().toNova(pos));
Optional<LightEmitter> opEmitter = blockInstance.components.getOp(LightEmitter.class);

if (opEmitter.isPresent()) {
Expand All @@ -349,23 +349,23 @@ public EnumWorldBlockLayer getBlockLayer() {

@Override
public boolean canConnectRedstone(IBlockAccess access, BlockPos pos, EnumFacing side) {
Block blockInstance = getBlockInstance(access, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
Block blockInstance = getBlockInstance(access, VectorConverter.instance().toNova(pos));
WrapperEvent.RedstoneConnect event = new WrapperEvent.RedstoneConnect(blockInstance.world(), blockInstance.position(), Direction.fromOrdinal(side.ordinal()));
Game.events().publish(event);
return event.canConnect;
}

@Override
public int isProvidingWeakPower(IBlockAccess access, BlockPos pos, IBlockState state, EnumFacing side) {
Block blockInstance = getBlockInstance(access, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
Block blockInstance = getBlockInstance(access, VectorConverter.instance().toNova(pos));
WrapperEvent.WeakRedstone event = new WrapperEvent.WeakRedstone(blockInstance.world(), blockInstance.position(), Direction.fromOrdinal(side.ordinal()));
Game.events().publish(event);
return event.power;
}

@Override
public int isProvidingStrongPower(IBlockAccess access, BlockPos pos, IBlockState state, EnumFacing side) {
Block blockInstance = getBlockInstance(access, new Vector3D(pos.getX(), pos.getY(), pos.getZ()));
Block blockInstance = getBlockInstance(access, VectorConverter.instance().toNova(pos));
WrapperEvent.StrongRedstone event = new WrapperEvent.StrongRedstone(blockInstance.world(), blockInstance.position(), Direction.fromOrdinal(side.ordinal()));
Game.events().publish(event);
return event.power;
Expand All @@ -384,11 +384,34 @@ public String getLocalizedName() {
@Override
public float getExplosionResistance(World world, BlockPos pos, Entity exploder, Explosion explosion) {
// TODO: Maybe do something withPriority these parameters.
return (float) getBlockInstance(world, new Vector3D(pos.getX(), pos.getY(), pos.getZ())).getResistance() * 30;

// This number was calculated from the blast resistance of Stone,
// which requires exactly one cubic meter of TNT to get blown up.
//
// 1. During construction, the setResistance method is called
// on minecraft:stone with a value of 10.
//
// 2. The setResistance method multiplies that by 3 and assigns
// the result to the blockResistance instance variable.
//
// 3. Finally, the getExplosionResistance method divides the
// blockResistance instance variable by 5 and returns the result.
//
// From this we see that minecraft:stone’s final blast resistance is 6.

return (float) getBlockInstance(world, VectorConverter.instance().toNova(pos)).getResistance() * 6;
}

@Override
public float getBlockHardness(World world, BlockPos pos) {
return (float) getBlockInstance(world, new Vector3D(pos.getX(), pos.getY(), pos.getZ())).getHardness() * 2;
return (float) getBlockInstance(world, VectorConverter.instance().toNova(pos)).getHardness() * 2;
}

@Override
public boolean isReplaceable(World world, BlockPos pos) {
return getBlockInstance(world, VectorConverter.instance().toNova(pos))
.components.getOp(BlockProperty.Replaceable.class)
.filter(BlockProperty.Replaceable::isReplaceable)
.isPresent();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
* Copyright (c) 2016 NOVA, All rights reserved.
* This library is free software, licensed under GNU Lesser General Public License version 3
*
* This file is part of NOVA.
*
* NOVA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NOVA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.wrapper.mc.forge.v18.wrapper.block.forward;

import net.minecraft.block.Block;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
* Copyright (c) 2016 NOVA, All rights reserved.
* This library is free software, licensed under GNU Lesser General Public License version 3
*
* This file is part of NOVA.
*
* NOVA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NOVA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.wrapper.mc.forge.v18.wrapper.block.forward;

import net.minecraft.block.material.MapColor;
Expand All @@ -17,6 +37,7 @@ public class ProxyMaterial extends Material {
* Construct a new proxy material.
* @param color The map color.
* @param opacity The Opacity to use.
* @param replaceable If this block is replaceable.
*/
public ProxyMaterial(MapColor color, Optional<BlockProperty.Opacity> opacity, Optional<BlockProperty.Replaceable> replaceable) {
super(color);
Expand All @@ -26,16 +47,16 @@ public ProxyMaterial(MapColor color, Optional<BlockProperty.Opacity> opacity, Op

@Override
public boolean blocksLight() {
return opacity.isPresent() ? opacity.get().opacity == 1 : super.blocksLight();
return opacity.isPresent() ? opacity.get().isOpaque() : super.blocksLight();
}

@Override
public boolean isOpaque() {
return opacity.isPresent() ? opacity.get().opacity == 1 : super.isOpaque();
return opacity.isPresent() ? opacity.get().isOpaque() : super.isOpaque();
}

@Override
public boolean isReplaceable() {
return replaceable.isPresent() || super.isReplaceable();
return replaceable.map(BlockProperty.Replaceable::isReplaceable).orElseGet(super::isReplaceable);
}
}
2 changes: 1 addition & 1 deletion src/main/java/nova/core/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public double getResistance() {
* @return True if this block can be replaced.
*/
public boolean canReplace() {
return false;
return components.getOp(BlockProperty.Replaceable.class).filter(BlockProperty.Replaceable::isReplaceable).isPresent();
}

/**
Expand Down
Loading

0 comments on commit 6ee0f33

Please sign in to comment.