2017-05-07 00:18:59 +00:00
|
|
|
/*
|
2017-05-01 13:32:39 +00:00
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2019-01-01 01:10:18 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
2017-05-01 13:32:39 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dan200.computercraft.shared.peripheral.diskdrive;
|
|
|
|
|
|
|
|
import dan200.computercraft.api.lua.ILuaContext;
|
|
|
|
import dan200.computercraft.api.lua.LuaException;
|
|
|
|
import dan200.computercraft.api.media.IMedia;
|
|
|
|
import dan200.computercraft.api.peripheral.IComputerAccess;
|
|
|
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
import dan200.computercraft.shared.media.items.ItemDisk;
|
2017-05-01 21:23:54 +00:00
|
|
|
import dan200.computercraft.shared.util.StringUtil;
|
2017-05-01 13:32:39 +00:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
2017-05-06 23:07:42 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2017-06-12 09:28:31 +00:00
|
|
|
import static dan200.computercraft.core.apis.ArgumentHelper.optString;
|
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
public class DiskDrivePeripheral implements IPeripheral
|
|
|
|
{
|
|
|
|
private final TileDiskDrive m_diskDrive;
|
|
|
|
|
|
|
|
public DiskDrivePeripheral( TileDiskDrive diskDrive )
|
|
|
|
{
|
|
|
|
m_diskDrive = diskDrive;
|
|
|
|
}
|
|
|
|
|
2017-05-06 23:07:42 +00:00
|
|
|
@Nonnull
|
2017-05-01 13:32:39 +00:00
|
|
|
@Override
|
|
|
|
public String getType()
|
|
|
|
{
|
|
|
|
return "drive";
|
|
|
|
}
|
|
|
|
|
2017-05-06 23:07:42 +00:00
|
|
|
@Nonnull
|
2017-05-01 13:32:39 +00:00
|
|
|
@Override
|
|
|
|
public String[] getMethodNames()
|
|
|
|
{
|
|
|
|
return new String[] {
|
|
|
|
"isDiskPresent",
|
|
|
|
"getDiskLabel",
|
|
|
|
"setDiskLabel",
|
|
|
|
"hasData",
|
|
|
|
"getMountPath",
|
|
|
|
"hasAudio",
|
|
|
|
"getAudioTitle",
|
|
|
|
"playAudio",
|
|
|
|
"stopAudio",
|
|
|
|
"ejectDisk",
|
2019-06-07 23:28:03 +00:00
|
|
|
"getDiskID",
|
2017-05-01 13:32:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-06 23:07:42 +00:00
|
|
|
public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
|
|
|
switch( method )
|
|
|
|
{
|
2019-03-29 21:21:39 +00:00
|
|
|
case 0: // isDiskPresent
|
2018-12-30 16:14:07 +00:00
|
|
|
return new Object[] { !m_diskDrive.getDiskStack().isEmpty() };
|
2019-03-29 21:21:39 +00:00
|
|
|
case 1: // getDiskLabel
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
|
|
|
IMedia media = m_diskDrive.getDiskMedia();
|
2018-12-30 16:14:07 +00:00
|
|
|
return media == null ? null : new Object[] { media.getLabel( m_diskDrive.getDiskStack() ) };
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
2019-03-29 21:21:39 +00:00
|
|
|
case 2: // setDiskLabel
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
2017-06-12 09:28:31 +00:00
|
|
|
String label = optString( arguments, 0, null );
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
IMedia media = m_diskDrive.getDiskMedia();
|
2018-12-30 16:14:07 +00:00
|
|
|
if( media == null ) return null;
|
|
|
|
|
|
|
|
ItemStack disk = m_diskDrive.getDiskStack();
|
|
|
|
label = StringUtil.normaliseLabel( label );
|
|
|
|
if( !media.setLabel( disk, label ) ) throw new LuaException( "Disk label cannot be changed" );
|
|
|
|
m_diskDrive.setDiskStack( disk );
|
2017-05-01 13:32:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-03-29 21:21:39 +00:00
|
|
|
case 3: // hasData
|
2018-12-30 16:14:07 +00:00
|
|
|
return new Object[] { m_diskDrive.getDiskMountPath( computer ) != null };
|
2019-03-29 21:21:39 +00:00
|
|
|
case 4: // getMountPath
|
2018-12-30 16:14:07 +00:00
|
|
|
return new Object[] { m_diskDrive.getDiskMountPath( computer ) };
|
2017-05-01 13:32:39 +00:00
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
// hasAudio
|
|
|
|
IMedia media = m_diskDrive.getDiskMedia();
|
2018-12-30 16:14:07 +00:00
|
|
|
return new Object[] { media != null && media.getAudio( m_diskDrive.getDiskStack() ) != null };
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
// getAudioTitle
|
|
|
|
IMedia media = m_diskDrive.getDiskMedia();
|
2018-12-30 16:14:07 +00:00
|
|
|
return new Object[] { media != null ? media.getAudioTitle( m_diskDrive.getDiskStack() ) : false };
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
2019-03-29 21:21:39 +00:00
|
|
|
case 7: // playAudio
|
2017-05-01 13:32:39 +00:00
|
|
|
m_diskDrive.playDiskAudio();
|
|
|
|
return null;
|
2019-03-29 21:21:39 +00:00
|
|
|
case 8: // stopAudio
|
2017-05-01 13:32:39 +00:00
|
|
|
m_diskDrive.stopDiskAudio();
|
|
|
|
return null;
|
2019-03-29 21:21:39 +00:00
|
|
|
case 9: // eject
|
2017-05-01 13:32:39 +00:00
|
|
|
m_diskDrive.ejectDisk();
|
|
|
|
return null;
|
2019-03-29 21:21:39 +00:00
|
|
|
case 10: // getDiskID
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
|
|
|
ItemStack disk = m_diskDrive.getDiskStack();
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
return disk.getItem() instanceof ItemDisk ? new Object[] { ItemDisk.getDiskID( disk ) } : null;
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-06 23:07:42 +00:00
|
|
|
public void attach( @Nonnull IComputerAccess computer )
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
|
|
|
m_diskDrive.mount( computer );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-06 23:07:42 +00:00
|
|
|
public void detach( @Nonnull IComputerAccess computer )
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
|
|
|
m_diskDrive.unmount( computer );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals( IPeripheral other )
|
|
|
|
{
|
2018-12-30 16:14:07 +00:00
|
|
|
if( this == other ) return true;
|
2019-03-29 21:21:39 +00:00
|
|
|
return other instanceof DiskDrivePeripheral && ((DiskDrivePeripheral) other).m_diskDrive == m_diskDrive;
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
2018-02-21 15:25:20 +00:00
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public Object getTarget()
|
|
|
|
{
|
|
|
|
return m_diskDrive;
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|