2018-12-10 23:46:50 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2019-01-01 01:10:18 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
2018-12-10 23:46:50 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dan200.computercraft.shared;
|
|
|
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
import dan200.computercraft.ComputerCraft;
|
|
|
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
|
|
|
import dan200.computercraft.shared.util.InventoryUtil;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public final class PocketUpgrades
|
|
|
|
{
|
|
|
|
private static final Map<String, IPocketUpgrade> upgrades = new HashMap<>();
|
|
|
|
|
|
|
|
public static void register( @Nonnull IPocketUpgrade upgrade )
|
|
|
|
{
|
|
|
|
Preconditions.checkNotNull( upgrade, "upgrade cannot be null" );
|
|
|
|
|
|
|
|
String id = upgrade.getUpgradeID().toString();
|
|
|
|
IPocketUpgrade existing = upgrades.get( id );
|
|
|
|
if( existing != null )
|
|
|
|
{
|
|
|
|
throw new IllegalStateException( "Error registering '" + upgrade.getUnlocalisedAdjective() + " pocket computer'. UpgradeID '" + id + "' is already registered by '" + existing.getUnlocalisedAdjective() + " pocket computer'" );
|
|
|
|
}
|
|
|
|
|
|
|
|
upgrades.put( id, upgrade );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static IPocketUpgrade get( String id )
|
|
|
|
{
|
|
|
|
return upgrades.get( id );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IPocketUpgrade get( @Nonnull ItemStack stack )
|
|
|
|
{
|
|
|
|
if( stack.isEmpty() ) return null;
|
|
|
|
|
|
|
|
for( IPocketUpgrade upgrade : upgrades.values() )
|
|
|
|
{
|
|
|
|
ItemStack craftingStack = upgrade.getCraftingItem();
|
|
|
|
if( !craftingStack.isEmpty() && InventoryUtil.areItemsStackable( stack, craftingStack ) )
|
|
|
|
{
|
|
|
|
return upgrade;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Iterable<IPocketUpgrade> getVanillaUpgrades()
|
|
|
|
{
|
|
|
|
List<IPocketUpgrade> vanilla = new ArrayList<>();
|
|
|
|
vanilla.add( ComputerCraft.PocketUpgrades.wirelessModem );
|
|
|
|
vanilla.add( ComputerCraft.PocketUpgrades.advancedModem );
|
|
|
|
vanilla.add( ComputerCraft.PocketUpgrades.pocketSpeaker );
|
|
|
|
return vanilla;
|
|
|
|
}
|
|
|
|
}
|