Skip to content

Commit

Permalink
Fix BlockProperty (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss authored and RX14 committed Feb 24, 2017
1 parent 820c854 commit 22a3d8d
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.IBlockAccess;
import nova.core.block.Block;
import nova.core.block.component.BlockProperty;
import nova.core.block.component.LightEmitter;
import nova.core.component.misc.Collider;
import nova.core.component.renderer.StaticRenderer;
Expand All @@ -38,6 +39,7 @@
import nova.core.retention.Data;
import nova.core.retention.Storable;
import nova.core.retention.Store;
import nova.core.sound.Sound;
import nova.core.util.shape.Cuboid;
import nova.core.world.World;
import nova.core.wrapper.mc.forge.v17.util.WrapperEvent;
Expand Down Expand Up @@ -65,7 +67,17 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
BlockTransform transform = components.add(new BlockTransform());
transform.setWorld(world);
transform.setPosition(pos);
components.add(new LightEmitter()).setEmittedLevel(() -> mcBlock.getLightValue(getMcBlockAccess(), x(), y(), z()) / 15.0F);

components.add(new BlockProperty.Opacity().setOpacity(mcBlock.getMaterial().isOpaque() ? 1 : 0));
if (mcBlock.isReplaceable(getMcBlockAccess(), 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.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 Collider(this))
.setBoundingBox(() -> new Cuboid(mcBlock.getBlockBoundsMinX(), mcBlock.getBlockBoundsMinY(), mcBlock.getBlockBoundsMinZ(), mcBlock.getBlockBoundsMaxX(), mcBlock.getBlockBoundsMaxY(), mcBlock.getBlockBoundsMaxZ()))
.setOcclusionBoxes(entity -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
Expand All @@ -42,14 +43,17 @@
import nova.core.block.Block;
import nova.core.block.BlockFactory;
import nova.core.block.Stateful;
import nova.core.block.component.BlockProperty;
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;
import nova.core.sound.Sound;
import nova.core.util.Direction;
import nova.core.util.math.MathUtil;
import nova.core.util.math.MatrixStack;
import nova.core.util.shape.Cuboid;
import nova.core.wrapper.mc.forge.v17.util.WrapperEvent;
Expand Down Expand Up @@ -86,11 +90,32 @@ public class FWBlock extends net.minecraft.block.Block implements ISimpleBlockRe

private Map<BlockPosition, Block> harvestedBlocks = new HashMap<>();

// TODO: Resolve unknown material issue
private static Material getMcMaterial(BlockFactory factory) {
Block dummy = factory.build();
if (dummy.components.has(BlockProperty.Opacity.class) || dummy.components.has(BlockProperty.Replaceable.class)) {
// TODO allow color selection
return new ProxyMaterial(MapColor.grayColor,
dummy.components.getOp(BlockProperty.Opacity.class),
dummy.components.getOp(BlockProperty.Replaceable.class));
} else {
return Material.piston;
}
}

public FWBlock(BlockFactory factory) {
super(Material.piston);
//TODO: Hack build() method
super(getMcMaterial(factory));
this.factory = factory;
this.dummy = factory.build();
if (dummy.components.has(BlockProperty.BlockSound.class)) {
this.stepSound = new FWBlockSound(dummy.components.get(BlockProperty.BlockSound.class));
} else {
BlockProperty.BlockSound properties = dummy.components.add(new BlockProperty.BlockSound());
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK, new Sound("", soundTypeStone.getBreakSound()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", soundTypeStone.func_150496_b()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK, new Sound("", soundTypeStone.getStepResourcePath()));
this.stepSound = soundTypeStone;
}
this.blockClass = dummy.getClass();
this.setBlockName(dummy.getID());

Expand Down Expand Up @@ -128,6 +153,13 @@ public Block getBlockInstance(net.minecraft.world.IBlockAccess access, Vector3D
private Block getBlockInstance(nova.core.world.World world, Vector3D position) {
Block block = factory.build();
block.components.add(new MCBlockTransform(block, world, position));
if (!block.components.has(BlockProperty.BlockSound.class)) {
BlockProperty.BlockSound properties = block.components.add(new BlockProperty.BlockSound());
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK, new Sound("", soundTypeStone.getBreakSound()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", soundTypeStone.func_150496_b()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK, new Sound("", soundTypeStone.getStepResourcePath()));
this.stepSound = soundTypeStone;
}
return block;
}

Expand Down Expand Up @@ -174,7 +206,15 @@ public boolean hasTileEntity(int metadata) {

@Override
public TileEntity createTileEntity(World world, int metadata) {
return FWTileLoader.loadTile(dummy.getID());
FWTile fwTile = FWTileLoader.loadTile(dummy.getID());
if (!fwTile.block.components.has(BlockProperty.BlockSound.class)) {
BlockProperty.BlockSound properties = fwTile.block.components.add(new BlockProperty.BlockSound());
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK, new Sound("", soundTypeStone.getBreakSound()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", soundTypeStone.func_150496_b()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK, new Sound("", soundTypeStone.getStepResourcePath()));
this.stepSound = soundTypeStone;
}
return fwTile;
}

@Override
Expand Down Expand Up @@ -330,7 +370,7 @@ public int getLightValue(IBlockAccess access, int x, int y, int z) {
Optional<LightEmitter> opEmitter = blockInstance.components.getOp(LightEmitter.class);

if (opEmitter.isPresent()) {
return Math.round(opEmitter.get().emittedLevel.get() * 15.0F);
return (int) MathUtil.clamp(Math.round(opEmitter.get().emittedLevel.getAsDouble() * 15), 0, 15);
} else {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nova.core.wrapper.mc.forge.v17.wrapper.block.forward;

import net.minecraft.block.Block;
import nova.core.block.component.BlockProperty;

/**
* @author winsock, soniex2, ExE Boss
*/
public class FWBlockSound extends Block.SoundType {
private final BlockProperty.BlockSound blockSound;

/**
* Construct a new FWBlockSound
* @param blockSound The BlockSound to use.
*/
public FWBlockSound(BlockProperty.BlockSound blockSound) {
super("", 1f, 1f);
this.blockSound = blockSound;
}

@Override
public String getBreakSound() {
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK)
.map(sound -> (sound.domain.isEmpty() && !sound.name.contains(".")) ? "dig." + sound.name : sound.getID())
.orElseGet(super::getBreakSound);
}

@Override
public String getStepResourcePath() {
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK)
.map(sound -> (sound.domain.isEmpty() && !sound.name.contains(".")) ? "step." + sound.name : sound.getID())
.orElseGet(super::getStepResourcePath);
}

@Override
public String func_150496_b() {
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE)
.map(sound -> sound.domain.isEmpty() ? sound.name : sound.getID())
.orElseGet(this::getBreakSound); // By default MC uses the block break sound for block placement
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
*/
public class FWTile extends TileEntity {

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

public FWTile() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nova.core.wrapper.mc.forge.v17.wrapper.block.forward;

import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import nova.core.block.component.BlockProperty;

import java.util.Optional;

/**
* @author soniex2, ExE Boss
*/
public class ProxyMaterial extends Material {
private final Optional<BlockProperty.Opacity> opacity;
private final Optional<BlockProperty.Replaceable> replaceable;

/**
* Construct a new proxy material.
* @param color The map color.
* @param opacity The Opacity to use.
*/
public ProxyMaterial(MapColor color, Optional<BlockProperty.Opacity> opacity, Optional<BlockProperty.Replaceable> replaceable) {
super(color);
this.opacity = opacity;
this.replaceable = replaceable;
}

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

@Override
public boolean isReplaceable() {
return replaceable.isPresent() || super.isReplaceable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ public String getID() {

@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.domain.isEmpty() ? sound.name : sound.getID(), sound.pitch, sound.volume, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ public BWBlock(net.minecraft.block.Block block, World world, Vector3D pos) {
transform.setWorld(world);
transform.setPosition(pos);

components.add(new BlockProperty.Opacity().setLightTransmission(!mcBlock.getMaterial().blocksLight()));
components.add(new BlockProperty.Opacity().setOpacity(mcBlock.getMaterial().blocksLight() ? 1 : 0));
if (mcBlock.isReplaceable((net.minecraft.world.World) getMcBlockAccess(), 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.0F);
components.add(new LightEmitter()).setEmittedLevel(() -> mcBlock.getLightValue(getMcBlockAccess(), 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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import nova.core.retention.Storable;
import nova.core.sound.Sound;
import nova.core.util.Direction;
import nova.core.util.math.MathUtil;
import nova.core.util.shape.Cuboid;
import nova.core.wrapper.mc.forge.v18.util.WrapperEvent;
import nova.internal.core.Game;
Expand Down Expand Up @@ -76,9 +77,11 @@ public class FWBlock extends net.minecraft.block.Block {

private static Material getMcMaterial(BlockFactory factory) {
Block dummy = factory.build();
if (dummy.components.has(BlockProperty.Opacity.class)) {
if (dummy.components.has(BlockProperty.Opacity.class) || dummy.components.has(BlockProperty.Replaceable.class)) {
// TODO allow color selection
return new ProxyMaterial(MapColor.grayColor, dummy.components.get(BlockProperty.Opacity.class));
return new ProxyMaterial(MapColor.grayColor,
dummy.components.getOp(BlockProperty.Opacity.class),
dummy.components.getOp(BlockProperty.Replaceable.class));
} else {
return Material.piston;
}
Expand Down Expand Up @@ -133,6 +136,13 @@ private Block getBlockInstance(nova.core.world.World world, Vector3D position) {
// TODO: Implement obj args
Block block = factory.build();
block.components.add(new MCBlockTransform(block, world, position));
if (!block.components.has(BlockProperty.BlockSound.class)) {
BlockProperty.BlockSound properties = block.components.add(new BlockProperty.BlockSound());
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK, new Sound("", soundTypeStone.getBreakSound()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", soundTypeStone.getPlaceSound()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK, new Sound("", soundTypeStone.getStepSound()));
this.stepSound = soundTypeStone;
}
return block;
}

Expand Down Expand Up @@ -184,6 +194,13 @@ public TileEntity createTileEntity(World world, IBlockState state) {
fwTile.block.components.getOrAdd(new MCBlockTransform(dummy, Game.natives().toNova(world), new Vector3D(lastExtendedStatePos.getX(), lastExtendedStatePos.getY(), lastExtendedStatePos.getZ())));
lastExtendedStatePos = null;
}
if (!fwTile.block.components.has(BlockProperty.BlockSound.class)) {
BlockProperty.BlockSound properties = fwTile.block.components.add(new BlockProperty.BlockSound());
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK, new Sound("", soundTypeStone.getBreakSound()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE, new Sound("", soundTypeStone.getPlaceSound()));
properties.setBlockSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK, new Sound("", soundTypeStone.getStepSound()));
this.stepSound = soundTypeStone;
}
return fwTile;
}

Expand Down Expand Up @@ -312,7 +329,7 @@ public int getLightValue(IBlockAccess access, BlockPos pos) {
Optional<LightEmitter> opEmitter = blockInstance.components.getOp(LightEmitter.class);

if (opEmitter.isPresent()) {
return Math.round(opEmitter.get().emittedLevel.get() * 15.0F);
return (int) MathUtil.clamp(Math.round(opEmitter.get().emittedLevel.getAsDouble() * 15), 0, 15);
} else {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,41 @@
package nova.core.wrapper.mc.forge.v18.wrapper.block.forward;

import net.minecraft.block.Block;
import nova.core.block.component.BlockProperty.BlockSound;
import nova.core.sound.Sound;
import nova.core.block.component.BlockProperty;

/**
* @author winsock, soniex2
* @author winsock, soniex2, ExE Boss
*/
public class FWBlockSound extends Block.SoundType {
private final BlockSound blockSound;
private final BlockProperty.BlockSound blockSound;

/**
* Construct a new FWBlockSound
* @param blockSound The BlockSound to use.
*/
public FWBlockSound(BlockSound blockSound) {
public FWBlockSound(BlockProperty.BlockSound blockSound) {
super("", 1f, 1f);
this.blockSound = blockSound;
}

@Override
public String getBreakSound() {
if (blockSound.blockSoundSoundMap.containsKey(BlockSound.BlockSoundTrigger.BREAK)) {
Sound sound = blockSound.blockSoundSoundMap.get(BlockSound.BlockSoundTrigger.BREAK);
if (sound.domain.isEmpty() && !sound.name.contains(".")) {
return "dig." + sound.name;
}
return sound.getID();
}
return super.getBreakSound();
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.BREAK)
.map(sound -> (sound.domain.isEmpty() && !sound.name.contains(".")) ? "dig." + sound.name : sound.getID())
.orElseGet(super::getBreakSound);
}

@Override
public String getStepSound() {
if (blockSound.blockSoundSoundMap.containsKey(BlockSound.BlockSoundTrigger.WALK)) {
Sound sound = blockSound.blockSoundSoundMap.get(BlockSound.BlockSoundTrigger.WALK);
if (sound.domain.isEmpty() && !sound.name.contains(".")) {
return "step." + sound.name;
}
return sound.getID();
}
return super.getStepSound();
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.WALK)
.map(sound -> (sound.domain.isEmpty() && !sound.name.contains(".")) ? "step." + sound.name : sound.getID())
.orElseGet(super::getStepSound);
}

@Override
public String getPlaceSound() {
if (blockSound.blockSoundSoundMap.containsKey(BlockSound.BlockSoundTrigger.WALK)) {
Sound sound = blockSound.blockSoundSoundMap.get(BlockSound.BlockSoundTrigger.WALK);
if (sound.domain.isEmpty()) {
return sound.name;
}
return sound.getID();
}
// By default MC uses the block break sound for block placement
return this.getBreakSound();
return blockSound.getSound(BlockProperty.BlockSound.BlockSoundTrigger.PLACE)
.map(sound -> sound.domain.isEmpty() ? sound.name : sound.getID())
.orElseGet(this::getBreakSound); // By default MC uses the block break sound for block placement
}
}
Loading

0 comments on commit 22a3d8d

Please sign in to comment.