Applied-Energistics-2-tiler.../src/main/java/appeng/items/tools/ToolMemoryCard.java

164 lines
4.5 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.items.tools;
import java.util.EnumSet;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
2015-12-24 02:07:03 +01:00
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.items.IMemoryCard;
import appeng.api.implementations.items.MemoryCardMessages;
import appeng.core.features.AEFeature;
2014-01-30 19:54:32 +01:00
import appeng.core.localization.GuiText;
import appeng.core.localization.PlayerMessages;
import appeng.items.AEBaseItem;
import appeng.util.Platform;
public class ToolMemoryCard extends AEBaseItem implements IMemoryCard
{
public ToolMemoryCard()
{
2014-12-29 15:13:47 +01:00
this.setFeature( EnumSet.of( AEFeature.Core ) );
this.setMaxStackSize( 1 );
}
@Override
2015-09-30 14:24:40 +02:00
public void addCheckedInformation( final ItemStack stack, final EntityPlayer player, final List<String> lines, final boolean displayMoreInfo )
{
lines.add( this.getLocalizedName( this.getSettingsName( stack ) + ".name", this.getSettingsName( stack ) ) );
2015-09-30 14:24:40 +02:00
final NBTTagCompound data = this.getData( stack );
if( data.hasKey( "tooltip" ) )
2015-04-29 02:30:53 +02:00
{
lines.add( StatCollector.translateToLocal( this.getLocalizedName( data.getString( "tooltip" ) + ".name", data.getString( "tooltip" ) ) ) );
2015-04-29 02:30:53 +02:00
}
}
/**
* Find the localized string...
2015-02-03 12:04:13 +01:00
*
2014-09-27 23:17:47 +02:00
* @param name possible names for the localized string
*
2014-09-27 23:17:47 +02:00
* @return localized name
*/
2015-09-30 14:24:40 +02:00
private String getLocalizedName( final String... name )
{
2015-09-30 14:24:40 +02:00
for( final String n : name )
{
2015-09-30 14:24:40 +02:00
final String l = StatCollector.translateToLocal( n );
if( !l.equals( n ) )
2015-04-29 02:30:53 +02:00
{
return l;
2015-04-29 02:30:53 +02:00
}
}
2015-09-30 14:24:40 +02:00
for( final String n : name )
2015-04-29 02:30:53 +02:00
{
return n;
2015-04-29 02:30:53 +02:00
}
return "";
}
@Override
2015-09-30 14:24:40 +02:00
public void setMemoryCardContents( final ItemStack is, final String settingsName, final NBTTagCompound data )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = Platform.openNbtData( is );
c.setString( "Config", settingsName );
2014-02-09 02:34:52 +01:00
c.setTag( "Data", data );
}
@Override
2015-09-30 14:24:40 +02:00
public String getSettingsName( final ItemStack is )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = Platform.openNbtData( is );
final String name = c.getString( "Config" );
return name == null || name.isEmpty() ? GuiText.Blank.getUnlocalized() : name;
}
@Override
2015-09-30 14:24:40 +02:00
public NBTTagCompound getData( final ItemStack is )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = Platform.openNbtData( is );
NBTTagCompound o = c.getCompoundTag( "Data" );
if( o == null )
2015-04-29 02:30:53 +02:00
{
o = new NBTTagCompound();
2015-04-29 02:30:53 +02:00
}
return (NBTTagCompound) o.copy();
}
@Override
2015-09-30 14:24:40 +02:00
public void notifyUser( final EntityPlayer player, final MemoryCardMessages msg )
{
if( Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}
switch( msg )
{
case SETTINGS_CLEARED:
player.addChatMessage( PlayerMessages.SettingCleared.get() );
break;
case INVALID_MACHINE:
player.addChatMessage( PlayerMessages.InvalidMachine.get() );
break;
case SETTINGS_LOADED:
player.addChatMessage( PlayerMessages.LoadedSettings.get() );
break;
case SETTINGS_SAVED:
player.addChatMessage( PlayerMessages.SavedSettings.get() );
break;
default:
}
}
@Override
public boolean onItemUse( final ItemStack is, final EntityPlayer player, final World w, final BlockPos pos, final EnumFacing side, final float hx, final float hy, final float hz )
{
if( player.isSneaking() && !w.isRemote )
{
2015-09-30 14:24:40 +02:00
final IMemoryCard mem = (IMemoryCard) is.getItem();
mem.notifyUser( player, MemoryCardMessages.SETTINGS_CLEARED );
is.setTagCompound( null );
return true;
}
else
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return super.onItemUse( is, player, w, pos, side, hx, hy, hz );
2015-04-29 02:30:53 +02:00
}
}
2015-12-24 02:03:16 +01:00
@Override
public boolean doesSneakBypassUse( final World world, final BlockPos pos, final EntityPlayer player )
{
return true;
}
}