Merge master into startup-dir

This commit is contained in:
Lignum 2017-05-16 23:25:54 +02:00
commit 03794970ba
No known key found for this signature in database
GPG Key ID: E4DE8F54CA0912BA
296 changed files with 3421 additions and 2278 deletions

View File

@ -63,6 +63,12 @@
}
jar {
manifest {
attributes('FMLAT': 'computercraft_at.cfg')
}
}
processResources
{
// this will ensure that this task is redone when the versions change.

0
deploy.sh Normal file → Executable file
View File

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -40,6 +40,7 @@
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.pocket.peripherals.PocketSpeaker;
import dan200.computercraft.shared.proxy.ICCTurtleProxy;
import dan200.computercraft.shared.proxy.IComputerCraftProxy;
import dan200.computercraft.shared.turtle.blocks.BlockTurtle;
@ -68,6 +69,7 @@
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
@ -90,6 +92,7 @@
public class ComputerCraft
{
public static final String MOD_ID = "ComputerCraft";
public static final String LOWER_ID = "computercraft";
// GUI IDs
public static final int diskDriveGUIID = 100;
@ -105,6 +108,7 @@ public class ComputerCraft
public static String http_whitelist = "*";
public static boolean disable_lua51_features = false;
public static String default_computer_settings = "";
public static boolean logPeripheralErrors = false;
public static boolean enableCommandBlock = false;
public static boolean turtlesNeedFuel = true;
@ -131,6 +135,8 @@ public class ComputerCraft
public static int floppySpaceLimit = 125 * 1000;
public static int maximumFilesOpen = 128;
public static int maxNotesPerTick = 8;
// Blocks and Items
public static class Blocks
{
@ -163,12 +169,14 @@ public static class Upgrades
public static TurtleAxe diamondAxe;
public static TurtleHoe diamondHoe;
public static TurtleModem advancedModem;
public static TurtleSpeaker turtleSpeaker;
}
public static class PocketUpgrades
{
public static PocketModem wirelessModem;
public static PocketModem advancedModem;
public static PocketSpeaker pocketSpeaker;
}
public static class Config {
@ -178,6 +186,7 @@ public static class Config {
public static Property http_whitelist;
public static Property disable_lua51_features;
public static Property default_computer_settings;
public static Property logPeripheralErrors;
public static Property enableCommandBlock;
public static Property turtlesNeedFuel;
@ -194,6 +203,8 @@ public static class Config {
public static Property computerSpaceLimit;
public static Property floppySpaceLimit;
public static Property maximumFilesOpen;
public static Property maxNotesPerTick;
}
// Registries
@ -206,6 +217,9 @@ public static class Config {
// Creative
public static CreativeTabMain mainCreativeTab;
// Logging
public static Logger log;
// API users
private static List<IPeripheralProvider> peripheralProviders = new ArrayList<IPeripheralProvider>();
private static List<IBundledRedstoneProvider> bundledRedstoneProviders = new ArrayList<IBundledRedstoneProvider>();
@ -230,6 +244,8 @@ public ComputerCraft()
@Mod.EventHandler
public void preInit( FMLPreInitializationEvent event )
{
log = event.getModLog();
// Load config
Config.config = new Configuration( event.getSuggestedConfigurationFile() );
Config.config.load();
@ -246,6 +262,10 @@ public void preInit( FMLPreInitializationEvent event )
Config.default_computer_settings = Config.config.get( Configuration.CATEGORY_GENERAL, "default_computer_settings", default_computer_settings );
Config.default_computer_settings.setComment( "A comma seperated list of default system settings to set on new computers. Example: \"shell.autocomplete=false,lua.autocomplete=false,edit.autocomplete=false\" will disable all autocompletion" );
Config.logPeripheralErrors = Config.config.get( Configuration.CATEGORY_GENERAL, "logPeripheralErrors", logPeripheralErrors );
Config.logPeripheralErrors.setComment( "Log exceptions thrown by peripherals and other Lua objects.\n" +
"This makes it easier for mod authors to debug problems, but may result in log spam should people use buggy methods." );
Config.enableCommandBlock = Config.config.get( Configuration.CATEGORY_GENERAL, "enableCommandBlock", enableCommandBlock );
Config.enableCommandBlock.setComment( "Enable Command Block peripheral support" );
@ -285,6 +305,9 @@ public void preInit( FMLPreInitializationEvent event )
Config.turtlesCanPush = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesCanPush", turtlesCanPush );
Config.turtlesCanPush.setComment( "If set to true, Turtles will push entities out of the way instead of stopping if there is space to do so" );
Config.maxNotesPerTick = Config.config.get( Configuration.CATEGORY_GENERAL, "maxNotesPerTick", maxNotesPerTick );
Config.maxNotesPerTick.setComment( "Maximum amount of notes a speaker can play at once" );
for (Property property : Config.config.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues())
{
property.setLanguageKey( "gui.computercraft:config." + CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, property.getName() ) );
@ -324,6 +347,8 @@ public static void syncConfig() {
turtlesObeyBlockProtection = Config.turtlesObeyBlockProtection.getBoolean();
turtlesCanPush = Config.turtlesCanPush.getBoolean();
maxNotesPerTick = Math.max(1, Config.maxNotesPerTick.getInt());
Config.config.save();
}
@ -334,6 +359,14 @@ public void init( FMLInitializationEvent event )
turtleProxy.init();
}
@Mod.EventHandler
public void onMissingMappings( FMLMissingMappingsEvent event )
{
proxy.remap( event );
turtleProxy.remap( event );
}
@Mod.EventHandler
public void onServerStarting( FMLServerStartingEvent event )
{
@ -596,7 +629,7 @@ public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing
}
catch( Exception e )
{
// mod misbehaved, ignore it
ComputerCraft.log.error( "Peripheral provider " + peripheralProvider + " errored.", e );
}
}
return null;
@ -640,7 +673,7 @@ public static int getBundledRedstoneOutput( World world, BlockPos pos, EnumFacin
}
catch( Exception e )
{
// mod misbehaved, ignore it
ComputerCraft.log.error( "Bundled redstone provider " + bundledRedstoneProvider + " errored.", e );
}
}
return combinedSignal;
@ -664,6 +697,7 @@ public static IMedia getMedia( ItemStack stack )
catch( Exception e )
{
// mod misbehaved, ignore it
ComputerCraft.log.error( "Media provider " + mediaProvider + " errored.", e );
}
}
return null;
@ -694,7 +728,7 @@ public static IPocketUpgrade getPocketUpgrade( ItemStack stack )
public static Iterable<IPocketUpgrade> getVanillaPocketUpgrades() {
List<IPocketUpgrade> upgrades = new ArrayList<IPocketUpgrade>();
for(IPocketUpgrade upgrade : pocketUpgrades.values()) {
if(upgrade instanceof PocketModem) {
if(upgrade instanceof PocketModem || upgrade instanceof PocketSpeaker) {
upgrades.add( upgrade );
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -23,26 +23,40 @@ public interface IPocketAccess
Entity getEntity();
/**
* Get the colour of the pocket computer's light.
* Get the colour of this pocket computer as a RGB number.
*
* See {@link #setLight(int)} for the values this may return.
* @return The colour this pocket computer is. This will be a RGB colour between {@code 0x000000} and
* {@code 0xFFFFFF} or -1 if it has no colour.
* @see #setColour(int)
*/
int getColour();
/**
* Set the colour of the pocket computer to a RGB number.
*
* @return The colour of the pocket computer's light.
* @param colour The colour this pocket computer should be changed to. This should be a RGB colour between
* {@code 0x000000} and {@code 0xFFFFFF} or -1 to reset to the default colour.
* @see #getColour()
*/
void setColour( int colour );
/**
* Get the colour of this pocket computer's light as a RGB number.
*
* @return The colour this light is. This will be a RGB colour between {@code 0x000000} and {@code 0xFFFFFF} or
* -1 if it has no colour.
* @see #setLight(int)
*/
int getLight();
/**
* Set the colour of the pocket computer's light. Use {@link 0} to turn it off.
* Set the colour of the pocket computer's light to a RGB number.
*
* Colours take the form of an integer between 0 and 15, using the opposite order to those in
* {@link <a href="http://www.computercraft.info/wiki/Colors_(API)#Colors">The colors API</a>} - so 0 being black,
* 1 representing red, 2 representing green all the way up to 15 for white.
*
* @param value The colour the light should have.
* @param colour The colour this modem's light will be changed to. This should be a RGB colour between
* {@code 0x000000} and {@code 0xFFFFFF} or -1 to reset to the default colour.
* @see #getLight()
*/
void setLight( int value );
void setLight( int colour );
/**
* Get the upgrade-specific NBT.

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/
@ -15,6 +15,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandlerModifiable;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -117,31 +118,43 @@ public interface ITurtleAccess
void setSelectedSlot( int slot );
/**
* Sets the colour of the turtle, as if the player had dyed it with a dye item.
* Set the colour of the turtle to a RGB number.
*
* @param dyeColour 0-15 to dye the turtle one of the 16 standard Minecraft <em>dye</em> colours, or -1 to remove
* the dye from the turtle.
* @see #getDyeColour()
* @param colour The colour this turtle should be changed to. This should be a RGB colour between {@code 0x000000}
* and {@code 0xFFFFFF} or -1 to reset to the default colour.
* @see #getColour()
*/
void setDyeColour( int dyeColour );
void setColour( int colour );
/**
* Gets the colour the turtle has been dyed.
* Get the colour of this turtle as a RGB number.
*
* @return 0-15 if the turtle has been dyed one of the 16 standard Minecraft <em>dye</em> colours, -1 if the turtle
* is clean.
* @see #getDyeColour()
* @return The colour this turtle is. This will be a RGB colour between {@code 0x000000} and {@code 0xFFFFFF} or
* -1 if it has no colour.
* @see #setColour(int)
*/
int getDyeColour();
int getColour();
/**
* Get the inventory of this turtle
*
* @return This turtle's inventory
* @see #getItemHandler()
*/
@Nonnull
IInventory getInventory();
/**
* Get the inventory of this turtle as an {@link IItemHandlerModifiable}.
*
* @return This turtle's inventory
* @see #getInventory()
* @see IItemHandlerModifiable
* @see net.minecraftforge.items.CapabilityItemHandler#ITEM_HANDLER_CAPABILITY
*/
@Nonnull
IItemHandlerModifiable getItemHandler();
/**
* Determine whether this turtle will require fuel when performing actions.
*

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of the public ComputerCraft API - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. This API may be redistributed unmodified and in full only.
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
* For help using the API, and posting your mods, visit the forums at computercraft.info.
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -14,7 +14,6 @@
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import dan200.computercraft.shared.turtle.core.TurtleBrain;
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
import dan200.computercraft.shared.util.Colour;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemMeshDefinition;
@ -156,6 +155,8 @@ public void onModelBakeEvent( ModelBakeEvent event )
loadModel( event, "advanced_turtle_modem_on_left" );
loadModel( event, "advanced_turtle_modem_off_right" );
loadModel( event, "advanced_turtle_modem_on_right" );
loadModel( event, "turtle_speaker_upgrade_left" );
loadModel( event, "turtle_speaker_upgrade_right" );
loadSmartModel( event, "turtle_dynamic", m_turtleSmartItemModel );
}
@ -199,8 +200,8 @@ public int getColorFromItemstack( @Nonnull ItemStack stack, int tintIndex )
if( tintIndex == 0 )
{
ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
Colour colour = turtle.getColour( stack );
if( colour != null ) return colour.getHex();
int colour = turtle.getColour( stack );
if( colour != -1 ) return colour;
}
return 0xFFFFFF;

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -9,6 +9,7 @@
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.client.gui.*;
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
import dan200.computercraft.shared.computer.blocks.ComputerState;
import dan200.computercraft.shared.computer.blocks.TileComputer;
import dan200.computercraft.shared.computer.core.ClientComputer;
import dan200.computercraft.shared.computer.core.ComputerFamily;
@ -47,7 +48,6 @@
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.client.registry.ClientRegistry;
@ -100,6 +100,7 @@ public ModelResourceLocation getModelLocation( @Nonnull ItemStack stack )
registerItemModel( ComputerCraft.Blocks.cable, 1, "wired_modem" );
registerItemModel( ComputerCraft.Blocks.commandComputer, "command_computer" );
registerItemModel( ComputerCraft.Blocks.advancedModem, "advanced_modem" );
registerItemModel( ComputerCraft.Blocks.peripheral, 5, "speaker" );
registerItemModel( ComputerCraft.Items.disk, "disk" );
registerItemModel( ComputerCraft.Items.diskExpanded, "disk_expanded" );
@ -112,61 +113,66 @@ public ModelResourceLocation getModelLocation( @Nonnull ItemStack stack )
private ModelResourceLocation pocket_computer_off = new ModelResourceLocation( "computercraft:pocket_computer", "inventory" );
private ModelResourceLocation pocket_computer_on = new ModelResourceLocation( "computercraft:pocket_computer_on", "inventory" );
private ModelResourceLocation pocket_computer_blinking = new ModelResourceLocation( "computercraft:pocket_computer_blinking", "inventory" );
private ModelResourceLocation advanced_pocket_computer_off = new ModelResourceLocation( "computercraft:advanced_pocket_computer_off", "inventory" );
private ModelResourceLocation advanced_pocket_computer_off = new ModelResourceLocation( "computercraft:advanced_pocket_computer", "inventory" );
private ModelResourceLocation advanced_pocket_computer_on = new ModelResourceLocation( "computercraft:advanced_pocket_computer_on", "inventory" );
private ModelResourceLocation advanced_pocket_computer_blinking = new ModelResourceLocation( "computercraft:advanced_pocket_computer_blinking", "inventory" );
private ModelResourceLocation colour_pocket_computer_off = new ModelResourceLocation( "computercraft:pocket_computer_colour", "inventory" );
private ModelResourceLocation colour_pocket_computer_on = new ModelResourceLocation( "computercraft:pocket_computer_colour_on", "inventory" );
private ModelResourceLocation colour_pocket_computer_blinking = new ModelResourceLocation( "computercraft:pocket_computer_colour_blinking", "inventory" );
@Nonnull
@Override
public ModelResourceLocation getModelLocation( @Nonnull ItemStack stack )
{
ItemPocketComputer itemPocketComputer = (ItemPocketComputer)stack.getItem();
switch( itemPocketComputer.getFamily( stack ) )
ItemPocketComputer itemPocketComputer = (ItemPocketComputer) stack.getItem();
ComputerState state = itemPocketComputer.getState( stack );
if( itemPocketComputer.getColour( stack ) == -1 )
{
case Advanced:
switch( itemPocketComputer.getFamily( stack ) )
{
switch( itemPocketComputer.getState( stack ) )
{
case Off:
default:
case Advanced:
switch( state )
{
return advanced_pocket_computer_off;
case Off:
default:
return advanced_pocket_computer_off;
case On:
return advanced_pocket_computer_on;
case Blinking:
return advanced_pocket_computer_blinking;
}
case On:
case Normal:
default:
switch( state )
{
return advanced_pocket_computer_on;
case Off:
default:
return pocket_computer_off;
case On:
return pocket_computer_on;
case Blinking:
return pocket_computer_blinking;
}
case Blinking:
{
return advanced_pocket_computer_blinking;
}
}
}
case Normal:
default:
}
else
{
switch( state )
{
switch( itemPocketComputer.getState( stack ) )
{
case Off:
default:
{
return pocket_computer_off;
}
case On:
{
return pocket_computer_on;
}
case Blinking:
{
return pocket_computer_blinking;
}
}
case Off:
default:
return colour_pocket_computer_off;
case On:
return colour_pocket_computer_on;
case Blinking:
return colour_pocket_computer_blinking;
}
}
}
}, new String[] {
"pocket_computer", "pocket_computer_on", "pocket_computer_blinking",
"advanced_pocket_computer_off", "advanced_pocket_computer_on", "advanced_pocket_computer_blinking",
"advanced_pocket_computer", "advanced_pocket_computer_on", "advanced_pocket_computer_blinking",
"pocket_computer_colour", "pocket_computer_colour_on", "pocket_computer_colour_blinking",
} );
// Setup client forge handlers
@ -189,12 +195,26 @@ public void init()
mc.getItemColors().registerItemColorHandler( new IItemColor()
{
@Override
public int getColorFromItemstack( @Nonnull ItemStack stack, int layout )
public int getColorFromItemstack( @Nonnull ItemStack stack, int layer )
{
if( layout != 1 ) return 0xFFFFFF;
Colour colour = Colour.fromInt( ComputerCraft.Items.pocketComputer.getLightState( stack ) );
return colour == null ? Colour.Black.getHex() : colour.getHex();
switch( layer )
{
case 0:
default:
return 0xFFFFFF;
case 1:
{
// Frame colour
int colour = ComputerCraft.Items.pocketComputer.getColour( stack );
return colour == -1 ? 0xFFFFFF : colour;
}
case 2:
{
// Light colour
int colour = ComputerCraft.Items.pocketComputer.getLightState( stack );
return colour == -1 ? Colour.Black.getHex() : colour;
}
}
}
}, ComputerCraft.Items.pocketComputer );
@ -515,20 +535,20 @@ public void onRenderTick( TickEvent.RenderTickEvent event )
}
}
@SideOnly(Side.CLIENT)
private static class DiskColorHandler implements IItemColor
{
private final ItemDiskLegacy disk;
@SideOnly(Side.CLIENT)
private static class DiskColorHandler implements IItemColor
{
private final ItemDiskLegacy disk;
private DiskColorHandler(ItemDiskLegacy disk)
{
this.disk = disk;
}
private DiskColorHandler( ItemDiskLegacy disk )
{
this.disk = disk;
}
@Override
public int getColorFromItemstack( @Nonnull ItemStack stack, int layer)
{
return layer == 0 ? 0xFFFFFF : disk.getColor(stack);
}
}
@Override
public int getColorFromItemstack( @Nonnull ItemStack stack, int layer )
{
return layer == 0 ? 0xFFFFFF : disk.getColour( stack );
}
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -12,7 +12,6 @@
import dan200.computercraft.shared.computer.core.IComputer;
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import dan200.computercraft.shared.turtle.entity.TurtleVisionCamera;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.shared.util.Holiday;
import dan200.computercraft.shared.util.HolidayUtil;
import net.minecraft.block.state.IBlockState;
@ -51,24 +50,7 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
private static ModelResourceLocation ADVANCED_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_advanced", "inventory" );
private static ModelResourceLocation COLOUR_TURTLE_MODEL = new ModelResourceLocation( "computercraft:turtle_white", "inventory" );
private static ModelResourceLocation BEGINNER_TURTLE_MODEL = new ModelResourceLocation( "computercraftedu:CC-TurtleJunior", "inventory" );
private static ModelResourceLocation[] BEGINNER_TURTLE_COLOUR_MODELS = new ModelResourceLocation[] {
new ModelResourceLocation( "computercraftedu:turtleJunior_black", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_red", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_green", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_brown", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_blue", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_purple", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_cyan", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_lightGrey", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_grey", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_pink", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_lime", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_yellow", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_lightBlue", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_magenta", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_orange", "inventory" ),
new ModelResourceLocation( "computercraftedu:turtleJunior_white", "inventory" ),
};
private static ModelResourceLocation BEGINNER_TURTLE_COLOUR_MODEL = new ModelResourceLocation( "computercraftedu:turtleJunior_white", "inventory" );
private static ModelResourceLocation ELF_OVERLAY_MODEL = new ModelResourceLocation( "computercraft:turtle_elf_overlay", "inventory" );
public TileEntityTurtleRenderer()
@ -96,17 +78,17 @@ public void renderTileEntityAt( @Nonnull TileTurtle tileEntity, double posX, dou
}
}
public static ModelResourceLocation getTurtleModel( ComputerFamily family, Colour colour )
public static ModelResourceLocation getTurtleModel( ComputerFamily family, boolean coloured )
{
switch( family )
{
case Normal:
default:
return colour != null ? COLOUR_TURTLE_MODEL : NORMAL_TURTLE_MODEL;
return coloured ? COLOUR_TURTLE_MODEL : NORMAL_TURTLE_MODEL;
case Advanced:
return colour != null ? COLOUR_TURTLE_MODEL : ADVANCED_TURTLE_MODEL;
return coloured ? COLOUR_TURTLE_MODEL : ADVANCED_TURTLE_MODEL;
case Beginners:
return colour != null ? BEGINNER_TURTLE_COLOUR_MODELS[ colour.ordinal() ] : BEGINNER_TURTLE_MODEL;
return coloured ? BEGINNER_TURTLE_COLOUR_MODEL : BEGINNER_TURTLE_MODEL;
}
}
@ -161,7 +143,7 @@ private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double
GlStateManager.translate( -0.5f, 0.0f, -0.5f );
// Render the turtle
Colour colour;
int colour;
ComputerFamily family;
ResourceLocation overlay;
if( turtle != null )
@ -172,12 +154,12 @@ private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double
}
else
{
colour = null;
colour = -1;
family = ComputerFamily.Normal;
overlay = null;
}
renderModel( state, getTurtleModel( family, colour ), colour == null ? null : new int[] { colour.getHex() } );
renderModel( state, getTurtleModel( family, colour != -1 ), colour == -1 ? null : new int[] { colour } );
// Render the overlay
ModelResourceLocation overlayModel = getTurtleOverlayModel(

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -12,7 +12,6 @@
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.shared.util.Holiday;
import dan200.computercraft.shared.util.HolidayUtil;
import net.minecraft.block.state.IBlockState;
@ -40,13 +39,13 @@ public class TurtleSmartItemModel implements IBakedModel, IResourceManagerReload
private static class TurtleModelCombination
{
public final ComputerFamily m_family;
public final Colour m_colour;
public final boolean m_colour;
public final ITurtleUpgrade m_leftUpgrade;
public final ITurtleUpgrade m_rightUpgrade;
public final ResourceLocation m_overlay;
public final boolean m_christmas;
public TurtleModelCombination( ComputerFamily family, Colour colour, ITurtleUpgrade leftUpgrade, ITurtleUpgrade rightUpgrade, ResourceLocation overlay, boolean christmas )
public TurtleModelCombination( ComputerFamily family, boolean colour, ITurtleUpgrade leftUpgrade, ITurtleUpgrade rightUpgrade, ResourceLocation overlay, boolean christmas )
{
m_family = family;
m_colour = colour;
@ -83,7 +82,7 @@ public int hashCode()
final int prime = 31;
int result = 1;
result = prime * result + m_family.hashCode();
result = prime * result + (m_colour != null ? m_colour.hashCode() : 0);
result = prime * result + (m_colour ? 1 : 0);
result = prime * result + (m_leftUpgrade != null ? m_leftUpgrade.hashCode() : 0);
result = prime * result + (m_rightUpgrade != null ? m_rightUpgrade.hashCode() : 0);
result = prime * result + (m_overlay != null ? m_overlay.hashCode() : 0);
@ -98,7 +97,7 @@ public int hashCode()
public TurtleSmartItemModel()
{
m_defaultItem = TurtleItemFactory.create( -1, null, null, ComputerFamily.Normal, null, null, 0, null );
m_defaultItem = TurtleItemFactory.create( -1, null, -1, ComputerFamily.Normal, null, null, 0, null );
m_cachedModels = new HashMap<TurtleModelCombination, IBakedModel>();
m_overrides = new ItemOverrideList( new ArrayList<ItemOverride>() )
{
@ -108,12 +107,12 @@ public IBakedModel handleItemState( @Nonnull IBakedModel originalModel, ItemStac
{
ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
ComputerFamily family = turtle.getFamily( stack );
Colour colour = turtle.getColour( stack );
int colour = turtle.getColour( stack );
ITurtleUpgrade leftUpgrade = turtle.getUpgrade( stack, TurtleSide.Left );
ITurtleUpgrade rightUpgrade = turtle.getUpgrade( stack, TurtleSide.Right );
ResourceLocation overlay = turtle.getOverlay( stack );
boolean christmas = HolidayUtil.getCurrentHoliday() == Holiday.Christmas;
TurtleModelCombination combo = new TurtleModelCombination( family, colour, leftUpgrade, rightUpgrade, overlay, christmas );
TurtleModelCombination combo = new TurtleModelCombination( family, colour != -1, leftUpgrade, rightUpgrade, overlay, christmas );
if( m_cachedModels.containsKey( combo ) )
{
return m_cachedModels.get( combo );

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,21 +1,23 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaObject;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.handles.BinaryInputHandle;
import dan200.computercraft.core.apis.handles.BinaryOutputHandle;
import dan200.computercraft.core.apis.handles.EncodedInputHandle;
import dan200.computercraft.core.apis.handles.EncodedOutputHandle;
import dan200.computercraft.core.filesystem.FileSystem;
import dan200.computercraft.core.filesystem.FileSystemException;
import dan200.computercraft.core.filesystem.IMountedFileBinary;
import dan200.computercraft.core.filesystem.IMountedFileNormal;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
@ -259,40 +261,40 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
try {
if( mode.equals( "r" ) ) {
// Open the file for reading, then create a wrapper around the reader
IMountedFileNormal reader = m_fileSystem.openForRead( path );
return wrapBufferedReader( reader );
InputStream reader = m_fileSystem.openForRead( path );
return new Object[] { new EncodedInputHandle( reader ) };
} else if( mode.equals( "w" ) ) {
// Open the file for writing, then create a wrapper around the writer
IMountedFileNormal writer = m_fileSystem.openForWrite( path, false );
return wrapBufferedWriter( writer );
OutputStream writer = m_fileSystem.openForWrite( path, false );
return new Object[] { new EncodedOutputHandle( writer ) };
} else if( mode.equals( "a" ) ) {
// Open the file for appending, then create a wrapper around the writer
IMountedFileNormal writer = m_fileSystem.openForWrite( path, true );
return wrapBufferedWriter( writer );
OutputStream writer = m_fileSystem.openForWrite( path, true );
return new Object[] { new EncodedOutputHandle( writer ) };
} else if( mode.equals( "rb" ) ) {
// Open the file for binary reading, then create a wrapper around the reader
IMountedFileBinary reader = m_fileSystem.openForBinaryRead( path );
return wrapInputStream( reader );
InputStream reader = m_fileSystem.openForRead( path );
return new Object[] { new BinaryInputHandle( reader ) };
} else if( mode.equals( "wb" ) ) {
// Open the file for binary writing, then create a wrapper around the writer
IMountedFileBinary writer = m_fileSystem.openForBinaryWrite( path, false );
return wrapOutputStream( writer );
OutputStream writer = m_fileSystem.openForWrite( path, false );
return new Object[] { new BinaryOutputHandle( writer ) };
} else if( mode.equals( "ab" ) ) {
// Open the file for binary appending, then create a wrapper around the reader
IMountedFileBinary writer = m_fileSystem.openForBinaryWrite( path, true );
return wrapOutputStream( writer );
OutputStream writer = m_fileSystem.openForWrite( path, true );
return new Object[] { new BinaryOutputHandle( writer ) };
} else {
throw new LuaException( "Unsupported mode" );
}
} catch( FileSystemException e ) {
return null;
return new Object[] { null, e.getMessage() };
}
}
case 12:
@ -368,269 +370,4 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
}
}
}
private static Object[] wrapBufferedReader( final IMountedFileNormal reader )
{
return new Object[] { new ILuaObject() {
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"readLine",
"readAll",
"close"
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
{
switch( method )
{
case 0:
{
// readLine
try {
String line = reader.readLine();
if( line != null ) {
return new Object[] { line };
} else {
return null;
}
} catch( IOException e ) {
return null;
}
}
case 1:
{
// readAll
try {
StringBuilder result = new StringBuilder( "" );
String line = reader.readLine();
while( line != null ) {
result.append( line );
line = reader.readLine();
if( line != null ) {
result.append( "\n" );
}
}
return new Object[] { result.toString() };
} catch( IOException e ) {
return null;
}
}
case 2:
{
// close
try {
reader.close();
return null;
} catch( IOException e ) {
return null;
}
}
default:
{
return null;
}
}
}
} };
}
private static Object[] wrapBufferedWriter( final IMountedFileNormal writer )
{
return new Object[] { new ILuaObject() {
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"write",
"writeLine",
"close",
"flush"
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
{
switch( method )
{
case 0:
{
// write
String text;
if( args.length > 0 && args[0] != null ) {
text = args[0].toString();
} else {
text = "";
}
try {
writer.write( text, 0, text.length(), false );
return null;
} catch( IOException e ) {
throw new LuaException( e.getMessage() );
}
}
case 1:
{
// writeLine
String text;
if( args.length > 0 && args[0] != null ) {
text = args[0].toString();
} else {
text = "";
}
try {
writer.write( text, 0, text.length(), true );
return null;
} catch( IOException e ) {
throw new LuaException( e.getMessage() );
}
}
case 2:
{
// close
try {
writer.close();
return null;
} catch( IOException e ) {
return null;
}
}
case 3:
{
try {
writer.flush();
return null;
} catch ( IOException e ) {
return null;
}
}
default:
{
assert( false );
return null;
}
}
}
} };
}
private static Object[] wrapInputStream( final IMountedFileBinary reader )
{
return new Object[] { new ILuaObject() {
@Nonnull
@Override
public String[] getMethodNames() {
return new String[] {
"read",
"close"
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args) throws LuaException {
switch( method )
{
case 0:
{
// read
try {
int b = reader.read();
if( b != -1 ) {
return new Object[] { b };
} else {
return null;
}
} catch( IOException e ) {
return null;
}
}
case 1:
{
//close
try {
reader.close();
return null;
} catch( IOException e ) {
return null;
}
}
default:
{
assert( false );
return null;
}
}
}
}};
}
private static Object[] wrapOutputStream( final IMountedFileBinary writer )
{
return new Object[] { new ILuaObject() {
@Nonnull
@Override
public String[] getMethodNames() {
return new String[] {
"write",
"close",
"flush"
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args) throws LuaException {
switch( method )
{
case 0:
{
// write
try {
if( args.length > 0 && args[0] instanceof Number )
{
int number = ((Number)args[0]).intValue();
writer.write( number );
}
return null;
} catch( IOException e ) {
throw new LuaException(e.getMessage());
}
}
case 1:
{
//close
try {
writer.close();
return null;
} catch( IOException e ) {
return null;
}
}
case 2:
{
try {
writer.flush();
return null;
} catch ( IOException e ) {
return null;
}
}
default:
{
assert( false );
return null;
}
}
}
}};
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -9,10 +9,11 @@
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaObject;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.core.apis.handles.BinaryInputHandle;
import dan200.computercraft.core.apis.handles.EncodedInputHandle;
import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class HTTPAPI implements ILuaAPI
@ -52,15 +53,21 @@ public void advance( double _dt )
final String url = h.getURL();
if( h.wasSuccessful() ) {
// Queue the "http_success" event
final BufferedReader contents = h.getContents();
final Object result = wrapBufferedReader( contents, h.getResponseCode(), h.getResponseHeaders() );
InputStream contents = h.getContents();
Object result = wrapStream(
h.isBinary() ? new BinaryInputHandle( contents ) : new EncodedInputHandle( contents, h.getEncoding() ),
h.getResponseCode(), h.getResponseHeaders()
);
m_apiEnvironment.queueEvent( "http_success", new Object[] { url, result } );
} else {
// Queue the "http_failure" event
BufferedReader contents = h.getContents();
InputStream contents = h.getContents();
Object result = null;
if( contents != null ) {
result = wrapBufferedReader( contents, h.getResponseCode(), h.getResponseHeaders() );
result = wrapStream(
h.isBinary() ? new BinaryInputHandle( contents ) : new EncodedInputHandle( contents, h.getEncoding() ),
h.getResponseCode(), h.getResponseHeaders()
);
}
m_apiEnvironment.queueEvent( "http_failure", new Object[]{ url, "Could not connect", result } );
}
@ -69,76 +76,40 @@ public void advance( double _dt )
}
}
}
private static ILuaObject wrapBufferedReader( final BufferedReader reader, final int responseCode, final Map<String, String> responseHeaders )
private static ILuaObject wrapStream( final ILuaObject reader, final int responseCode, final Map<String, String> responseHeaders )
{
return new ILuaObject() {
String[] oldMethods = reader.getMethodNames();
final int methodOffset = oldMethods.length;
final String[] newMethods = Arrays.copyOf( oldMethods, oldMethods.length + 2 );
newMethods[ methodOffset + 0 ] = "getResponseCode";
newMethods[ methodOffset + 1 ] = "getResponseHeaders";
return new ILuaObject()
{
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"readLine",
"readAll",
"close",
"getResponseCode",
"getResponseHeaders",
};
return newMethods;
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException, InterruptedException
{
switch( method )
if( method < methodOffset )
{
return reader.callMethod( context, method, args );
}
switch( method - methodOffset )
{
case 0:
{
// readLine
try {
String line = reader.readLine();
if( line != null ) {
return new Object[] { line };
} else {
return null;
}
} catch( IOException e ) {
return null;
}
}
case 1:
{
// readAll
try {
StringBuilder result = new StringBuilder( "" );
String line = reader.readLine();
while( line != null ) {
result.append( line );
line = reader.readLine();
if( line != null ) {
result.append( "\n" );
}
}
return new Object[] { result.toString() };
} catch( IOException e ) {
return null;
}
}
case 2:
{
// close
try {
reader.close();
return null;
} catch( IOException e ) {
return null;
}
}
case 3:
{
// getResponseCode
return new Object[] { responseCode };
}
case 4:
case 1:
{
// getResponseHeaders
return new Object[] { responseHeaders };
@ -212,11 +183,18 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
}
}
}
// Get binary
boolean binary = false;
if( args.length >= 4 )
{
binary = args[ 3 ] != null && !args[ 3 ].equals( Boolean.FALSE );
}
// Make the request
try
{
HTTPRequest request = new HTTPRequest( urlString, postString, headers );
HTTPRequest request = new HTTPRequest( urlString, postString, headers, binary );
synchronized( m_httpRequests )
{
m_httpRequests.add( request );

View File

@ -1,12 +1,13 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.apis;
import com.google.common.base.Joiner;
import com.google.common.io.ByteStreams;
import dan200.computercraft.ComputerCraft;
import java.io.*;
@ -60,11 +61,12 @@ public static URL checkURL( String urlString ) throws HTTPRequestException
return url;
}
public HTTPRequest( String url, final String postText, final Map<String, String> headers ) throws HTTPRequestException
public HTTPRequest( String url, final String postText, final Map<String, String> headers, boolean binary ) throws HTTPRequestException
{
// Parse the URL
m_urlString = url;
m_url = checkURL( m_urlString );
m_binary = binary;
// Start the thread
m_cancelled = false;
@ -136,54 +138,10 @@ public void run()
is = connection.getErrorStream();
responseSuccess = false;
}
InputStreamReader isr;
try
{
String contentEncoding = connection.getContentEncoding();
if( contentEncoding != null )
{
try
{
isr = new InputStreamReader( is, contentEncoding );
}
catch( UnsupportedEncodingException e )
{
isr = new InputStreamReader( is, "UTF-8" );
}
}
else
{
isr = new InputStreamReader( is, "UTF-8" );
}
}
catch( UnsupportedEncodingException e )
{
isr = new InputStreamReader( is );
}
// Download the contents
BufferedReader reader = new BufferedReader( isr );
StringBuilder result = new StringBuilder();
while( true )
{
synchronized( m_lock )
{
if( m_cancelled )
{
break;
}
}
String line = reader.readLine();
if( line == null )
{
break;
}
result.append( line );
result.append( '\n' );
}
reader.close();
byte[] result = ByteStreams.toByteArray( is );
is.close();
synchronized( m_lock )
{
if( m_cancelled )
@ -198,8 +156,9 @@ public void run()
// We completed
m_complete = true;
m_success = responseSuccess;
m_result = result.toString();
m_result = result;
m_responseCode = connection.getResponseCode();
m_encoding = connection.getContentEncoding();
Joiner joiner = Joiner.on( ',' );
Map<String, String> headers = m_responseHeaders = new HashMap<String, String>();
@ -264,20 +223,29 @@ public boolean wasSuccessful()
return m_success;
}
}
public BufferedReader getContents()
public boolean isBinary()
{
String result;
return m_binary;
}
public InputStream getContents()
{
byte[] result;
synchronized(m_lock) {
result = m_result;
}
if( result != null ) {
return new BufferedReader( new StringReader( result ) );
return new ByteArrayInputStream( result );
}
return null;
}
public String getEncoding() {
return m_encoding;
}
private final Object m_lock = new Object();
private final URL m_url;
private final String m_urlString;
@ -285,7 +253,9 @@ public BufferedReader getContents()
private boolean m_complete;
private boolean m_cancelled;
private boolean m_success;
private String m_result;
private String m_encoding;
private byte[] m_result;
private boolean m_binary;
private int m_responseCode;
private Map<String, String> m_responseHeaders;
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -0,0 +1,93 @@
package dan200.computercraft.core.apis.handles;
import com.google.common.io.ByteStreams;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
public class BinaryInputHandle extends HandleGeneric
{
private final InputStream m_stream;
public BinaryInputHandle( InputStream reader )
{
super( reader );
this.m_stream = reader;
}
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"read",
"readAll",
"close",
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
{
switch( method )
{
case 0:
// read
checkOpen();
try
{
if( args.length > 0 && args[ 0 ] != null )
{
if( !(args[ 0 ] instanceof Number) )
{
throw new LuaException( "Expected number" );
}
int count = ((Number) args[ 0 ]).intValue();
if( count <= 0 || count >= 1024 * 16 )
{
throw new LuaException( "Count out of range" );
}
byte[] bytes = new byte[ count ];
count = m_stream.read( bytes );
if( count < 0 ) return null;
if( count < bytes.length ) bytes = Arrays.copyOf( bytes, count );
return new Object[] { bytes };
}
else
{
int b = m_stream.read();
return b == -1 ? null : new Object[] { b };
}
}
catch( IOException e )
{
return null;
}
case 1:
// readAll
checkOpen();
try
{
byte[] out = ByteStreams.toByteArray( m_stream );
return out == null ? null : new Object[] { out };
}
catch( IOException e )
{
return null;
}
case 2:
//close
close();
return null;
default:
return null;
}
}
}

View File

@ -0,0 +1,82 @@
package dan200.computercraft.core.apis.handles;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.shared.util.StringUtil;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.OutputStream;
public class BinaryOutputHandle extends HandleGeneric
{
private final OutputStream m_writer;
public BinaryOutputHandle( OutputStream writer )
{
super( writer );
this.m_writer = writer;
}
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"write",
"flush",
"close",
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
{
switch( method )
{
case 0:
// write
checkOpen();
try
{
if( args.length > 0 && args[ 0 ] instanceof Number )
{
int number = ((Number) args[ 0 ]).intValue();
m_writer.write( number );
}
else if( args.length > 0 && args[ 0 ] instanceof String )
{
String value = (String) args[ 0 ];
m_writer.write( StringUtil.encodeString( value ) );
}
else
{
throw new LuaException( "Expected number" );
}
return null;
}
catch( IOException e )
{
throw new LuaException( e.getMessage() );
}
case 1:
// flush
checkOpen();
try
{
m_writer.flush();
return null;
}
catch( IOException e )
{
return null;
}
case 2:
//close
close();
return null;
default:
return null;
}
}
}

View File

@ -0,0 +1,105 @@
package dan200.computercraft.core.apis.handles;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import javax.annotation.Nonnull;
import java.io.*;
public class EncodedInputHandle extends HandleGeneric
{
private final BufferedReader m_reader;
public EncodedInputHandle( BufferedReader reader )
{
super( reader );
this.m_reader = reader;
}
public EncodedInputHandle( InputStream stream )
{
this( stream, "UTF-8" );
}
public EncodedInputHandle( InputStream stream, String encoding )
{
super( stream );
if( encoding == null ) encoding = "UTF-8";
InputStreamReader streamReader;
try
{
streamReader = new InputStreamReader( stream, encoding );
}
catch( UnsupportedEncodingException e )
{
streamReader = new InputStreamReader( stream );
}
this.m_reader = new BufferedReader( streamReader );
}
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"readLine",
"readAll",
"close",
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
{
switch( method )
{
case 0:
// readLine
checkOpen();
try
{
String line = m_reader.readLine();
if( line != null )
{
return new Object[] { line };
}
else
{
return null;
}
}
catch( IOException e )
{
return null;
}
case 1:
// readAll
checkOpen();
try
{
StringBuilder result = new StringBuilder( "" );
String line = m_reader.readLine();
while( line != null )
{
result.append( line );
line = m_reader.readLine();
if( line != null )
{
result.append( "\n" );
}
}
return new Object[] { result.toString() };
}
catch( IOException e )
{
return null;
}
case 2:
// close
close();
return null;
default:
return null;
}
}
}

View File

@ -0,0 +1,124 @@
package dan200.computercraft.core.apis.handles;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import javax.annotation.Nonnull;
import java.io.*;
public class EncodedOutputHandle extends HandleGeneric
{
private final BufferedWriter m_writer;
public EncodedOutputHandle( BufferedWriter writer )
{
super( writer );
this.m_writer = writer;
}
public EncodedOutputHandle( OutputStream stream )
{
this( stream, "UTF-8" );
}
public EncodedOutputHandle( OutputStream stream, String encoding )
{
super( stream );
if( encoding == null ) encoding = "UTF-8";
OutputStreamWriter streamWriter;
try
{
streamWriter = new OutputStreamWriter( stream, encoding );
}
catch( UnsupportedEncodingException e )
{
streamWriter = new OutputStreamWriter( stream );
}
this.m_writer = new BufferedWriter( streamWriter );
}
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"write",
"writeLine",
"flush",
"close",
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
{
switch( method )
{
case 0:
{
// write
checkOpen();
String text;
if( args.length > 0 && args[ 0 ] != null )
{
text = args[ 0 ].toString();
}
else
{
text = "";
}
try
{
m_writer.write( text, 0, text.length() );
return null;
}
catch( IOException e )
{
throw new LuaException( e.getMessage() );
}
}
case 1:
{
// writeLine
checkOpen();
String text;
if( args.length > 0 && args[ 0 ] != null )
{
text = args[ 0 ].toString();
}
else
{
text = "";
}
try
{
m_writer.write( text, 0, text.length() );
m_writer.newLine();
return null;
}
catch( IOException e )
{
throw new LuaException( e.getMessage() );
}
}
case 2:
// flush
checkOpen();
try
{
m_writer.flush();
return null;
}
catch( IOException e )
{
return null;
}
case 3:
// close
close();
return null;
default:
return null;
}
}
}

View File

@ -0,0 +1,35 @@
package dan200.computercraft.core.apis.handles;
import dan200.computercraft.api.lua.ILuaObject;
import dan200.computercraft.api.lua.LuaException;
import java.io.Closeable;
import java.io.IOException;
public abstract class HandleGeneric implements ILuaObject
{
protected final Closeable m_closable;
protected boolean m_open = true;
public HandleGeneric( Closeable m_closable )
{
this.m_closable = m_closable;
}
protected void checkOpen() throws LuaException
{
if( !m_open ) throw new LuaException( "attempt to use a closed file" );
}
protected void close()
{
try
{
m_closable.close();
m_open = false;
}
catch( IOException ignored )
{
}
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -469,7 +469,7 @@ private boolean initFileSystem()
}
catch( FileSystemException e )
{
e.printStackTrace();
ComputerCraft.log.error( "Cannot mount rom", e );
return false;
}
}

View File

@ -1,11 +1,13 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.computer;
import dan200.computercraft.ComputerCraft;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.WeakHashMap;
@ -106,8 +108,7 @@ public void run() {
try {
task.execute();
} catch( Throwable e ) {
System.out.println( "ComputerCraft: Error running task." );
e.printStackTrace();
ComputerCraft.log.error( "Error running task", e );
}
}
} );
@ -139,7 +140,7 @@ public void run() {
// Step 3: abandon
if( worker.isAlive() )
{
//System.out.println( "computercraft: Warning! Failed to abort Computer " + computercraft.getDescription() + ". Dangling lua thread could cause errors." );
// ComputerCraft.log.warn( "Failed to abort Computer " + computer.getID() + ". Dangling lua thread could cause errors." );
worker.interrupt();
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -9,6 +9,7 @@
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount;
import net.minecraftforge.fml.common.FMLLog;
import java.io.*;
import java.util.*;
@ -291,7 +292,7 @@ private String toLocal( String path )
}
private final Map<String, MountWrapper> m_mounts = new HashMap<String, MountWrapper>();
private final Set<IMountedFile> m_openFiles = Collections.newSetFromMap( new WeakHashMap<IMountedFile, Boolean>() );
private final Set<Closeable> m_openFiles = Collections.newSetFromMap( new WeakHashMap<Closeable, Boolean>() );
public FileSystem( String rootLabel, IMount rootMount ) throws FileSystemException
{
@ -308,7 +309,7 @@ public void unload()
// Close all dangling open files
synchronized( m_openFiles )
{
for(IMountedFile file : m_openFiles)
for( Closeable file : m_openFiles )
{
try {
file.close();
@ -647,7 +648,7 @@ private synchronized void copyRecursive( String sourcePath, MountWrapper sourceM
}
}
private synchronized <T extends IMountedFile> T openFile(T file, Closeable handle) throws FileSystemException
private synchronized <T> T openFile( T file, Closeable handle ) throws FileSystemException
{
synchronized( m_openFiles )
{
@ -665,199 +666,44 @@ private synchronized <T extends IMountedFile> T openFile(T file, Closeable handl
throw new FileSystemException("Too many files already open");
}
m_openFiles.add( file );
m_openFiles.add( handle );
return file;
}
}
private synchronized void closeFile( IMountedFile file, Closeable handle ) throws IOException
private synchronized void closeFile( Closeable handle ) throws IOException
{
synchronized( m_openFiles )
{
m_openFiles.remove( file );
if( handle != null )
{
handle.close();
}
m_openFiles.remove( handle );
handle.close();
}
}
public synchronized IMountedFileNormal openForRead( String path ) throws FileSystemException
public synchronized InputStream openForRead( String path ) throws FileSystemException
{
path = sanitizePath ( path );
MountWrapper mount = getMount( path );
InputStream stream = mount.openForRead( path );
if( stream != null )
{
InputStreamReader isr;
try
{
isr = new InputStreamReader( stream, "UTF-8" );
}
catch( UnsupportedEncodingException e )
{
isr = new InputStreamReader( stream );
}
final BufferedReader reader = new BufferedReader( isr );
IMountedFileNormal file = new IMountedFileNormal()
{
@Override
public String readLine() throws IOException
{
return reader.readLine();
}
@Override
public void write(String s, int off, int len, boolean newLine) throws IOException
{
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException
{
closeFile( this, reader );
}
@Override
public void flush() throws IOException
{
throw new UnsupportedOperationException();
}
};
return openFile( file, reader );
return openFile( new ClosingInputStream( stream ), stream );
}
return null;
}
public synchronized IMountedFileNormal openForWrite( String path, boolean append ) throws FileSystemException
public synchronized OutputStream openForWrite( String path, boolean append ) throws FileSystemException
{
path = sanitizePath ( path );
MountWrapper mount = getMount( path );
OutputStream stream = append ? mount.openForAppend( path ) : mount.openForWrite( path );
if( stream != null )
{
OutputStreamWriter osw;
try
{
osw = new OutputStreamWriter( stream, "UTF-8" );
}
catch( UnsupportedEncodingException e )
{
osw = new OutputStreamWriter( stream );
}
final BufferedWriter writer = new BufferedWriter( osw );
IMountedFileNormal file = new IMountedFileNormal()
{
@Override
public String readLine() throws IOException
{
throw new UnsupportedOperationException();
}
@Override
public void write( String s, int off, int len, boolean newLine ) throws IOException
{
writer.write( s, off, len );
if( newLine )
{
writer.newLine();
}
}
@Override
public void close() throws IOException
{
closeFile( this, writer );
}
@Override
public void flush() throws IOException
{
writer.flush();
}
};
return openFile( file, writer );
return openFile( new ClosingOutputStream( stream ), stream );
}
return null;
}
public synchronized IMountedFileBinary openForBinaryRead( String path ) throws FileSystemException
{
path = sanitizePath ( path );
MountWrapper mount = getMount( path );
final InputStream stream = mount.openForRead( path );
if( stream != null )
{
IMountedFileBinary file = new IMountedFileBinary()
{
@Override
public int read() throws IOException
{
return stream.read();
}
@Override
public void write(int i) throws IOException
{
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException
{
closeFile( this, stream );
}
@Override
public void flush() throws IOException
{
throw new UnsupportedOperationException();
}
};
return openFile( file, stream );
}
return null;
}
public synchronized IMountedFileBinary openForBinaryWrite( String path, boolean append ) throws FileSystemException
{
path = sanitizePath ( path );
MountWrapper mount = getMount( path );
final OutputStream stream = append ? mount.openForAppend( path ) : mount.openForWrite( path );
if( stream != null )
{
IMountedFileBinary file = new IMountedFileBinary()
{
@Override
public int read() throws IOException
{
throw new UnsupportedOperationException();
}
@Override
public void write(int i) throws IOException
{
stream.write(i);
}
@Override
public void close() throws IOException
{
closeFile( this, stream );
}
@Override
public void flush() throws IOException
{
stream.flush();
}
};
return openFile( file, stream );
}
return null;
}
public long getFreeSpace( String path ) throws FileSystemException
{
path = sanitizePath( path );
@ -1010,4 +856,34 @@ public static String toLocal( String path, String location )
return local;
}
}
private class ClosingInputStream extends FilterInputStream
{
protected ClosingInputStream( InputStream in )
{
super( in );
}
@Override
public void close() throws IOException
{
super.close();
closeFile( in );
}
}
private class ClosingOutputStream extends FilterOutputStream
{
protected ClosingOutputStream( OutputStream out )
{
super( out );
}
@Override
public void close() throws IOException
{
super.close();
closeFile( out );
}
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,13 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.filesystem;
import java.io.IOException;
public interface IMountedFile {
void close() throws IOException;
}

View File

@ -1,16 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.filesystem;
import java.io.IOException;
public interface IMountedFileBinary extends IMountedFile {
int read() throws IOException;
void write( int i ) throws IOException;
void close() throws IOException;
void flush() throws IOException;
}

View File

@ -1,16 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.filesystem;
import java.io.IOException;
public interface IMountedFileNormal extends IMountedFile {
String readLine() throws IOException;
void write( String s, int off, int len, boolean newLine ) throws IOException;
void close() throws IOException;
void flush() throws IOException;
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -15,6 +15,7 @@
import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.ITask;
import dan200.computercraft.core.computer.MainThread;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
@ -23,6 +24,7 @@
import javax.annotation.Nonnull;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
@ -183,6 +185,7 @@ public void loadBios( InputStream bios )
}
catch( LuaError e )
{
ComputerCraft.log.warn( "Could not load bios.lua ", e );
if( m_mainRoutine != null )
{
((LuaThread)m_mainRoutine).abandon();
@ -327,7 +330,8 @@ private LuaTable wrapLuaObject( ILuaObject object )
{
final int method = i;
final ILuaObject apiObject = object;
table.set( methods[i], new VarArgFunction() {
final String methodName = methods[i];
table.set( methodName, new VarArgFunction() {
@Override
public Varargs invoke( Varargs _args )
{
@ -412,6 +416,10 @@ public void execute()
}
catch( Throwable t )
{
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error running task", t );
}
m_computer.queueEvent( "task_complete", new Object[] {
taskID, false, "Java Exception Thrown: " + t.toString()
} );
@ -478,6 +486,10 @@ public Object[] executeMainThreadTask( @Nonnull final ILuaTask task ) throws Lua
}
catch( Throwable t )
{
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error calling " + methodName + " on " + apiObject, t );
}
throw new LuaError( "Java Exception Thrown: " + t.toString(), 0 );
}
return LuaValue.varargsOf( toValues( results, 0 ) );
@ -509,6 +521,11 @@ else if( object instanceof String )
String s = object.toString();
return LuaValue.valueOf( s );
}
else if( object instanceof byte[] )
{
byte[] b = (byte[]) object;
return LuaValue.valueOf( Arrays.copyOf( b, b.length ) );
}
else if( object instanceof Map )
{
// Table:

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
@ -134,7 +134,7 @@ public final ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult
}
@Override
protected final ItemStack createStackedBlock( @Nonnull IBlockState state )
public final ItemStack createStackedBlock( @Nonnull IBlockState state )
{
return null;
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -0,0 +1,106 @@
package dan200.computercraft.shared.common;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.shared.util.ColourTracker;
import dan200.computercraft.shared.util.ColourUtils;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class ColourableRecipe implements IRecipe
{
@Override
public boolean matches( @Nonnull InventoryCrafting inv, @Nonnull World worldIn )
{
boolean hasColourable = false;
boolean hasDye = false;
for( int i = 0; i < inv.getSizeInventory(); i++ )
{
ItemStack stack = inv.getStackInSlot( i );
if( stack == null ) continue;
if( stack.getItem() instanceof IColouredItem )
{
if( hasColourable ) return false;
hasColourable = true;
}
else if( ColourUtils.getStackColour( stack ) >= 0 )
{
hasDye = true;
}
else
{
return false;
}
}
return hasColourable && hasDye;
}
@Nullable
@Override
public ItemStack getCraftingResult( @Nonnull InventoryCrafting inv )
{
ItemStack colourable = null;
ColourTracker tracker = new ColourTracker();
for( int i = 0; i < inv.getSizeInventory(); ++i )
{
ItemStack stack = inv.getStackInSlot( i );
if( stack == null ) continue;
if( stack.getItem() instanceof IColouredItem )
{
colourable = stack;
}
else
{
int index = ColourUtils.getStackColour( stack );
if( index < 0 ) continue;
Colour colour = Colour.values()[ index ];
tracker.addColour( colour.getR(), colour.getG(), colour.getB() );
}
}
if( colourable == null )
{
return null;
}
return ((IColouredItem) colourable.getItem()).setColour( colourable, tracker.getColour() );
}
@Override
public int getRecipeSize()
{
return 2;
}
@Nullable
@Override
public ItemStack getRecipeOutput()
{
return null;
}
@Nonnull
@Override
public ItemStack[] getRemainingItems( @Nonnull InventoryCrafting inv )
{
ItemStack[] results = new ItemStack[ inv.getSizeInventory() ];
for( int i = 0; i < results.length; ++i )
{
ItemStack stack = inv.getStackInSlot( i );
results[ i ] = ForgeHooks.getContainerItem( stack );
}
return results;
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -0,0 +1,10 @@
package dan200.computercraft.shared.common;
import net.minecraft.item.ItemStack;
public interface IColouredItem
{
int getColour( ItemStack stack );
ItemStack setColour( ItemStack stack, int colour );
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

View File

@ -1,12 +1,13 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.computer.apis;
import com.google.common.collect.ImmutableMap;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaTask;
import dan200.computercraft.api.lua.LuaException;
@ -98,6 +99,10 @@ private Object[] doCommand( String command )
}
catch( Throwable t )
{
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error running command.", t );
}
return new Object[]{ false, createOutput( "Java Exception Thrown: " + t.toString() ) };
}
}
@ -208,6 +213,10 @@ public Object[] execute() throws LuaException
catch( Throwable t )
{
// Ignore buggy command
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error checking permissions of command.", t );
}
}
}
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/

Some files were not shown because too many files have changed in this diff Show More