mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-20 16:37:39 +00:00
Move API registration into separate classes
There's several reasons for this change: - Try to make ComputerCraft.java less monolithic by moving functionality into separate module-specific classes. - Hopefully make the core class less Minecraft dependent, meaning emulators are a little less dependent on anything outside of /core. Note we still need /some/ methods in the main ComputerCraft class in order to maintain backwards compatibility with Plethora and Computronics.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
|
||||
* 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.api.turtle.ITurtleUpgrade;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user