1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/media/items/ItemTreasureDisk.java

154 lines
4.3 KiB
Java
Raw Normal View History

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.media.items;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.media.IMedia;
import dan200.computercraft.core.filesystem.SubMount;
import dan200.computercraft.shared.util.Colour;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2017-05-11 00:08:26 +00:00
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
2017-05-06 23:07:42 +00:00
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.List;
public class ItemTreasureDisk extends Item
2017-05-01 14:48:44 +00:00
implements IMedia
{
public ItemTreasureDisk()
{
setMaxStackSize( 1 );
2017-05-01 14:48:44 +00:00
setHasSubtypes( true );
setTranslationKey( "computercraft:treasure_disk" );
}
2017-05-01 14:48:44 +00:00
@Override
public void getSubItems( @Nonnull CreativeTabs tabs, @Nonnull NonNullList<ItemStack> list )
{
}
2017-05-01 14:48:44 +00:00
@Override
public void addInformation( @Nonnull ItemStack stack, World world, List<String> list, ITooltipFlag flag )
{
2017-05-01 14:48:44 +00:00
String label = getTitle( stack );
if( label != null && label.length() > 0 )
{
list.add( label );
}
}
2017-05-01 14:48:44 +00:00
@Override
2017-05-11 00:08:26 +00:00
public boolean doesSneakBypassUse( @Nonnull ItemStack stack, IBlockAccess world, BlockPos pos, EntityPlayer player )
{
return true;
}
2017-05-01 14:48:44 +00:00
// IMedia implementation
@Override
2017-05-06 23:07:42 +00:00
public String getLabel( @Nonnull ItemStack stack )
2017-05-01 14:48:44 +00:00
{
return getTitle( stack );
}
@Override
2017-05-06 23:07:42 +00:00
public IMount createDataMount( @Nonnull ItemStack stack, @Nonnull World world )
{
IMount rootTreasure = getTreasureMount();
String subPath = getSubPath( stack );
try
{
if( rootTreasure.exists( subPath ) )
{
return new SubMount( rootTreasure, subPath );
}
else if( rootTreasure.exists( "deprecated/" + subPath ) )
{
return new SubMount( rootTreasure, "deprecated/" + subPath );
}
else
{
return null;
}
}
catch( IOException e )
{
return null;
}
}
public static ItemStack create( String subPath, int colourIndex )
{
2017-05-01 14:48:44 +00:00
NBTTagCompound nbt = new NBTTagCompound();
nbt.setString( "subPath", subPath );
2017-05-01 14:48:44 +00:00
int slash = subPath.indexOf( "/" );
if( slash >= 0 )
{
String author = subPath.substring( 0, slash );
String title = subPath.substring( slash + 1 );
nbt.setString( "title", "\"" + title + "\" by " + author );
}
else
{
nbt.setString( "title", "untitled" );
}
nbt.setInteger( "colour", Colour.values()[colourIndex].getHex() );
2017-05-01 14:48:44 +00:00
ItemStack result = new ItemStack( ComputerCraft.Items.treasureDisk, 1, 0 );
result.setTagCompound( nbt );
return result;
}
private static IMount getTreasureMount()
{
2017-05-01 14:48:44 +00:00
return ComputerCraft.createResourceMount( ComputerCraft.class, "computercraft", "lua/treasure" );
}
// private stuff
2017-05-11 00:08:26 +00:00
public String getTitle( @Nonnull ItemStack stack )
{
NBTTagCompound nbt = stack.getTagCompound();
if( nbt != null && nbt.hasKey( "title" ) )
{
2017-05-01 14:48:44 +00:00
return nbt.getString( "title" );
}
2017-05-01 14:48:44 +00:00
return "'alongtimeago' by dan200";
}
2017-05-11 00:08:26 +00:00
public String getSubPath( @Nonnull ItemStack stack )
{
NBTTagCompound nbt = stack.getTagCompound();
if( nbt != null && nbt.hasKey( "subPath" ) )
{
2017-05-01 14:48:44 +00:00
return nbt.getString( "subPath" );
}
2017-05-01 14:48:44 +00:00
return "dan200/alongtimeago";
}
2017-05-11 00:08:26 +00:00
public int getColour( @Nonnull ItemStack stack )
{
NBTTagCompound nbt = stack.getTagCompound();
if( nbt != null && nbt.hasKey( "colour" ) )
{
2017-05-01 14:48:44 +00:00
return nbt.getInteger( "colour" );
}
2017-05-01 14:48:44 +00:00
return Colour.Blue.getHex();
}
}