mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-26 17:06:54 +00:00
173ea72001
OK, so let's get this out of the way, there's some actual changes mixed in here too. I'm really sorry: - Turtles can now not be renamed with unnamed item tags (previously it would clear the name, this seemed a little unideal). - commands.getBlock(s)Data will also include NBT. Now, onto the horror story which is these inspection changes: - Make a lot of methods static - Typo fixes - Make utility classes final + private constructor - Lots of reformatting (ifs -> ternary, invert control flow, etc...) - ??? - Profit! I'm so going to regret this - can pretty much guarantee this is going to break something.
88 lines
2.9 KiB
Java
88 lines
2.9 KiB
Java
/*
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
*/
|
|
|
|
package dan200.computercraft.shared;
|
|
|
|
import dan200.computercraft.ComputerCraft;
|
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
|
import dan200.computercraft.shared.util.InventoryUtil;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraftforge.fml.common.Loader;
|
|
import net.minecraftforge.fml.common.ModContainer;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import javax.annotation.Nullable;
|
|
import java.util.*;
|
|
|
|
public final class PocketUpgrades
|
|
{
|
|
private static final Map<String, IPocketUpgrade> upgrades = new HashMap<>();
|
|
private static final IdentityHashMap<IPocketUpgrade, String> upgradeOwners = new IdentityHashMap<>();
|
|
|
|
private PocketUpgrades() {}
|
|
|
|
public static void register( @Nonnull IPocketUpgrade upgrade )
|
|
{
|
|
Objects.requireNonNull( 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 );
|
|
|
|
ModContainer mc = Loader.instance().activeModContainer();
|
|
if( mc != null && mc.getModId() != null ) upgradeOwners.put( upgrade, mc.getModId() );
|
|
}
|
|
|
|
public static IPocketUpgrade get( String id )
|
|
{
|
|
// Fix a typo in the advanced modem upgrade's name. I'm sorry, I realise this is horrible.
|
|
if( id.equals( "computercraft:advanved_modem" ) ) id = "computercraft:advanced_modem";
|
|
|
|
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.areItemsSimilar( stack, craftingStack ) )
|
|
{
|
|
return upgrade;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Nullable
|
|
public static String getOwner( IPocketUpgrade upgrade )
|
|
{
|
|
return upgradeOwners.get( upgrade );
|
|
}
|
|
|
|
public static Iterable<IPocketUpgrade> getVanillaUpgrades()
|
|
{
|
|
List<IPocketUpgrade> vanilla = new ArrayList<>();
|
|
vanilla.add( ComputerCraft.PocketUpgrades.wirelessModem );
|
|
vanilla.add( ComputerCraft.PocketUpgrades.advancedModem );
|
|
vanilla.add( ComputerCraft.PocketUpgrades.speaker );
|
|
return vanilla;
|
|
}
|
|
|
|
public static Iterable<IPocketUpgrade> getUpgrades()
|
|
{
|
|
return Collections.unmodifiableCollection( upgrades.values() );
|
|
}
|
|
}
|