mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 10:20:28 +00:00
Add registry for pocket computer upgrades
This commit is contained in:
parent
ff16868dd8
commit
5faceac7ba
@ -14,6 +14,7 @@ import dan200.computercraft.api.media.IMediaProvider;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||
import dan200.computercraft.api.permissions.ITurtlePermissionProvider;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import dan200.computercraft.core.filesystem.ComboMount;
|
||||
@ -38,15 +39,13 @@ import dan200.computercraft.shared.peripheral.modem.BlockAdvancedModem;
|
||||
import dan200.computercraft.shared.peripheral.modem.WirelessNetwork;
|
||||
import dan200.computercraft.shared.peripheral.printer.TilePrinter;
|
||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||
import dan200.computercraft.shared.pocket.peripherals.PocketModem;
|
||||
import dan200.computercraft.shared.proxy.ICCTurtleProxy;
|
||||
import dan200.computercraft.shared.proxy.IComputerCraftProxy;
|
||||
import dan200.computercraft.shared.turtle.blocks.BlockTurtle;
|
||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||
import dan200.computercraft.shared.turtle.upgrades.*;
|
||||
import dan200.computercraft.shared.util.CreativeTabMain;
|
||||
import dan200.computercraft.shared.util.IDAssigner;
|
||||
import dan200.computercraft.shared.util.IEntityDropConsumer;
|
||||
import dan200.computercraft.shared.util.WorldUtil;
|
||||
import dan200.computercraft.shared.util.*;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
@ -75,9 +74,7 @@ import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
///////////////
|
||||
// UNIVERSAL //
|
||||
@ -163,6 +160,12 @@ public class ComputerCraft
|
||||
public static TurtleModem advancedModem;
|
||||
}
|
||||
|
||||
public static class PocketUpgrades
|
||||
{
|
||||
public static PocketModem wirelessModem;
|
||||
public static PocketModem advancedModem;
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
public static Configuration config;
|
||||
|
||||
@ -186,7 +189,6 @@ public class ComputerCraft
|
||||
public static Property computerSpaceLimit;
|
||||
public static Property floppySpaceLimit;
|
||||
public static Property maximumFilesOpen;
|
||||
|
||||
}
|
||||
|
||||
// Registries
|
||||
@ -204,6 +206,7 @@ public class ComputerCraft
|
||||
private static List<IBundledRedstoneProvider> bundledRedstoneProviders = new ArrayList<IBundledRedstoneProvider>();
|
||||
private static List<IMediaProvider> mediaProviders = new ArrayList<IMediaProvider>();
|
||||
private static List<ITurtlePermissionProvider> permissionProviders = new ArrayList<ITurtlePermissionProvider>();
|
||||
private static final Map<String, IPocketUpgrade> pocketUpgrades = new HashMap<String, IPocketUpgrade>();
|
||||
|
||||
// Implementation
|
||||
@Mod.Instance( value = "ComputerCraft" )
|
||||
@ -539,6 +542,18 @@ public class ComputerCraft
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void registerPocketUpgrade( IPocketUpgrade upgrade )
|
||||
{
|
||||
String id = upgrade.getUpgradeID().toString();
|
||||
IPocketUpgrade existing = pocketUpgrades.get( id );
|
||||
if( existing != null )
|
||||
{
|
||||
throw new RuntimeException( "Error registering '" + upgrade.getUnlocalisedAdjective() + " pocket computer'. UpgradeID '" + id + "' is already registered by '" + existing.getUnlocalisedAdjective() + " pocket computer'" );
|
||||
}
|
||||
|
||||
pocketUpgrades.put( id, upgrade );
|
||||
}
|
||||
|
||||
public static void registerPeripheralProvider( IPeripheralProvider provider )
|
||||
{
|
||||
if( provider != null && !peripheralProviders.contains( provider ) )
|
||||
@ -659,6 +674,37 @@ public class ComputerCraft
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IPocketUpgrade getPocketUpgrade(String id) {
|
||||
return pocketUpgrades.get( id );
|
||||
}
|
||||
|
||||
public static IPocketUpgrade getPocketUpgrade( ItemStack stack )
|
||||
{
|
||||
if( stack == null ) return null;
|
||||
|
||||
for (IPocketUpgrade upgrade : pocketUpgrades.values())
|
||||
{
|
||||
ItemStack craftingStack = upgrade.getCraftingItem();
|
||||
if( craftingStack != null && InventoryUtil.areItemsStackable( stack, craftingStack ) )
|
||||
{
|
||||
return upgrade;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Iterable<IPocketUpgrade> getVanillaPocketUpgrades() {
|
||||
List<IPocketUpgrade> upgrades = new ArrayList<IPocketUpgrade>();
|
||||
for(IPocketUpgrade upgrade : pocketUpgrades.values()) {
|
||||
if(upgrade instanceof PocketModem) {
|
||||
upgrades.add( upgrade );
|
||||
}
|
||||
}
|
||||
|
||||
return upgrades;
|
||||
}
|
||||
|
||||
public static int createUniqueNumberedSaveDir( World world, String parentSubPath )
|
||||
{
|
||||
return IDAssigner.getNextIDFromDirectory(new File(getWorldDir(world), parentSubPath));
|
||||
|
@ -14,6 +14,7 @@ import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||
import dan200.computercraft.api.permissions.ITurtlePermissionProvider;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
@ -270,6 +271,17 @@ public final class ComputerCraftAPI
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerPocketUpgrade(IPocketUpgrade upgrade) {
|
||||
findCC();
|
||||
if(computerCraft_registerPocketUpgrade != null) {
|
||||
try {
|
||||
computerCraft_registerPocketUpgrade.invoke( null, upgrade );
|
||||
} catch (Exception e) {
|
||||
// It failed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The functions below here are private, and are used to interface with the non-API ComputerCraft classes.
|
||||
// Reflection is used here so you can develop your mod without decompiling ComputerCraft and including
|
||||
// it in your solution, and so your mod won't crash if ComputerCraft is installed.
|
||||
@ -308,6 +320,9 @@ public final class ComputerCraftAPI
|
||||
computerCraft_registerPermissionProvider = findCCMethod( "registerPermissionProvider", new Class[] {
|
||||
ITurtlePermissionProvider.class
|
||||
} );
|
||||
computerCraft_registerPocketUpgrade = findCCMethod( "registerPocketUpgrade", new Class[] {
|
||||
IPocketUpgrade.class
|
||||
} );
|
||||
} catch( Exception e ) {
|
||||
System.out.println( "ComputerCraftAPI: ComputerCraft not found." );
|
||||
} finally {
|
||||
@ -342,4 +357,5 @@ public final class ComputerCraftAPI
|
||||
private static Method computerCraft_getDefaultBundledRedstoneOutput = null;
|
||||
private static Method computerCraft_registerMediaProvider = null;
|
||||
private static Method computerCraft_registerPermissionProvider = null;
|
||||
private static Method computerCraft_registerPocketUpgrade = null;
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
package dan200.computercraft.api.pocket;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Wrapper class for pocket computers
|
||||
*/
|
||||
public interface IPocketAccess
|
||||
{
|
||||
/**
|
||||
* Gets the holding entity of this item
|
||||
*
|
||||
* @return The holding entity, may be {@code null}.
|
||||
*/
|
||||
@Nullable
|
||||
Entity getEntity();
|
||||
|
||||
/**
|
||||
* Get if the modem light is turned on
|
||||
*
|
||||
* @return If the modem light is turned on
|
||||
*/
|
||||
boolean getModemLight();
|
||||
|
||||
/**
|
||||
* Turn on/off the modem light
|
||||
*
|
||||
* @param value If the light should be on
|
||||
*/
|
||||
void setModemLight( boolean value );
|
||||
|
||||
/**
|
||||
* Get the upgrade specific NBT
|
||||
*
|
||||
* @return The upgrade's NBT
|
||||
*/
|
||||
@Nonnull
|
||||
NBTTagCompound getUpgradeNBTData();
|
||||
|
||||
/**
|
||||
* Mark the upgrade specific NBT as dirty
|
||||
*/
|
||||
void updateUpgradeNBTData();
|
||||
|
||||
/**
|
||||
* Remove the current peripheral and create a new one. You
|
||||
* may wish to do this if the methods available change.
|
||||
*/
|
||||
void invalidatePeripheral();
|
||||
|
||||
/**
|
||||
* Get a list of all upgrades for the pocket computer
|
||||
*
|
||||
* @return A collection of all upgrade names
|
||||
*/
|
||||
@Nonnull
|
||||
Map<ResourceLocation, IPeripheral> getUpgrades();
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package dan200.computercraft.api.pocket;
|
||||
|
||||
import dan200.computercraft.api.ComputerCraftAPI;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import dan200.computercraft.api.turtle.TurtleSide;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Additional peripherals for pocket computers.
|
||||
* <p>
|
||||
* This is similar to {@link dan200.computercraft.api.turtle.ITurtleUpgrade}.
|
||||
*/
|
||||
public interface IPocketUpgrade
|
||||
{
|
||||
|
||||
/**
|
||||
* Gets a unique identifier representing this type of turtle upgrade. eg: "computercraft:wireless_modem" or "my_mod:my_upgrade".
|
||||
* You should use a unique resource domain to ensure this upgrade is uniquely identified.
|
||||
* The peripheral will fail registration if an already used ID is specified.
|
||||
*
|
||||
* @return The id
|
||||
* @see IPocketUpgrade#getUpgradeID()
|
||||
* @see ComputerCraftAPI#registerPocketUpgrade(IPocketUpgrade)
|
||||
*/
|
||||
@Nonnull
|
||||
ResourceLocation getUpgradeID();
|
||||
|
||||
/**
|
||||
* Return a String to describe this type of turtle in turtle item names.
|
||||
* An example of built-in adjectives is "Wireless".
|
||||
*
|
||||
* @return The unlocalised adjective
|
||||
* @see ITurtleUpgrade#getUnlocalisedAdjective()
|
||||
*/
|
||||
@Nonnull
|
||||
String getUnlocalisedAdjective();
|
||||
|
||||
/**
|
||||
* Return an item stack representing the type of item that a turtle must be crafted
|
||||
* with to create a turtle which holds this upgrade. This item stack is also used
|
||||
* to determine the upgrade given by turtle.equip()
|
||||
*
|
||||
* @return The item stack used for crafting. This can be {@code null} if crafting is disabled.
|
||||
* @see ITurtleUpgrade#getCraftingItem()
|
||||
*/
|
||||
@Nullable
|
||||
ItemStack getCraftingItem();
|
||||
|
||||
/**
|
||||
* Creates a peripheral for the pocket computer.
|
||||
* <p>
|
||||
* The peripheral created will be stored for the lifetime of the upgrade, will have update() called
|
||||
* once-per-tick, and will be attached, detached and have methods called in the same manner as a Computer peripheral.
|
||||
*
|
||||
* @param access The access object for the pocket item stack
|
||||
* @return The newly created peripheral.
|
||||
* @see ITurtleUpgrade#createPeripheral(ITurtleAccess, TurtleSide)
|
||||
*/
|
||||
@Nullable
|
||||
IPeripheral createPeripheral( @Nonnull IPocketAccess access );
|
||||
|
||||
/**
|
||||
* Called when the pocket computer item stack updates
|
||||
*
|
||||
* @param access The access object for the pocket item stack
|
||||
* @param peripheral The peripheral for this upgrade
|
||||
*/
|
||||
void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral );
|
||||
|
||||
/**
|
||||
* Called when the pocket computer is right clicked on something
|
||||
*
|
||||
* @param world The world the computer is in
|
||||
* @param access The access object for the pocket item stack
|
||||
* @param peripheral The peripheral for this upgrade
|
||||
* @return {@code true} to stop the gui from opening, otherwise false.
|
||||
*/
|
||||
boolean onRightClick( @Nonnull World world, @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral );
|
||||
}
|
@ -129,7 +129,7 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
public ModelResourceLocation getModelLocation( ItemStack stack )
|
||||
{
|
||||
ItemPocketComputer itemPocketComputer = (ItemPocketComputer)stack.getItem();
|
||||
boolean modemOn = itemPocketComputer.getModemState( stack );
|
||||
boolean modemOn = itemPocketComputer.getLightState( stack );
|
||||
switch( itemPocketComputer.getFamily( stack ) )
|
||||
{
|
||||
case Advanced:
|
||||
|
@ -0,0 +1,145 @@
|
||||
package dan200.computercraft.shared.pocket.core;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.pocket.IPocketAccess;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
public class PocketServerComputer extends ServerComputer implements IPocketAccess
|
||||
{
|
||||
private IPocketUpgrade m_upgrade;
|
||||
private Entity m_entity;
|
||||
private ItemStack m_stack;
|
||||
|
||||
public PocketServerComputer( World world, int computerID, String label, int instanceID, ComputerFamily family, ItemStack stack, Entity entity )
|
||||
{
|
||||
super( world, computerID, label, instanceID, family, ComputerCraft.terminalWidth_pocketComputer, ComputerCraft.terminalHeight_pocketComputer );
|
||||
update( entity, stack );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Entity getEntity()
|
||||
{
|
||||
return m_entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getModemLight()
|
||||
{
|
||||
return getUserData().getBoolean( "modemLight" );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setModemLight( boolean value )
|
||||
{
|
||||
NBTTagCompound tag = getUserData();
|
||||
if( tag.getBoolean( "modemLight" ) != value )
|
||||
{
|
||||
tag.setBoolean( "modemLight", value );
|
||||
updateUserData();
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public NBTTagCompound getUpgradeNBTData()
|
||||
{
|
||||
NBTTagCompound tag;
|
||||
if( m_stack.hasTagCompound() )
|
||||
{
|
||||
tag = m_stack.getTagCompound();
|
||||
}
|
||||
else
|
||||
{
|
||||
tag = new NBTTagCompound();
|
||||
m_stack.setTagCompound( tag );
|
||||
}
|
||||
|
||||
if( tag.hasKey( "upgrade_info", Constants.NBT.TAG_COMPOUND ) )
|
||||
{
|
||||
return tag.getCompoundTag( "upgrade_info" );
|
||||
}
|
||||
else
|
||||
{
|
||||
NBTTagCompound sub = new NBTTagCompound();
|
||||
|
||||
tag.setTag( "upgrade_info", sub );
|
||||
updateUpgradeNBTData();
|
||||
|
||||
return sub;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUpgradeNBTData()
|
||||
{
|
||||
InventoryPlayer inventory = m_entity instanceof EntityPlayer ? ((EntityPlayer) m_entity).inventory : null;
|
||||
if( inventory != null )
|
||||
{
|
||||
inventory.markDirty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidatePeripheral()
|
||||
{
|
||||
IPeripheral peripheral = m_upgrade == null ? null : m_upgrade.createPeripheral( this );
|
||||
setPeripheral( 2, peripheral );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Map<ResourceLocation, IPeripheral> getUpgrades()
|
||||
{
|
||||
if( m_upgrade == null )
|
||||
{
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Collections.singletonMap( m_upgrade.getUpgradeID(), getPeripheral( 2 ) );
|
||||
}
|
||||
}
|
||||
|
||||
public void update( Entity entity, ItemStack stack )
|
||||
{
|
||||
if( m_entity != null ) setPosition( entity.getPosition() );
|
||||
m_entity = entity;
|
||||
m_stack = stack;
|
||||
}
|
||||
|
||||
public synchronized void setUpgrade( IPocketUpgrade upgrade )
|
||||
{
|
||||
if( this.m_upgrade == upgrade ) return;
|
||||
|
||||
// Clear the old upgrade NBT
|
||||
if( m_stack.hasTagCompound() )
|
||||
{
|
||||
NBTTagCompound tag = m_stack.getTagCompound();
|
||||
if( tag.hasKey( "upgrade_info", 10 ) )
|
||||
{
|
||||
tag.removeTag( "upgrade_info" );
|
||||
updateUpgradeNBTData();
|
||||
}
|
||||
}
|
||||
|
||||
this.m_upgrade = upgrade;
|
||||
invalidatePeripheral();
|
||||
}
|
||||
}
|
@ -10,18 +10,17 @@ import com.google.common.base.Objects;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.computer.blocks.ComputerState;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputer;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import dan200.computercraft.shared.computer.items.IComputerItem;
|
||||
import dan200.computercraft.shared.pocket.apis.PocketAPI;
|
||||
import dan200.computercraft.shared.pocket.peripherals.PocketModemPeripheral;
|
||||
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
|
||||
import dan200.computercraft.shared.util.StringUtil;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.Item;
|
||||
@ -32,11 +31,10 @@ import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
;
|
||||
|
||||
public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
{
|
||||
public ItemPocketComputer()
|
||||
@ -47,7 +45,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
public ItemStack create( int id, String label, ComputerFamily family, boolean modem )
|
||||
public ItemStack create( int id, String label, ComputerFamily family, IPocketUpgrade upgrade )
|
||||
{
|
||||
// Ignore types we can't handle
|
||||
if( family != ComputerFamily.Normal && family != ComputerFamily.Advanced )
|
||||
@ -58,16 +56,16 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
// Build the stack
|
||||
int damage = (family == ComputerFamily.Advanced) ? 1 : 0;
|
||||
ItemStack result = new ItemStack( this, 1, damage );
|
||||
if( id >= 0 || modem )
|
||||
if( id >= 0 || upgrade != null )
|
||||
{
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
if( id >= 0 )
|
||||
{
|
||||
compound.setInteger( "computerID", id );
|
||||
}
|
||||
if( modem )
|
||||
if( upgrade != null )
|
||||
{
|
||||
compound.setInteger( "upgrade", 1 );
|
||||
compound.setString( "upgrade", upgrade.getUpgradeID().toString() );
|
||||
}
|
||||
result.setTagCompound( compound );
|
||||
}
|
||||
@ -79,12 +77,19 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSubItems( Item itemID, CreativeTabs tabs, List list )
|
||||
public void getSubItems( Item itemID, CreativeTabs tabs, List<ItemStack> list )
|
||||
{
|
||||
list.add( PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, false ) );
|
||||
list.add( PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, true ) );
|
||||
list.add( PocketComputerItemFactory.create( -1, null, ComputerFamily.Advanced, false ) );
|
||||
list.add( PocketComputerItemFactory.create( -1, null, ComputerFamily.Advanced, true ) );
|
||||
getSubItems( list, ComputerFamily.Normal );
|
||||
getSubItems( list, ComputerFamily.Advanced );
|
||||
}
|
||||
|
||||
private void getSubItems( List<ItemStack> list, ComputerFamily family )
|
||||
{
|
||||
list.add( PocketComputerItemFactory.create( -1, null, family, null ) );
|
||||
for (IPocketUpgrade upgrade : ComputerCraft.getVanillaPocketUpgrades())
|
||||
{
|
||||
list.add( PocketComputerItemFactory.create( -1, null, family, upgrade ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -94,12 +99,13 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
{
|
||||
// Server side
|
||||
IInventory inventory = (entity instanceof EntityPlayer) ? ((EntityPlayer) entity).inventory : null;
|
||||
ServerComputer computer = createServerComputer( world, inventory, stack );
|
||||
PocketServerComputer computer = createServerComputer( world, inventory, entity, stack );
|
||||
if( computer != null )
|
||||
{
|
||||
// Ping computer
|
||||
computer.keepAlive();
|
||||
computer.setWorld( world );
|
||||
computer.update( entity, stack );
|
||||
|
||||
// Sync ID
|
||||
int id = computer.getID();
|
||||
@ -124,29 +130,10 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
}
|
||||
|
||||
// Update modem
|
||||
IPeripheral peripheral = computer.getPeripheral( 2 );
|
||||
if( peripheral != null && peripheral instanceof PocketModemPeripheral )
|
||||
IPocketUpgrade upgrade = getUpgrade( stack );
|
||||
if( upgrade != null )
|
||||
{
|
||||
// Location
|
||||
PocketModemPeripheral modem = (PocketModemPeripheral)peripheral;
|
||||
if( entity instanceof EntityLivingBase )
|
||||
{
|
||||
EntityLivingBase player = (EntityLivingBase)entity;
|
||||
modem.setLocation( world, player.posX, player.posY + player.getEyeHeight(), player.posZ );
|
||||
}
|
||||
else
|
||||
{
|
||||
modem.setLocation( world, entity.posX, entity.posY, entity.posZ );
|
||||
}
|
||||
|
||||
// Light
|
||||
boolean modemLight = modem.isActive();
|
||||
NBTTagCompound modemNBT = computer.getUserData();
|
||||
if( modemNBT.getBoolean( "modemLight" ) != modemLight )
|
||||
{
|
||||
modemNBT.setBoolean( "modemLight", modemLight );
|
||||
computer.updateUserData();
|
||||
}
|
||||
upgrade.update( computer, computer.getPeripheral( 2 ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -166,12 +153,21 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
{
|
||||
if( !world.isRemote )
|
||||
{
|
||||
ServerComputer computer = createServerComputer( world, player.inventory, stack );
|
||||
PocketServerComputer computer = createServerComputer( world, player.inventory, player, stack );
|
||||
|
||||
boolean stop = false;
|
||||
if( computer != null )
|
||||
{
|
||||
computer.turnOn();
|
||||
|
||||
IPocketUpgrade upgrade = getUpgrade( stack );
|
||||
if( upgrade != null )
|
||||
{
|
||||
stop = upgrade.onRightClick( world, computer, computer.getPeripheral( 2 ) );
|
||||
}
|
||||
ComputerCraft.openPocketComputerGUI( player, hand );
|
||||
}
|
||||
|
||||
if( !stop ) ComputerCraft.openPocketComputerGUI( player, hand );
|
||||
}
|
||||
return new ActionResult<ItemStack>( EnumActionResult.SUCCESS, stack );
|
||||
}
|
||||
@ -197,12 +193,12 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
public String getItemStackDisplayName( ItemStack stack )
|
||||
{
|
||||
String baseString = getUnlocalizedName( stack );
|
||||
boolean modem = getHasModem( stack );
|
||||
if( modem )
|
||||
IPocketUpgrade upgrade = getUpgrade( stack );
|
||||
if( upgrade != null )
|
||||
{
|
||||
return StringUtil.translateToLocalFormatted(
|
||||
baseString + ".upgraded.name",
|
||||
StringUtil.translateToLocal( "upgrade.computercraft:wireless_modem.adjective" )
|
||||
StringUtil.translateToLocal( upgrade.getUnlocalisedAdjective() )
|
||||
);
|
||||
}
|
||||
else
|
||||
@ -224,14 +220,14 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
}
|
||||
}
|
||||
|
||||
private ServerComputer createServerComputer( final World world, IInventory inventory, ItemStack stack )
|
||||
private PocketServerComputer createServerComputer( final World world, IInventory inventory, Entity entity, ItemStack stack )
|
||||
{
|
||||
if( world.isRemote )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ServerComputer computer;
|
||||
PocketServerComputer computer;
|
||||
int instanceID = getInstanceID( stack );
|
||||
int sessionID = getSessionID( stack );
|
||||
int correctSessionID = ComputerCraft.serverComputerRegistry.getSessionID();
|
||||
@ -239,7 +235,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
if( instanceID >= 0 && sessionID == correctSessionID &&
|
||||
ComputerCraft.serverComputerRegistry.contains( instanceID ) )
|
||||
{
|
||||
computer = ComputerCraft.serverComputerRegistry.get( instanceID );
|
||||
computer = (PocketServerComputer) ComputerCraft.serverComputerRegistry.get( instanceID );
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -255,20 +251,17 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
computerID = ComputerCraft.createUniqueNumberedSaveDir( world, "computer" );
|
||||
setComputerID( stack, computerID );
|
||||
}
|
||||
computer = new ServerComputer(
|
||||
computer = new PocketServerComputer(
|
||||
world,
|
||||
computerID,
|
||||
getLabel( stack ),
|
||||
instanceID,
|
||||
getFamily( stack ),
|
||||
ComputerCraft.terminalWidth_pocketComputer,
|
||||
ComputerCraft.terminalHeight_pocketComputer
|
||||
stack,
|
||||
entity
|
||||
);
|
||||
computer.addAPI( new PocketAPI() );
|
||||
if( getHasModem( stack ) )
|
||||
{
|
||||
computer.setPeripheral( 2, new PocketModemPeripheral( false ) );
|
||||
}
|
||||
computer.setUpgrade( getUpgrade( stack ) );
|
||||
ComputerCraft.serverComputerRegistry.add( instanceID, computer );
|
||||
if( inventory != null )
|
||||
{
|
||||
@ -394,7 +387,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
@Override
|
||||
public IMount createDataMount( ItemStack stack, World world )
|
||||
{
|
||||
ServerComputer computer = createServerComputer( world, null, stack );
|
||||
ServerComputer computer = createServerComputer( world, null, null, stack );
|
||||
if( computer != null )
|
||||
{
|
||||
return computer.getRootMount();
|
||||
@ -450,7 +443,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
return ComputerState.Off;
|
||||
}
|
||||
|
||||
public boolean getModemState( ItemStack stack )
|
||||
public boolean getLightState( ItemStack stack )
|
||||
{
|
||||
ClientComputer computer = getClientComputer( stack );
|
||||
if( computer != null && computer.isOn() )
|
||||
@ -464,13 +457,26 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getHasModem( ItemStack stack )
|
||||
public static IPocketUpgrade getUpgrade( ItemStack stack )
|
||||
{
|
||||
NBTTagCompound compound = stack.getTagCompound();
|
||||
if( compound != null && compound.hasKey( "upgrade" ) )
|
||||
if( compound != null )
|
||||
{
|
||||
return (compound.getInteger( "upgrade" ) == 1);
|
||||
if( compound.hasKey( "upgrade", Constants.NBT.TAG_STRING ) )
|
||||
{
|
||||
String name = compound.getString( "upgrade" );
|
||||
return ComputerCraft.getPocketUpgrade( name );
|
||||
}
|
||||
return false;
|
||||
else if( compound.hasKey( "upgrade", Constants.NBT.TAG_ANY_NUMERIC ) )
|
||||
{
|
||||
int id = compound.getInteger( "upgrade" );
|
||||
if( id == 1 )
|
||||
{
|
||||
return ComputerCraft.getPocketUpgrade( "computercraft:wireless_modem" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -7,12 +7,13 @@
|
||||
package dan200.computercraft.shared.pocket.items;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class PocketComputerItemFactory
|
||||
{
|
||||
public static ItemStack create( int id, String label, ComputerFamily family, boolean modem )
|
||||
public static ItemStack create( int id, String label, ComputerFamily family, IPocketUpgrade upgrade )
|
||||
{
|
||||
ItemPocketComputer computer = ComputerCraft.Items.pocketComputer;
|
||||
switch( family )
|
||||
@ -20,7 +21,7 @@ public class PocketComputerItemFactory
|
||||
case Normal:
|
||||
case Advanced:
|
||||
{
|
||||
return computer.create( id, label, family, modem );
|
||||
return computer.create( id, label, family, upgrade );
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -0,0 +1,88 @@
|
||||
package dan200.computercraft.shared.pocket.peripherals;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.pocket.IPocketAccess;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class PocketModem implements IPocketUpgrade
|
||||
{
|
||||
private final boolean m_advanced;
|
||||
|
||||
public PocketModem( boolean m_advanced )
|
||||
{
|
||||
this.m_advanced = m_advanced;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ResourceLocation getUpgradeID()
|
||||
{
|
||||
return m_advanced
|
||||
? new ResourceLocation( "computercraft", "advanved_modem" )
|
||||
: new ResourceLocation( "computercraft", "wireless_modem" );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getUnlocalisedAdjective()
|
||||
{
|
||||
return m_advanced
|
||||
? "upgrade.computercraft:advanced_modem.adjective"
|
||||
: "upgrade.computercraft:wireless_modem.adjective";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ItemStack getCraftingItem()
|
||||
{
|
||||
return PeripheralItemFactory.create(
|
||||
m_advanced ? PeripheralType.AdvancedModem : PeripheralType.WirelessModem,
|
||||
null, 1
|
||||
);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IPeripheral createPeripheral( @Nonnull IPocketAccess access )
|
||||
{
|
||||
return new PocketModemPeripheral( m_advanced );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral )
|
||||
{
|
||||
if( peripheral instanceof PocketModemPeripheral )
|
||||
{
|
||||
Entity entity = access.getEntity();
|
||||
|
||||
PocketModemPeripheral modem = (PocketModemPeripheral) peripheral;
|
||||
if( entity instanceof EntityLivingBase )
|
||||
{
|
||||
EntityLivingBase player = (EntityLivingBase) entity;
|
||||
modem.setLocation( entity.getEntityWorld(), player.posX, player.posY + player.getEyeHeight(), player.posZ );
|
||||
}
|
||||
else if( entity != null )
|
||||
{
|
||||
modem.setLocation( entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ );
|
||||
}
|
||||
|
||||
access.setModemLight( modem.isActive() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onRightClick( @Nonnull World world, @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
@ -6,9 +6,9 @@
|
||||
|
||||
package dan200.computercraft.shared.pocket.recipes;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.IPeripheralItem;
|
||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||
import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
@ -31,7 +31,7 @@ public class PocketComputerUpgradeRecipe implements IRecipe
|
||||
@Override
|
||||
public ItemStack getRecipeOutput()
|
||||
{
|
||||
return PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, true );
|
||||
return PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, null );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -71,8 +71,14 @@ public class PocketComputerUpgradeRecipe implements IRecipe
|
||||
return null;
|
||||
}
|
||||
|
||||
ItemPocketComputer itemComputer = (ItemPocketComputer)computer.getItem();
|
||||
if( itemComputer.getUpgrade( computer ) != null )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check for upgrades around the item
|
||||
ItemStack upgrade = null;
|
||||
IPocketUpgrade upgrade = null;
|
||||
for (int y = 0; y < inventory.getHeight(); ++y)
|
||||
{
|
||||
for (int x = 0; x < inventory.getWidth(); ++x)
|
||||
@ -84,24 +90,14 @@ public class PocketComputerUpgradeRecipe implements IRecipe
|
||||
}
|
||||
else if( x == computerX && y == computerY - 1 )
|
||||
{
|
||||
if( item != null && item.getItem() instanceof IPeripheralItem &&
|
||||
((IPeripheralItem)item.getItem()).getPeripheralType( item ) == PeripheralType.WirelessModem )
|
||||
{
|
||||
upgrade = item;
|
||||
upgrade = ComputerCraft.getPocketUpgrade( item );
|
||||
if( upgrade == null ) return null;
|
||||
}
|
||||
else
|
||||
else if( item != null )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( item != null )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( upgrade == null )
|
||||
@ -109,18 +105,11 @@ public class PocketComputerUpgradeRecipe implements IRecipe
|
||||
return null;
|
||||
}
|
||||
|
||||
// At this point we have a computer + 1 upgrade
|
||||
ItemPocketComputer itemComputer = (ItemPocketComputer)computer.getItem();
|
||||
if( itemComputer.getHasModem( computer ) )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Construct the new stack
|
||||
ComputerFamily family = itemComputer.getFamily( computer );
|
||||
int computerID = itemComputer.getComputerID( computer );
|
||||
String label = itemComputer.getLabel( computer );
|
||||
return PocketComputerItemFactory.create( computerID, label, family, true );
|
||||
return PocketComputerItemFactory.create( computerID, label, family, upgrade );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -8,6 +8,7 @@ package dan200.computercraft.shared.proxy;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.ComputerCraftAPI;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.core.computer.MainThread;
|
||||
import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider;
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
@ -45,6 +46,7 @@ import dan200.computercraft.shared.peripheral.printer.TilePrinter;
|
||||
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||
import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory;
|
||||
import dan200.computercraft.shared.pocket.peripherals.PocketModem;
|
||||
import dan200.computercraft.shared.pocket.recipes.PocketComputerUpgradeRecipe;
|
||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
|
||||
@ -410,7 +412,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
GameRegistry.addRecipe( new ImpostorShapelessRecipe( bookPrintout, new Object[]{leather, singlePrintout, string} ) );
|
||||
|
||||
// Pocket Computer
|
||||
ItemStack pocketComputer = PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, false );
|
||||
ItemStack pocketComputer = PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, null );
|
||||
GameRegistry.addRecipe( pocketComputer,
|
||||
"XXX", "XYX", "XZX",
|
||||
'X', Blocks.STONE,
|
||||
@ -419,7 +421,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
);
|
||||
|
||||
// Advanced Pocket Computer
|
||||
ItemStack advancedPocketComputer = PocketComputerItemFactory.create( -1, null, ComputerFamily.Advanced, false );
|
||||
ItemStack advancedPocketComputer = PocketComputerItemFactory.create( -1, null, ComputerFamily.Advanced, null );
|
||||
GameRegistry.addRecipe( advancedPocketComputer,
|
||||
"XXX", "XYX", "XZX",
|
||||
'X', Items.GOLD_INGOT,
|
||||
@ -427,16 +429,30 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
'Z', Blocks.GLASS_PANE
|
||||
);
|
||||
|
||||
// Register pocket upgrades
|
||||
ComputerCraft.PocketUpgrades.wirelessModem = new PocketModem( false );
|
||||
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.wirelessModem );
|
||||
ComputerCraft.PocketUpgrades.advancedModem = new PocketModem( true );
|
||||
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.advancedModem );
|
||||
|
||||
// Wireless Pocket Computer
|
||||
ItemStack wirelessPocketComputer = PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, true );
|
||||
GameRegistry.addRecipe( new PocketComputerUpgradeRecipe() );
|
||||
|
||||
// Advanced Wireless Pocket Computer
|
||||
ItemStack advancedWirelessPocketComputer = PocketComputerItemFactory.create( -1, null, ComputerFamily.Advanced, true );
|
||||
|
||||
// Impostor Pocket Computer recipes (to fool NEI)
|
||||
GameRegistry.addRecipe( new ImpostorRecipe( 1, 2, new ItemStack[]{wirelessModem, pocketComputer}, wirelessPocketComputer ) );
|
||||
GameRegistry.addRecipe( new ImpostorRecipe( 1, 2, new ItemStack[]{wirelessModem, advancedPocketComputer}, advancedWirelessPocketComputer ) );
|
||||
for (IPocketUpgrade upgrade : ComputerCraft.getVanillaPocketUpgrades())
|
||||
{
|
||||
GameRegistry.addRecipe( new ImpostorRecipe(
|
||||
1, 2,
|
||||
new ItemStack[]{ upgrade.getCraftingItem(), pocketComputer },
|
||||
PocketComputerItemFactory.create( -1, null, ComputerFamily.Normal, upgrade )
|
||||
) );
|
||||
|
||||
GameRegistry.addRecipe( new ImpostorRecipe(
|
||||
1, 2,
|
||||
new ItemStack[]{ upgrade.getCraftingItem(), advancedPocketComputer },
|
||||
PocketComputerItemFactory.create( -1, null, ComputerFamily.Advanced, upgrade )
|
||||
) );
|
||||
}
|
||||
|
||||
// Skulls (Easter Egg)
|
||||
// Dan
|
||||
|
Loading…
Reference in New Issue
Block a user