mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-16 06:27:39 +00:00
Compare commits
30 Commits
v1.80pr1.7
...
v1.80pr1.1
Author | SHA1 | Date | |
---|---|---|---|
![]() |
14c9558ee6 | ||
![]() |
d53a73e7e7 | ||
![]() |
7e334bd4a5 | ||
![]() |
51e787f631 | ||
![]() |
8080699030 | ||
![]() |
e555f9f7f0 | ||
![]() |
822db6e9b5 | ||
![]() |
ac1f30ef43 | ||
![]() |
0fc1b8c46b | ||
![]() |
e7c19bcf55 | ||
![]() |
63ca8aca4c | ||
![]() |
2caa9c57fc | ||
![]() |
33fad2da15 | ||
![]() |
518eefbe10 | ||
![]() |
1ba73454c1 | ||
![]() |
ee4735c17c | ||
![]() |
b008edae90 | ||
![]() |
c6bd88f3ad | ||
![]() |
efa57521c7 | ||
![]() |
4700f8831b | ||
![]() |
9428bee316 | ||
![]() |
89c7183a1d | ||
![]() |
d2a9e7e458 | ||
![]() |
1774f1a079 | ||
![]() |
de1307913b | ||
![]() |
093132533d | ||
![]() |
0685be6bfa | ||
![]() |
f40733e9a6 | ||
![]() |
a3d1cff298 | ||
![]() |
b8957cab5c |
16
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
16
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Report some misbehaviour in the mod
|
||||
|
||||
---
|
||||
|
||||
<!--
|
||||
## Before reporting
|
||||
- Search for the bug both here and [on the ComputerCraft issues page](https://github.com/dan200/ComputerCraft/issues?utf8=%E2%9C%93&q=is%3Aissue+)
|
||||
- If possible, try to reproduce on vanilla ComputerCraft. If it still occurs, [report on the ComputerCraft repo](https://github.com/dan200/ComputerCraft/issues/new) instead.
|
||||
-->
|
||||
|
||||
## Useful information to include:
|
||||
- Minecraft version
|
||||
- CC: Tweaked version
|
||||
- Detailed reproduction steps!** Sometimes I can spot a bug pretty easily, but often it's much more obscure. Anything you can give which will help reproduce it means it'll get fixed quicker.
|
15
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
15
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea or improvement
|
||||
|
||||
---
|
||||
|
||||
<!--
|
||||
## Before reporting
|
||||
- Search for the suggestion both here and [on the ComputerCraft issues page](https://github.com/dan200/ComputerCraft/issues?utf8=%E2%9C%93&q=is%3Aissue+). It's possible someone's suggested it before!
|
||||
- Unless something is specific to CC:Tweaked, try to [suggest them on the ComputerCraft repo](https://github.com/dan200/ComputerCraft/issues/new). There's a lot more people watching it, so it allows the wider community to contribute.
|
||||
-->
|
||||
|
||||
## Useful information to include:
|
||||
- Explanation of how the feature/change chould work.
|
||||
- Some rationale/use case for a feature. I'd like to keep CC:T as minimal
|
9
.github/pull_request_template.md
vendored
Normal file
9
.github/pull_request_template.md
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
<!--
|
||||
Unless this feature is specific to CC:Tweaked, try to [target the original ComputerCraft repo](https://github.com/dan200/ComputerCraft/) instead. There's a lot more people watching it, so it allows the wider community to contribute.
|
||||
-->
|
||||
|
||||
## Useful information to include:
|
||||
- Brief explanation of the changes you've made.
|
||||
- Rationale of why this change has been made/reasoning behind it.
|
||||
|
||||
The more information you can provide, the easier it is to review something now _and_ to see why a change was made, when the code needs updating in the future.
|
@@ -28,6 +28,8 @@ to see the full changes, but here's a couple of the more interesting changes:
|
||||
computers remotely.
|
||||
- Add full-block wired modems, allowing one to wrap non-solid peripherals (such as turtles, or chests if Plethora is
|
||||
installed).
|
||||
- Extended binary file handles. They support file seeking, and reading new lines, allowing full (and accurate)
|
||||
emulation of the standard Lua `io` library.
|
||||
|
||||
## Relation to CCTweaks?
|
||||
This mod has nothing to do with CCTweaks, though there is no denying the name is a throwback to it. That being said,
|
||||
@@ -36,7 +38,7 @@ computers.
|
||||
|
||||
## Contributing
|
||||
Any contribution is welcome, be that using the mod, reporting bugs or contributing code. If you do wish to contribute
|
||||
code, do consider submitting it to the ComputerCraft repository instead.
|
||||
code, do consider submitting it to the ComputerCraft repository first.
|
||||
|
||||
That being said, in order to start helping develop CC:T, you'll need to follow these steps:
|
||||
|
||||
|
15
build.gradle
15
build.gradle
@@ -23,12 +23,12 @@ apply plugin: 'org.ajoberstar.grgit'
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'maven'
|
||||
|
||||
version = "1.80pr1.7"
|
||||
version = "1.80pr1.10"
|
||||
group = "org.squiddev"
|
||||
archivesBaseName = "cc-tweaked"
|
||||
|
||||
minecraft {
|
||||
version = "1.12.2-14.23.2.2634"
|
||||
version = "1.12.2-14.23.4.2749"
|
||||
runDir = "run"
|
||||
replace '${version}', project.version
|
||||
|
||||
@@ -37,7 +37,7 @@ minecraft {
|
||||
// stable_# stables are built at the discretion of the MCP team.
|
||||
// Use non-default mappings at your own risk. they may not allways work.
|
||||
// simply re-run your setup task after changing the mappings to update your workspace.
|
||||
mappings = "snapshot_20180324"
|
||||
mappings = "snapshot_20180724"
|
||||
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ repositories {
|
||||
name = "squiddev"
|
||||
url = "https://dl.bintray.com/squiddev/maven"
|
||||
}
|
||||
|
||||
ivy { artifactPattern "https://asie.pl/files/mods/Charset/LibOnly/[module]-[revision](-[classifier]).[ext]" }
|
||||
}
|
||||
|
||||
configurations {
|
||||
@@ -60,8 +62,11 @@ configurations {
|
||||
|
||||
dependencies {
|
||||
deobfProvided "mezz.jei:jei_1.12.2:4.8.5.159:api"
|
||||
deobfProvided "pl.asie:Charset-Lib:0.5.4.6"
|
||||
|
||||
runtime "mezz.jei:jei_1.12.2:4.8.5.159"
|
||||
shade 'org.squiddev:Cobalt:0.3.1'
|
||||
|
||||
shade 'org.squiddev:Cobalt:0.3.2'
|
||||
|
||||
testCompile 'junit:junit:4.11'
|
||||
|
||||
@@ -126,7 +131,7 @@ curseforge {
|
||||
project {
|
||||
id = '282001'
|
||||
releaseType = 'beta'
|
||||
changelog = ''
|
||||
changelog = "Release notes can be found on the GitHub repository (https://github.com/SquidDev-CC/CC-Tweaked/releases/tag/v${project.version})."
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -26,7 +26,7 @@ import dan200.computercraft.api.turtle.event.TurtleAction;
|
||||
import dan200.computercraft.core.apis.AddressPredicate;
|
||||
import dan200.computercraft.core.filesystem.ComboMount;
|
||||
import dan200.computercraft.core.filesystem.FileMount;
|
||||
import dan200.computercraft.core.filesystem.JarMount;
|
||||
import dan200.computercraft.core.filesystem.FileSystemMount;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
import dan200.computercraft.shared.command.CommandComputer;
|
||||
@@ -97,8 +97,11 @@ import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.ProviderNotFoundException;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
@@ -922,11 +925,12 @@ public class ComputerCraft
|
||||
{
|
||||
try
|
||||
{
|
||||
IMount jarMount = new JarMount( modJar, subPath );
|
||||
mounts.add( jarMount );
|
||||
FileSystem fs = FileSystems.newFileSystem( modJar.toPath(), ComputerCraft.class.getClassLoader() );
|
||||
mounts.add( new FileSystemMount( fs, subPath ) );
|
||||
}
|
||||
catch( IOException e )
|
||||
catch( IOException | RuntimeException | ServiceConfigurationError e )
|
||||
{
|
||||
ComputerCraft.log.error( "Could not load mount from mod jar", e );
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
@@ -936,16 +940,16 @@ public class ComputerCraft
|
||||
if( resourcePackDir.exists() && resourcePackDir.isDirectory() )
|
||||
{
|
||||
String[] resourcePacks = resourcePackDir.list();
|
||||
for( String resourcePack1 : resourcePacks )
|
||||
for( String resourcePackName : resourcePacks )
|
||||
{
|
||||
try
|
||||
{
|
||||
File resourcePack = new File( resourcePackDir, resourcePack1 );
|
||||
File resourcePack = new File( resourcePackDir, resourcePackName );
|
||||
if( !resourcePack.isDirectory() )
|
||||
{
|
||||
// Mount a resource pack from a jar
|
||||
IMount resourcePackMount = new JarMount( resourcePack, subPath );
|
||||
mounts.add( resourcePackMount );
|
||||
FileSystem fs = FileSystems.newFileSystem( resourcePack.toPath(), ComputerCraft.class.getClassLoader() );
|
||||
mounts.add( new FileSystemMount( fs, subPath ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -958,9 +962,9 @@ public class ComputerCraft
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( IOException e )
|
||||
catch( IOException | RuntimeException | ServiceConfigurationError e )
|
||||
{
|
||||
// Ignore
|
||||
ComputerCraft.log.error( "Could not load resource pack '" + resourcePackName + "'", e );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1109,7 +1113,7 @@ public class ComputerCraft
|
||||
{
|
||||
return turtleProxy.getTurtleUpgrade( id );
|
||||
}
|
||||
|
||||
|
||||
public static ITurtleUpgrade getTurtleUpgrade( int legacyID )
|
||||
{
|
||||
return turtleProxy.getTurtleUpgrade( legacyID );
|
||||
@@ -1125,18 +1129,18 @@ public class ComputerCraft
|
||||
turtleProxy.addAllUpgradedTurtles( list );
|
||||
}
|
||||
|
||||
public static void setDropConsumer( Entity entity, Consumer<ItemStack> consumer )
|
||||
public static void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
turtleProxy.setDropConsumer( entity, consumer );
|
||||
}
|
||||
|
||||
public static void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> consumer )
|
||||
public static void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
turtleProxy.setDropConsumer( world, pos, consumer );
|
||||
}
|
||||
|
||||
public static void clearDropConsumer( )
|
||||
public static List<ItemStack> clearDropConsumer( )
|
||||
{
|
||||
turtleProxy.clearDropConsumer();
|
||||
return turtleProxy.clearDropConsumer();
|
||||
}
|
||||
}
|
||||
|
@@ -13,6 +13,8 @@ import net.minecraft.world.World;
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -72,7 +74,25 @@ public interface IMount
|
||||
* @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram".
|
||||
* @return A stream representing the contents of the file.
|
||||
* @throws IOException If the file does not exist, or could not be opened.
|
||||
* @deprecated Use {@link #openChannelForRead(String)} instead
|
||||
*/
|
||||
@Nonnull
|
||||
@Deprecated
|
||||
InputStream openForRead( @Nonnull String path ) throws IOException;
|
||||
|
||||
/**
|
||||
* Opens a file with a given path, and returns an {@link ReadableByteChannel} representing its contents.
|
||||
*
|
||||
* @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram".
|
||||
* @return A channel representing the contents of the file. If the channel implements
|
||||
* {@link java.nio.channels.SeekableByteChannel}, one will be able to seek to arbitrary positions when using binary
|
||||
* mode.
|
||||
* @throws IOException If the file does not exist, or could not be opened.
|
||||
*/
|
||||
@Nonnull
|
||||
@SuppressWarnings("deprecation")
|
||||
default ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newChannel( openForRead( path ) );
|
||||
}
|
||||
}
|
||||
|
@@ -13,6 +13,8 @@ import net.minecraft.world.World;
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
/**
|
||||
* Represents a part of a virtual filesystem that can be mounted onto a computer using {@link IComputerAccess#mount(String, IMount)}
|
||||
@@ -50,20 +52,54 @@ public interface IWritableMount extends IMount
|
||||
* @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram".
|
||||
* @return A stream for writing to
|
||||
* @throws IOException If the file could not be opened for writing.
|
||||
* @deprecated Use {@link #openChannelForWrite(String)} instead.
|
||||
*/
|
||||
@Nonnull
|
||||
@Deprecated
|
||||
OutputStream openForWrite( @Nonnull String path ) throws IOException;
|
||||
|
||||
/**
|
||||
* Opens a file with a given path, and returns an {@link OutputStream} for writing to it.
|
||||
*
|
||||
* @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram".
|
||||
* @return A stream for writing to. If the channel implements {@link java.nio.channels.SeekableByteChannel}, one
|
||||
* will be able to seek to arbitrary positions when using binary mode.
|
||||
* @throws IOException If the file could not be opened for writing.
|
||||
*/
|
||||
@Nonnull
|
||||
@SuppressWarnings("deprecation")
|
||||
default WritableByteChannel openChannelForWrite( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newChannel( openForWrite( path ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a file with a given path, and returns an {@link OutputStream} for appending to it.
|
||||
*
|
||||
* @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram".
|
||||
* @return A stream for writing to.
|
||||
* @throws IOException If the file could not be opened for writing.
|
||||
* @deprecated Use {@link #openChannelForAppend(String)} instead.
|
||||
*/
|
||||
@Nonnull
|
||||
@Deprecated
|
||||
OutputStream openForAppend( @Nonnull String path ) throws IOException;
|
||||
|
||||
/**
|
||||
* Opens a file with a given path, and returns an {@link OutputStream} for appending to it.
|
||||
*
|
||||
* @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram".
|
||||
* @return A stream for writing to. If the channel implements {@link java.nio.channels.SeekableByteChannel}, one
|
||||
* will be able to seek to arbitrary positions when using binary mode.
|
||||
* @throws IOException If the file could not be opened for writing.
|
||||
*/
|
||||
@Nonnull
|
||||
@SuppressWarnings("deprecation")
|
||||
default WritableByteChannel openChannelForAppend( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newChannel( openForAppend( path ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of free space on the mount, in bytes. You should decrease this value as the user writes to the
|
||||
* mount, and write operations should fail once it reaches zero.
|
||||
|
@@ -65,6 +65,10 @@ public class PrintoutRenderer
|
||||
|
||||
public static void drawText( int x, int y, int start, String[] text, String[] colours )
|
||||
{
|
||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||
GlStateManager.enableBlend();
|
||||
GlStateManager.enableTexture2D();
|
||||
|
||||
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer) ComputerCraft.getFixedWidthFontRenderer();
|
||||
|
||||
for( int line = 0; line < LINES_PER_PAGE && line < text.length; ++line )
|
||||
@@ -76,6 +80,9 @@ public class PrintoutRenderer
|
||||
public static void drawBorder( double x, double y, double z, int page, int pages, boolean isBook )
|
||||
{
|
||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||
GlStateManager.enableBlend();
|
||||
GlStateManager.enableTexture2D();
|
||||
|
||||
Minecraft.getMinecraft().getTextureManager().bindTexture( BG );
|
||||
|
||||
Tessellator tessellator = Tessellator.getInstance();
|
||||
|
@@ -71,7 +71,7 @@ public class TileEntityCableRenderer extends TileEntitySpecialRenderer<TileCable
|
||||
buffer.setTranslation( x - pos.getX(), y - pos.getY(), z - pos.getZ() );
|
||||
buffer.noColor();
|
||||
|
||||
ForgeHooksClient.setRenderLayer( block.getBlockLayer() );
|
||||
ForgeHooksClient.setRenderLayer( block.getRenderLayer() );
|
||||
|
||||
// See BlockRendererDispatcher#renderBlockDamage
|
||||
TextureAtlasSprite breakingTexture = mc.getTextureMapBlocks().getAtlasSprite( "minecraft:blocks/destroy_stage_" + destroyStage );
|
||||
|
@@ -9,19 +9,23 @@ package dan200.computercraft.core.apis;
|
||||
import dan200.computercraft.api.lua.ILuaAPI;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
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.apis.handles.BinaryReadableHandle;
|
||||
import dan200.computercraft.core.apis.handles.BinaryWritableHandle;
|
||||
import dan200.computercraft.core.apis.handles.EncodedReadableHandle;
|
||||
import dan200.computercraft.core.apis.handles.EncodedWritableHandle;
|
||||
import dan200.computercraft.core.filesystem.FileSystem;
|
||||
import dan200.computercraft.core.filesystem.FileSystemException;
|
||||
import dan200.computercraft.core.filesystem.FileSystemWrapper;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
|
||||
|
||||
@@ -221,38 +225,38 @@ public class FSAPI implements ILuaAPI
|
||||
case "r":
|
||||
{
|
||||
// Open the file for reading, then create a wrapper around the reader
|
||||
InputStream reader = m_fileSystem.openForRead( path );
|
||||
return new Object[] { new EncodedInputHandle( reader ) };
|
||||
FileSystemWrapper<BufferedReader> reader = m_fileSystem.openForRead( path, EncodedReadableHandle::openUtf8 );
|
||||
return new Object[] { new EncodedReadableHandle( reader.get(), reader ) };
|
||||
}
|
||||
case "w":
|
||||
{
|
||||
// Open the file for writing, then create a wrapper around the writer
|
||||
OutputStream writer = m_fileSystem.openForWrite( path, false );
|
||||
return new Object[] { new EncodedOutputHandle( writer ) };
|
||||
FileSystemWrapper<BufferedWriter> writer = m_fileSystem.openForWrite( path, false, EncodedWritableHandle::openUtf8 );
|
||||
return new Object[] { new EncodedWritableHandle( writer.get(), writer ) };
|
||||
}
|
||||
case "a":
|
||||
{
|
||||
// Open the file for appending, then create a wrapper around the writer
|
||||
OutputStream writer = m_fileSystem.openForWrite( path, true );
|
||||
return new Object[] { new EncodedOutputHandle( writer ) };
|
||||
FileSystemWrapper<BufferedWriter> writer = m_fileSystem.openForWrite( path, true, EncodedWritableHandle::openUtf8 );
|
||||
return new Object[] { new EncodedWritableHandle( writer.get(), writer ) };
|
||||
}
|
||||
case "rb":
|
||||
{
|
||||
// Open the file for binary reading, then create a wrapper around the reader
|
||||
InputStream reader = m_fileSystem.openForRead( path );
|
||||
return new Object[] { new BinaryInputHandle( reader ) };
|
||||
FileSystemWrapper<ReadableByteChannel> reader = m_fileSystem.openForRead( path, Function.identity() );
|
||||
return new Object[] { new BinaryReadableHandle( reader.get(), reader ) };
|
||||
}
|
||||
case "wb":
|
||||
{
|
||||
// Open the file for binary writing, then create a wrapper around the writer
|
||||
OutputStream writer = m_fileSystem.openForWrite( path, false );
|
||||
return new Object[] { new BinaryOutputHandle( writer ) };
|
||||
FileSystemWrapper<WritableByteChannel> writer = m_fileSystem.openForWrite( path, false, Function.identity() );
|
||||
return new Object[] { new BinaryWritableHandle( writer.get(), writer ) };
|
||||
}
|
||||
case "ab":
|
||||
{
|
||||
// Open the file for binary appending, then create a wrapper around the reader
|
||||
OutputStream writer = m_fileSystem.openForWrite( path, true );
|
||||
return new Object[] { new BinaryOutputHandle( writer ) };
|
||||
FileSystemWrapper<WritableByteChannel> writer = m_fileSystem.openForWrite( path, true, Function.identity() );
|
||||
return new Object[] { new BinaryWritableHandle( writer.get(), writer ) };
|
||||
}
|
||||
default:
|
||||
throw new LuaException( "Unsupported mode" );
|
||||
|
@@ -64,7 +64,8 @@ public class TermAPI implements ILuaAPI
|
||||
"setPaletteColour",
|
||||
"setPaletteColor",
|
||||
"getPaletteColour",
|
||||
"getPaletteColor"
|
||||
"getPaletteColor",
|
||||
"getCursorBlink",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -284,6 +285,9 @@ public class TermAPI implements ILuaAPI
|
||||
}
|
||||
return null;
|
||||
}
|
||||
case 23:
|
||||
// getCursorBlink
|
||||
return new Object[] { m_terminal.getCursorBlink() };
|
||||
default:
|
||||
{
|
||||
return null;
|
||||
|
@@ -0,0 +1,91 @@
|
||||
package dan200.computercraft.core.apis.handles;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.NonWritableChannelException;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
|
||||
/**
|
||||
* A seekable, readable byte channel which is backed by a simple byte array.
|
||||
*/
|
||||
public class ArrayByteChannel implements SeekableByteChannel
|
||||
{
|
||||
private boolean closed = false;
|
||||
private int position = 0;
|
||||
|
||||
private final byte[] backing;
|
||||
|
||||
public ArrayByteChannel( byte[] backing )
|
||||
{
|
||||
this.backing = backing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read( ByteBuffer destination ) throws IOException
|
||||
{
|
||||
if( closed ) throw new ClosedChannelException();
|
||||
Preconditions.checkNotNull( destination, "destination" );
|
||||
|
||||
if( position >= backing.length ) return -1;
|
||||
|
||||
int remaining = Math.min( backing.length - position, destination.remaining() );
|
||||
destination.put( backing, position, remaining );
|
||||
position += remaining;
|
||||
return remaining;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int write( ByteBuffer src ) throws IOException
|
||||
{
|
||||
if( closed ) throw new ClosedChannelException();
|
||||
throw new NonWritableChannelException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long position() throws IOException
|
||||
{
|
||||
if( closed ) throw new ClosedChannelException();
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SeekableByteChannel position( long newPosition ) throws IOException
|
||||
{
|
||||
if( closed ) throw new ClosedChannelException();
|
||||
if( newPosition < 0 || newPosition > Integer.MAX_VALUE )
|
||||
{
|
||||
throw new IllegalArgumentException( "Position out of bounds" );
|
||||
}
|
||||
position = (int) newPosition;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long size() throws IOException
|
||||
{
|
||||
if( closed ) throw new ClosedChannelException();
|
||||
return backing.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SeekableByteChannel truncate( long size ) throws IOException
|
||||
{
|
||||
if( closed ) throw new ClosedChannelException();
|
||||
throw new NonWritableChannelException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen()
|
||||
{
|
||||
return !closed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
closed = true;
|
||||
}
|
||||
}
|
@@ -1,122 +0,0 @@
|
||||
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.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.getInt;
|
||||
|
||||
public class BinaryInputHandle extends HandleGeneric
|
||||
{
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
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 )
|
||||
{
|
||||
int count = getInt( args, 0 );
|
||||
if( count < 0 )
|
||||
{
|
||||
// Whilst this may seem absurd to allow reading 0 bytes, PUC Lua it so
|
||||
// it seems best to remain somewhat consistent.
|
||||
throw new LuaException( "Cannot read a negative number of bytes" );
|
||||
}
|
||||
else if( count <= BUFFER_SIZE )
|
||||
{
|
||||
// If we've got a small count, then allocate that and read it.
|
||||
byte[] bytes = new byte[ count ];
|
||||
int read = m_stream.read( bytes );
|
||||
|
||||
if( read < 0 ) return null;
|
||||
if( read < count ) bytes = Arrays.copyOf( bytes, read );
|
||||
return new Object[] { bytes };
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] buffer = new byte[ BUFFER_SIZE ];
|
||||
|
||||
// Read the initial set of bytes, failing if none are read.
|
||||
int read = m_stream.read( buffer, 0, Math.min( buffer.length, count ) );
|
||||
if( read == -1 ) return null;
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream( read );
|
||||
count -= read;
|
||||
out.write( buffer, 0, read );
|
||||
|
||||
// Otherwise read until we either reach the limit or we no longer consume
|
||||
// the full buffer.
|
||||
while( read >= buffer.length && count > 0 )
|
||||
{
|
||||
read = m_stream.read( buffer, 0, Math.min( BUFFER_SIZE, count ) );
|
||||
if( read == -1 ) break;
|
||||
count -= read;
|
||||
out.write( buffer, 0, read );
|
||||
}
|
||||
|
||||
return new Object[] { out.toByteArray() };
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,205 @@
|
||||
package dan200.computercraft.core.apis.handles;
|
||||
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.getInt;
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
|
||||
|
||||
public class BinaryReadableHandle extends HandleGeneric
|
||||
{
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
private static final String[] METHOD_NAMES = new String[] { "read", "readAll", "readLine", "close" };
|
||||
private static final String[] METHOD_SEEK_NAMES = ObjectArrays.concat( METHOD_NAMES, new String[] { "seek" }, String.class );
|
||||
|
||||
private final ReadableByteChannel m_reader;
|
||||
private final SeekableByteChannel m_seekable;
|
||||
private final ByteBuffer single = ByteBuffer.allocate( 1 );
|
||||
|
||||
public BinaryReadableHandle( ReadableByteChannel channel, Closeable closeable )
|
||||
{
|
||||
super( closeable );
|
||||
this.m_reader = channel;
|
||||
this.m_seekable = asSeekable( channel );
|
||||
}
|
||||
|
||||
public BinaryReadableHandle( ReadableByteChannel channel )
|
||||
{
|
||||
this( channel, channel );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
return m_seekable == null ? METHOD_NAMES : METHOD_SEEK_NAMES;
|
||||
}
|
||||
|
||||
@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 )
|
||||
{
|
||||
int count = getInt( args, 0 );
|
||||
if( count < 0 )
|
||||
{
|
||||
throw new LuaException( "Cannot read a negative number of bytes" );
|
||||
}
|
||||
else if( count == 0 && m_seekable != null )
|
||||
{
|
||||
return m_seekable.position() >= m_seekable.size() ? null : new Object[] { "" };
|
||||
}
|
||||
|
||||
if( count <= BUFFER_SIZE )
|
||||
{
|
||||
ByteBuffer buffer = ByteBuffer.allocate( count );
|
||||
|
||||
int read = m_reader.read( buffer );
|
||||
if( read < 0 ) return null;
|
||||
return new Object[] { read < count ? Arrays.copyOf( buffer.array(), read ) : buffer.array() };
|
||||
}
|
||||
else
|
||||
{
|
||||
ByteBuffer buffer = ByteBuffer.allocate( BUFFER_SIZE );
|
||||
|
||||
int read = m_reader.read( buffer );
|
||||
if( read < 0 ) return null;
|
||||
int totalRead = read;
|
||||
|
||||
// If we failed to read "enough" here, let's just abort
|
||||
if( totalRead >= count || read < BUFFER_SIZE )
|
||||
{
|
||||
return new Object[] { Arrays.copyOf( buffer.array(), read ) };
|
||||
}
|
||||
|
||||
// Build up an array of ByteBuffers. Hopefully this means we can perform less allocation
|
||||
// than doubling up the buffer each time.
|
||||
List<ByteBuffer> parts = new ArrayList<>( 4 );
|
||||
parts.add( buffer );
|
||||
while( totalRead < count && read >= BUFFER_SIZE )
|
||||
{
|
||||
buffer = ByteBuffer.allocate( BUFFER_SIZE );
|
||||
totalRead += read = m_reader.read( buffer );
|
||||
parts.add( buffer );
|
||||
}
|
||||
|
||||
// Now just copy all the bytes across!
|
||||
byte[] bytes = new byte[ totalRead ];
|
||||
int pos = 0;
|
||||
for( ByteBuffer part : parts )
|
||||
{
|
||||
System.arraycopy( part.array(), 0, bytes, pos, part.position() );
|
||||
pos += part.position();
|
||||
}
|
||||
return new Object[] { bytes };
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
single.clear();
|
||||
int b = m_reader.read( single );
|
||||
return b == -1 ? null : new Object[] { single.get( 0 ) & 0xFF };
|
||||
}
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 1:
|
||||
// readAll
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
int expected = 32;
|
||||
if( m_seekable != null )
|
||||
{
|
||||
expected = Math.max( expected, (int) (m_seekable.size() - m_seekable.position()) );
|
||||
}
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream( expected );
|
||||
|
||||
ByteBuffer buf = ByteBuffer.allocate( 8192 );
|
||||
boolean readAnything = false;
|
||||
while( true )
|
||||
{
|
||||
buf.clear();
|
||||
int r = m_reader.read( buf );
|
||||
if( r == -1 ) break;
|
||||
|
||||
readAnything = true;
|
||||
stream.write( buf.array(), 0, r );
|
||||
}
|
||||
return readAnything ? new Object[] { stream.toByteArray() } : null;
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
// readLine
|
||||
checkOpen();
|
||||
boolean withTrailing = optBoolean( args, 0, false );
|
||||
try
|
||||
{
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
|
||||
boolean readAnything = false;
|
||||
while( true )
|
||||
{
|
||||
single.clear();
|
||||
int r = m_reader.read( single );
|
||||
if( r == -1 ) break;
|
||||
|
||||
readAnything = true;
|
||||
byte b = single.get( 0 );
|
||||
if( b == '\n' )
|
||||
{
|
||||
if( withTrailing ) stream.write( b );
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.write( b );
|
||||
}
|
||||
}
|
||||
|
||||
return readAnything ? new Object[] { stream.toByteArray() } : null;
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
case 3:
|
||||
//close
|
||||
close();
|
||||
return null;
|
||||
case 4:
|
||||
// seek
|
||||
checkOpen();
|
||||
return handleSeek( m_seekable, args );
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,33 +1,45 @@
|
||||
package dan200.computercraft.core.apis.handles;
|
||||
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.core.apis.ArgumentHelper;
|
||||
import dan200.computercraft.shared.util.StringUtil;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
public class BinaryOutputHandle extends HandleGeneric
|
||||
public class BinaryWritableHandle extends HandleGeneric
|
||||
{
|
||||
private final OutputStream m_writer;
|
||||
private static final String[] METHOD_NAMES = new String[] { "write", "flush", "close" };
|
||||
private static final String[] METHOD_SEEK_NAMES = ObjectArrays.concat( METHOD_NAMES, new String[] { "seek" }, String.class );
|
||||
|
||||
public BinaryOutputHandle( OutputStream writer )
|
||||
private final WritableByteChannel m_writer;
|
||||
private final SeekableByteChannel m_seekable;
|
||||
private final ByteBuffer single = ByteBuffer.allocate( 1 );
|
||||
|
||||
public BinaryWritableHandle( WritableByteChannel channel, Closeable closeable )
|
||||
{
|
||||
super( writer );
|
||||
this.m_writer = writer;
|
||||
super( closeable );
|
||||
this.m_writer = channel;
|
||||
this.m_seekable = asSeekable( channel );
|
||||
}
|
||||
|
||||
public BinaryWritableHandle( WritableByteChannel channel )
|
||||
{
|
||||
this( channel, channel );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
return new String[] {
|
||||
"write",
|
||||
"flush",
|
||||
"close",
|
||||
};
|
||||
return m_seekable == null ? METHOD_NAMES : METHOD_SEEK_NAMES;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,12 +55,16 @@ public class BinaryOutputHandle extends HandleGeneric
|
||||
if( args.length > 0 && args[ 0 ] instanceof Number )
|
||||
{
|
||||
int number = ((Number) args[ 0 ]).intValue();
|
||||
m_writer.write( number );
|
||||
single.clear();
|
||||
single.put( (byte) number );
|
||||
single.flip();
|
||||
|
||||
m_writer.write( single );
|
||||
}
|
||||
else if( args.length > 0 && args[ 0 ] instanceof String )
|
||||
{
|
||||
String value = (String) args[ 0 ];
|
||||
m_writer.write( StringUtil.encodeString( value ) );
|
||||
m_writer.write( ByteBuffer.wrap( StringUtil.encodeString( value ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -65,7 +81,9 @@ public class BinaryOutputHandle extends HandleGeneric
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
m_writer.flush();
|
||||
// Technically this is not needed
|
||||
if( m_writer instanceof FileChannel ) ((FileChannel) m_writer).force( false );
|
||||
|
||||
return null;
|
||||
}
|
||||
catch( IOException e )
|
||||
@@ -76,6 +94,10 @@ public class BinaryOutputHandle extends HandleGeneric
|
||||
//close
|
||||
close();
|
||||
return null;
|
||||
case 3:
|
||||
// seek
|
||||
checkOpen();
|
||||
return handleSeek( m_seekable, args );
|
||||
default:
|
||||
return null;
|
||||
}
|
@@ -4,45 +4,32 @@ import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optInt;
|
||||
|
||||
public class EncodedInputHandle extends HandleGeneric
|
||||
public class EncodedReadableHandle extends HandleGeneric
|
||||
{
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
private final BufferedReader m_reader;
|
||||
private BufferedReader m_reader;
|
||||
|
||||
public EncodedInputHandle( BufferedReader reader )
|
||||
public EncodedReadableHandle( @Nonnull BufferedReader reader, @Nonnull Closeable closable )
|
||||
{
|
||||
super( reader );
|
||||
super( closable );
|
||||
this.m_reader = reader;
|
||||
}
|
||||
|
||||
public EncodedInputHandle( InputStream stream )
|
||||
public EncodedReadableHandle( @Nonnull BufferedReader reader )
|
||||
{
|
||||
this( stream, "UTF-8" );
|
||||
}
|
||||
|
||||
public EncodedInputHandle( InputStream stream, String encoding )
|
||||
{
|
||||
this( makeReader( stream, encoding ) );
|
||||
}
|
||||
|
||||
private static BufferedReader makeReader( InputStream stream, String encoding )
|
||||
{
|
||||
if( encoding == null ) encoding = "UTF-8";
|
||||
InputStreamReader streamReader;
|
||||
try
|
||||
{
|
||||
streamReader = new InputStreamReader( stream, encoding );
|
||||
}
|
||||
catch( UnsupportedEncodingException e )
|
||||
{
|
||||
streamReader = new InputStreamReader( stream );
|
||||
}
|
||||
return new BufferedReader( streamReader );
|
||||
this( reader, reader );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -63,13 +50,17 @@ public class EncodedInputHandle extends HandleGeneric
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
// readLine
|
||||
checkOpen();
|
||||
boolean withTrailing = optBoolean( args, 0, false );
|
||||
try
|
||||
{
|
||||
String line = m_reader.readLine();
|
||||
if( line != null )
|
||||
{
|
||||
// While this is technically inaccurate, it's better than nothing
|
||||
if( withTrailing ) line += "\n";
|
||||
return new Object[] { line };
|
||||
}
|
||||
else
|
||||
@@ -81,6 +72,7 @@ public class EncodedInputHandle extends HandleGeneric
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
// readAll
|
||||
checkOpen();
|
||||
@@ -108,7 +100,6 @@ public class EncodedInputHandle extends HandleGeneric
|
||||
close();
|
||||
return null;
|
||||
case 3:
|
||||
// read
|
||||
checkOpen();
|
||||
try
|
||||
{
|
||||
@@ -161,4 +152,14 @@ public class EncodedInputHandle extends HandleGeneric
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static BufferedReader openUtf8( ReadableByteChannel channel )
|
||||
{
|
||||
return open( channel, StandardCharsets.UTF_8 );
|
||||
}
|
||||
|
||||
public static BufferedReader open( ReadableByteChannel channel, Charset charset )
|
||||
{
|
||||
return new BufferedReader( Channels.newReader( channel, charset.newDecoder(), -1 ) );
|
||||
}
|
||||
}
|
@@ -4,41 +4,27 @@ import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.*;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class EncodedOutputHandle extends HandleGeneric
|
||||
public class EncodedWritableHandle extends HandleGeneric
|
||||
{
|
||||
private final BufferedWriter m_writer;
|
||||
private BufferedWriter m_writer;
|
||||
|
||||
public EncodedOutputHandle( BufferedWriter writer )
|
||||
public EncodedWritableHandle( @Nonnull BufferedWriter writer, @Nonnull Closeable closable )
|
||||
{
|
||||
super( writer );
|
||||
super( closable );
|
||||
this.m_writer = writer;
|
||||
}
|
||||
|
||||
public EncodedOutputHandle( OutputStream stream )
|
||||
public EncodedWritableHandle( @Nonnull BufferedWriter writer )
|
||||
{
|
||||
this( stream, "UTF-8" );
|
||||
}
|
||||
|
||||
public EncodedOutputHandle( OutputStream stream, String encoding )
|
||||
{
|
||||
this( makeWriter( stream, encoding ) );
|
||||
}
|
||||
|
||||
private static BufferedWriter makeWriter( OutputStream stream, String encoding )
|
||||
{
|
||||
if( encoding == null ) encoding = "UTF-8";
|
||||
OutputStreamWriter streamWriter;
|
||||
try
|
||||
{
|
||||
streamWriter = new OutputStreamWriter( stream, encoding );
|
||||
}
|
||||
catch( UnsupportedEncodingException e )
|
||||
{
|
||||
streamWriter = new OutputStreamWriter( stream );
|
||||
}
|
||||
return new BufferedWriter( streamWriter );
|
||||
this( writer, writer );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -125,4 +111,15 @@ public class EncodedOutputHandle extends HandleGeneric
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static BufferedWriter openUtf8( WritableByteChannel channel )
|
||||
{
|
||||
return open( channel, StandardCharsets.UTF_8 );
|
||||
}
|
||||
|
||||
public static BufferedWriter open( WritableByteChannel channel, Charset charset )
|
||||
{
|
||||
return new BufferedWriter( Channels.newWriter( channel, charset.newEncoder(), -1 ) );
|
||||
}
|
||||
}
|
@@ -3,17 +3,23 @@ package dan200.computercraft.core.apis.handles;
|
||||
import dan200.computercraft.api.lua.ILuaObject;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optInt;
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optString;
|
||||
|
||||
public abstract class HandleGeneric implements ILuaObject
|
||||
{
|
||||
protected final Closeable m_closable;
|
||||
protected boolean m_open = true;
|
||||
private Closeable m_closable;
|
||||
private boolean m_open = true;
|
||||
|
||||
public HandleGeneric( Closeable m_closable )
|
||||
protected HandleGeneric( @Nonnull Closeable closable )
|
||||
{
|
||||
this.m_closable = m_closable;
|
||||
this.m_closable = closable;
|
||||
}
|
||||
|
||||
protected void checkOpen() throws LuaException
|
||||
@@ -21,7 +27,7 @@ public abstract class HandleGeneric implements ILuaObject
|
||||
if( !m_open ) throw new LuaException( "attempt to use a closed file" );
|
||||
}
|
||||
|
||||
protected void close()
|
||||
protected final void close()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -31,5 +37,64 @@ public abstract class HandleGeneric implements ILuaObject
|
||||
catch( IOException ignored )
|
||||
{
|
||||
}
|
||||
m_closable = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared implementation for various file handle types
|
||||
*
|
||||
* @param channel The channel to seek in
|
||||
* @param args The Lua arguments to process, like Lua's {@code file:seek}.
|
||||
* @return The new position of the file, or null if some error occured.
|
||||
* @throws LuaException If the arguments were invalid
|
||||
* @see <a href="https://www.lua.org/manual/5.1/manual.html#pdf-file:seek">{@code file:seek} in the Lua manual.</a>
|
||||
*/
|
||||
protected static Object[] handleSeek( SeekableByteChannel channel, Object[] args ) throws LuaException
|
||||
{
|
||||
try
|
||||
{
|
||||
String whence = optString( args, 0, "cur" );
|
||||
long offset = optInt( args, 1, 0 );
|
||||
switch( whence )
|
||||
{
|
||||
case "set":
|
||||
channel.position( offset );
|
||||
break;
|
||||
case "cur":
|
||||
channel.position( channel.position() + offset );
|
||||
break;
|
||||
case "end":
|
||||
channel.position( channel.size() + offset );
|
||||
break;
|
||||
default:
|
||||
throw new LuaException( "bad argument #1 to 'seek' (invalid option '" + whence + "'" );
|
||||
}
|
||||
|
||||
return new Object[]{ channel.position() };
|
||||
}
|
||||
catch( IllegalArgumentException e )
|
||||
{
|
||||
return new Object[]{ false, "Position is negative" };
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected static SeekableByteChannel asSeekable( Channel channel )
|
||||
{
|
||||
if( !(channel instanceof SeekableByteChannel) ) return null;
|
||||
|
||||
SeekableByteChannel seekable = (SeekableByteChannel) channel;
|
||||
try
|
||||
{
|
||||
seekable.position( seekable.position() );
|
||||
return seekable;
|
||||
}
|
||||
catch( IOException | UnsupportedOperationException e )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -13,13 +13,17 @@ import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.ILuaObject;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.core.apis.IAPIEnvironment;
|
||||
import dan200.computercraft.core.apis.handles.BinaryInputHandle;
|
||||
import dan200.computercraft.core.apis.handles.EncodedInputHandle;
|
||||
import dan200.computercraft.core.apis.handles.ArrayByteChannel;
|
||||
import dan200.computercraft.core.apis.handles.BinaryReadableHandle;
|
||||
import dan200.computercraft.core.apis.handles.EncodedReadableHandle;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -182,6 +186,10 @@ public class HTTPRequest implements Runnable
|
||||
byte[] result = ByteStreams.toByteArray( is );
|
||||
is.close();
|
||||
|
||||
String encoding = connection.getContentEncoding();
|
||||
Charset charset = encoding != null && Charset.isSupported( encoding )
|
||||
? Charset.forName( encoding ) : StandardCharsets.UTF_8;
|
||||
|
||||
// We've got some sort of response, so let's build a resulting object.
|
||||
Joiner joiner = Joiner.on( ',' );
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
@@ -193,9 +201,11 @@ public class HTTPRequest implements Runnable
|
||||
m_environment.addTrackingChange( TrackingField.HTTP_DOWNLOAD,
|
||||
getHeaderSize( connection.getHeaderFields() ) + result.length );
|
||||
|
||||
InputStream contents = new ByteArrayInputStream( result );
|
||||
SeekableByteChannel contents = new ArrayByteChannel( result );
|
||||
ILuaObject stream = wrapStream(
|
||||
m_binary ? new BinaryInputHandle( contents ) : new EncodedInputHandle( contents, connection.getContentEncoding() ),
|
||||
m_binary
|
||||
? new BinaryReadableHandle( contents )
|
||||
: new EncodedReadableHandle( EncodedReadableHandle.open( contents, charset ) ),
|
||||
connection.getResponseCode(), headers
|
||||
);
|
||||
|
||||
|
@@ -13,7 +13,9 @@ import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.core.apis.HTTPAPI;
|
||||
import dan200.computercraft.core.apis.IAPIEnvironment;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
import dan200.computercraft.shared.util.StringUtil;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
@@ -26,6 +28,8 @@ import javax.annotation.Nullable;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean;
|
||||
|
||||
public class WebsocketConnection extends SimpleChannelInboundHandler<Object> implements ILuaObject, Closeable
|
||||
{
|
||||
public static final String SUCCESS_EVENT = "websocket_success";
|
||||
@@ -174,8 +178,11 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
{
|
||||
checkOpen();
|
||||
String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";
|
||||
boolean binary = optBoolean(arguments, 1, false);
|
||||
computer.addTrackingChange( TrackingField.WEBSOCKET_OUTGOING, text.length() );
|
||||
channel.writeAndFlush( new TextWebSocketFrame( text ) );
|
||||
channel.writeAndFlush( binary
|
||||
? new BinaryWebSocketFrame( Unpooled.wrappedBuffer( StringUtil.encodeString( text ) ) )
|
||||
: new TextWebSocketFrame( text ) );
|
||||
return null;
|
||||
}
|
||||
case 2:
|
||||
|
@@ -11,6 +11,7 @@ import dan200.computercraft.api.filesystem.IMount;
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -114,6 +115,7 @@ public class ComboMount implements IMount
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public InputStream openForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
for( int i=m_parts.length-1; i>=0; --i )
|
||||
@@ -126,4 +128,19 @@ public class ComboMount implements IMount
|
||||
}
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
for( int i=m_parts.length-1; i>=0; --i )
|
||||
{
|
||||
IMount part = m_parts[i];
|
||||
if( part.exists( path ) && !part.isDirectory( path ) )
|
||||
{
|
||||
return part.openChannelForRead( path );
|
||||
}
|
||||
}
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
}
|
||||
|
@@ -9,34 +9,36 @@ package dan200.computercraft.core.filesystem;
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.List;
|
||||
|
||||
public class EmptyMount implements IMount
|
||||
{
|
||||
{
|
||||
public EmptyMount()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// IMount implementation
|
||||
|
||||
|
||||
@Override
|
||||
public boolean exists( @Nonnull String path )
|
||||
{
|
||||
return path.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isDirectory( @Nonnull String path )
|
||||
{
|
||||
return path.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void list( @Nonnull String path, @Nonnull List<String> contents )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getSize( @Nonnull String path )
|
||||
{
|
||||
@@ -45,8 +47,17 @@ public class EmptyMount implements IMount
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InputStream openForRead( @Nonnull String path )
|
||||
@Deprecated
|
||||
public InputStream openForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return null;
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
}
|
||||
|
@@ -10,64 +10,43 @@ import dan200.computercraft.api.filesystem.IWritableMount;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
|
||||
public class FileMount implements IWritableMount
|
||||
{
|
||||
private static int MINIMUM_FILE_SIZE = 500;
|
||||
|
||||
private class CountingOutputStream extends OutputStream
|
||||
private static final int MINIMUM_FILE_SIZE = 500;
|
||||
|
||||
private class WritableCountingChannel implements WritableByteChannel
|
||||
{
|
||||
private OutputStream m_innerStream;
|
||||
private long m_ignoredBytesLeft;
|
||||
|
||||
public CountingOutputStream( OutputStream innerStream, long bytesToIgnore )
|
||||
|
||||
private final WritableByteChannel m_inner;
|
||||
long m_ignoredBytesLeft;
|
||||
|
||||
WritableCountingChannel( WritableByteChannel inner, long bytesToIgnore )
|
||||
{
|
||||
m_innerStream = innerStream;
|
||||
m_inner = inner;
|
||||
m_ignoredBytesLeft = bytesToIgnore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
m_innerStream.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException
|
||||
{
|
||||
m_innerStream.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write( @Nonnull byte[] b ) throws IOException
|
||||
{
|
||||
count( b.length );
|
||||
m_innerStream.write( b );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write( @Nonnull byte[] b, int off, int len ) throws IOException
|
||||
{
|
||||
count( len );
|
||||
m_innerStream.write( b, off, len );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write( int b ) throws IOException
|
||||
public int write( @Nonnull ByteBuffer b ) throws IOException
|
||||
{
|
||||
count( 1 );
|
||||
m_innerStream.write( b );
|
||||
count( b.remaining() );
|
||||
return m_inner.write( b );
|
||||
}
|
||||
|
||||
private void count( long n ) throws IOException
|
||||
void count( long n ) throws IOException
|
||||
{
|
||||
m_ignoredBytesLeft -= n;
|
||||
if( m_ignoredBytesLeft < 0 )
|
||||
{
|
||||
long newBytes = -m_ignoredBytesLeft;
|
||||
m_ignoredBytesLeft = 0;
|
||||
|
||||
|
||||
long bytesLeft = m_capacity - m_usedSpace;
|
||||
if( newBytes > bytesLeft )
|
||||
{
|
||||
@@ -79,12 +58,79 @@ public class FileMount implements IWritableMount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen()
|
||||
{
|
||||
return m_inner.isOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
m_inner.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class SeekableCountingChannel extends WritableCountingChannel implements SeekableByteChannel
|
||||
{
|
||||
private final SeekableByteChannel m_inner;
|
||||
|
||||
SeekableCountingChannel( SeekableByteChannel inner, long bytesToIgnore )
|
||||
{
|
||||
super( inner, bytesToIgnore );
|
||||
this.m_inner = inner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SeekableByteChannel position( long newPosition ) throws IOException
|
||||
{
|
||||
if( !isOpen() ) throw new ClosedChannelException();
|
||||
if( newPosition < 0 ) throw new IllegalArgumentException();
|
||||
|
||||
long delta = newPosition - m_inner.position();
|
||||
if( delta < 0 )
|
||||
{
|
||||
m_ignoredBytesLeft -= delta;
|
||||
}
|
||||
else
|
||||
{
|
||||
count( delta );
|
||||
}
|
||||
|
||||
return m_inner.position( newPosition );
|
||||
}
|
||||
|
||||
@Override
|
||||
public SeekableByteChannel truncate( long size ) throws IOException
|
||||
{
|
||||
throw new IOException( "Not yet implemented" );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read( ByteBuffer dst ) throws IOException
|
||||
{
|
||||
if( !m_inner.isOpen() ) throw new ClosedChannelException();
|
||||
throw new NonReadableChannelException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long position() throws IOException
|
||||
{
|
||||
return m_inner.position();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long size() throws IOException
|
||||
{
|
||||
return m_inner.size();
|
||||
}
|
||||
}
|
||||
|
||||
private File m_rootPath;
|
||||
private long m_capacity;
|
||||
private long m_usedSpace;
|
||||
|
||||
|
||||
public FileMount( File rootPath, long capacity )
|
||||
{
|
||||
m_rootPath = rootPath;
|
||||
@@ -93,7 +139,7 @@ public class FileMount implements IWritableMount
|
||||
}
|
||||
|
||||
// IMount implementation
|
||||
|
||||
|
||||
@Override
|
||||
public boolean exists( @Nonnull String path )
|
||||
{
|
||||
@@ -107,7 +153,7 @@ public class FileMount implements IWritableMount
|
||||
return file.exists();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isDirectory( @Nonnull String path )
|
||||
{
|
||||
@@ -121,7 +167,7 @@ public class FileMount implements IWritableMount
|
||||
return file.exists() && file.isDirectory();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void list( @Nonnull String path, @Nonnull List<String> contents ) throws IOException
|
||||
{
|
||||
@@ -150,7 +196,7 @@ public class FileMount implements IWritableMount
|
||||
{
|
||||
throw new IOException( "/" + path + ": Not a directory" );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -180,9 +226,10 @@ public class FileMount implements IWritableMount
|
||||
}
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public InputStream openForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
if( created() )
|
||||
@@ -193,11 +240,26 @@ public class FileMount implements IWritableMount
|
||||
return new FileInputStream( file );
|
||||
}
|
||||
}
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
if( created() )
|
||||
{
|
||||
File file = getRealPath( path );
|
||||
if( file.exists() && !file.isDirectory() )
|
||||
{
|
||||
return FileChannel.open( file.toPath(), StandardOpenOption.READ );
|
||||
}
|
||||
}
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
|
||||
// IWritableMount implementation
|
||||
|
||||
|
||||
@Override
|
||||
public void makeDirectory( @Nonnull String path ) throws IOException
|
||||
{
|
||||
@@ -224,7 +286,7 @@ public class FileMount implements IWritableMount
|
||||
{
|
||||
throw new IOException( "/" + path + ": Out of space" );
|
||||
}
|
||||
|
||||
|
||||
boolean success = file.mkdirs();
|
||||
if( success )
|
||||
{
|
||||
@@ -236,7 +298,7 @@ public class FileMount implements IWritableMount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void delete( @Nonnull String path ) throws IOException
|
||||
{
|
||||
@@ -244,7 +306,7 @@ public class FileMount implements IWritableMount
|
||||
{
|
||||
throw new IOException( "/" + path + ": Access denied" );
|
||||
}
|
||||
|
||||
|
||||
if( created() )
|
||||
{
|
||||
File file = getRealPath( path );
|
||||
@@ -254,7 +316,7 @@ public class FileMount implements IWritableMount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void deleteRecursively( File file ) throws IOException
|
||||
{
|
||||
// Empty directories first
|
||||
@@ -266,7 +328,7 @@ public class FileMount implements IWritableMount
|
||||
deleteRecursively( new File( file, aChildren ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Then delete
|
||||
long fileSize = file.isDirectory() ? 0 : file.length();
|
||||
boolean success = file.delete();
|
||||
@@ -279,10 +341,26 @@ public class FileMount implements IWritableMount
|
||||
throw new IOException( "Access denied" );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public OutputStream openForWrite( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newOutputStream( openChannelForWrite( path ) );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public OutputStream openForAppend( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newOutputStream( openChannelForAppend( path ) );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public WritableByteChannel openChannelForWrite( @Nonnull String path ) throws IOException
|
||||
{
|
||||
create();
|
||||
File file = getRealPath( path );
|
||||
@@ -308,13 +386,14 @@ public class FileMount implements IWritableMount
|
||||
m_usedSpace -= Math.max( file.length(), MINIMUM_FILE_SIZE );
|
||||
m_usedSpace += MINIMUM_FILE_SIZE;
|
||||
}
|
||||
return new CountingOutputStream( new FileOutputStream( file, false ), MINIMUM_FILE_SIZE );
|
||||
return new SeekableCountingChannel( Files.newByteChannel( file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE ),
|
||||
MINIMUM_FILE_SIZE );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public OutputStream openForAppend( @Nonnull String path ) throws IOException
|
||||
public WritableByteChannel openChannelForAppend( @Nonnull String path ) throws IOException
|
||||
{
|
||||
if( created() )
|
||||
{
|
||||
@@ -329,7 +408,9 @@ public class FileMount implements IWritableMount
|
||||
}
|
||||
else
|
||||
{
|
||||
return new CountingOutputStream( new FileOutputStream( file, true ), Math.max( MINIMUM_FILE_SIZE - file.length(), 0 ) );
|
||||
// Allowing seeking when appending is not recommended, so we use a separate channel.
|
||||
return new WritableCountingChannel( Files.newByteChannel( file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND ),
|
||||
Math.max( MINIMUM_FILE_SIZE - file.length(), 0 ) );
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -337,23 +418,23 @@ public class FileMount implements IWritableMount
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getRemainingSpace()
|
||||
{
|
||||
return Math.max( m_capacity - m_usedSpace, 0 );
|
||||
}
|
||||
|
||||
|
||||
public File getRealPath( String path )
|
||||
{
|
||||
return new File( m_rootPath, path );
|
||||
}
|
||||
|
||||
|
||||
private boolean created()
|
||||
{
|
||||
return m_rootPath.exists();
|
||||
}
|
||||
|
||||
|
||||
private void create() throws IOException
|
||||
{
|
||||
if( !m_rootPath.exists() )
|
||||
@@ -365,7 +446,7 @@ public class FileMount implements IWritableMount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private long measureUsedSpace( File file )
|
||||
{
|
||||
if( !file.exists() )
|
||||
|
@@ -6,13 +6,23 @@
|
||||
|
||||
package dan200.computercraft.core.filesystem;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.filesystem.IFileSystem;
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||
|
||||
import java.io.*;
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class FileSystem
|
||||
@@ -146,14 +156,14 @@ public class FileSystem
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream openForRead( String path ) throws FileSystemException
|
||||
public ReadableByteChannel openForRead( String path ) throws FileSystemException
|
||||
{
|
||||
path = toLocal( path );
|
||||
try
|
||||
{
|
||||
if( m_mount.exists( path ) && !m_mount.isDirectory( path ) )
|
||||
{
|
||||
return m_mount.openForRead( path );
|
||||
return m_mount.openChannelForRead( path );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -209,13 +219,17 @@ public class FileSystem
|
||||
m_writableMount.delete( path );
|
||||
}
|
||||
}
|
||||
catch( AccessDeniedException e )
|
||||
{
|
||||
throw new FileSystemException( "Access denied" );
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
throw new FileSystemException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
public OutputStream openForWrite( String path ) throws FileSystemException
|
||||
public WritableByteChannel openForWrite( String path ) throws FileSystemException
|
||||
{
|
||||
if( m_writableMount == null )
|
||||
{
|
||||
@@ -238,16 +252,20 @@ public class FileSystem
|
||||
m_writableMount.makeDirectory( dir );
|
||||
}
|
||||
}
|
||||
return m_writableMount.openForWrite( path );
|
||||
return m_writableMount.openChannelForWrite( path );
|
||||
}
|
||||
}
|
||||
catch( AccessDeniedException e )
|
||||
{
|
||||
throw new FileSystemException( "Access denied" );
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
throw new FileSystemException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
public OutputStream openForAppend( String path ) throws FileSystemException
|
||||
public WritableByteChannel openForAppend( String path ) throws FileSystemException
|
||||
{
|
||||
if( m_writableMount == null )
|
||||
{
|
||||
@@ -266,7 +284,7 @@ public class FileSystem
|
||||
m_writableMount.makeDirectory( dir );
|
||||
}
|
||||
}
|
||||
return m_writableMount.openForWrite( path );
|
||||
return m_writableMount.openChannelForWrite( path );
|
||||
}
|
||||
else if( m_mount.isDirectory( path ) )
|
||||
{
|
||||
@@ -274,9 +292,13 @@ public class FileSystem
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_writableMount.openForAppend( path );
|
||||
return m_writableMount.openChannelForAppend( path );
|
||||
}
|
||||
}
|
||||
catch( AccessDeniedException e )
|
||||
{
|
||||
throw new FileSystemException( "Access denied" );
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
throw new FileSystemException( e.getMessage() );
|
||||
@@ -291,10 +313,12 @@ public class FileSystem
|
||||
}
|
||||
}
|
||||
|
||||
private final FileSystemMount m_wrapper = new FileSystemMount( this );
|
||||
private final FileSystemWrapperMount m_wrapper = new FileSystemWrapperMount( this );
|
||||
private final Map<String, MountWrapper> m_mounts = new HashMap<>();
|
||||
private final Set<Closeable> m_openFiles = Collections.newSetFromMap( new WeakHashMap<Closeable, Boolean>() );
|
||||
|
||||
|
||||
private final HashMap<WeakReference<FileSystemWrapper<?>>, Closeable> m_openFiles = new HashMap<>();
|
||||
private final ReferenceQueue<FileSystemWrapper<?>> m_openFileQueue = new ReferenceQueue<>();
|
||||
|
||||
public FileSystem( String rootLabel, IMount rootMount ) throws FileSystemException
|
||||
{
|
||||
mount( rootLabel, "", rootMount );
|
||||
@@ -310,24 +334,15 @@ public class FileSystem
|
||||
// Close all dangling open files
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
for( Closeable file : m_openFiles )
|
||||
{
|
||||
try {
|
||||
file.close();
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
for( Closeable file : m_openFiles.values() ) closeQuietly( file );
|
||||
m_openFiles.clear();
|
||||
while( m_openFileQueue.poll() != null ) ;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void mount( String label, String location, IMount mount ) throws FileSystemException
|
||||
{
|
||||
if( mount == null )
|
||||
{
|
||||
throw new NullPointerException();
|
||||
}
|
||||
if( mount == null ) throw new NullPointerException();
|
||||
location = sanitizePath( location );
|
||||
if( location.contains( ".." ) ) {
|
||||
throw new FileSystemException( "Cannot mount below the root" );
|
||||
@@ -348,24 +363,18 @@ public class FileSystem
|
||||
}
|
||||
mount( new MountWrapper( label, location, mount ) );
|
||||
}
|
||||
|
||||
|
||||
private synchronized void mount( MountWrapper wrapper )
|
||||
{
|
||||
String location = wrapper.getLocation();
|
||||
if( m_mounts.containsKey( location ) )
|
||||
{
|
||||
m_mounts.remove( location );
|
||||
}
|
||||
m_mounts.remove( location );
|
||||
m_mounts.put( location, wrapper );
|
||||
}
|
||||
|
||||
public synchronized void unmount( String path )
|
||||
{
|
||||
path = sanitizePath( path );
|
||||
if( m_mounts.containsKey( path ) )
|
||||
{
|
||||
m_mounts.remove( path );
|
||||
}
|
||||
m_mounts.remove( path );
|
||||
}
|
||||
|
||||
public synchronized String combine( String path, String childPath )
|
||||
@@ -599,108 +608,85 @@ public class FileSystem
|
||||
else
|
||||
{
|
||||
// Copy a file:
|
||||
InputStream source = null;
|
||||
OutputStream destination = null;
|
||||
try
|
||||
try( ReadableByteChannel source = sourceMount.openForRead( sourcePath );
|
||||
WritableByteChannel destination = destinationMount.openForWrite( destinationPath ) )
|
||||
{
|
||||
// Open both files
|
||||
source = sourceMount.openForRead( sourcePath );
|
||||
destination = destinationMount.openForWrite( destinationPath );
|
||||
|
||||
// Copy bytes as fast as we can
|
||||
byte[] buffer = new byte[1024];
|
||||
while( true )
|
||||
{
|
||||
int bytesRead = source.read( buffer );
|
||||
if( bytesRead >= 0 )
|
||||
{
|
||||
destination.write( buffer, 0, bytesRead );
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
ByteStreams.copy( source, destination );
|
||||
}
|
||||
catch( AccessDeniedException e )
|
||||
{
|
||||
throw new FileSystemException( "Access denied" );
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
throw new FileSystemException( e.getMessage() );
|
||||
}
|
||||
finally
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup()
|
||||
{
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
Reference<?> ref;
|
||||
while( (ref = m_openFileQueue.poll()) != null )
|
||||
{
|
||||
// Close both files
|
||||
if( source != null )
|
||||
{
|
||||
try {
|
||||
source.close();
|
||||
} catch( IOException e ) {
|
||||
// nobody cares
|
||||
}
|
||||
}
|
||||
if( destination != null )
|
||||
{
|
||||
try {
|
||||
destination.close();
|
||||
} catch( IOException e ) {
|
||||
// nobody cares
|
||||
}
|
||||
}
|
||||
Closeable file = m_openFiles.remove( ref );
|
||||
if( file != null ) closeQuietly( file );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized <T> T openFile( T file, Closeable handle ) throws FileSystemException
|
||||
private synchronized <T extends Closeable> FileSystemWrapper<T> openFile( @Nonnull T file ) throws FileSystemException
|
||||
{
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
if( ComputerCraft.maximumFilesOpen > 0 &&
|
||||
m_openFiles.size() >= ComputerCraft.maximumFilesOpen )
|
||||
{
|
||||
if( handle != null )
|
||||
{
|
||||
try {
|
||||
handle.close();
|
||||
} catch ( IOException ignored ) {
|
||||
// shrug
|
||||
}
|
||||
}
|
||||
throw new FileSystemException("Too many files already open");
|
||||
closeQuietly( file );
|
||||
throw new FileSystemException( "Too many files already open" );
|
||||
}
|
||||
|
||||
m_openFiles.add( handle );
|
||||
return file;
|
||||
FileSystemWrapper<T> wrapper = new FileSystemWrapper<>( this, file, m_openFileQueue );
|
||||
m_openFiles.put( wrapper.self, file );
|
||||
return wrapper;
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void closeFile( Closeable handle ) throws IOException
|
||||
synchronized void removeFile( FileSystemWrapper<?> handle )
|
||||
{
|
||||
synchronized( m_openFiles )
|
||||
{
|
||||
m_openFiles.remove( handle );
|
||||
handle.close();
|
||||
m_openFiles.remove( handle.self );
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized InputStream openForRead( String path ) throws FileSystemException
|
||||
|
||||
public synchronized <T extends Closeable> FileSystemWrapper<T> openForRead( String path, Function<ReadableByteChannel, T> open ) throws FileSystemException
|
||||
{
|
||||
path = sanitizePath ( path );
|
||||
cleanup();
|
||||
|
||||
path = sanitizePath( path );
|
||||
MountWrapper mount = getMount( path );
|
||||
InputStream stream = mount.openForRead( path );
|
||||
if( stream != null )
|
||||
ReadableByteChannel channel = mount.openForRead( path );
|
||||
if( channel != null )
|
||||
{
|
||||
return openFile( new ClosingInputStream( stream ), stream );
|
||||
return openFile( open.apply( channel ) );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized OutputStream openForWrite( String path, boolean append ) throws FileSystemException
|
||||
public synchronized <T extends Closeable> FileSystemWrapper<T> openForWrite( String path, boolean append, Function<WritableByteChannel, T> open ) throws FileSystemException
|
||||
{
|
||||
path = sanitizePath ( path );
|
||||
cleanup();
|
||||
|
||||
path = sanitizePath( path );
|
||||
MountWrapper mount = getMount( path );
|
||||
OutputStream stream = append ? mount.openForAppend( path ) : mount.openForWrite( path );
|
||||
if( stream != null )
|
||||
WritableByteChannel channel = append ? mount.openForAppend( path ) : mount.openForWrite( path );
|
||||
if( channel != null )
|
||||
{
|
||||
return openFile( new ClosingOutputStream( stream ), stream );
|
||||
return openFile( open.apply( channel ) );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -865,33 +851,14 @@ public class FileSystem
|
||||
}
|
||||
}
|
||||
|
||||
private class ClosingInputStream extends FilterInputStream
|
||||
private static void closeQuietly( Closeable c )
|
||||
{
|
||||
protected ClosingInputStream( InputStream in )
|
||||
try
|
||||
{
|
||||
super( in );
|
||||
c.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
catch( IOException ignored )
|
||||
{
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,185 +1,132 @@
|
||||
package dan200.computercraft.core.filesystem;
|
||||
|
||||
import dan200.computercraft.api.filesystem.IFileSystem;
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FileSystemMount implements IFileSystem
|
||||
public class FileSystemMount implements IMount
|
||||
{
|
||||
private final FileSystem m_filesystem;
|
||||
private final Entry rootEntry;
|
||||
|
||||
public FileSystemMount( FileSystem m_filesystem )
|
||||
public FileSystemMount( FileSystem fileSystem, String root ) throws IOException
|
||||
{
|
||||
this.m_filesystem = m_filesystem;
|
||||
}
|
||||
Path rootPath = fileSystem.getPath( root );
|
||||
rootEntry = new Entry( "", rootPath );
|
||||
|
||||
@Override
|
||||
public void makeDirectory( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
Queue<Entry> entries = new ArrayDeque<>();
|
||||
entries.add( rootEntry );
|
||||
while( !entries.isEmpty() )
|
||||
{
|
||||
m_filesystem.makeDir( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
Entry entry = entries.remove();
|
||||
try( Stream<Path> childStream = Files.list( entry.path ) )
|
||||
{
|
||||
Iterator<Path> children = childStream.iterator();
|
||||
while( children.hasNext() )
|
||||
{
|
||||
Path childPath = children.next();
|
||||
Entry child = new Entry( childPath.getFileName().toString(), childPath );
|
||||
entry.children.put( child.name, child );
|
||||
if( child.directory ) entries.add( child );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete( @Nonnull String path ) throws IOException
|
||||
public boolean exists( @Nonnull String path )
|
||||
{
|
||||
try
|
||||
{
|
||||
m_filesystem.delete( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public OutputStream openForWrite( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.openForWrite( path, false );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public OutputStream openForAppend( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.openForWrite( path, true );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
return getFile( path ) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getRemainingSpace() throws IOException
|
||||
public boolean isDirectory( @Nonnull String path )
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.getFreeSpace( "/" );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.exists( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirectory( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.exists( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
Entry entry = getFile( path );
|
||||
return entry != null && entry.directory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void list( @Nonnull String path, @Nonnull List<String> contents ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
Collections.addAll( contents, m_filesystem.list( path ) );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
Entry entry = getFile( path );
|
||||
if( entry == null || !entry.directory ) throw new IOException( "/" + path + ": Not a directory" );
|
||||
|
||||
contents.addAll( entry.children.keySet() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.getSize( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
Entry file = getFile( path );
|
||||
if( file == null ) throw new IOException( "/" + path + ": No such file" );
|
||||
return file.size;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public InputStream openForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.openForRead( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
Entry file = getFile( path );
|
||||
if( file == null || file.directory ) throw new IOException( "/" + path + ": No such file" );
|
||||
|
||||
return Files.newInputStream( file.path, StandardOpenOption.READ );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String combine( String path, String child )
|
||||
public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return m_filesystem.combine( path, child );
|
||||
Entry file = getFile( path );
|
||||
if( file == null || file.directory ) throw new IOException( "/" + path + ": No such file" );
|
||||
|
||||
return Files.newByteChannel( file.path, StandardOpenOption.READ );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copy( String from, String to ) throws IOException
|
||||
private Entry getFile( String path )
|
||||
{
|
||||
try
|
||||
if( path.equals( "" ) ) return rootEntry;
|
||||
if( !path.contains( "/" ) ) return rootEntry.children.get( path );
|
||||
|
||||
String[] components = path.split( "/" );
|
||||
Entry entry = rootEntry;
|
||||
for( String component : components )
|
||||
{
|
||||
m_filesystem.copy( from, to );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
if( entry == null || entry.children == null ) return null;
|
||||
entry = entry.children.get( component );
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move( String from, String to ) throws IOException
|
||||
private static class Entry
|
||||
{
|
||||
try
|
||||
final String name;
|
||||
final Path path;
|
||||
|
||||
final boolean directory;
|
||||
final long size;
|
||||
final Map<String, Entry> children;
|
||||
|
||||
private Entry( String name, Path path ) throws IOException
|
||||
{
|
||||
m_filesystem.move( from, to );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
if( name.endsWith( "/" ) || name.endsWith( "\\" ) ) name = name.substring( 0, name.length() - 1 );
|
||||
|
||||
this.name = name;
|
||||
this.path = path;
|
||||
|
||||
BasicFileAttributes attributes = Files.readAttributes( path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS );
|
||||
this.directory = attributes.isDirectory();
|
||||
this.size = directory ? 0 : attributes.size();
|
||||
this.children = directory ? new HashMap<>() : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,42 @@
|
||||
package dan200.computercraft.core.filesystem;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
* An alternative closeable implementation that will free up resources in the filesystem.
|
||||
*
|
||||
* In an ideal world, we'd just wrap the closeable. However, as we do some {@code instanceof} checks
|
||||
* on the stream, it's not really possible as it'd require numerous instances.
|
||||
*
|
||||
* @param <T> The stream to wrap.
|
||||
*/
|
||||
public class FileSystemWrapper<T extends Closeable> implements Closeable
|
||||
{
|
||||
private final FileSystem fileSystem;
|
||||
private final T closeable;
|
||||
final WeakReference<FileSystemWrapper<?>> self;
|
||||
|
||||
FileSystemWrapper( FileSystem fileSystem, T closeable, ReferenceQueue<FileSystemWrapper<?>> queue )
|
||||
{
|
||||
this.fileSystem = fileSystem;
|
||||
this.closeable = closeable;
|
||||
this.self = new WeakReference<>( this, queue );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
fileSystem.removeFile( this );
|
||||
closeable.close();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public T get()
|
||||
{
|
||||
return closeable;
|
||||
}
|
||||
}
|
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.core.filesystem;
|
||||
|
||||
import dan200.computercraft.api.filesystem.IFileSystem;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FileSystemWrapperMount implements IFileSystem
|
||||
{
|
||||
private final FileSystem m_filesystem;
|
||||
|
||||
public FileSystemWrapperMount( FileSystem m_filesystem )
|
||||
{
|
||||
this.m_filesystem = m_filesystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeDirectory( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
m_filesystem.makeDir( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
m_filesystem.delete( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
// FIXME: Think of a better way of implementing this, so closing this will close on the computer.
|
||||
return m_filesystem.openForRead( path, Function.identity() ).get();
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public WritableByteChannel openChannelForWrite( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.openForWrite( path, false, Function.identity() ).get();
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public WritableByteChannel openChannelForAppend( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.openForWrite( path, true, Function.identity() ).get();
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public InputStream openForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newInputStream( openChannelForRead( path ) );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public OutputStream openForWrite( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newOutputStream( openChannelForWrite( path ) );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public OutputStream openForAppend( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return Channels.newOutputStream( openChannelForAppend( path ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getRemainingSpace() throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.getFreeSpace( "/" );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.exists( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirectory( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.exists( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void list( @Nonnull String path, @Nonnull List<String> contents ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
Collections.addAll( contents, m_filesystem.list( path ) );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize( @Nonnull String path ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return m_filesystem.getSize( path );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String combine( String path, String child )
|
||||
{
|
||||
return m_filesystem.combine( path, child );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copy( String from, String to ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
m_filesystem.copy( from, to );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move( String from, String to ) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
m_filesystem.move( from, to );
|
||||
}
|
||||
catch( FileSystemException e )
|
||||
{
|
||||
throw new IOException( e.getMessage() );
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,248 +0,0 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.core.filesystem;
|
||||
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
public class JarMount implements IMount
|
||||
{
|
||||
private class FileInZip
|
||||
{
|
||||
private String m_path;
|
||||
private boolean m_directory;
|
||||
private long m_size;
|
||||
private Map<String, FileInZip> m_children;
|
||||
|
||||
public FileInZip( String path, boolean directory, long size )
|
||||
{
|
||||
m_path = path;
|
||||
m_directory = directory;
|
||||
m_size = m_directory ? 0 : size;
|
||||
m_children = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public String getPath()
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
public boolean isDirectory()
|
||||
{
|
||||
return m_directory;
|
||||
}
|
||||
|
||||
public long getSize()
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
public void list( List<String> contents )
|
||||
{
|
||||
contents.addAll( m_children.keySet() );
|
||||
}
|
||||
|
||||
public void insertChild( FileInZip child )
|
||||
{
|
||||
String localPath = FileSystem.toLocal( child.getPath(), m_path );
|
||||
m_children.put( localPath, child );
|
||||
}
|
||||
|
||||
public FileInZip getFile( String path )
|
||||
{
|
||||
// If we've reached the target, return this
|
||||
if( path.equals( m_path ) )
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
// Otherwise, get the next component of the path
|
||||
String localPath = FileSystem.toLocal( path, m_path );
|
||||
int slash = localPath.indexOf("/");
|
||||
if( slash >= 0 )
|
||||
{
|
||||
localPath = localPath.substring( 0, slash );
|
||||
}
|
||||
|
||||
// And recurse down using it
|
||||
FileInZip subFile = m_children.get( localPath );
|
||||
if( subFile != null )
|
||||
{
|
||||
return subFile.getFile( path );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public FileInZip getParent( String path )
|
||||
{
|
||||
if( path.length() == 0 )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FileInZip file = getFile( FileSystem.getDirectory( path ) );
|
||||
if( file.isDirectory() )
|
||||
{
|
||||
return file;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ZipFile m_zipFile;
|
||||
private FileInZip m_root;
|
||||
private String m_rootPath;
|
||||
|
||||
public JarMount( File jarFile, String subPath ) throws IOException
|
||||
{
|
||||
if( !jarFile.exists() || jarFile.isDirectory() )
|
||||
{
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
// Open the zip file
|
||||
try
|
||||
{
|
||||
m_zipFile = new ZipFile( jarFile );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
throw new IOException( "Error loading zip file" );
|
||||
}
|
||||
|
||||
if( m_zipFile.getEntry( subPath ) == null )
|
||||
{
|
||||
m_zipFile.close();
|
||||
throw new IOException( "Zip does not contain path" );
|
||||
}
|
||||
|
||||
// Read in all the entries
|
||||
Enumeration<? extends ZipEntry> zipEntries = m_zipFile.entries();
|
||||
while( zipEntries.hasMoreElements() )
|
||||
{
|
||||
ZipEntry entry = zipEntries.nextElement();
|
||||
String entryName = entry.getName();
|
||||
if( entryName.startsWith( subPath ) )
|
||||
{
|
||||
entryName = FileSystem.toLocal( entryName, subPath );
|
||||
if( m_root == null )
|
||||
{
|
||||
if( entryName.equals( "" ) )
|
||||
{
|
||||
m_root = new FileInZip( entryName, entry.isDirectory(), entry.getSize() );
|
||||
m_rootPath = subPath;
|
||||
if( !m_root.isDirectory() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: handle this case. The code currently assumes we find the root before anything else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FileInZip parent = m_root.getParent( entryName );
|
||||
if( parent != null )
|
||||
{
|
||||
parent.insertChild( new FileInZip( entryName, entry.isDirectory(), entry.getSize() ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: handle this case. The code currently assumes we find folders before their contents
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IMount implementation
|
||||
|
||||
@Override
|
||||
public boolean exists( @Nonnull String path )
|
||||
{
|
||||
FileInZip file = m_root.getFile( path );
|
||||
return file != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirectory( @Nonnull String path )
|
||||
{
|
||||
FileInZip file = m_root.getFile( path );
|
||||
if( file != null )
|
||||
{
|
||||
return file.isDirectory();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void list( @Nonnull String path, @Nonnull List<String> contents ) throws IOException
|
||||
{
|
||||
FileInZip file = m_root.getFile( path );
|
||||
if( file != null && file.isDirectory() )
|
||||
{
|
||||
file.list( contents );
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IOException( "/" + path + ": Not a directory" );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize( @Nonnull String path ) throws IOException
|
||||
{
|
||||
FileInZip file = m_root.getFile( path );
|
||||
if( file != null )
|
||||
{
|
||||
return file.getSize();
|
||||
}
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public InputStream openForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
FileInZip file = m_root.getFile( path );
|
||||
if( file != null && !file.isDirectory() )
|
||||
{
|
||||
try
|
||||
{
|
||||
String fullPath = m_rootPath;
|
||||
if( path.length() > 0 )
|
||||
{
|
||||
fullPath = fullPath + "/" + path;
|
||||
}
|
||||
ZipEntry entry = m_zipFile.getEntry( fullPath );
|
||||
if( entry != null )
|
||||
{
|
||||
return m_zipFile.getInputStream( entry );
|
||||
}
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
// treat errors as non-existance of file
|
||||
}
|
||||
}
|
||||
throw new IOException( "/" + path + ": No such file" );
|
||||
}
|
||||
}
|
@@ -11,6 +11,7 @@ import dan200.computercraft.api.filesystem.IMount;
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.List;
|
||||
|
||||
public class SubMount implements IMount
|
||||
@@ -52,11 +53,19 @@ public class SubMount implements IMount
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public InputStream openForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return m_parent.openForRead( getFullPath( path ) );
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException
|
||||
{
|
||||
return m_parent.openChannelForRead( getFullPath( path ) );
|
||||
}
|
||||
|
||||
private String getFullPath( String path )
|
||||
{
|
||||
if( path.length() == 0 )
|
||||
|
@@ -128,7 +128,7 @@ public class CobaltLuaMachine implements ILuaMachine
|
||||
LibFunction.bind( state, m_globals, PrefixLoader.class, new String[]{ "load", "loadstring" } );
|
||||
|
||||
// Remove globals we don't want to expose
|
||||
m_globals.rawset( "collectgarbage", Constants.NIL );
|
||||
// m_globals.rawset( "collectgarbage", Constants.NIL );
|
||||
m_globals.rawset( "dofile", Constants.NIL );
|
||||
m_globals.rawset( "loadfile", Constants.NIL );
|
||||
m_globals.rawset( "print", Constants.NIL );
|
||||
|
@@ -23,6 +23,7 @@ import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static dan200.computercraft.shared.command.framework.ChatHelpers.*;
|
||||
|
||||
@@ -147,26 +148,15 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||
{
|
||||
Set<ServerComputer> computers = Sets.newHashSet();
|
||||
if( arguments.size() > 0 )
|
||||
{
|
||||
for( String arg : arguments )
|
||||
withComputers( arguments, computers -> {
|
||||
int shutdown = 0;
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
computers.addAll( ComputerSelector.getComputers( arg ) );
|
||||
if( computer.isOn() ) shutdown++;
|
||||
computer.unload();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
}
|
||||
|
||||
int shutdown = 0;
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
if( computer.isOn() ) shutdown++;
|
||||
computer.unload();
|
||||
}
|
||||
context.getSender().sendMessage( text( "Shutdown " + shutdown + " / " + computers.size() + " computers" ) );
|
||||
context.getSender().sendMessage( text( "Shutdown " + shutdown + " / " + computers.size() + " computers" ) );
|
||||
} );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -188,26 +178,15 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||
{
|
||||
Set<ServerComputer> computers = Sets.newHashSet();
|
||||
if( arguments.size() > 0 )
|
||||
{
|
||||
for( String arg : arguments )
|
||||
withComputers( arguments, computers -> {
|
||||
int on = 0;
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
computers.addAll( ComputerSelector.getComputers( arg ) );
|
||||
if( !computer.isOn() ) on++;
|
||||
computer.turnOn();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
}
|
||||
|
||||
int on = 0;
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
if( !computer.isOn() ) on++;
|
||||
computer.turnOn();
|
||||
}
|
||||
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
|
||||
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
|
||||
} );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -489,8 +468,6 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
if( server.getID() > maxId ) maxId = server.getID();
|
||||
}
|
||||
|
||||
ICommandSender sender = context.getSender();
|
||||
|
||||
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
|
||||
|
||||
boolean defaultLayout = field == TrackingField.TASKS || field == TrackingField.TOTAL_TIME
|
||||
@@ -526,4 +503,30 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
|
||||
table.displayTo( context.getSender() );
|
||||
}
|
||||
|
||||
private static void withComputers( List<String> selectors, Consumer<Collection<ServerComputer>> action ) throws CommandException
|
||||
{
|
||||
Set<ServerComputer> computers = Sets.newHashSet();
|
||||
List<String> failed = new ArrayList<>();
|
||||
if( selectors.isEmpty() )
|
||||
{
|
||||
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
}
|
||||
else
|
||||
{
|
||||
for( String selector : selectors )
|
||||
{
|
||||
List<ServerComputer> selected = ComputerSelector.getComputers( selector );
|
||||
computers.addAll( selected );
|
||||
if( selected.isEmpty() ) failed.add( selector );
|
||||
}
|
||||
}
|
||||
|
||||
action.accept( computers );
|
||||
|
||||
if( !failed.isEmpty() )
|
||||
{
|
||||
throw new CommandException( "Could not find computers matching " + String.join( ", ", failed ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,30 +12,22 @@ import java.util.function.Predicate;
|
||||
|
||||
public final class ComputerSelector
|
||||
{
|
||||
private static List<ServerComputer> getComputers( Predicate<ServerComputer> predicate, String selector ) throws CommandException
|
||||
private static List<ServerComputer> getComputers( Predicate<ServerComputer> predicate ) throws CommandException
|
||||
{
|
||||
// We copy it to prevent concurrent modifications.
|
||||
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
List<ServerComputer> candidates = Lists.newArrayList();
|
||||
for( ServerComputer searchComputer : computers )
|
||||
{
|
||||
if( predicate.test( searchComputer ) ) candidates.add( searchComputer );
|
||||
}
|
||||
|
||||
if( candidates.isEmpty() )
|
||||
{
|
||||
throw new CommandException( "No computer matching " + selector );
|
||||
}
|
||||
else
|
||||
{
|
||||
return candidates;
|
||||
}
|
||||
ArrayList<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
computers.removeIf( predicate.negate() );
|
||||
return computers;
|
||||
}
|
||||
|
||||
public static ServerComputer getComputer( String selector ) throws CommandException
|
||||
{
|
||||
List<ServerComputer> computers = getComputers( selector );
|
||||
if( computers.size() == 1 )
|
||||
if( computers.size() == 0 )
|
||||
{
|
||||
throw new CommandException( "No computer matching " + selector );
|
||||
}
|
||||
else if( computers.size() == 1 )
|
||||
{
|
||||
return computers.get( 0 );
|
||||
}
|
||||
@@ -71,17 +63,17 @@ public final class ComputerSelector
|
||||
throw new CommandException( "'" + selector + "' is not a valid number" );
|
||||
}
|
||||
|
||||
return getComputers( x -> x.getID() == id, selector );
|
||||
return getComputers( x -> x.getID() == id );
|
||||
}
|
||||
else if( selector.length() > 0 && selector.charAt( 0 ) == '@' )
|
||||
{
|
||||
String label = selector.substring( 1 );
|
||||
return getComputers( x -> Objects.equals( label, x.getLabel() ), selector );
|
||||
return getComputers( x -> Objects.equals( label, x.getLabel() ) );
|
||||
}
|
||||
else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
|
||||
{
|
||||
String familyName = selector.substring( 1 );
|
||||
return getComputers( x -> x.getFamily().name().equalsIgnoreCase( familyName ), selector );
|
||||
return getComputers( x -> x.getFamily().name().equalsIgnoreCase( familyName ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -96,14 +88,7 @@ public final class ComputerSelector
|
||||
}
|
||||
|
||||
ServerComputer computer = ComputerCraft.serverComputerRegistry.get( instance );
|
||||
if( computer == null )
|
||||
{
|
||||
throw new CommandException( "No such computer for instance id " + instance );
|
||||
}
|
||||
else
|
||||
{
|
||||
return Collections.singletonList( computer );
|
||||
}
|
||||
return computer == null ? Collections.emptyList() : Collections.singletonList( computer );
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -42,7 +42,7 @@ public class BlockCommandComputer extends BlockComputerBase
|
||||
super( Material.IRON );
|
||||
setBlockUnbreakable();
|
||||
setResistance( 6000000.0F );
|
||||
setUnlocalizedName( "computercraft:command_computer" );
|
||||
setTranslationKey( "computercraft:command_computer" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( this.blockState.getBaseState()
|
||||
.withProperty( Properties.FACING, EnumFacing.NORTH )
|
||||
@@ -62,7 +62,7 @@ public class BlockCommandComputer extends BlockComputerBase
|
||||
@Deprecated
|
||||
public IBlockState getStateFromMeta( int meta )
|
||||
{
|
||||
EnumFacing dir = EnumFacing.getFront( meta & 0x7 );
|
||||
EnumFacing dir = EnumFacing.byIndex( meta & 0x7 );
|
||||
if( dir.getAxis() == EnumFacing.Axis.Y )
|
||||
{
|
||||
dir = EnumFacing.NORTH;
|
||||
|
@@ -44,7 +44,7 @@ public class BlockComputer extends BlockComputerBase
|
||||
{
|
||||
super( Material.ROCK );
|
||||
setHardness( 2.0f );
|
||||
setUnlocalizedName( "computercraft:computer" );
|
||||
setTranslationKey( "computercraft:computer" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( this.blockState.getBaseState()
|
||||
.withProperty( Properties.FACING, EnumFacing.NORTH )
|
||||
@@ -65,7 +65,7 @@ public class BlockComputer extends BlockComputerBase
|
||||
@Deprecated
|
||||
public IBlockState getStateFromMeta( int meta )
|
||||
{
|
||||
EnumFacing dir = EnumFacing.getFront( meta & 0x7 );
|
||||
EnumFacing dir = EnumFacing.byIndex( meta & 0x7 );
|
||||
if( dir.getAxis() == EnumFacing.Axis.Y )
|
||||
{
|
||||
dir = EnumFacing.NORTH;
|
||||
|
@@ -24,7 +24,7 @@ public class ItemCommandComputer extends ItemComputer
|
||||
super( block );
|
||||
setMaxStackSize( 64 );
|
||||
setHasSubtypes( true );
|
||||
setUnlocalizedName( "computercraft:command_computer" );
|
||||
setTranslationKey( "computercraft:command_computer" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
|
@@ -33,7 +33,7 @@ public class ItemComputer extends ItemComputerBase
|
||||
super( block );
|
||||
setMaxStackSize( 64 );
|
||||
setHasSubtypes( true );
|
||||
setUnlocalizedName( "computercraft:computer" );
|
||||
setTranslationKey( "computercraft:computer" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public class ItemComputer extends ItemComputerBase
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getUnlocalizedName( @Nonnull ItemStack stack )
|
||||
public String getTranslationKey( @Nonnull ItemStack stack )
|
||||
{
|
||||
switch( getFamily( stack ) )
|
||||
{
|
||||
|
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.integration.charset;
|
||||
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import pl.asie.charset.api.wires.IBundledEmitter;
|
||||
import pl.asie.charset.api.wires.IBundledReceiver;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static dan200.computercraft.shared.integration.charset.IntegrationCharset.CAPABILITY_EMITTER;
|
||||
import static dan200.computercraft.shared.integration.charset.IntegrationCharset.CAPABILITY_RECEIVER;
|
||||
|
||||
final class BundledCapabilityProvider implements ICapabilityProvider
|
||||
{
|
||||
private final TileGeneric tile;
|
||||
private IBundledReceiver receiver;
|
||||
private IBundledEmitter[] emitters;
|
||||
|
||||
BundledCapabilityProvider( TileGeneric tile )
|
||||
{
|
||||
this.tile = tile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability( @Nonnull Capability<?> capability, @Nullable EnumFacing side )
|
||||
{
|
||||
return capability == CAPABILITY_EMITTER || capability == CAPABILITY_RECEIVER;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getCapability( @Nonnull Capability<T> capability, @Nullable EnumFacing side )
|
||||
{
|
||||
if( capability == CAPABILITY_RECEIVER )
|
||||
{
|
||||
IBundledReceiver receiver = this.receiver;
|
||||
if( receiver == null ) receiver = this.receiver = tile::onNeighbourChange;
|
||||
|
||||
return CAPABILITY_RECEIVER.cast( receiver );
|
||||
}
|
||||
else if( capability == CAPABILITY_EMITTER )
|
||||
{
|
||||
IBundledEmitter[] emitters = this.emitters;
|
||||
if( emitters == null ) emitters = this.emitters = new IBundledEmitter[7];
|
||||
|
||||
int index = side == null ? 6 : side.getIndex();
|
||||
IBundledEmitter emitter = emitters[index];
|
||||
if( emitter == null )
|
||||
{
|
||||
if( side == null )
|
||||
{
|
||||
emitter = emitters[index] = () -> {
|
||||
int flags = 0;
|
||||
for( EnumFacing facing : EnumFacing.VALUES ) flags |= tile.getBundledRedstoneOutput( facing );
|
||||
return toBytes( flags );
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
emitter = emitters[index] = () -> toBytes( tile.getBundledRedstoneOutput( side ) );
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
return CAPABILITY_EMITTER.cast( emitter );
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] toBytes( int flag )
|
||||
{
|
||||
byte[] channels = new byte[16];
|
||||
for( int i = 0; i < 16; i++ ) channels[i] = (flag & (1 << i)) == 0 ? (byte) 0 : 15;
|
||||
return channels;
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.integration.charset;
|
||||
|
||||
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static dan200.computercraft.shared.integration.charset.IntegrationCharset.CAPABILITY_EMITTER;
|
||||
|
||||
public class BundledRedstoneProvider implements IBundledRedstoneProvider
|
||||
{
|
||||
@Override
|
||||
public int getBundledRedstoneOutput( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
||||
{
|
||||
TileEntity tile = world.getTileEntity( pos );
|
||||
if( tile == null || !tile.hasCapability( CAPABILITY_EMITTER, side ) ) return -1;
|
||||
|
||||
byte[] signal = tile.getCapability( CAPABILITY_EMITTER, side ).getBundledSignal();
|
||||
if( signal == null ) return -1;
|
||||
|
||||
int flag = 0;
|
||||
for( int i = 0; i < signal.length; i++ ) flag |= signal[i] > 0 ? (1 << i) : 0;
|
||||
return flag;
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.integration.charset;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.ComputerCraftAPI;
|
||||
import dan200.computercraft.shared.common.TileGeneric;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.CapabilityInject;
|
||||
import net.minecraftforge.event.AttachCapabilitiesEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import pl.asie.charset.api.wires.IBundledEmitter;
|
||||
import pl.asie.charset.api.wires.IBundledReceiver;
|
||||
|
||||
public final class IntegrationCharset
|
||||
{
|
||||
private static final ResourceLocation CAPABILITY_KEY = new ResourceLocation( ComputerCraft.MOD_ID, "charset" );
|
||||
|
||||
@CapabilityInject( IBundledEmitter.class )
|
||||
public static final Capability<IBundledEmitter> CAPABILITY_EMITTER = null;
|
||||
|
||||
@CapabilityInject( IBundledReceiver.class )
|
||||
public static final Capability<IBundledReceiver> CAPABILITY_RECEIVER = null;
|
||||
|
||||
private IntegrationCharset()
|
||||
{
|
||||
}
|
||||
|
||||
public static void register()
|
||||
{
|
||||
if( CAPABILITY_EMITTER == null || CAPABILITY_RECEIVER == null ) return;
|
||||
|
||||
MinecraftForge.EVENT_BUS.register( new IntegrationCharset() );
|
||||
ComputerCraftAPI.registerBundledRedstoneProvider( new BundledRedstoneProvider() );
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void attachGenericCapabilities( AttachCapabilitiesEvent<TileEntity> event)
|
||||
{
|
||||
TileEntity tile = event.getObject();
|
||||
if(tile instanceof TileGeneric)
|
||||
{
|
||||
event.addCapability( CAPABILITY_KEY, new BundledCapabilityProvider( (TileGeneric) tile ) );
|
||||
}
|
||||
}
|
||||
}
|
@@ -33,7 +33,7 @@ public class ItemDiskLegacy extends Item
|
||||
{
|
||||
setMaxStackSize( 1 );
|
||||
setHasSubtypes( true );
|
||||
setUnlocalizedName( "computercraft:disk" );
|
||||
setTranslationKey( "computercraft:disk" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
|
@@ -39,7 +39,7 @@ public class ItemPrintout extends Item
|
||||
{
|
||||
setMaxStackSize( 1 );
|
||||
setHasSubtypes( true );
|
||||
setUnlocalizedName( "computercraft:page" );
|
||||
setTranslationKey( "computercraft:page" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public class ItemPrintout extends Item
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getUnlocalizedName( @Nonnull ItemStack stack )
|
||||
public String getTranslationKey( @Nonnull ItemStack stack )
|
||||
{
|
||||
Type type = getType( stack );
|
||||
switch( type )
|
||||
|
@@ -34,7 +34,7 @@ public class ItemTreasureDisk extends Item
|
||||
{
|
||||
setMaxStackSize( 1 );
|
||||
setHasSubtypes( true );
|
||||
setUnlocalizedName( "computercraft:treasure_disk" );
|
||||
setTranslationKey( "computercraft:treasure_disk" );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -55,7 +55,7 @@ public class BlockCable extends BlockPeripheralBase
|
||||
public BlockCable()
|
||||
{
|
||||
setHardness( 1.5f );
|
||||
setUnlocalizedName( "computercraft:cable" );
|
||||
setTranslationKey( "computercraft:cable" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( this.blockState.getBaseState()
|
||||
.withProperty( Properties.MODEM, BlockCableModemVariant.None )
|
||||
@@ -94,12 +94,12 @@ public class BlockCable extends BlockPeripheralBase
|
||||
if( meta < 6 )
|
||||
{
|
||||
state = state.withProperty( Properties.CABLE, BlockCableCableVariant.NONE );
|
||||
state = state.withProperty( Properties.MODEM, BlockCableModemVariant.fromFacing( EnumFacing.getFront( meta ) ) );
|
||||
state = state.withProperty( Properties.MODEM, BlockCableModemVariant.fromFacing( EnumFacing.byIndex( meta ) ) );
|
||||
}
|
||||
else if( meta < 12 )
|
||||
{
|
||||
state = state.withProperty( Properties.CABLE, BlockCableCableVariant.ANY );
|
||||
state = state.withProperty( Properties.MODEM, BlockCableModemVariant.fromFacing( EnumFacing.getFront( meta - 6 ) ) );
|
||||
state = state.withProperty( Properties.MODEM, BlockCableModemVariant.fromFacing( EnumFacing.byIndex( meta - 6 ) ) );
|
||||
}
|
||||
else if( meta == 13 )
|
||||
{
|
||||
@@ -296,7 +296,7 @@ public class BlockCable extends BlockPeripheralBase
|
||||
}
|
||||
}
|
||||
|
||||
return result == null ? null : new RayTraceResult( result.hitVec.addVector( pos.getX(), pos.getY(), pos.getZ() ), result.sideHit, pos );
|
||||
return result == null ? null : new RayTraceResult( result.hitVec.add( pos.getX(), pos.getY(), pos.getZ() ), result.sideHit, pos );
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@@ -44,7 +44,7 @@ public class BlockPeripheral extends BlockPeripheralBase
|
||||
public BlockPeripheral()
|
||||
{
|
||||
setHardness( 2.0f );
|
||||
setUnlocalizedName( "computercraft:peripheral" );
|
||||
setTranslationKey( "computercraft:peripheral" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( this.blockState.getBaseState()
|
||||
.withProperty( Properties.FACING, EnumFacing.NORTH )
|
||||
@@ -55,7 +55,7 @@ public class BlockPeripheral extends BlockPeripheralBase
|
||||
@Override
|
||||
@Nonnull
|
||||
@SideOnly( Side.CLIENT)
|
||||
public BlockRenderLayer getBlockLayer()
|
||||
public BlockRenderLayer getRenderLayer()
|
||||
{
|
||||
return BlockRenderLayer.CUTOUT;
|
||||
}
|
||||
@@ -76,7 +76,7 @@ public class BlockPeripheral extends BlockPeripheralBase
|
||||
if( meta >= 2 && meta <= 5 )
|
||||
{
|
||||
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.DiskDriveEmpty );
|
||||
state = state.withProperty( Properties.FACING, EnumFacing.getFront( meta ) );
|
||||
state = state.withProperty( Properties.FACING, EnumFacing.byIndex( meta ) );
|
||||
}
|
||||
else if( meta <= 9 )
|
||||
{
|
||||
@@ -93,7 +93,7 @@ public class BlockPeripheral extends BlockPeripheralBase
|
||||
else
|
||||
{
|
||||
state = state.withProperty( Properties.VARIANT, BlockPeripheralVariant.WirelessModemOff );
|
||||
state = state.withProperty( Properties.FACING, EnumFacing.getFront( meta - 4 ) );
|
||||
state = state.withProperty( Properties.FACING, EnumFacing.byIndex( meta - 4 ) );
|
||||
}
|
||||
}
|
||||
else if( meta == 10 )
|
||||
@@ -651,7 +651,7 @@ public class BlockPeripheral extends BlockPeripheralBase
|
||||
public boolean causesSuffocation(IBlockState state)
|
||||
{
|
||||
// This normally uses the default state
|
||||
return blockMaterial.blocksMovement() && state.isOpaqueCube();
|
||||
return material.blocksMovement() && state.isOpaqueCube();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -34,7 +34,7 @@ public class BlockWiredModemFull extends BlockPeripheralBase
|
||||
public BlockWiredModemFull()
|
||||
{
|
||||
setHardness( 1.5f );
|
||||
setUnlocalizedName( "computercraft:wired_modem_full" );
|
||||
setTranslationKey( "computercraft:wired_modem_full" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( blockState.getBaseState()
|
||||
.withProperty( Properties.MODEM_ON, false )
|
||||
|
@@ -21,7 +21,7 @@ public class ItemAdvancedModem extends ItemPeripheralBase
|
||||
public ItemAdvancedModem( Block block )
|
||||
{
|
||||
super( block );
|
||||
setUnlocalizedName( "computercraft:advanced_modem" );
|
||||
setTranslationKey( "computercraft:advanced_modem" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
|
@@ -28,7 +28,7 @@ public class ItemCable extends ItemPeripheralBase
|
||||
public ItemCable( Block block )
|
||||
{
|
||||
super( block );
|
||||
setUnlocalizedName( "computercraft:cable" );
|
||||
setTranslationKey( "computercraft:cable" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
|
@@ -20,7 +20,7 @@ public class ItemPeripheral extends ItemPeripheralBase
|
||||
public ItemPeripheral( Block block )
|
||||
{
|
||||
super( block );
|
||||
setUnlocalizedName( "computercraft:peripheral" );
|
||||
setTranslationKey( "computercraft:peripheral" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
|
@@ -59,7 +59,7 @@ public abstract class ItemPeripheralBase extends ItemBlock implements IPeriphera
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getUnlocalizedName( @Nonnull ItemStack stack )
|
||||
public String getTranslationKey( @Nonnull ItemStack stack )
|
||||
{
|
||||
PeripheralType type = getPeripheralType( stack );
|
||||
switch( type )
|
||||
|
@@ -112,12 +112,12 @@ public abstract class TilePeripheralBase extends TileGeneric
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized int getAnim()
|
||||
public int getAnim()
|
||||
{
|
||||
return m_anim;
|
||||
}
|
||||
|
||||
public synchronized void setAnim( int anim )
|
||||
public void setAnim( int anim )
|
||||
{
|
||||
if( anim != m_anim )
|
||||
{
|
||||
@@ -127,12 +127,12 @@ public abstract class TilePeripheralBase extends TileGeneric
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void update()
|
||||
public void update()
|
||||
{
|
||||
if( m_changed )
|
||||
{
|
||||
updateBlock();
|
||||
m_changed = false;
|
||||
updateBlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ public abstract class TilePeripheralBase extends TileGeneric
|
||||
super.readFromNBT(nbttagcompound);
|
||||
if( nbttagcompound.hasKey( "dir" ) )
|
||||
{
|
||||
m_dir = EnumFacing.getFront( nbttagcompound.getInteger( "dir" ) );
|
||||
m_dir = EnumFacing.byIndex( nbttagcompound.getInteger( "dir" ) );
|
||||
}
|
||||
if( nbttagcompound.hasKey( "anim" ) )
|
||||
{
|
||||
@@ -174,7 +174,7 @@ public abstract class TilePeripheralBase extends TileGeneric
|
||||
public void readDescription( @Nonnull NBTTagCompound nbttagcompound )
|
||||
{
|
||||
super.readDescription( nbttagcompound );
|
||||
m_dir = EnumFacing.getFront( nbttagcompound.getInteger( "dir" ) );
|
||||
m_dir = EnumFacing.byIndex( nbttagcompound.getInteger( "dir" ) );
|
||||
m_anim = nbttagcompound.getInteger( "anim" );
|
||||
if( nbttagcompound.hasKey( "label" ) )
|
||||
{
|
||||
|
@@ -80,12 +80,9 @@ public class TileDiskDrive extends TilePeripheralBase
|
||||
public void destroy()
|
||||
{
|
||||
ejectContents( true );
|
||||
synchronized( this )
|
||||
if( m_recordPlaying )
|
||||
{
|
||||
if( m_recordPlaying )
|
||||
{
|
||||
stopRecord();
|
||||
}
|
||||
stopRecord();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,13 +167,10 @@ public class TileDiskDrive extends TilePeripheralBase
|
||||
super.update();
|
||||
|
||||
// Ejection
|
||||
synchronized( this )
|
||||
if( m_ejectQueued )
|
||||
{
|
||||
if( m_ejectQueued )
|
||||
{
|
||||
ejectContents( false );
|
||||
m_ejectQueued = false;
|
||||
}
|
||||
ejectContents( false );
|
||||
m_ejectQueued = false;
|
||||
}
|
||||
|
||||
// Music
|
||||
@@ -391,10 +385,7 @@ public class TileDiskDrive extends TilePeripheralBase
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
synchronized( this )
|
||||
{
|
||||
setInventorySlotContents( 0, ItemStack.EMPTY );
|
||||
}
|
||||
setInventorySlotContents( 0, ItemStack.EMPTY );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -425,18 +416,12 @@ public class TileDiskDrive extends TilePeripheralBase
|
||||
@Nonnull
|
||||
public ItemStack getDiskStack()
|
||||
{
|
||||
synchronized( this )
|
||||
{
|
||||
return getStackInSlot( 0 );
|
||||
}
|
||||
return getStackInSlot( 0 );
|
||||
}
|
||||
|
||||
public void setDiskStack( @Nonnull ItemStack stack )
|
||||
{
|
||||
synchronized( this )
|
||||
{
|
||||
setInventorySlotContents( 0, stack );
|
||||
}
|
||||
setInventorySlotContents( 0, stack );
|
||||
}
|
||||
|
||||
public IMedia getDiskMedia()
|
||||
@@ -569,7 +554,7 @@ public class TileDiskDrive extends TilePeripheralBase
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void updateAnim()
|
||||
private void updateAnim()
|
||||
{
|
||||
if( !m_diskStack.isEmpty() )
|
||||
{
|
||||
@@ -605,8 +590,8 @@ public class TileDiskDrive extends TilePeripheralBase
|
||||
if( !destroyed )
|
||||
{
|
||||
EnumFacing dir = getDirection();
|
||||
xOff = dir.getFrontOffsetX();
|
||||
zOff = dir.getFrontOffsetZ();
|
||||
xOff = dir.getXOffset();
|
||||
zOff = dir.getZOffset();
|
||||
}
|
||||
|
||||
BlockPos pos = getPos();
|
||||
|
@@ -33,7 +33,7 @@ public class BlockAdvancedModem extends BlockPeripheralBase
|
||||
public BlockAdvancedModem()
|
||||
{
|
||||
setHardness( 2.0f );
|
||||
setUnlocalizedName( "computercraft:advanced_modem" );
|
||||
setTranslationKey( "computercraft:advanced_modem" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( this.blockState.getBaseState()
|
||||
.withProperty( Properties.FACING, EnumFacing.NORTH )
|
||||
@@ -54,7 +54,7 @@ public class BlockAdvancedModem extends BlockPeripheralBase
|
||||
public IBlockState getStateFromMeta( int meta )
|
||||
{
|
||||
IBlockState state = getDefaultState();
|
||||
state = state.withProperty( Properties.FACING, EnumFacing.getFront( meta ) );
|
||||
state = state.withProperty( Properties.FACING, EnumFacing.byIndex( meta ) );
|
||||
state = state.withProperty( Properties.ON, false );
|
||||
return state;
|
||||
}
|
||||
|
@@ -75,7 +75,7 @@ public abstract class ModemPeripheral
|
||||
m_open = false;
|
||||
}
|
||||
|
||||
public synchronized boolean pollChanged()
|
||||
public boolean pollChanged()
|
||||
{
|
||||
if( m_changed )
|
||||
{
|
||||
@@ -85,9 +85,9 @@ public abstract class ModemPeripheral
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean isActive()
|
||||
public boolean isActive()
|
||||
{
|
||||
return (m_computer != null) && m_open;
|
||||
return m_computer != null && m_open;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -37,7 +37,7 @@ public abstract class TileModemBase extends TilePeripheralBase
|
||||
protected abstract ModemPeripheral createPeripheral();
|
||||
|
||||
@Override
|
||||
public synchronized void destroy()
|
||||
public void destroy()
|
||||
{
|
||||
if( m_modem != null )
|
||||
{
|
||||
|
@@ -66,7 +66,8 @@ public class MonitorPeripheral implements IPeripheral
|
||||
"setPaletteColor",
|
||||
"getPaletteColour",
|
||||
"getPaletteColor",
|
||||
"getTextScale"
|
||||
"getTextScale",
|
||||
"getCursorBlink",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -245,8 +246,12 @@ public class MonitorPeripheral implements IPeripheral
|
||||
// getTextScale
|
||||
return new Object[] { monitor.getTextScale() / 2.0 };
|
||||
}
|
||||
case 25:
|
||||
// getCursorBlink
|
||||
return new Object[] { terminal.getCursorBlink() };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -344,7 +344,7 @@ public class TileMonitor extends TilePeripheralBase
|
||||
|
||||
public EnumFacing getFront()
|
||||
{
|
||||
return m_dir <= 5 ? EnumFacing.getFront( m_dir ) : (m_dir <= 11 ? EnumFacing.DOWN : EnumFacing.UP);
|
||||
return m_dir <= 5 ? EnumFacing.byIndex( m_dir ) : (m_dir <= 11 ? EnumFacing.DOWN : EnumFacing.UP);
|
||||
}
|
||||
|
||||
public EnumFacing getRight()
|
||||
|
@@ -190,10 +190,7 @@ public class TilePrinter extends TilePeripheralBase
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int i)
|
||||
{
|
||||
synchronized( m_inventory )
|
||||
{
|
||||
return m_inventory.get( i );
|
||||
}
|
||||
return m_inventory.get( i );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@@ -8,10 +8,11 @@ package dan200.computercraft.shared.peripheral.speaker;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.ILuaTask;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import net.minecraft.network.play.server.SPacketCustomSound;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
@@ -20,33 +21,35 @@ import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optReal;
|
||||
|
||||
public class SpeakerPeripheral implements IPeripheral {
|
||||
private TileSpeaker m_speaker;
|
||||
public class SpeakerPeripheral implements IPeripheral
|
||||
{
|
||||
private final TileSpeaker m_speaker;
|
||||
private long m_clock;
|
||||
private long m_lastPlayTime;
|
||||
private int m_notesThisTick;
|
||||
private final AtomicInteger m_notesThisTick;
|
||||
|
||||
public SpeakerPeripheral()
|
||||
{
|
||||
m_clock = 0;
|
||||
m_lastPlayTime = 0;
|
||||
m_notesThisTick = 0;
|
||||
this( null );
|
||||
}
|
||||
|
||||
SpeakerPeripheral(TileSpeaker speaker)
|
||||
SpeakerPeripheral( TileSpeaker speaker )
|
||||
{
|
||||
this();
|
||||
m_clock = 0;
|
||||
m_lastPlayTime = 0;
|
||||
m_notesThisTick = new AtomicInteger( );
|
||||
m_speaker = speaker;
|
||||
}
|
||||
|
||||
public synchronized void update()
|
||||
public void update()
|
||||
{
|
||||
m_clock++;
|
||||
m_notesThisTick = 0;
|
||||
m_notesThisTick.set( 0 );
|
||||
}
|
||||
|
||||
public World getWorld()
|
||||
@@ -59,9 +62,9 @@ public class SpeakerPeripheral implements IPeripheral {
|
||||
return m_speaker.getPos();
|
||||
}
|
||||
|
||||
public synchronized boolean madeSound(long ticks)
|
||||
public boolean madeSound(long ticks)
|
||||
{
|
||||
return (m_clock - m_lastPlayTime <= ticks) ;
|
||||
return m_clock - m_lastPlayTime <= ticks;
|
||||
}
|
||||
|
||||
/* IPeripheral implementation */
|
||||
@@ -69,15 +72,9 @@ public class SpeakerPeripheral implements IPeripheral {
|
||||
@Override
|
||||
public boolean equals( IPeripheral other )
|
||||
{
|
||||
if( other != null && other instanceof SpeakerPeripheral )
|
||||
{
|
||||
SpeakerPeripheral otherSpeaker = (SpeakerPeripheral) other;
|
||||
return otherSpeaker.m_speaker == m_speaker;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if( other == this ) return true;
|
||||
if( !(other instanceof SpeakerPeripheral) ) return false;
|
||||
return m_speaker == ((SpeakerPeripheral) other).m_speaker;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -92,8 +89,8 @@ public class SpeakerPeripheral implements IPeripheral {
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
return new String[] {
|
||||
"playSound", // Plays sound at resourceLocator
|
||||
"playNote" // Plays note
|
||||
"playSound", // Plays sound at resourceLocator
|
||||
"playNote" // Plays note
|
||||
};
|
||||
}
|
||||
|
||||
@@ -105,18 +102,22 @@ public class SpeakerPeripheral implements IPeripheral {
|
||||
// playSound
|
||||
case 0:
|
||||
{
|
||||
return playSound(args, context, false);
|
||||
String name = getString( args, 0 );
|
||||
float volume = (float) optReal( args, 1, 1.0 );
|
||||
float pitch = (float) optReal( args, 2, 1.0 );
|
||||
|
||||
return new Object[] { playSound( context, name, volume, pitch, false ) };
|
||||
}
|
||||
|
||||
// playNote
|
||||
case 1:
|
||||
{
|
||||
return playNote(args, context);
|
||||
return playNote( args, context );
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
throw new LuaException("Method index out of range!");
|
||||
throw new LuaException( "Method index out of range!" );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -125,75 +126,53 @@ public class SpeakerPeripheral implements IPeripheral {
|
||||
@Nonnull
|
||||
private synchronized Object[] playNote( Object[] arguments, ILuaContext context ) throws LuaException
|
||||
{
|
||||
String name = getString(arguments, 0);
|
||||
String name = getString( arguments, 0 );
|
||||
float volume = (float) optReal( arguments, 1, 1.0 );
|
||||
float pitch = (float) optReal( arguments, 2, 1.0 );
|
||||
|
||||
String noteName = "block.note." + name;
|
||||
|
||||
// Check if sound exists
|
||||
if ( !SoundEvent.REGISTRY.containsKey( new ResourceLocation( "block.note." + name ) ) )
|
||||
// Check if the note exists
|
||||
if( !SoundEvent.REGISTRY.containsKey( new ResourceLocation( noteName ) ) )
|
||||
{
|
||||
throw new LuaException("Invalid instrument, \"" + arguments[0] + "\"!");
|
||||
throw new LuaException( "Invalid instrument, \"" + name + "\"!" );
|
||||
}
|
||||
|
||||
// If the resource location for note block notes changes, this method call will need to be updated
|
||||
Object[] returnValue = playSound(
|
||||
new Object[] {
|
||||
"block.note." + name,
|
||||
(double)Math.min( volume, 3f ),
|
||||
Math.pow( 2.0f, ( pitch - 12.0f ) / 12.0f)
|
||||
}, context, true
|
||||
);
|
||||
boolean success = playSound( context, noteName, volume, (float) Math.pow( 2.0, (pitch - 12.0) / 12.0 ), true );
|
||||
|
||||
if( returnValue[0] instanceof Boolean && (Boolean) returnValue[0] )
|
||||
{
|
||||
m_notesThisTick++;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
if( success ) m_notesThisTick.incrementAndGet();
|
||||
return new Object[] { success };
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private synchronized Object[] playSound( Object[] arguments, ILuaContext context, boolean isNote ) throws LuaException
|
||||
private synchronized boolean playSound( ILuaContext context, String name, float volume, float pitch, boolean isNote ) throws LuaException
|
||||
{
|
||||
String name = getString(arguments, 0);
|
||||
float volume = (float) optReal( arguments, 1, 1.0 );
|
||||
float pitch = (float) optReal( arguments, 2, 1.0 );
|
||||
|
||||
ResourceLocation resourceName = new ResourceLocation( name );
|
||||
|
||||
if( m_clock - m_lastPlayTime >= TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS || ( ( m_clock - m_lastPlayTime == 0 ) && ( m_notesThisTick < ComputerCraft.maxNotesPerTick ) && isNote ) )
|
||||
if( m_clock - m_lastPlayTime < TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS &&
|
||||
(!isNote || m_clock - m_lastPlayTime != 0 || m_notesThisTick.get() >= ComputerCraft.maxNotesPerTick) )
|
||||
{
|
||||
if( SoundEvent.REGISTRY.containsKey(resourceName) )
|
||||
{
|
||||
final World world = getWorld();
|
||||
final BlockPos pos = getPos();
|
||||
final ResourceLocation resource = resourceName;
|
||||
final float vol = volume;
|
||||
final float soundPitch = pitch;
|
||||
|
||||
context.issueMainThreadTask(new ILuaTask()
|
||||
{
|
||||
@Nullable
|
||||
@Override
|
||||
public Object[] execute()
|
||||
{
|
||||
world.playSound( null, pos, SoundEvent.REGISTRY.getObject( resource ), SoundCategory.RECORDS, Math.min( vol, 3f ), soundPitch );
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
m_lastPlayTime = m_clock;
|
||||
return new Object[]{true}; // Success, return true
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Object[]{false}; // Failed - sound not existent, return false
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Object[]{false}; // Failed - rate limited, return false
|
||||
// Rate limiting occurs when we've already played a sound within the last tick, or we've
|
||||
// played more notes than allowable within the current tick.
|
||||
return false;
|
||||
}
|
||||
|
||||
World world = getWorld();
|
||||
BlockPos pos = getPos();
|
||||
|
||||
context.issueMainThreadTask( () -> {
|
||||
MinecraftServer server = world.getMinecraftServer();
|
||||
if( server == null ) return null;
|
||||
|
||||
double x = pos.getX() + 0.5, y = pos.getY() + 0.5, z = pos.getZ() + 0.5;
|
||||
float adjVolume = Math.min( volume, 3.0f );
|
||||
server.getPlayerList().sendToAllNearExcept(
|
||||
null, x, y, z, adjVolume > 1.0f ? 16 * adjVolume : 16.0, world.provider.getDimension(),
|
||||
new SPacketCustomSound( name, SoundCategory.RECORDS, x, y, z, adjVolume, pitch )
|
||||
);
|
||||
return null;
|
||||
} );
|
||||
|
||||
m_lastPlayTime = m_clock;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -25,7 +25,7 @@ public class TileSpeaker extends TilePeripheralBase
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void update()
|
||||
public void update()
|
||||
{
|
||||
m_peripheral.update();
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I
|
||||
{
|
||||
setMaxStackSize( 1 );
|
||||
setHasSubtypes( true );
|
||||
setUnlocalizedName( "computercraft:pocket_computer" );
|
||||
setTranslationKey( "computercraft:pocket_computer" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getUnlocalizedName( @Nonnull ItemStack stack )
|
||||
public String getTranslationKey( @Nonnull ItemStack stack )
|
||||
{
|
||||
switch( getFamily( stack ) )
|
||||
{
|
||||
@@ -209,7 +209,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I
|
||||
@Override
|
||||
public String getItemStackDisplayName( @Nonnull ItemStack stack )
|
||||
{
|
||||
String baseString = getUnlocalizedName( stack );
|
||||
String baseString = getTranslationKey( stack );
|
||||
IPocketUpgrade upgrade = getUpgrade( stack );
|
||||
if( upgrade != null )
|
||||
{
|
||||
|
@@ -48,17 +48,19 @@ import net.minecraftforge.registries.IForgeRegistry;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
{
|
||||
private Map<Integer, ITurtleUpgrade> m_legacyTurtleUpgrades;
|
||||
private Map<String, ITurtleUpgrade> m_turtleUpgrades;
|
||||
|
||||
private Consumer<ItemStack> dropConsumer;
|
||||
private Function<ItemStack, ItemStack> dropConsumer;
|
||||
private List<ItemStack> remainingDrops;
|
||||
private WeakReference<World> dropWorld;
|
||||
private BlockPos dropPos;
|
||||
private AxisAlignedBB dropBounds;
|
||||
@@ -200,9 +202,10 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDropConsumer( Entity entity, Consumer<ItemStack> consumer )
|
||||
public void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
dropConsumer = consumer;
|
||||
remainingDrops = new ArrayList<>();
|
||||
dropEntity = new WeakReference<>( entity );
|
||||
dropWorld = new WeakReference<>( entity.world );
|
||||
dropPos = null;
|
||||
@@ -212,9 +215,10 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> consumer )
|
||||
public void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
dropConsumer = consumer;
|
||||
remainingDrops = new ArrayList<>();
|
||||
dropEntity = null;
|
||||
dropWorld = new WeakReference<>( world );
|
||||
dropPos = pos;
|
||||
@@ -222,7 +226,7 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearDropConsumer()
|
||||
public List<ItemStack> clearDropConsumer()
|
||||
{
|
||||
if( dropEntity != null )
|
||||
{
|
||||
@@ -232,17 +236,22 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
entity.captureDrops = false;
|
||||
if( entity.capturedDrops != null )
|
||||
{
|
||||
for( EntityItem entityItem : entity.capturedDrops ) dropConsumer.accept( entityItem.getItem() );
|
||||
for( EntityItem entityItem : entity.capturedDrops ) handleDrops( entityItem.getItem() );
|
||||
entity.capturedDrops.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ItemStack> remainingStacks = remainingDrops;
|
||||
|
||||
dropConsumer = null;
|
||||
remainingDrops = null;
|
||||
dropEntity = null;
|
||||
dropWorld = null;
|
||||
dropPos = null;
|
||||
dropBounds = null;
|
||||
|
||||
return remainingStacks;
|
||||
}
|
||||
|
||||
private void registerTurtleUpgradeInternal( ITurtleUpgrade upgrade )
|
||||
@@ -413,10 +422,10 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
// We have to use mappings.getAllMappings() as the mod ID is upper case but the domain lower.
|
||||
for( RegistryEvent.MissingMappings.Mapping<Item> mapping : mappings.getAllMappings() )
|
||||
{
|
||||
String domain = mapping.key.getResourceDomain();
|
||||
String domain = mapping.key.getNamespace();
|
||||
if( !domain.equalsIgnoreCase( ComputerCraft.MOD_ID ) ) continue;
|
||||
|
||||
String key = mapping.key.getResourcePath();
|
||||
String key = mapping.key.getPath();
|
||||
if( key.equalsIgnoreCase( "CC-Turtle" ) )
|
||||
{
|
||||
mapping.remap( Item.getItemFromBlock( ComputerCraft.Blocks.turtle ) );
|
||||
@@ -438,10 +447,10 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
// We have to use mappings.getAllMappings() as the mod ID is upper case but the domain lower.
|
||||
for( RegistryEvent.MissingMappings.Mapping<Block> mapping : mappings.getAllMappings() )
|
||||
{
|
||||
String domain = mapping.key.getResourceDomain();
|
||||
String domain = mapping.key.getNamespace();
|
||||
if( !domain.equalsIgnoreCase( ComputerCraft.MOD_ID ) ) continue;
|
||||
|
||||
String key = mapping.key.getResourcePath();
|
||||
String key = mapping.key.getPath();
|
||||
if( key.equalsIgnoreCase( "CC-Turtle" ) )
|
||||
{
|
||||
mapping.remap( ComputerCraft.Blocks.turtle );
|
||||
@@ -471,6 +480,12 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
MinecraftForge.EVENT_BUS.register( handlers );
|
||||
}
|
||||
|
||||
private void handleDrops(ItemStack stack)
|
||||
{
|
||||
ItemStack remaining = dropConsumer.apply(stack);
|
||||
if (!remaining.isEmpty()) remainingDrops.add(remaining);
|
||||
}
|
||||
|
||||
private class ForgeHandlers
|
||||
{
|
||||
@SubscribeEvent(priority = EventPriority.LOWEST)
|
||||
@@ -480,7 +495,7 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
if( dropEntity != null && event.getEntity() == dropEntity.get() )
|
||||
{
|
||||
List<EntityItem> drops = event.getDrops();
|
||||
for( EntityItem entityItem : drops ) dropConsumer.accept( entityItem.getItem() );
|
||||
for( EntityItem entityItem : drops ) handleDrops( entityItem.getItem() );
|
||||
drops.clear();
|
||||
}
|
||||
}
|
||||
@@ -494,7 +509,7 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
{
|
||||
for( ItemStack item : event.getDrops() )
|
||||
{
|
||||
if( event.getWorld().rand.nextFloat() < event.getDropChance() ) dropConsumer.accept( item );
|
||||
if( event.getWorld().rand.nextFloat() < event.getDropChance() ) handleDrops( item );
|
||||
}
|
||||
event.getDrops().clear();
|
||||
}
|
||||
@@ -507,7 +522,7 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
if( dropWorld != null && dropWorld.get() == event.getWorld() && event.getEntity() instanceof EntityItem
|
||||
&& dropBounds.contains( event.getEntity().getPositionVector() ) )
|
||||
{
|
||||
dropConsumer.accept( ((EntityItem) event.getEntity()).getItem() );
|
||||
handleDrops( ((EntityItem) event.getEntity()).getItem() );
|
||||
event.setCanceled( true );
|
||||
}
|
||||
}
|
||||
|
@@ -24,6 +24,7 @@ import dan200.computercraft.shared.computer.core.*;
|
||||
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
|
||||
import dan200.computercraft.shared.computer.items.ItemCommandComputer;
|
||||
import dan200.computercraft.shared.computer.items.ItemComputer;
|
||||
import dan200.computercraft.shared.integration.charset.IntegrationCharset;
|
||||
import dan200.computercraft.shared.media.common.DefaultMediaProvider;
|
||||
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
|
||||
import dan200.computercraft.shared.media.items.ItemDiskExpanded;
|
||||
@@ -76,6 +77,7 @@ import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
|
||||
import net.minecraftforge.fml.common.Loader;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
|
||||
@@ -83,6 +85,7 @@ import net.minecraftforge.fml.common.network.IGuiHandler;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
import net.minecraftforge.registries.IForgeRegistry;
|
||||
import pl.asie.charset.ModCharset;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.File;
|
||||
@@ -117,6 +120,8 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
{
|
||||
registerTileEntities();
|
||||
registerForgeHandlers();
|
||||
|
||||
if( Loader.isModLoaded( ModCharset.MODID ) ) IntegrationCharset.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -414,10 +419,10 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
// We have to use mappings.getAllMappings() as the mod ID is upper case but the domain lower.
|
||||
for( RegistryEvent.MissingMappings.Mapping<Item> mapping : mappings.getAllMappings() )
|
||||
{
|
||||
String domain = mapping.key.getResourceDomain();
|
||||
String domain = mapping.key.getNamespace();
|
||||
if( !domain.equalsIgnoreCase( ComputerCraft.MOD_ID ) ) continue;
|
||||
|
||||
String key = mapping.key.getResourcePath();
|
||||
String key = mapping.key.getPath();
|
||||
if( key.equalsIgnoreCase( "CC-Computer" ) )
|
||||
{
|
||||
mapping.remap( Item.getItemFromBlock( ComputerCraft.Blocks.computer ) );
|
||||
@@ -451,10 +456,10 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
// We have to use mappings.getAllMappings() as the mod ID is upper case but the domain lower.
|
||||
for( RegistryEvent.MissingMappings.Mapping<Block> mapping : mappings.getAllMappings() )
|
||||
{
|
||||
String domain = mapping.key.getResourceDomain();
|
||||
String domain = mapping.key.getNamespace();
|
||||
if( !domain.equalsIgnoreCase( ComputerCraft.MOD_ID ) ) continue;
|
||||
|
||||
String key = mapping.key.getResourcePath();
|
||||
String key = mapping.key.getPath();
|
||||
if( key.equalsIgnoreCase( "CC-Computer" ) )
|
||||
{
|
||||
mapping.remap( ComputerCraft.Blocks.computer );
|
||||
|
@@ -14,7 +14,9 @@ import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface ICCTurtleProxy
|
||||
{
|
||||
@@ -27,7 +29,7 @@ public interface ICCTurtleProxy
|
||||
ITurtleUpgrade getTurtleUpgrade( @Nonnull ItemStack item );
|
||||
void addAllUpgradedTurtles( NonNullList<ItemStack> list );
|
||||
|
||||
void setDropConsumer( Entity entity, Consumer<ItemStack> consumer );
|
||||
void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> consumer );
|
||||
void clearDropConsumer();
|
||||
void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer );
|
||||
void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer );
|
||||
List<ItemStack> clearDropConsumer();
|
||||
}
|
||||
|
@@ -48,7 +48,7 @@ public class BlockTurtle extends BlockComputerBase
|
||||
{
|
||||
super( Material.IRON );
|
||||
setHardness( 2.5f );
|
||||
setUnlocalizedName( "computercraft:turtle" );
|
||||
setTranslationKey( "computercraft:turtle" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( this.blockState.getBaseState()
|
||||
.withProperty( Properties.FACING, EnumFacing.NORTH )
|
||||
|
@@ -226,7 +226,7 @@ public class TurtleBrain implements ITurtleAccess
|
||||
public void readFromNBT( NBTTagCompound nbttagcompound )
|
||||
{
|
||||
// Read state
|
||||
m_direction = EnumFacing.getFront( nbttagcompound.getInteger( "dir" ) );
|
||||
m_direction = EnumFacing.byIndex( nbttagcompound.getInteger( "dir" ) );
|
||||
m_selectedSlot = nbttagcompound.getInteger( "selectedSlot" );
|
||||
if( nbttagcompound.hasKey( "fuelLevel" ) )
|
||||
{
|
||||
@@ -370,8 +370,8 @@ public class TurtleBrain implements ITurtleAccess
|
||||
// Write overlay
|
||||
if( m_overlay != null )
|
||||
{
|
||||
nbttagcompound.setString( "overlay_mod", m_overlay.getResourceDomain() );
|
||||
nbttagcompound.setString( "overlay_path", m_overlay.getResourcePath() );
|
||||
nbttagcompound.setString( "overlay_mod", m_overlay.getNamespace() );
|
||||
nbttagcompound.setString( "overlay_path", m_overlay.getPath() );
|
||||
}
|
||||
|
||||
// Write NBT
|
||||
@@ -429,8 +429,8 @@ public class TurtleBrain implements ITurtleAccess
|
||||
// Overlay
|
||||
if( m_overlay != null )
|
||||
{
|
||||
nbttagcompound.setString( "overlay_mod", m_overlay.getResourceDomain() );
|
||||
nbttagcompound.setString( "overlay_path", m_overlay.getResourcePath() );
|
||||
nbttagcompound.setString( "overlay_mod", m_overlay.getNamespace() );
|
||||
nbttagcompound.setString( "overlay_path", m_overlay.getPath() );
|
||||
}
|
||||
|
||||
// Animation
|
||||
@@ -505,7 +505,7 @@ public class TurtleBrain implements ITurtleAccess
|
||||
m_lastAnimationProgress = 0;
|
||||
}
|
||||
|
||||
m_direction = EnumFacing.getFront( nbttagcompound.getInteger( "direction" ) );
|
||||
m_direction = EnumFacing.byIndex( nbttagcompound.getInteger( "direction" ) );
|
||||
m_fuelLevel = nbttagcompound.getInteger( "fuelLevel" );
|
||||
}
|
||||
|
||||
@@ -998,9 +998,9 @@ public class TurtleBrain implements ITurtleAccess
|
||||
|
||||
double distance = -1.0 + getAnimationFraction( f );
|
||||
return new Vec3d(
|
||||
distance * dir.getFrontOffsetX(),
|
||||
distance * dir.getFrontOffsetY(),
|
||||
distance * dir.getFrontOffsetZ()
|
||||
distance * dir.getXOffset(),
|
||||
distance * dir.getYOffset(),
|
||||
distance * dir.getZOffset()
|
||||
);
|
||||
}
|
||||
default:
|
||||
@@ -1184,31 +1184,31 @@ public class TurtleBrain implements ITurtleAccess
|
||||
|
||||
float pushFrac = 1.0f - ((float)(m_animationProgress + 1) / (float)ANIM_DURATION);
|
||||
float push = Math.max( pushFrac + 0.0125f, 0.0f );
|
||||
if (moveDir.getFrontOffsetX() < 0)
|
||||
if (moveDir.getXOffset() < 0)
|
||||
{
|
||||
minX += moveDir.getFrontOffsetX() * push;
|
||||
minX += moveDir.getXOffset() * push;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxX -= moveDir.getFrontOffsetX() * push;
|
||||
maxX -= moveDir.getXOffset() * push;
|
||||
}
|
||||
|
||||
if (moveDir.getFrontOffsetY() < 0)
|
||||
if (moveDir.getYOffset() < 0)
|
||||
{
|
||||
minY += moveDir.getFrontOffsetY() * push;
|
||||
minY += moveDir.getYOffset() * push;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxY -= moveDir.getFrontOffsetY() * push;
|
||||
maxY -= moveDir.getYOffset() * push;
|
||||
}
|
||||
|
||||
if (moveDir.getFrontOffsetZ() < 0)
|
||||
if (moveDir.getZOffset() < 0)
|
||||
{
|
||||
minZ += moveDir.getFrontOffsetZ() * push;
|
||||
minZ += moveDir.getZOffset() * push;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxZ -= moveDir.getFrontOffsetZ() * push;
|
||||
maxZ -= moveDir.getZOffset() * push;
|
||||
}
|
||||
|
||||
AxisAlignedBB aabb = new AxisAlignedBB( minX, minY, minZ, maxX, maxY, maxZ );
|
||||
@@ -1216,9 +1216,9 @@ public class TurtleBrain implements ITurtleAccess
|
||||
if( !list.isEmpty() )
|
||||
{
|
||||
double pushStep = 1.0f / ANIM_DURATION;
|
||||
double pushStepX = moveDir.getFrontOffsetX() * pushStep;
|
||||
double pushStepY = moveDir.getFrontOffsetY() * pushStep;
|
||||
double pushStepZ = moveDir.getFrontOffsetZ() * pushStep;
|
||||
double pushStepX = moveDir.getXOffset() * pushStep;
|
||||
double pushStepY = moveDir.getYOffset() * pushStep;
|
||||
double pushStepZ = moveDir.getZOffset() * pushStep;
|
||||
for (Entity entity : list)
|
||||
{
|
||||
entity.move( MoverType.PISTON, pushStepX, pushStepY, pushStepZ );
|
||||
|
@@ -126,7 +126,7 @@ public class TurtleCompareCommand implements ITurtleCommand
|
||||
{
|
||||
return TurtleCommandResult.success();
|
||||
}
|
||||
else if( selectedStack.getUnlocalizedName().equals( lookAtStack.getUnlocalizedName() ) )
|
||||
else if( selectedStack.getTranslationKey().equals( lookAtStack.getTranslationKey() ) )
|
||||
{
|
||||
return TurtleCommandResult.success();
|
||||
}
|
||||
|
@@ -87,9 +87,9 @@ public class TurtleMoveCommand implements ITurtleCommand
|
||||
if( entityBB != null )
|
||||
{
|
||||
AxisAlignedBB pushedBB = entityBB.offset(
|
||||
direction.getFrontOffsetX(),
|
||||
direction.getFrontOffsetY(),
|
||||
direction.getFrontOffsetZ()
|
||||
direction.getXOffset(),
|
||||
direction.getYOffset(),
|
||||
direction.getZOffset()
|
||||
);
|
||||
if( !oldWorld.getCollisionBoxes( null, pushedBB ).isEmpty() )
|
||||
{
|
||||
|
@@ -30,7 +30,6 @@ import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraftforge.common.ForgeHooks;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
||||
@@ -38,6 +37,7 @@ import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
|
||||
public class TurtlePlaceCommand implements ITurtleCommand
|
||||
{
|
||||
@@ -194,9 +194,9 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
// Stop intersection with the turtle itself
|
||||
if( turtle.getPosition().equals( position ) )
|
||||
{
|
||||
turtlePlayer.posX += 0.48 * direction.getFrontOffsetX();
|
||||
turtlePlayer.posY += 0.48 * direction.getFrontOffsetY();
|
||||
turtlePlayer.posZ += 0.48 * direction.getFrontOffsetZ();
|
||||
turtlePlayer.posX += 0.48 * direction.getXOffset();
|
||||
turtlePlayer.posY += 0.48 * direction.getYOffset();
|
||||
turtlePlayer.posZ += 0.48 * direction.getZOffset();
|
||||
}
|
||||
|
||||
if( direction.getAxis() != EnumFacing.Axis.Y )
|
||||
@@ -241,14 +241,10 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
// Start claiming entity drops
|
||||
Entity hitEntity = hit.getKey();
|
||||
Vec3d hitPos = hit.getValue();
|
||||
ComputerCraft.setDropConsumer( hitEntity, ( drop ) ->
|
||||
{
|
||||
ItemStack remainder = InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() );
|
||||
if( !remainder.isEmpty() )
|
||||
{
|
||||
WorldUtil.dropItemStack( remainder, world, position, turtle.getDirection().getOpposite() );
|
||||
}
|
||||
} );
|
||||
ComputerCraft.setDropConsumer(
|
||||
hitEntity,
|
||||
drop -> InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() )
|
||||
);
|
||||
|
||||
// Place on the entity
|
||||
boolean placed = false;
|
||||
@@ -285,7 +281,11 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
}
|
||||
|
||||
// Stop claiming drops
|
||||
ComputerCraft.clearDropConsumer();
|
||||
List<ItemStack> remainingDrops = ComputerCraft.clearDropConsumer();
|
||||
for( ItemStack remaining : remainingDrops )
|
||||
{
|
||||
WorldUtil.dropItemStack( remaining, world, position, turtle.getDirection().getOpposite() );
|
||||
}
|
||||
|
||||
// Put everything we collected into the turtles inventory, then return
|
||||
ItemStack remainder = turtlePlayer.unloadInventory( turtle );
|
||||
@@ -367,9 +367,9 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
orientPlayer( turtle, turtlePlayer, playerPosition, playerDir );
|
||||
|
||||
// Calculate where the turtle would hit the block
|
||||
float hitX = 0.5f + side.getFrontOffsetX() * 0.5f;
|
||||
float hitY = 0.5f + side.getFrontOffsetY() * 0.5f;
|
||||
float hitZ = 0.5f + side.getFrontOffsetZ() * 0.5f;
|
||||
float hitX = 0.5f + side.getXOffset() * 0.5f;
|
||||
float hitY = 0.5f + side.getYOffset() * 0.5f;
|
||||
float hitZ = 0.5f + side.getZOffset() * 0.5f;
|
||||
if( Math.abs( hitY - 0.5f ) < 0.01f )
|
||||
{
|
||||
hitY = 0.45f;
|
||||
|
@@ -15,7 +15,7 @@ public class ItemTurtleAdvanced extends ItemTurtleNormal
|
||||
public ItemTurtleAdvanced( Block block )
|
||||
{
|
||||
super( block );
|
||||
setUnlocalizedName( "computercraft:advanced_turtle" );
|
||||
setTranslationKey( "computercraft:advanced_turtle" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
|
@@ -114,7 +114,7 @@ public abstract class ItemTurtleBase extends ItemComputerBase implements ITurtle
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getUnlocalizedName( @Nonnull ItemStack stack )
|
||||
public String getTranslationKey( @Nonnull ItemStack stack )
|
||||
{
|
||||
ComputerFamily family = getFamily( stack );
|
||||
switch( family )
|
||||
@@ -139,7 +139,7 @@ public abstract class ItemTurtleBase extends ItemComputerBase implements ITurtle
|
||||
@Override
|
||||
public String getItemStackDisplayName( @Nonnull ItemStack stack )
|
||||
{
|
||||
String baseString = getUnlocalizedName( stack );
|
||||
String baseString = getTranslationKey( stack );
|
||||
ITurtleUpgrade left = getUpgrade( stack, TurtleSide.Left );
|
||||
ITurtleUpgrade right = getUpgrade( stack, TurtleSide.Right );
|
||||
if( left != null && right != null )
|
||||
|
@@ -23,7 +23,7 @@ public class ItemTurtleLegacy extends ItemTurtleBase
|
||||
public ItemTurtleLegacy( Block block )
|
||||
{
|
||||
super( block );
|
||||
setUnlocalizedName( "computercraft:turtle" );
|
||||
setTranslationKey( "computercraft:turtle" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
|
@@ -24,7 +24,7 @@ public class ItemTurtleNormal extends ItemTurtleBase
|
||||
public ItemTurtleNormal( Block block )
|
||||
{
|
||||
super( block );
|
||||
setUnlocalizedName( "computercraft:turtle" );
|
||||
setTranslationKey( "computercraft:turtle" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
}
|
||||
|
||||
@@ -72,8 +72,8 @@ public class ItemTurtleNormal extends ItemTurtleBase
|
||||
}
|
||||
if( overlay != null )
|
||||
{
|
||||
nbt.setString( "overlay_mod", overlay.getResourceDomain() );
|
||||
nbt.setString( "overlay_path", overlay.getResourcePath() );
|
||||
nbt.setString( "overlay_mod", overlay.getNamespace() );
|
||||
nbt.setString( "overlay_path", overlay.getPath() );
|
||||
}
|
||||
stack.setTagCompound( nbt );
|
||||
|
||||
|
@@ -42,9 +42,8 @@ import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.vecmath.Matrix4f;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TurtleTool implements ITurtleUpgrade
|
||||
{
|
||||
@@ -103,7 +102,7 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@SideOnly(Side.CLIENT)
|
||||
@SideOnly( Side.CLIENT )
|
||||
public Pair<IBakedModel, Matrix4f> getModel( ITurtleAccess turtle, @Nonnull TurtleSide side )
|
||||
{
|
||||
float xOffset = (side == TurtleSide.Left) ? -0.40625f : 0.40625f;
|
||||
@@ -154,7 +153,7 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
{
|
||||
return 3.0f;
|
||||
}
|
||||
|
||||
|
||||
private TurtleCommandResult attack( final ITurtleAccess turtle, EnumFacing direction, TurtleSide side )
|
||||
{
|
||||
// Create a fake player, and orient it appropriately
|
||||
@@ -185,10 +184,9 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
{
|
||||
return TurtleCommandResult.failure( attackEvent.getFailureMessage() );
|
||||
}
|
||||
|
||||
|
||||
// Start claiming entity drops
|
||||
List<ItemStack> extra = new ArrayList<>( );
|
||||
ComputerCraft.setDropConsumer( hitEntity, turtleDropConsumer( turtle, extra ) );
|
||||
ComputerCraft.setDropConsumer( hitEntity, turtleDropConsumer( turtle ) );
|
||||
|
||||
// Attack the entity
|
||||
boolean attacked = false;
|
||||
@@ -220,7 +218,7 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
}
|
||||
|
||||
// Stop claiming drops
|
||||
stopConsuming( turtle, extra );
|
||||
stopConsuming( turtle );
|
||||
|
||||
// Put everything we collected into the turtles inventory, then return
|
||||
if( attacked )
|
||||
@@ -232,7 +230,7 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
|
||||
return TurtleCommandResult.failure( "Nothing to attack here" );
|
||||
}
|
||||
|
||||
|
||||
private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction, TurtleSide side )
|
||||
{
|
||||
// Get ready to dig
|
||||
@@ -277,8 +275,7 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
}
|
||||
|
||||
// Consume the items the block drops
|
||||
List<ItemStack> extra = new ArrayList<>( );
|
||||
ComputerCraft.setDropConsumer( world, blockPosition, turtleDropConsumer( turtle, extra ) );
|
||||
ComputerCraft.setDropConsumer( world, blockPosition, turtleDropConsumer( turtle ) );
|
||||
|
||||
TileEntity tile = world.getTileEntity( blockPosition );
|
||||
|
||||
@@ -291,13 +288,13 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
// Destroy the block
|
||||
boolean canHarvest = state.getBlock().canHarvestBlock( world, blockPosition, turtlePlayer );
|
||||
boolean canBreak = state.getBlock().removedByPlayer( state, world, blockPosition, turtlePlayer, canHarvest );
|
||||
if( canBreak ) state.getBlock().onBlockDestroyedByPlayer( world, blockPosition, state );
|
||||
if( canBreak ) state.getBlock().onPlayerDestroy( world, blockPosition, state );
|
||||
if( canHarvest )
|
||||
{
|
||||
state.getBlock().harvestBlock( world, turtlePlayer, blockPosition, state, tile, turtlePlayer.getHeldItemMainhand() );
|
||||
}
|
||||
|
||||
stopConsuming( turtle, extra );
|
||||
stopConsuming( turtle );
|
||||
|
||||
// Remember the previous block
|
||||
if( turtle instanceof TurtleBrain )
|
||||
@@ -312,18 +309,14 @@ public class TurtleTool implements ITurtleUpgrade
|
||||
return TurtleCommandResult.failure( "Nothing to dig here" );
|
||||
}
|
||||
|
||||
private Consumer<ItemStack> turtleDropConsumer( ITurtleAccess turtle, List<ItemStack> extra )
|
||||
private Function<ItemStack, ItemStack> turtleDropConsumer( ITurtleAccess turtle )
|
||||
{
|
||||
return ( drop ) ->
|
||||
{
|
||||
ItemStack remainder = InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() );
|
||||
if( !remainder.isEmpty() ) extra.add( remainder );
|
||||
};
|
||||
return drop -> InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() );
|
||||
}
|
||||
|
||||
private void stopConsuming( ITurtleAccess turtle, List<ItemStack> extra )
|
||||
private void stopConsuming( ITurtleAccess turtle )
|
||||
{
|
||||
ComputerCraft.clearDropConsumer();
|
||||
List<ItemStack> extra = ComputerCraft.clearDropConsumer();
|
||||
for( ItemStack remainder : extra )
|
||||
{
|
||||
WorldUtil.dropItemStack( remainder, turtle.getWorld(), turtle.getPosition(), turtle.getDirection().getOpposite() );
|
||||
|
@@ -24,7 +24,7 @@ public class CreativeTabMain extends CreativeTabs
|
||||
@Nonnull
|
||||
@Override
|
||||
@SideOnly(Side.CLIENT)
|
||||
public ItemStack getTabIconItem()
|
||||
public ItemStack createIcon()
|
||||
{
|
||||
return new ItemStack( ComputerCraft.Blocks.computer );
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class CreativeTabMain extends CreativeTabs
|
||||
@Nonnull
|
||||
@Override
|
||||
@SideOnly(Side.CLIENT)
|
||||
public String getTranslatedTabLabel()
|
||||
public String getTranslationKey()
|
||||
{
|
||||
return getTabLabel();
|
||||
}
|
||||
|
@@ -21,14 +21,14 @@ public class CreativeTabTreasure extends CreativeTabs
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ItemStack getTabIconItem()
|
||||
public ItemStack createIcon()
|
||||
{
|
||||
return new ItemStack( ComputerCraft.Items.treasureDisk );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getTranslatedTabLabel()
|
||||
public String getTranslationKey()
|
||||
{
|
||||
return getTabLabel();
|
||||
}
|
||||
|
@@ -73,13 +73,13 @@ public class InventoryUtil
|
||||
|
||||
// Look for entity with inventory
|
||||
Vec3d vecStart = new Vec3d(
|
||||
pos.getX() + 0.5 + 0.6 * side.getFrontOffsetX(),
|
||||
pos.getY() + 0.5 + 0.6 * side.getFrontOffsetY(),
|
||||
pos.getZ() + 0.5 + 0.6 * side.getFrontOffsetZ()
|
||||
pos.getX() + 0.5 + 0.6 * side.getXOffset(),
|
||||
pos.getY() + 0.5 + 0.6 * side.getYOffset(),
|
||||
pos.getZ() + 0.5 + 0.6 * side.getZOffset()
|
||||
);
|
||||
EnumFacing dir = side.getOpposite();
|
||||
Vec3d vecDir = new Vec3d(
|
||||
dir.getFrontOffsetX(), dir.getFrontOffsetY(), dir.getFrontOffsetZ()
|
||||
dir.getXOffset(), dir.getYOffset(), dir.getZOffset()
|
||||
);
|
||||
Pair<Entity, Vec3d> hit = WorldUtil.rayTraceEntities( world, vecStart, vecDir, 1.1 );
|
||||
if( hit != null )
|
||||
|
@@ -43,14 +43,14 @@ public class WorldUtil
|
||||
|
||||
public static Pair<Entity, Vec3d> rayTraceEntities( World world, Vec3d vecStart, Vec3d vecDir, double distance )
|
||||
{
|
||||
Vec3d vecEnd = vecStart.addVector( vecDir.x * distance, vecDir.y * distance, vecDir.z * distance );
|
||||
Vec3d vecEnd = vecStart.add( vecDir.x * distance, vecDir.y * distance, vecDir.z * distance );
|
||||
|
||||
// Raycast for blocks
|
||||
RayTraceResult result = world.rayTraceBlocks( vecStart.addVector(0.0,0.0,0.0), vecEnd.addVector(0.0,0.0,0.0) );
|
||||
RayTraceResult result = world.rayTraceBlocks( vecStart, vecEnd );
|
||||
if( result != null && result.typeOfHit == RayTraceResult.Type.BLOCK )
|
||||
{
|
||||
distance = vecStart.distanceTo( result.hitVec );
|
||||
vecEnd = vecStart.addVector( vecDir.x * distance, vecDir.y * distance, vecDir.z * distance );
|
||||
vecEnd = vecStart.add( vecDir.x * distance, vecDir.y * distance, vecDir.z * distance );
|
||||
}
|
||||
|
||||
// Check for entities
|
||||
@@ -114,7 +114,7 @@ public class WorldUtil
|
||||
}
|
||||
if( closest != null && closestDist <= distance )
|
||||
{
|
||||
Vec3d closestPos = vecStart.addVector( vecDir.x * closestDist, vecDir.y * closestDist, vecDir.z * closestDist );
|
||||
Vec3d closestPos = vecStart.add( vecDir.x * closestDist, vecDir.y * closestDist, vecDir.z * closestDist );
|
||||
return Pair.of( closest, closestPos );
|
||||
}
|
||||
return null;
|
||||
@@ -128,7 +128,7 @@ public class WorldUtil
|
||||
public static Vec3d getRayEnd( EntityPlayer player) {
|
||||
double reach = player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
|
||||
Vec3d look = player.getLookVec();
|
||||
return getRayStart( player ).addVector( look.x * reach, look.y * reach, look.z * reach );
|
||||
return getRayStart( player ).add( look.x * reach, look.y * reach, look.z * reach );
|
||||
}
|
||||
|
||||
public static boolean isVecInsideInclusive(AxisAlignedBB bb , Vec3d vec) {
|
||||
@@ -147,9 +147,9 @@ public class WorldUtil
|
||||
double zDir;
|
||||
if( direction != null )
|
||||
{
|
||||
xDir = direction.getFrontOffsetX();
|
||||
yDir = direction.getFrontOffsetY();
|
||||
zDir = direction.getFrontOffsetZ();
|
||||
xDir = direction.getXOffset();
|
||||
yDir = direction.getYOffset();
|
||||
zDir = direction.getZOffset();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
67
src/main/resources/assets/computercraft/lang/de_de.lang
Normal file
67
src/main/resources/assets/computercraft/lang/de_de.lang
Normal file
@@ -0,0 +1,67 @@
|
||||
tile.computercraft:computer.name=Computer
|
||||
tile.computercraft:advanced_computer.name=Erweiterter Computer
|
||||
tile.computercraft:drive.name=Diskettenlaufwerk
|
||||
tile.computercraft:printer.name=Drucker
|
||||
tile.computercraft:monitor.name=Monitor
|
||||
tile.computercraft:advanced_monitor.name=Erweiterter Monitor
|
||||
tile.computercraft:wireless_modem.name=Kabelloses Modem
|
||||
tile.computercraft:wired_modem.name=Kabelmodem
|
||||
tile.computercraft:cable.name=Netzwerkkabel
|
||||
tile.computercraft:command_computer.name=Befehlscomputer
|
||||
tile.computercraft:advanced_modem.name=Endermodem
|
||||
tile.computercraft:speaker.name=Lautsprecher
|
||||
|
||||
tile.computercraft:turtle.name=Turtle
|
||||
tile.computercraft:turtle.upgraded.name=Turtle (%s)
|
||||
tile.computercraft:turtle.upgraded_twice.name=Turtle (%s, %s)
|
||||
tile.computercraft:advanced_turtle.name=Erweiterte Turtle
|
||||
tile.computercraft:advanced_turtle.upgraded.name=Erweiterte Turtle (%s)
|
||||
tile.computercraft:advanced_turtle.upgraded_twice.name=Erweiterte Turtle (%s, %s)
|
||||
|
||||
item.computercraft:disk.name=Diskette
|
||||
item.computercraft:treasure_disk.name=Diskette
|
||||
item.computercraft:page.name=Gedruckte Seite
|
||||
item.computercraft:pages.name=Gedruckte Seiten
|
||||
item.computercraft:book.name=Gedrucktes Buch
|
||||
|
||||
item.computercraft:pocket_computer.name=Taschencomputer
|
||||
item.computercraft:pocket_computer.upgraded.name=Taschencomputer (%s)
|
||||
item.computercraft:advanced_pocket_computer.name=Erweiterter Taschencomputer
|
||||
item.computercraft:advanced_pocket_computer.upgraded.name=Erweiterter Taschencomputer (%s)
|
||||
|
||||
upgrade.minecraft:diamond_sword.adjective=Nahkampf
|
||||
upgrade.minecraft:diamond_shovel.adjective=Graben
|
||||
upgrade.minecraft:diamond_pickaxe.adjective=Bergbau
|
||||
upgrade.minecraft:diamond_axe.adjective=Holzfällen
|
||||
upgrade.minecraft:diamond_hoe.adjective=Ackerbau
|
||||
upgrade.computercraft:wireless_modem.adjective=Kabellos
|
||||
upgrade.minecraft:crafting_table.adjective=Fabrizierung
|
||||
upgrade.computercraft:advanced_modem.adjective=Ender
|
||||
upgrade.computercraft:speaker.adjective=Laut
|
||||
|
||||
gui.computercraft:wired_modem.peripheral_connected=Peripheriegerät "%s" mit dem Netzwerk verbunden
|
||||
gui.computercraft:wired_modem.peripheral_disconnected=Peripheriegerät "%s" vom Netzwerk getrennt
|
||||
|
||||
gui.computercraft:config.http_enable=HTTP-API aktivieren
|
||||
gui.computercraft:config.http_websocket_enable=HTTP-Websockets aktivieren
|
||||
gui.computercraft:config.http_whitelist=HTTP-Whitelist
|
||||
gui.computercraft:config.http_blacklist=HTTP-Blacklist
|
||||
gui.computercraft:config.disable_lua51_features=Lua 5.1-Funktionen deaktivieren
|
||||
gui.computercraft:config.default_computer_settings=Computer-Standardeinstellungen
|
||||
gui.computercraft:config.debug_enable=Debug-Library aktivieren
|
||||
gui.computercraft:config.log_peripheral_errors=Peripheriefehler loggen
|
||||
gui.computercraft:config.enable_command_block=Befehlsblöcke als Peripheriegerät erlauben
|
||||
gui.computercraft:config.modem_range=Modemreichweite (normal)
|
||||
gui.computercraft:config.modem_high_altitude_range=Modemreichweite (in großer Höhe)
|
||||
gui.computercraft:config.modem_range_during_storm=Modemreichweite (bei schlechtem Wetter)
|
||||
gui.computercraft:config.modem_high_altitude_range_during_storm=Modemreichweite (in großer Höhe bei schlechtem Wetter)
|
||||
gui.computercraft:config.computer_space_limit=Speicherplatz von Computern (bytes)
|
||||
gui.computercraft:config.floppy_space_limit=Speicherplatz von Disketten (bytes)
|
||||
gui.computercraft:config.turtles_need_fuel=Treibstoffverbrauch aktivieren
|
||||
gui.computercraft:config.turtle_fuel_limit=Treibstofflimit von Turtles
|
||||
gui.computercraft:config.advanced_turtle_fuel_limit=Treibstofflimit von Erweiterten Turtles
|
||||
gui.computercraft:config.turtles_obey_block_protection=Turtles unterliegen Blockschutzrichtlinien
|
||||
gui.computercraft:config.turtles_can_push=Turtles können Entities bewegen
|
||||
gui.computercraft:config.turtle_disabled_actions=Deaktivierte Turtle-Aktionen
|
||||
gui.computercraft:config.maximum_files_open=Maximalanzahl an gleichzeitig offenen Dateien je Computer
|
||||
gui.computercraft:config.max_notes_per_tick=Maximalanzahl an Noten, die ein Computer gleichzeitig spielen kann
|
@@ -18,9 +18,6 @@ if _VERSION == "Lua 5.1" then
|
||||
if env ~= nil and type( env) ~= "table" then
|
||||
error( "bad argument #4 (expected table, got " .. type( env ) .. ")", 2 )
|
||||
end
|
||||
if mode ~= nil and mode ~= "t" then
|
||||
error( "Binary chunk loading prohibited", 2 )
|
||||
end
|
||||
local ok, p1, p2 = pcall( function()
|
||||
if type(x) == "string" then
|
||||
local result, err = nativeloadstring( x, name )
|
||||
|
@@ -1,202 +1,249 @@
|
||||
-- Definition for the IO API
|
||||
local typeOf = _G.type
|
||||
|
||||
local g_defaultInput = {
|
||||
bFileHandle = true,
|
||||
bClosed = false,
|
||||
close = function( self )
|
||||
end,
|
||||
read = function( self, _sFormat )
|
||||
if _sFormat and _sFormat ~= "*l" then
|
||||
error( "Unsupported format" )
|
||||
end
|
||||
return _G.read()
|
||||
end,
|
||||
lines = function( self )
|
||||
return function()
|
||||
return _G.read()
|
||||
end
|
||||
end,
|
||||
}
|
||||
--- If we return nil then close the file, as we've reached the end.
|
||||
-- We use this weird wrapper function as we wish to preserve the varargs
|
||||
local function checkResult(handle, ...)
|
||||
if ... == nil and handle._autoclose and not handle._closed then handle:close() end
|
||||
return ...
|
||||
end
|
||||
|
||||
local g_defaultOutput = {
|
||||
bFileHandle = true,
|
||||
bClosed = false,
|
||||
close = function( self )
|
||||
end,
|
||||
write = function( self, ... )
|
||||
local nLimit = select("#", ... )
|
||||
for n = 1, nLimit do
|
||||
_G.write( select( n, ... ) )
|
||||
local handleMetatable
|
||||
handleMetatable = {
|
||||
__name = "FILE*",
|
||||
__tostring = function(self)
|
||||
if self._closed then
|
||||
return "file (closed)"
|
||||
else
|
||||
local hash = tostring(self._handle):match("table: (%x+)")
|
||||
return "file (" .. hash .. ")"
|
||||
end
|
||||
end,
|
||||
flush = function( self )
|
||||
end,
|
||||
end,
|
||||
__index = {
|
||||
close = function(self)
|
||||
if typeOf(self) ~= "table" or getmetatable(self) ~= handleMetatable then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(self) .. ")", 2)
|
||||
end
|
||||
if self._closed then error("attempt to use a closed file", 2) end
|
||||
|
||||
local handle = self._handle
|
||||
if handle.close then
|
||||
self._closed = true
|
||||
handle.close()
|
||||
return true
|
||||
else
|
||||
return nil, "attempt to close standard stream"
|
||||
end
|
||||
end,
|
||||
flush = function(self)
|
||||
if typeOf(self) ~= "table" or getmetatable(self) ~= handleMetatable then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(self) .. ")", 2)
|
||||
end
|
||||
if self._closed then error("attempt to use a closed file", 2) end
|
||||
|
||||
local handle = self._handle
|
||||
if handle.flush then handle.flush() end
|
||||
end,
|
||||
lines = function(self, ...)
|
||||
if typeOf(self) ~= "table" or getmetatable(self) ~= handleMetatable then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(self) .. ")", 2)
|
||||
end
|
||||
if self._closed then error("attempt to use a closed file", 2) end
|
||||
|
||||
local handle = self._handle
|
||||
if not handle.read then return nil, "file is not readable" end
|
||||
|
||||
local args = table.pack(...)
|
||||
return function() return checkResult(self, self:read(table.unpack(args, 1, args.n))) end
|
||||
end,
|
||||
read = function(self, ...)
|
||||
if typeOf(self) ~= "table" or getmetatable(self) ~= handleMetatable then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(self) .. ")", 2)
|
||||
end
|
||||
if self._closed then error("attempt to use a closed file", 2) end
|
||||
|
||||
local handle = self._handle
|
||||
if not handle.read and not handle.readLine then return nil, "Not opened for reading" end
|
||||
|
||||
local n = select('#', ...)
|
||||
local output = {}
|
||||
for i = 1, n do
|
||||
local arg = select(i, ...)
|
||||
local res
|
||||
if typeOf(arg) == "number" then
|
||||
if handle.read then res = handle.read(arg) end
|
||||
elseif typeOf(arg) == "string" then
|
||||
local format = arg:gsub("^%*", ""):sub(1, 1)
|
||||
|
||||
if format == "l" then
|
||||
if handle.readLine then res = handle.readLine() end
|
||||
elseif format == "L" and handle.readLine then
|
||||
if handle.readLine then res = handle.readLine(true) end
|
||||
elseif format == "a" then
|
||||
if handle.readAll then res = handle.readAll() or "" end
|
||||
elseif format == "n" then
|
||||
res = nil -- Skip this format as we can't really handle it
|
||||
else
|
||||
error("bad argument #" .. i .. " (invalid format)", 2)
|
||||
end
|
||||
else
|
||||
error("bad argument #" .. i .. " (expected string, got " .. typeOf(arg) .. ")", 2)
|
||||
end
|
||||
|
||||
output[i] = res
|
||||
if not res then break end
|
||||
end
|
||||
|
||||
-- Default to "l" if possible
|
||||
if n == 0 and handle.readLine then return handle.readLine() end
|
||||
return table.unpack(output, 1, n)
|
||||
end,
|
||||
seek = function(self, whence, offset)
|
||||
if typeOf(self) ~= "table" or getmetatable(self) ~= handleMetatable then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(self) .. ")", 2)
|
||||
end
|
||||
if self._closed then error("attempt to use a closed file", 2) end
|
||||
|
||||
local handle = self._handle
|
||||
if not handle.seek then return nil, "file is not seekable" end
|
||||
|
||||
-- It's a tail call, so error positions are preserved
|
||||
return handle.seek(whence, offset)
|
||||
end,
|
||||
setvbuf = function(self, mode, size) end,
|
||||
write = function(self, ...)
|
||||
if typeOf(self) ~= "table" or getmetatable(self) ~= handleMetatable then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(self) .. ")", 2)
|
||||
end
|
||||
if self._closed then error("attempt to use a closed file", 2) end
|
||||
|
||||
local handle = self._handle
|
||||
if not handle.write then return nil, "file is not writable" end
|
||||
|
||||
local n = select("#", ...)
|
||||
for i = 1, n do handle.write(select(i, ...)) end
|
||||
return self
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
||||
local g_currentInput = g_defaultInput
|
||||
local g_currentOutput = g_defaultOutput
|
||||
local defaultInput = setmetatable({
|
||||
_handle = { readLine = _G.read }
|
||||
}, handleMetatable)
|
||||
|
||||
function close( _file )
|
||||
(_file or g_currentOutput):close()
|
||||
local defaultOutput = setmetatable({
|
||||
_handle = { write = _G.write }
|
||||
}, handleMetatable)
|
||||
|
||||
local defaultError = setmetatable({
|
||||
_handle = {
|
||||
write = function(...)
|
||||
local oldColour
|
||||
if term.isColour() then
|
||||
oldColour = term.getTextColour()
|
||||
term.setTextColour(colors.red)
|
||||
end
|
||||
_G.write(...)
|
||||
if term.isColour() then term.setTextColour(oldColour) end
|
||||
end,
|
||||
}
|
||||
}, handleMetatable)
|
||||
|
||||
local currentInput = defaultInput
|
||||
local currentOutput = defaultOutput
|
||||
|
||||
stdin = defaultInput
|
||||
stdout = defaultOutput
|
||||
stderr = defaultError
|
||||
|
||||
function close(_file)
|
||||
if _file == nil then return currentOutput:close() end
|
||||
|
||||
if typeOf(_file) ~= "table" or getmetatable(_file) ~= handleMetatable then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(_file) .. ")", 2)
|
||||
end
|
||||
return _file:close()
|
||||
end
|
||||
|
||||
function flush()
|
||||
g_currentOutput:flush()
|
||||
return currentOutput:flush()
|
||||
end
|
||||
|
||||
function input( _arg )
|
||||
if _G.type( _arg ) == "string" then
|
||||
g_currentInput = open( _arg, "r" )
|
||||
elseif _G.type( _arg ) == "table" then
|
||||
g_currentInput = _arg
|
||||
elseif _G.type( _arg ) == "nil" then
|
||||
return g_currentInput
|
||||
else
|
||||
error( "bad argument #1 (expected string/table/nil, got " .. _G.type( _arg ) .. ")", 2 )
|
||||
end
|
||||
end
|
||||
|
||||
function lines( _sFileName )
|
||||
if _G.type( _sFileName ) ~= "string" then
|
||||
error( "bad argument #1 (expected string, got " .. _G.type( _sFileName ) .. ")", 2 )
|
||||
function input(_arg)
|
||||
if typeOf(_arg) == "string" then
|
||||
local res, err = open(_arg, "rb")
|
||||
if not res then error(err, 2) end
|
||||
currentInput = res
|
||||
elseif typeOf(_arg) == "table" and getmetatable(_arg) == handleMetatable then
|
||||
currentInput = _arg
|
||||
elseif _arg ~= nil then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(_arg) .. ")", 2)
|
||||
end
|
||||
if _sFileName then
|
||||
return open( _sFileName, "r" ):lines()
|
||||
else
|
||||
return g_currentInput:lines()
|
||||
end
|
||||
|
||||
return currentInput
|
||||
end
|
||||
|
||||
function open( _sPath, _sMode )
|
||||
if _G.type( _sPath ) ~= "string" then
|
||||
error( "bad argument #1 (expected string, got " .. _G.type( _sPath ) .. ")", 2 )
|
||||
function lines(_sFileName)
|
||||
if _sFileName ~= nil and typeOf(_sFileName) ~= "string" then
|
||||
error("bad argument #1 (expected string, got " .. typeOf(_sFileName) .. ")", 2)
|
||||
end
|
||||
if _sMode ~= nil and _G.type( _sMode ) ~= "string" then
|
||||
error( "bad argument #2 (expected string, got " .. _G.type( _sMode ) .. ")", 2 )
|
||||
if _sFileName then
|
||||
local ok, err = open(_sFileName, "rb")
|
||||
if not ok then error(err, 2) end
|
||||
|
||||
-- We set this magic flag to mark this file as being opened by io.lines and so should be
|
||||
-- closed automatically
|
||||
ok._autoclose = true
|
||||
return ok:lines()
|
||||
else
|
||||
return currentInput:lines()
|
||||
end
|
||||
local sMode = _sMode or "r"
|
||||
local file, err = fs.open( _sPath, sMode )
|
||||
if not file then
|
||||
return nil, err
|
||||
end
|
||||
|
||||
if sMode == "r"then
|
||||
return {
|
||||
bFileHandle = true,
|
||||
bClosed = false,
|
||||
close = function( self )
|
||||
file.close()
|
||||
self.bClosed = true
|
||||
end,
|
||||
read = function( self, _sFormat )
|
||||
local sFormat = _sFormat or "*l"
|
||||
if sFormat == "*l" then
|
||||
return file.readLine()
|
||||
elseif sFormat == "*a" then
|
||||
return file.readAll()
|
||||
elseif _G.type( sFormat ) == "number" then
|
||||
return file.read( sFormat )
|
||||
else
|
||||
error( "Unsupported format", 2 )
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
lines = function( self )
|
||||
return function()
|
||||
local sLine = file.readLine()
|
||||
if sLine == nil then
|
||||
file.close()
|
||||
self.bClosed = true
|
||||
end
|
||||
return sLine
|
||||
end
|
||||
end,
|
||||
}
|
||||
elseif sMode == "w" or sMode == "a" then
|
||||
return {
|
||||
bFileHandle = true,
|
||||
bClosed = false,
|
||||
close = function( self )
|
||||
file.close()
|
||||
self.bClosed = true
|
||||
end,
|
||||
write = function( self, ... )
|
||||
local nLimit = select("#", ... )
|
||||
for n = 1, nLimit do
|
||||
file.write( select( n, ... ) )
|
||||
end
|
||||
end,
|
||||
flush = function( self )
|
||||
file.flush()
|
||||
end,
|
||||
}
|
||||
|
||||
elseif sMode == "rb" then
|
||||
return {
|
||||
bFileHandle = true,
|
||||
bClosed = false,
|
||||
close = function( self )
|
||||
file.close()
|
||||
self.bClosed = true
|
||||
end,
|
||||
read = function( self )
|
||||
return file.read()
|
||||
end,
|
||||
}
|
||||
|
||||
elseif sMode == "wb" or sMode == "ab" then
|
||||
return {
|
||||
bFileHandle = true,
|
||||
bClosed = false,
|
||||
close = function( self )
|
||||
file.close()
|
||||
self.bClosed = true
|
||||
end,
|
||||
write = function( self, ... )
|
||||
local nLimit = select("#", ... )
|
||||
for n = 1, nLimit do
|
||||
file.write( select( n, ... ) )
|
||||
end
|
||||
end,
|
||||
flush = function( self )
|
||||
file.flush()
|
||||
end,
|
||||
}
|
||||
|
||||
else
|
||||
file.close()
|
||||
error( "Unsupported mode", 2 )
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
function output( _arg )
|
||||
if _G.type( _arg ) == "string" then
|
||||
g_currentOutput = open( _arg, "w" )
|
||||
elseif _G.type( _arg ) == "table" then
|
||||
g_currentOutput = _arg
|
||||
elseif _G.type( _arg ) == "nil" then
|
||||
return g_currentOutput
|
||||
else
|
||||
error( "bad argument #1 (expected string/table/nil, got " .. _G.type( _arg ) .. ")", 2 )
|
||||
end
|
||||
function open(_sPath, _sMode)
|
||||
if typeOf(_sPath) ~= "string" then
|
||||
error("bad argument #1 (expected string, got " .. typeOf(_sPath) .. ")", 2)
|
||||
end
|
||||
if _sMode ~= nil and typeOf(_sMode) ~= "string" then
|
||||
error("bad argument #2 (expected string, got " .. typeOf(_sMode) .. ")", 2)
|
||||
end
|
||||
|
||||
local sMode = _sMode and _sMode:gsub("%+", "") or "rb"
|
||||
local file, err = fs.open(_sPath, sMode)
|
||||
if not file then return nil, err end
|
||||
|
||||
return setmetatable({ _handle = file }, handleMetatable)
|
||||
end
|
||||
|
||||
function read( ... )
|
||||
return input():read( ... )
|
||||
function output(_arg)
|
||||
if typeOf(_arg) == "string" then
|
||||
local res, err = open(_arg, "w")
|
||||
if not res then error(err, 2) end
|
||||
currentOutput = res
|
||||
elseif typeOf(_arg) == "table" and getmetatable(_arg) == handleMetatable then
|
||||
currentOutput = _arg
|
||||
elseif _arg ~= nil then
|
||||
error("bad argument #1 (FILE expected, got " .. typeOf(_arg) .. ")", 2)
|
||||
end
|
||||
|
||||
return currentOutput
|
||||
end
|
||||
|
||||
function type( _handle )
|
||||
if _G.type( _handle ) == "table" and _handle.bFileHandle == true then
|
||||
if _handle.bClosed then
|
||||
return "closed file"
|
||||
else
|
||||
return "file"
|
||||
end
|
||||
end
|
||||
return nil
|
||||
function read(...)
|
||||
return currentInput:read(...)
|
||||
end
|
||||
|
||||
function write( ... )
|
||||
return output():write( ... )
|
||||
function type(handle)
|
||||
if typeOf(handle) == "table" and getmetatable(handle) == handleMetatable then
|
||||
if handle._closed then
|
||||
return "closed file"
|
||||
else
|
||||
return "file"
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function write(...)
|
||||
return currentOutput:write(...)
|
||||
end
|
||||
|
@@ -258,6 +258,10 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
|
||||
end
|
||||
end
|
||||
|
||||
function window.getCursorBlink()
|
||||
return bCursorBlink
|
||||
end
|
||||
|
||||
local function isColor()
|
||||
return parent.isColor()
|
||||
end
|
||||
@@ -421,8 +425,10 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
|
||||
function window.reposition( nNewX, nNewY, nNewWidth, nNewHeight )
|
||||
if type( nNewX ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( nNewX ) .. ")", 2 ) end
|
||||
if type( nNewY ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( nNewY ) .. ")", 2 ) end
|
||||
if nNewWidth ~= nil and type( nNewWidth ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nNewWidth ) .. ")", 2 ) end
|
||||
if nNewHeight ~= nil and type( nNewHeight ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( nNewHeight ) .. ")", 2 ) end
|
||||
if nNewWidth ~= nil or nNewWidth ~= nil then
|
||||
if type( nNewWidth ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nNewWidth ) .. ")", 2 ) end
|
||||
if type( nNewHeight ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( nNewHeight ) .. ")", 2 ) end
|
||||
end
|
||||
|
||||
nX = nNewX
|
||||
nY = nNewY
|
||||
|
@@ -13,18 +13,14 @@ if sTopic == "index" then
|
||||
return
|
||||
end
|
||||
|
||||
local w,h = term.getSize()
|
||||
local sFile = help.lookup( sTopic )
|
||||
local file = ((sFile ~= nil) and io.open( sFile )) or nil
|
||||
local nLinesPrinted = 0
|
||||
if file then
|
||||
local sLine = file:read()
|
||||
local nLines = 0
|
||||
while sLine do
|
||||
nLines = nLines + textutils.pagedPrint( sLine, (h-3) - nLines )
|
||||
sLine = file:read()
|
||||
end
|
||||
local sContents = file:read("*a")
|
||||
file:close()
|
||||
|
||||
local _, nHeight = term.getSize()
|
||||
textutils.pagedPrint( sContents, nHeight - 3 )
|
||||
else
|
||||
print( "No help available" )
|
||||
end
|
||||
|
@@ -113,7 +113,7 @@ elseif sCommand == "run" then
|
||||
printError( err )
|
||||
return
|
||||
end
|
||||
local success, msg = pcall(func, table.unpack(tArgs, 3))
|
||||
local success, msg = pcall(func, select(3, ...))
|
||||
if not success then
|
||||
printError( msg )
|
||||
end
|
||||
|
@@ -44,7 +44,7 @@ local ok, error = pcall( function()
|
||||
if sEvent == "modem_message" then
|
||||
-- Got a modem message, rebroadcast it if it's a rednet thing
|
||||
if nChannel == rednet.CHANNEL_REPEAT then
|
||||
if type( tMessage ) == "table" and tMessage.nMessageID and tMessage.nRecipient then
|
||||
if type( tMessage ) == "table" and tMessage.nMessageID and tMessage.nRecipient and type(tMessage.nRecipient) == "number" then
|
||||
if not tReceivedMessages[ tMessage.nMessageID ] then
|
||||
-- Ensure we only repeat a message once
|
||||
tReceivedMessages[ tMessage.nMessageID ] = true
|
||||
|
@@ -89,18 +89,17 @@ end
|
||||
|
||||
print( "Press any key to stop the groove" )
|
||||
|
||||
local bEnd = false
|
||||
parallel.waitForAll(
|
||||
parallel.waitForAny(
|
||||
function()
|
||||
while not bEnd do
|
||||
local event, key = os.pullEvent("key")
|
||||
if key ~= keys.escape then
|
||||
bEnd = true
|
||||
return
|
||||
end
|
||||
end
|
||||
end,
|
||||
function()
|
||||
while not bEnd do
|
||||
while true do
|
||||
local fnMove = tMoves[math.random(1,#tMoves)]
|
||||
fnMove()
|
||||
end
|
||||
|
Reference in New Issue
Block a user