Finished work on BOP Farmland & UseHoeEventHandler

This commit is contained in:
GirafiStudios 2016-02-07 06:24:54 +01:00
parent f9dd8bd523
commit ad5d266950
11 changed files with 128 additions and 68 deletions

View file

@ -112,7 +112,8 @@ public class BOPBlocks
public static Block grass;
public static Block waterlily;
public static Block dirt;
public static Block farmland;
public static Block farmland_0;
public static Block farmland_1;
public static Block stone_formations;
public static Block hard_ice;
public static Block dried_sand;

View file

@ -10,6 +10,7 @@ package biomesoplenty.common.block;
import java.util.Random;
import biomesoplenty.common.util.block.VariantPagingHelper;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
@ -35,7 +36,7 @@ import biomesoplenty.common.item.ItemBOPBlock;
public class BlockBOPDirt extends Block implements IBOPBlock, ISustainsPlantType
{
// add properties
public static enum BOPDirtType implements IStringSerializable
public static enum BOPDirtType implements IStringSerializable, VariantPagingHelper.IPagedVariants
{
LOAMY, SANDY, SILTY;
@Override

View file

@ -1,52 +1,65 @@
/*******************************************************************************
* Copyright 2014-2016, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.block;
import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.common.item.ItemBOPBlock;
import biomesoplenty.common.util.block.VariantPagingHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.BlockFarmland;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.common.IPlantable;
import java.util.ArrayList;
import java.util.Random;
public class BlockBOPFarmland extends BlockFarmland implements IBOPBlock
{
public static enum BOPFarmlandType implements IStringSerializable
public static VariantPagingHelper<BlockBOPFarmland, BlockBOPDirt.BOPDirtType> paging = new VariantPagingHelper<BlockBOPFarmland, BlockBOPDirt.BOPDirtType>(2, BlockBOPDirt.BOPDirtType.class);
private static IProperty currentVariantProperty;
public static void createAllPages()
{
LOAMY, SANDY, SILTY;
@Override
public String getName()
int numPages = paging.getNumPages();
for (int i = 0; i < numPages; ++i)
{
return this.name().toLowerCase();
}
@Override
public String toString()
{
return this.getName();
currentVariantProperty = paging.getVariantProperty(i);
paging.addBlock(i, new BlockBOPFarmland());
}
}
public static final PropertyEnum VARIANT = PropertyEnum.create("variant", BOPFarmlandType.class);
public IProperty variantProperty;
@Override
protected BlockState createBlockState() {return new BlockState(this, new IProperty[] { MOISTURE, VARIANT });}
protected BlockState createBlockState()
{
this.variantProperty = currentVariantProperty;
return new BlockState(this, new IProperty[] { MOISTURE, this.variantProperty });
}
@Override
public Class<? extends ItemBlock> getItemClass() { return ItemBOPBlock.class; }
@ -55,16 +68,15 @@ public class BlockBOPFarmland extends BlockFarmland implements IBOPBlock
public int getItemRenderColor(IBlockState state, int tintIndex) { return this.getRenderColor(state); }
@Override
public IProperty[] getPresetProperties() { return new IProperty[] { VARIANT }; }
public IProperty[] getPresetProperties() { return new IProperty[] { this.variantProperty }; }
@Override
public IProperty[] getNonRenderingProperties() { return null; }
@Override
public String getStateName(IBlockState state) {
BOPFarmlandType farmlandType = (BOPFarmlandType)state.getValue(VARIANT);
return farmlandType + "_farmland";
public String getStateName(IBlockState state)
{
return ((BlockBOPDirt.BOPDirtType) state.getValue(this.variantProperty)).getName() + "_farmland";
}
public BlockBOPFarmland()
@ -73,17 +85,20 @@ public class BlockBOPFarmland extends BlockFarmland implements IBOPBlock
this.setHardness(0.6F);
this.setHarvestLevel("shovel", 0);
this.setStepSound(soundTypeGravel);
this.setDefaultState(this.blockState.getBaseState().withProperty(MOISTURE, Integer.valueOf(0)).withProperty(VARIANT, BOPFarmlandType.LOAMY));
this.setDefaultState(this.blockState.getBaseState().withProperty(MOISTURE, Integer.valueOf(0)));
}
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(MOISTURE, Integer.valueOf(meta & 7)).withProperty(VARIANT, BOPFarmlandType.values()[Math.min(2, meta & 7)]);
return this.getDefaultState().withProperty(this.variantProperty, paging.getVariant(this, meta & 1)).withProperty(MOISTURE, Integer.valueOf(meta >> 1));
}
public int getMetaFromState(IBlockState state)
{
return ((BOPFarmlandType) state.getValue(VARIANT)).ordinal();
BlockBOPDirt.BOPDirtType dirt = (BlockBOPDirt.BOPDirtType) state.getValue(this.variantProperty);
int meta = paging.getIndex(dirt);
meta |= state.getValue(MOISTURE) << 1;
return meta;
}
@Override
@ -99,12 +114,12 @@ public class BlockBOPFarmland extends BlockFarmland implements IBOPBlock
}
else if (!this.hasCrops(world, pos))
{
world.setBlockState(pos, BOPBlocks.dirt.getDefaultState());
world.setBlockState(pos, this.getDirtBlockState(world.getBlockState(pos)));
}
}
else if (i < 7)
{
world.setBlockState(pos, state.withProperty(MOISTURE, Integer.valueOf(0)), 2);
world.setBlockState(pos, state.withProperty(MOISTURE, Integer.valueOf(7)), 2);
}
}
@ -130,11 +145,9 @@ public class BlockBOPFarmland extends BlockFarmland implements IBOPBlock
@Override
public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block neighborBlock)
{
super.onNeighborBlockChange(world, pos, state, neighborBlock);
if (world.getBlockState(pos.up()).getBlock().getMaterial().isSolid())
{
world.setBlockState(pos, BOPBlocks.dirt.getDefaultState());
world.setBlockState(pos, this.getDirtBlockState(world.getBlockState(pos)));
}
}
@ -150,23 +163,27 @@ public class BlockBOPFarmland extends BlockFarmland implements IBOPBlock
return;
}
world.setBlockState(pos, BOPBlocks.dirt.getDefaultState()); //TODO Check, was setBlock(pos, BOPBlocks.dirt, (world.getBlockMetadata(pos) / 2) * 2, 2);
world.setBlockState(pos, this.getDirtBlockState(world.getBlockState(pos)));
}
}
}
@Override
public ArrayList<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
public Item getItemDropped(IBlockState state, Random rand, int fortune)
{
ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
ret.add(new ItemStack(BOPBlocks.dirt, 1, (state.getBlock().getMetaFromState(state) / 2) * 2)); //TODO Check
return ret;
return Item.getItemFromBlock(BOPBlocks.dirt);
}
@Override
public int damageDropped(IBlockState state)
{
return BOPBlocks.dirt.getMetaFromState(this.getDirtBlockState(state));
}
@Override
public ItemStack getPickBlock(MovingObjectPosition target, World world, BlockPos pos, EntityPlayer player)
{
return new ItemStack(BOPBlocks.dirt, 1, this.getMetaFromState(world.getBlockState(pos)));
return new ItemStack(BOPBlocks.dirt, 1, BOPBlocks.dirt.getMetaFromState(this.getDirtBlockState(world.getBlockState(pos))));
}
@Override
@ -182,4 +199,19 @@ public class BlockBOPFarmland extends BlockFarmland implements IBOPBlock
return super.canSustainPlant(world, pos, direction, plantable);
}
}
public IBlockState getDirtBlockState(IBlockState state)
{
switch ((BlockBOPDirt.BOPDirtType) state.getValue(this.variantProperty))
{
case LOAMY:
return BOPBlocks.dirt.getDefaultState().withProperty(BlockBOPDirt.VARIANT, BlockBOPDirt.BOPDirtType.LOAMY);
case SANDY:
return BOPBlocks.dirt.getDefaultState().withProperty(BlockBOPDirt.VARIANT, BlockBOPDirt.BOPDirtType.SANDY);
case SILTY:
return BOPBlocks.dirt.getDefaultState().withProperty(BlockBOPDirt.VARIANT, BlockBOPDirt.BOPDirtType.SILTY);
default:
return Blocks.dirt.getStateFromMeta(BlockDirt.DirtType.DIRT.getMetadata());
}
}
}

View file

@ -1,17 +1,28 @@
/*******************************************************************************
* Copyright 2014-2016, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.handler;
import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.common.block.BlockBOPDirt;
import biomesoplenty.common.block.BlockBOPFarmland;
import biomesoplenty.common.block.BlockBOPGrass;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.UseHoeEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class UseHoeEventHandler {
public class UseHoeEventHandler
{
@SubscribeEvent
public void useHoe(UseHoeEvent event)
@ -21,29 +32,37 @@ public class UseHoeEventHandler {
return;
}
World world = event.world;
BlockPos pos = event.pos;
IBlockState state = event.world.getBlockState(pos);
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
boolean result = false;
if (block instanceof BlockBOPDirt || block instanceof BlockBOPGrass)
if (block instanceof BlockBOPDirt)
{
result = true;
if (!(block instanceof BlockBOPGrass))
if (state.getValue(BlockBOPDirt.COARSE))
{
if ((state.getBlock().getMetaFromState(state) & 1) == 1)
{
event.world.setBlockState(pos, state.getBlock().getStateFromMeta(state.getBlock().getMetaFromState(state) - 1), 2);
}
else
{
event.world.setBlockState(pos, BOPBlocks.farmland.getDefaultState(), 2);
}
world.setBlockState(pos, state.withProperty(BlockBOPDirt.COARSE, Boolean.valueOf(false)));
} else
{
world.setBlockState(pos, BlockBOPFarmland.paging.getVariantState((BlockBOPDirt.BOPDirtType) state.getValue(BlockBOPDirt.VARIANT)));
}
else
{
}
else if (block instanceof BlockBOPGrass)
{
result = true;
BlockBOPGrass grass = (BlockBOPGrass) state.getBlock();
Block dirtBlock = grass.getDirtBlockState(state).getBlock();
event.world.setBlockState(pos, Blocks.farmland.getDefaultState());
if (dirtBlock instanceof BlockBOPDirt)
{
BlockBOPDirt.BOPDirtType dirtType = (BlockBOPDirt.BOPDirtType) grass.getDirtBlockState(state).getValue(BlockBOPDirt.VARIANT);
world.setBlockState(pos, BlockBOPFarmland.paging.getVariantState(dirtType));
}
else if (dirtBlock instanceof BlockDirt && state.getValue(BlockBOPGrass.VARIANT) != BlockBOPGrass.BOPGrassType.SMOLDERING)
{
world.setBlockState(pos, Blocks.farmland.getDefaultState());
}
}
@ -53,8 +72,8 @@ public class UseHoeEventHandler {
{
event.current.damageItem(1, event.entityLiving);
}
event.world.playSoundEffect((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), block.stepSound.getStepSound(), (state.getBlock().stepSound.getVolume() + 1.0F) / 2.0F, state.getBlock().stepSound.getFrequency() * 0.8F);
event.world.playSoundEffect((double) ((float) pos.getX() + 0.5F), (double) ((float) pos.getY() + 0.5F), (double) ((float) pos.getZ() + 0.5F), block.stepSound.getStepSound(), (state.getBlock().stepSound.getVolume() + 1.0F) / 2.0F, state.getBlock().stepSound.getFrequency() * 0.8F);
event.entityPlayer.swingItem();
}
}
}
}

View file

@ -79,7 +79,6 @@ public class ModBlocks
grass = registerBlock( new BlockBOPGrass(), "grass" );
waterlily = registerBlock( new BlockBOPLilypad(), "waterlily" );
dirt = registerBlock( new BlockBOPDirt(), "dirt" );
farmland = registerBlock( new BlockBOPFarmland(), "farmland" );
crystal = registerBlock( new BlockBOPCrystal(), "crystal" );
biome_block = registerBlock( new BlockBOPBiomeBlock(), "biome_block" );
@ -136,7 +135,11 @@ public class ModBlocks
leaves_3 = registerBlock( BlockBOPLeaves.paging.getBlock(3), "leaves_3" );
leaves_4 = registerBlock( BlockBOPLeaves.paging.getBlock(4), "leaves_4" );
leaves_5 = registerBlock( BlockBOPLeaves.paging.getBlock(5), "leaves_5" );
BlockBOPFarmland.createAllPages();
farmland_0 = registerBlock( BlockBOPFarmland.paging.getBlock(0), "farmland_0");
farmland_1 = registerBlock( BlockBOPFarmland.paging.getBlock(1), "farmland_1");
// 22 tree types, 8 per BlockBOPSapling instance, needs 3 'pages'
BlockBOPSapling.createAllPages();
sapling_0 = registerBlock( BlockBOPSapling.paging.getBlock(0), "sapling_0");

View file

@ -15,14 +15,6 @@
"moisture=4,variant=sandy": { "model": "biomesoplenty:sandy_farmland_dry" },
"moisture=5,variant=sandy": { "model": "biomesoplenty:sandy_farmland_dry" },
"moisture=6,variant=sandy": { "model": "biomesoplenty:sandy_farmland_dry" },
"moisture=7,variant=sandy": { "model": "biomesoplenty:sandy_farmland_moist" },
"moisture=0,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=1,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=2,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=3,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=4,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=5,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=6,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=7,variant=silty": { "model": "biomesoplenty:silty_farmland_moist" }
"moisture=7,variant=sandy": { "model": "biomesoplenty:sandy_farmland_moist" }
}
}

View file

@ -0,0 +1,12 @@
{
"variants": {
"moisture=0,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=1,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=2,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=3,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=4,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=5,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=6,variant=silty": { "model": "biomesoplenty:silty_farmland_dry" },
"moisture=7,variant=silty": { "model": "biomesoplenty:silty_farmland_moist" }
}
}

View file

@ -206,9 +206,9 @@ tile.ethereal_fence.name=Ethereal Fence
tile.ethereal_fence_gate.name=Ethereal Fence Gate
tile.ethereal_wood_slab.name=Ethereal Wood Slab
tile.ethereal_stairs.name=Ethereal Wood Stairs
tile.farmland.loamy_farmland.name=Loamy Farmland
tile.farmland.sandy_farmland.name=Sandy Farmland
tile.farmland.silty_farmland.name=Silty Farmland
tile.farmland_0.loamy_farmland.name=Loamy Farmland
tile.farmland_0.sandy_farmland.name=Sandy Farmland
tile.farmland_1.silty_farmland.name=Silty Farmland
tile.fir_fence.name=Fir Fence
tile.fir_fence_gate.name=Fir Fence Gate
tile.fir_wood_slab.name=Fir Wood Slab

Binary file not shown.

Before

Width:  |  Height:  |  Size: 567 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 649 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 617 B