ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/blocks/BlockTreasureChest.java

174 lines
6.1 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("BlockTreasureChest.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2015-11-02 20:55:19 +01:00
* © 2015 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2015-07-10 13:08:20 +02:00
package ellpeck.actuallyadditions.blocks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2015-07-10 21:19:52 +02:00
import ellpeck.actuallyadditions.achievement.TheAchievements;
import ellpeck.actuallyadditions.blocks.base.BlockBase;
2015-07-10 13:08:20 +02:00
import ellpeck.actuallyadditions.recipe.TreasureChestHandler;
import ellpeck.actuallyadditions.util.ModUtil;
2015-11-22 18:58:23 +01:00
import ellpeck.actuallyadditions.util.Util;
2015-07-10 13:08:20 +02:00
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.util.WeightedRandom;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import java.util.Random;
public class BlockTreasureChest extends BlockBase{
2015-07-10 13:08:20 +02:00
2015-10-28 14:46:04 +01:00
@SideOnly(Side.CLIENT)
2015-07-10 13:08:20 +02:00
private IIcon topIcon;
2015-10-28 14:46:04 +01:00
@SideOnly(Side.CLIENT)
2015-07-12 04:31:05 +02:00
private IIcon bottomIcon;
2015-10-28 14:46:04 +01:00
@SideOnly(Side.CLIENT)
2015-07-10 13:08:20 +02:00
private IIcon frontIcon;
public BlockTreasureChest(String name){
super(Material.wood, name);
2015-07-10 13:08:20 +02:00
this.setHarvestLevel("axe", 0);
this.setHardness(300.0F);
this.setResistance(50.0F);
2015-07-10 13:08:20 +02:00
this.setStepSound(soundTypeWood);
this.setTickRandomly(true);
}
2015-07-10 14:36:50 +02:00
@Override
2015-10-28 14:46:04 +01:00
@SideOnly(Side.CLIENT)
2015-10-03 10:19:40 +02:00
public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side){
int meta = world.getBlockMetadata(x, y, z);
if(side == 1){
return this.topIcon;
2015-10-03 10:16:18 +02:00
}
2015-10-03 10:19:40 +02:00
if(side == meta+2){
return this.frontIcon;
2015-10-03 10:16:18 +02:00
}
2015-10-03 10:19:40 +02:00
if(side == 0){
return this.bottomIcon;
2015-10-03 10:16:18 +02:00
}
2015-10-03 10:19:40 +02:00
return this.blockIcon;
2015-07-10 13:08:20 +02:00
}
@Override
2015-10-28 14:46:04 +01:00
@SideOnly(Side.CLIENT)
2015-07-10 13:08:20 +02:00
public IIcon getIcon(int side, int meta){
2015-10-03 10:16:18 +02:00
if(side == 1){
return this.topIcon;
}
if(side == 0){
return this.bottomIcon;
}
if(side == 3){
return this.frontIcon;
}
2015-07-10 13:08:20 +02:00
return this.blockIcon;
}
@Override
2015-10-03 10:19:40 +02:00
@SideOnly(Side.CLIENT)
public void randomDisplayTick(World world, int x, int y, int z, Random rand){
for(int i = 0; i < 2; i++){
for(float f = 0; f <= 3; f += 0.5){
float particleX = rand.nextFloat();
float particleZ = rand.nextFloat();
world.spawnParticle("bubble", (double)x+particleX, (double)y+f+1, (double)z+particleZ, 0.0D, 0.2D, 0.0D);
}
2015-10-03 10:16:18 +02:00
}
2015-07-10 13:08:20 +02:00
}
@Override
2015-10-03 10:19:40 +02:00
public Item getItemDropped(int par1, Random rand, int par3){
return null;
2015-07-10 13:08:20 +02:00
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){
if(!world.isRemote){
2015-11-22 18:58:23 +01:00
world.playSoundAtEntity(player, "random.chestopen", 0.2F, Util.RANDOM.nextFloat()*0.1F+0.9F);
2015-07-10 13:08:20 +02:00
this.dropItems(world, x, y, z);
world.setBlockToAir(x, y, z);
2015-11-13 20:34:57 +01:00
player.triggerAchievement(TheAchievements.OPEN_TREASURE_CHEST.ach);
2015-07-10 13:08:20 +02:00
}
return true;
}
2015-10-03 10:19:40 +02:00
@Override
public boolean canSilkHarvest(){
return false;
}
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase player, ItemStack stack){
int rotation = MathHelper.floor_double((double)(player.rotationYaw*4.0F/360.0F)+0.5D) & 3;
if(rotation == 0){
world.setBlockMetadataWithNotify(x, y, z, 0, 2);
}
if(rotation == 1){
world.setBlockMetadataWithNotify(x, y, z, 3, 2);
}
if(rotation == 2){
world.setBlockMetadataWithNotify(x, y, z, 1, 2);
}
if(rotation == 3){
world.setBlockMetadataWithNotify(x, y, z, 2, 2);
}
}
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconReg){
this.blockIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getBaseName());
this.topIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getBaseName()+"Top");
this.bottomIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getBaseName()+"Bottom");
this.frontIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getBaseName()+"Front");
2015-10-03 10:19:40 +02:00
}
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.epic;
}
2015-07-10 13:08:20 +02:00
private void dropItems(World world, int x, int y, int z){
2015-11-22 18:58:23 +01:00
for(int i = 0; i < MathHelper.getRandomIntegerInRange(Util.RANDOM, 3, 6); i++){
TreasureChestHandler.Return theReturn = (TreasureChestHandler.Return)WeightedRandom.getRandomItem(Util.RANDOM, TreasureChestHandler.returns);
2015-07-10 13:08:20 +02:00
ItemStack itemStack = theReturn.returnItem.copy();
2015-11-22 18:58:23 +01:00
itemStack.stackSize = MathHelper.getRandomIntegerInRange(Util.RANDOM, theReturn.minAmount, theReturn.maxAmount);
2015-07-10 13:08:20 +02:00
2015-11-22 18:58:23 +01:00
float dX = Util.RANDOM.nextFloat()*0.8F+0.1F;
float dY = Util.RANDOM.nextFloat()*0.8F+0.1F;
float dZ = Util.RANDOM.nextFloat()*0.8F+0.1F;
2015-07-10 13:08:20 +02:00
EntityItem entityItem = new EntityItem(world, x+dX, y+dY, z+dZ, itemStack.copy());
2015-10-02 16:48:01 +02:00
if(itemStack.hasTagCompound()){
entityItem.getEntityItem().setTagCompound((NBTTagCompound)itemStack.getTagCompound().copy());
}
2015-07-10 13:08:20 +02:00
float factor = 0.05F;
2015-11-22 18:58:23 +01:00
entityItem.motionX = Util.RANDOM.nextGaussian()*factor;
entityItem.motionY = Util.RANDOM.nextGaussian()*factor+0.2F;
entityItem.motionZ = Util.RANDOM.nextGaussian()*factor;
2015-07-10 13:08:20 +02:00
world.spawnEntityInWorld(entityItem);
itemStack.stackSize = 0;
}
}
}