/
DiskDrivePeripheral.java
156 lines (135 loc) · 4.37 KB
/
DiskDrivePeripheral.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2020. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.diskdrive;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.media.IMedia;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.MediaProviders;
import dan200.computercraft.shared.media.items.ItemDisk;
import dan200.computercraft.shared.util.StringUtil;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
import java.util.Optional;
/**
* Disk drives are a peripheral which allow you to read and write to floppy disks and other "mountable media" (such as
* computers or turtles). They also allow you to {@link #playAudio play records}.
*
* When a disk drive attaches some mount (such as a floppy disk or computer), it attaches a folder called {@code disk},
* {@code disk2}, etc... to the root directory of the computer. This folder can be used to interact with the files on
* that disk.
*
* When a disk is inserted, a {@code disk} event is fired, with the side peripheral is on. Likewise, when the disk is
* detached, a {@code disk_eject} event is fired.
*
* @cc.module drive
*/
public class DiskDrivePeripheral implements IPeripheral
{
private final TileDiskDrive diskDrive;
DiskDrivePeripheral( TileDiskDrive diskDrive )
{
this.diskDrive = diskDrive;
}
@Nonnull
@Override
public String getType()
{
return "drive";
}
@LuaFunction
public final boolean isDiskPresent()
{
return !diskDrive.getDiskStack().isEmpty();
}
@LuaFunction
public final Object[] getDiskLabel()
{
ItemStack stack = diskDrive.getDiskStack();
IMedia media = MediaProviders.get( stack );
return media == null ? null : new Object[] { media.getLabel( stack ) };
}
@LuaFunction( mainThread = true )
public final void setDiskLabel( Optional<String> labelA ) throws LuaException
{
String label = labelA.orElse( null );
ItemStack stack = diskDrive.getDiskStack();
IMedia media = MediaProviders.get( stack );
if( media == null ) return;
if( !media.setLabel( stack, StringUtil.normaliseLabel( label ) ) )
{
throw new LuaException( "Disk label cannot be changed" );
}
diskDrive.setDiskStack( stack );
}
@LuaFunction
public final boolean hasData( IComputerAccess computer )
{
return diskDrive.getDiskMountPath( computer ) != null;
}
@LuaFunction
public final String getMountPath( IComputerAccess computer )
{
return diskDrive.getDiskMountPath( computer );
}
@LuaFunction
public final boolean hasAudio()
{
ItemStack stack = diskDrive.getDiskStack();
IMedia media = MediaProviders.get( stack );
return media != null && media.getAudio( stack ) != null;
}
@LuaFunction
public final Object getAudioTitle()
{
ItemStack stack = diskDrive.getDiskStack();
IMedia media = MediaProviders.get( stack );
return media != null ? media.getAudioTitle( stack ) : false;
}
@LuaFunction
public final void playAudio()
{
diskDrive.playDiskAudio();
}
@LuaFunction
public final void stopAudio()
{
diskDrive.stopDiskAudio();
}
@LuaFunction
public final void ejectDisk()
{
diskDrive.ejectDisk();
}
@LuaFunction
public final Object[] getDiskID()
{
ItemStack disk = diskDrive.getDiskStack();
return disk.getItem() instanceof ItemDisk ? new Object[] { ItemDisk.getDiskID( disk ) } : null;
}
@Override
public void attach( @Nonnull IComputerAccess computer )
{
diskDrive.mount( computer );
}
@Override
public void detach( @Nonnull IComputerAccess computer )
{
diskDrive.unmount( computer );
}
@Override
public boolean equals( IPeripheral other )
{
return this == other || other instanceof DiskDrivePeripheral && ((DiskDrivePeripheral) other).diskDrive == diskDrive;
}
@Nonnull
@Override
public Object getTarget()
{
return diskDrive;
}
}