Skip to content

Commit babd380

Browse files
committed
add passwords to embeded computers, finish HDD block
1 parent d420e42 commit babd380

14 files changed

Lines changed: 161 additions & 29 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ minecraft_version=1.20.1
66
yarn_mappings=1.20.1+build.10
77
loader_version=0.16.10
88
# Mod Properties
9-
mod_version=0.3.0-beta.2
9+
mod_version=0.3.0
1010
maven_group=org.featherwhisker
1111
archives_base_name=embeddedcomputer
1212
# Dependencies

src/main/java/org/windclan/embeddedcomputer/client/mainClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import net.fabricmc.api.ClientModInitializer;
44

55
public class mainClient implements ClientModInitializer {
6-
76
@Override
87
public void onInitializeClient() {
98
}

src/main/java/org/windclan/embeddedcomputer/embedded/EmbeddedComputerAPI.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@
99
import dan200.computercraft.api.filesystem.WritableMount;
1010
import dan200.computercraft.api.lua.ILuaAPI;
1111
import dan200.computercraft.api.lua.LuaFunction;
12+
import dan200.computercraft.core.apis.handles.WriteHandle;
1213
import dan200.computercraft.shared.computer.core.ServerComputer;
1314

15+
import java.nio.ByteBuffer;
16+
import java.nio.channels.SeekableByteChannel;
17+
import java.nio.charset.StandardCharsets;
18+
import java.nio.file.OpenOption;
19+
import java.nio.file.StandardOpenOption;
20+
import java.util.Collections;
21+
import java.util.Set;
22+
1423
import static java.util.Objects.isNull;
1524
import static org.windclan.embeddedcomputer.main.log;
1625

@@ -28,7 +37,7 @@ public final void format() { //
2837
ServerComputer comp = brain.getOwner().getServerComputer();
2938
if (!isNull(comp)) {
3039
try {
31-
WritableMount fs = ComputerCraftAPI.createSaveDirMount(comp.getLevel().getServer(), "computer/" + comp.getID(), 100);
40+
WritableMount fs = comp.createRootMount();
3241
try {
3342
fs.delete("/");
3443
}catch(Exception ignored){}

src/main/java/org/windclan/embeddedcomputer/embedded/EmbeddedComputerPeripheral.java

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@
1111
import dan200.computercraft.api.peripheral.IComputerAccess;
1212
import dan200.computercraft.api.peripheral.IPeripheral;
1313
import dan200.computercraft.shared.computer.blocks.AbstractComputerBlockEntity;
14+
import dan200.computercraft.shared.computer.core.ServerComputer;
1415
import net.minecraft.block.entity.BlockEntity;
1516
import net.minecraft.util.math.Direction;
1617
import org.windclan.embeddedcomputer.storage.harddrive.HardDrivePeripheral;
1718
import org.jetbrains.annotations.Nullable;
1819
import org.windclan.embeddedcomputer.embedded.block.EmbeddedComputerBlockEntity;
1920

21+
import java.nio.ByteBuffer;
22+
import java.nio.channels.SeekableByteChannel;
23+
import java.nio.charset.StandardCharsets;
24+
import java.nio.file.OpenOption;
25+
import java.nio.file.StandardOpenOption;
26+
import java.util.Collections;
27+
import java.util.Scanner;
28+
import java.util.Set;
29+
30+
import static dan200.computercraft.shared.pocket.items.PocketComputerItem.getServerComputer;
2031
import static java.util.Objects.isNull;
2132
import static org.windclan.embeddedcomputer.main.log;
2233

@@ -57,17 +68,66 @@ public final void reboot() {
5768
if (!isNull(comp1)) comp1.reboot();
5869
}
5970
@LuaFunction(mainThread = true)
60-
public final void format() {
71+
public final boolean format() {
6172
var comp1 = getServerComp();
73+
WritableMount mnt;
6274
if (!isNull(comp1)) {
6375
try {
64-
WritableMount fs = ComputerCraftAPI.createSaveDirMount(comp1.getLevel().getServer(), "computer/" + comp1.getID(), 100);
76+
mnt = comp1.createRootMount();
77+
if (mnt.exists(".LOCKED")) {
78+
return false;
79+
}
6580
try {
66-
fs.delete("/");
67-
}catch(Exception ignored){}
68-
comp1.reboot();
69-
}catch(Exception ignored){
70-
log.info(ignored.getMessage(),ignored.fillInStackTrace());
81+
mnt.delete("/");
82+
comp1.reboot();
83+
return true;
84+
}catch(Exception ignored){
85+
comp1.reboot();
86+
return false;
87+
}
88+
}catch(Exception ex){
89+
log.info(ex.getMessage(),ex.fillInStackTrace());
90+
return false;
91+
}
92+
}
93+
return false;
94+
}
95+
96+
@LuaFunction(mainThread = true)
97+
public final void unlock(String pass1) {
98+
ServerComputer comp1 = getServerComp();
99+
if (!isNull(comp)) {
100+
WritableMount mnt;
101+
SeekableByteChannel root = null;
102+
Scanner scan = null;
103+
try {
104+
mnt = comp1.createRootMount();
105+
if (mnt.exists(".LOCKED")) {
106+
String pass = "";
107+
root = comp1.createRootMount().openForRead(".LOCKED");
108+
scan = new Scanner(root);
109+
while (scan.hasNext()) {
110+
pass+=scan.next();
111+
}
112+
scan.close();
113+
if (!pass.equals(pass1)) {
114+
root.close();
115+
return;
116+
}
117+
mnt.delete(".LOCKED");
118+
return;
119+
}
120+
} catch (Exception ex) {
121+
log.warn(ex.toString());
122+
if (!isNull(root)) {
123+
try {
124+
root.close();
125+
} catch (Exception ignored) {}
126+
}
127+
if (!isNull(scan)) {
128+
scan.close();
129+
}
130+
return;
71131
}
72132
}
73133
}

src/main/java/org/windclan/embeddedcomputer/embedded/block/EmbeddedComputerBlockEntity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import dan200.computercraft.shared.computer.core.ComputerFamily;
1111
import dan200.computercraft.shared.computer.core.ServerComputer;
1212
import net.minecraft.block.BlockState;
13+
import net.minecraft.nbt.NbtCompound;
1314
import net.minecraft.server.world.ServerWorld;
1415
import net.minecraft.util.math.BlockPos;
1516
import org.windclan.embeddedcomputer.ComputerComponents;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.windclan.embeddedcomputer.peripherals.generics;
2+
3+
import dan200.computercraft.api.lua.LuaFunction;
4+
import dan200.computercraft.api.peripheral.GenericPeripheral;
5+
import net.minecraft.block.entity.PistonBlockEntity;
6+
7+
public class PistonPeripheral implements GenericPeripheral {
8+
@Override
9+
public String id() {
10+
return new String("embeddedcomputer:piston");
11+
}
12+
@LuaFunction(mainThread = true)
13+
public final void isExtending(PistonBlockEntity piston) {
14+
piston.isExtending();
15+
}
16+
}

src/main/java/org/windclan/embeddedcomputer/registry.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
import org.windclan.embeddedcomputer.embedded.block.EmbeddedComputerBlock;
2525
import org.windclan.embeddedcomputer.embedded.block.EmbeddedComputerBlockEntity;
2626
import org.windclan.embeddedcomputer.embedded.item.ComputerBlockItem;
27-
2827
import org.windclan.embeddedcomputer.storage.harddrive.HardDriveBlock;
2928
import org.windclan.embeddedcomputer.storage.harddrive.HardDriveBlockEntity;
30-
3129
import org.windclan.embeddedcomputer.platform.registry1;
3230
import org.windclan.embeddedcomputer.storage.harddrive.HardDriveItem;
3331
import org.windclan.embeddedcomputer.storage.harddrive.HardDrivePeripheral;

src/main/java/org/windclan/embeddedcomputer/storage/harddrive/HardDriveBlockEntity.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@
1717
import org.windclan.embeddedcomputer.registry;
1818
import org.jetbrains.annotations.Nullable;
1919

20+
import java.util.UUID;
21+
2022
import static java.util.Objects.isNull;
2123

2224
public class HardDriveBlockEntity extends BlockEntity {
2325
public HardDriveBlockEntity(BlockPos pos, BlockState state) {
2426
super(registry.HARD_DRIVE_ENTITY,pos, state);
2527
}
2628
private final HardDrivePeripheral periph = new HardDrivePeripheral(this);
27-
public static int id = -1;
29+
public String uuid = "";
2830
public String mount;
2931

30-
public static void tick(World world1, BlockPos pos, BlockState state1, BlockEntity be) {
31-
32-
}
33-
32+
public static void tick(World world1, BlockPos pos, BlockState state1, BlockEntity be) {}
3433
public WritableMount makeMount() {
35-
if (id < 0) {
36-
id = ComputerCraftAPI.createUniqueNumberedSaveDir(world.getServer(), "hdd");
34+
if (uuid.isEmpty()) {
35+
uuid = UUID.randomUUID().toString();
36+
markDirty();
3737
}
38-
return ComputerCraftAPI.createSaveDirMount(world.getServer(), "hdd/" + id, 25000000); // 25 Megabytes
38+
return ComputerCraftAPI.createSaveDirMount(world.getServer(), "hdd/" + uuid, 25000000); // 25 Megabytes
3939
}
4040
public boolean attach(IComputerAccess computer, @Nullable String str) {
4141
if (isNull(str)) {
@@ -57,15 +57,18 @@ public boolean detach(IComputerAccess computer, @Nullable String str) {
5757
}
5858
@Override
5959
public void writeNbt(NbtCompound nbt) {
60-
nbt.putInt("id", id);
60+
nbt.putString("uuid", uuid);
6161
super.writeNbt(nbt);
6262
}
6363

6464
@Override
6565
public void readNbt(NbtCompound nbt) {
6666
super.readNbt(nbt);
67-
if (!nbt.contains("id")) id = -1;
68-
else id = nbt.getInt("id");
67+
uuid = nbt.getString("uuid");
68+
if (uuid.isEmpty()) {
69+
uuid = UUID.randomUUID().toString();
70+
markDirty();
71+
}
6972
}
7073
public IPeripheral peripheral() {
7174
return periph;

src/main/java/org/windclan/embeddedcomputer/storage/harddrive/HardDriveItem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public HardDriveItem(Block block, Settings settings) {
2424
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
2525
NbtCompound nbt = BlockItem.getBlockEntityNbt(stack);
2626
if (nbt == null) return;
27-
if (!nbt.contains("id")) return;
28-
if (nbt.getInt("id") < 0) return;
29-
tooltip.add(Text.literal("Drive: "+nbt.getInt("id")).formatted(Formatting.DARK_GRAY));
27+
if (!nbt.contains("uuid")) return;
28+
if (nbt.getString("uuid").isEmpty()) return;
29+
tooltip.add(Text.literal("Drive: "+nbt.getString("uuid")).formatted(Formatting.DARK_GRAY));
3030
}
3131
}

src/main/java/org/windclan/embeddedcomputer/storage/harddrive/HardDrivePeripheral.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public class HardDrivePeripheral implements IPeripheral {
1818
public HardDriveBlockEntity hdd;
19-
private static String mount;
19+
private String mount;
2020
public HardDrivePeripheral(BlockEntity blockEntity) {
2121
hdd = (HardDriveBlockEntity) blockEntity;
2222
}
@@ -42,10 +42,24 @@ public final boolean mount(IComputerAccess computer, @Nullable String str) {
4242
mount = str;
4343
return hdd.attach(computer,str);
4444
}
45+
@LuaFunction(mainThread = true)
46+
public final boolean mount(IComputerAccess computer) {
47+
String str = "drive";
48+
if (Objects.equals(str, "rom")) {
49+
return false;
50+
}
51+
mount = str;
52+
return hdd.attach(computer,str);
53+
}
54+
4555
@LuaFunction(mainThread = true)
4656
public final boolean unmount(IComputerAccess computer, @Nullable String str) {
4757
return hdd.detach(computer,str);
4858
}
59+
@LuaFunction(mainThread = true)
60+
public final boolean unmount(IComputerAccess computer) {
61+
return hdd.detach(computer,mount);
62+
}
4963
public static IPeripheral getPeripheral(BlockEntity blockEntity, Direction direction) {
5064
return new HardDrivePeripheral(blockEntity);
5165
}

0 commit comments

Comments
 (0)