From 7747b4291504062be85a5cca228204035d802070 Mon Sep 17 00:00:00 2001 From: XyperCode Date: Sun, 26 Nov 2023 09:13:58 +0100 Subject: [PATCH] Fix the mess I accidentally created --- .../block_entity_type/network_devices.json | 8 ++++ .../java/com/ultreon/devices/BuiltinApps.java | 32 ++++++------- .../java/com/ultreon/devices/Devices.java | 42 ++++++++--------- .../ultreon/devices/core/network/Router.java | 24 ++++++++-- .../core/network/task/TaskGetDevices.java | 46 ++++++++++--------- .../com/ultreon/devices/init/ModTags.java | 11 ++++- 6 files changed, 100 insertions(+), 63 deletions(-) create mode 100644 common/src/main/generated/resources/data/devices/tags/block_entity_type/network_devices.json diff --git a/common/src/main/generated/resources/data/devices/tags/block_entity_type/network_devices.json b/common/src/main/generated/resources/data/devices/tags/block_entity_type/network_devices.json new file mode 100644 index 000000000..c758ed906 --- /dev/null +++ b/common/src/main/generated/resources/data/devices/tags/block_entity_type/network_devices.json @@ -0,0 +1,8 @@ +{ + "replace": false, + "values": [ + "devices:laptop", + "devices:printer", + "devices:mac_max_x" + ] +} \ No newline at end of file diff --git a/common/src/main/java/com/ultreon/devices/BuiltinApps.java b/common/src/main/java/com/ultreon/devices/BuiltinApps.java index 932b4b3f1..191603f08 100644 --- a/common/src/main/java/com/ultreon/devices/BuiltinApps.java +++ b/common/src/main/java/com/ultreon/devices/BuiltinApps.java @@ -15,23 +15,23 @@ public class BuiltinApps { public static void registerBuiltinApps() { -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "diagnostics"), () -> DiagnosticsApp::new, true); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "settings"), () -> SettingsApp::new, true); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "file_browser"), () -> FileBrowserApp::new, true); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "gitweb"), () -> GitWebApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "note_stash"), () -> NoteStashApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "pixel_painter"), () -> PixelPainterApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "ender_mail"), () -> EmailApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "app_store"), () -> AppStore::new, true); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "diagnostics"), () -> DiagnosticsApp::new, true); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "settings"), () -> SettingsApp::new, true); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "file_browser"), () -> FileBrowserApp::new, true); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "gitweb"), () -> GitWebApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "note_stash"), () -> NoteStashApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "pixel_painter"), () -> PixelPainterApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "ender_mail"), () -> EmailApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "app_store"), () -> AppStore::new, true); -// if (Platform.isDevelopmentEnvironment()) { -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "bank"), () -> BankApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "boat_racers"), () -> BoatRacersApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "mine_bay"), () -> MineBayApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "snake"), () -> SnakeApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "themes"), () -> ThemesApp::new, false); -// } + if (Platform.isDevelopmentEnvironment()) { + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "bank"), () -> BankApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "boat_racers"), () -> BoatRacersApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "mine_bay"), () -> MineBayApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "snake"), () -> SnakeApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "themes"), () -> ThemesApp::new, false); + } -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "vulnerability"), () -> VulnerabilityApp::new, true); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "vulnerability"), () -> VulnerabilityApp::new, true); } } diff --git a/common/src/main/java/com/ultreon/devices/Devices.java b/common/src/main/java/com/ultreon/devices/Devices.java index 362beea7d..9782925c5 100644 --- a/common/src/main/java/com/ultreon/devices/Devices.java +++ b/common/src/main/java/com/ultreon/devices/Devices.java @@ -216,29 +216,29 @@ private void registerApplications() { TaskManager.registerTask(TaskInstallApp::new); // Ender Mail -// TaskManager.registerTask(TaskUpdateInbox::new); -// TaskManager.registerTask(TaskSendEmail::new); -// TaskManager.registerTask(TaskCheckEmailAccount::new); -// TaskManager.registerTask(TaskRegisterEmailAccount::new); -// TaskManager.registerTask(TaskDeleteEmail::new); -// TaskManager.registerTask(TaskViewEmail::new); + TaskManager.registerTask(TaskUpdateInbox::new); + TaskManager.registerTask(TaskSendEmail::new); + TaskManager.registerTask(TaskCheckEmailAccount::new); + TaskManager.registerTask(TaskRegisterEmailAccount::new); + TaskManager.registerTask(TaskDeleteEmail::new); + TaskManager.registerTask(TaskViewEmail::new); // Auction -// TaskManager.registerTask(TaskAddAuction::new); -// TaskManager.registerTask(TaskGetAuctions::new); -// TaskManager.registerTask(TaskBuyItem::new); - -// if (DEVELOPER_MODE) { -// // Applications (Developers) -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "example"), () -> ExampleApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "icons"), () -> IconsApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "text_area"), () -> TextAreaApp::new, false); -// ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "test"), () -> TestApp::new, false); -// -// TaskManager.registerTask(TaskNotificationTest::new); -// } -// -// EnvExecutor.runInEnv(Env.CLIENT, () -> () -> PrintingManager.registerPrint(new ResourceLocation(Reference.MOD_ID, "picture"), PixelPainterApp.PicturePrint.class)); + TaskManager.registerTask(TaskAddAuction::new); + TaskManager.registerTask(TaskGetAuctions::new); + TaskManager.registerTask(TaskBuyItem::new); + + if (DEVELOPER_MODE) { + // Applications (Developers) + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "example"), () -> ExampleApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "icons"), () -> IconsApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "text_area"), () -> TextAreaApp::new, false); + ApplicationManager.registerApplication(new ResourceLocation(Reference.MOD_ID, "test"), () -> TestApp::new, false); + + TaskManager.registerTask(TaskNotificationTest::new); + } + + EnvExecutor.runInEnv(Env.CLIENT, () -> () -> PrintingManager.registerPrint(new ResourceLocation(Reference.MOD_ID, "picture"), PixelPainterApp.PicturePrint.class)); } public abstract int getBurnTime(ItemStack stack, RecipeType type); diff --git a/common/src/main/java/com/ultreon/devices/core/network/Router.java b/common/src/main/java/com/ultreon/devices/core/network/Router.java index 566f936fc..230b1d88c 100644 --- a/common/src/main/java/com/ultreon/devices/core/network/Router.java +++ b/common/src/main/java/com/ultreon/devices/core/network/Router.java @@ -3,10 +3,13 @@ import com.ultreon.devices.DeviceConfig; import com.ultreon.devices.block.entity.NetworkDeviceBlockEntity; import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; +import net.minecraft.tags.TagKey; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -80,18 +83,31 @@ public Collection getConnectedDevices(Level level) { return NETWORK_DEVICES.values().stream().filter(device -> device.getPos() != null).toList(); } - public Collection getConnectedDevices(final Level level, Class type) { - final Predicate DEVICE_TYPE = networkDevice -> { + public Collection getConnectedDevices(final Level level, BlockEntityType targetType) { + final Predicate deviceType = networkDevice -> { if (networkDevice.getPos() == null) return false; BlockEntity blockEntity = level.getBlockEntity(networkDevice.getPos()); + return blockEntity instanceof NetworkDeviceBlockEntity device && targetType.equals(device.getType()); + + }; + return getConnectedDevices(level).stream().filter(deviceType).toList(); + } + + public Collection getConnectedDevices(final Level level, TagKey> targetType) { + final Predicate deviceType = networkDevice -> { + if (networkDevice.getPos() == null) + return false; + + BlockEntity blockEntity = level.getBlockEntity(networkDevice.getPos()); + if (blockEntity instanceof NetworkDeviceBlockEntity device) { - return type.isAssignableFrom(device.getClass()); + return BuiltInRegistries.BLOCK_ENTITY_TYPE.wrapAsHolder(blockEntity.getType()).is(targetType); } return false; }; - return getConnectedDevices(level).stream().filter(DEVICE_TYPE).toList(); + return getConnectedDevices(level).stream().filter(deviceType).toList(); } private void sendBeacon(Level level) { diff --git a/common/src/main/java/com/ultreon/devices/core/network/task/TaskGetDevices.java b/common/src/main/java/com/ultreon/devices/core/network/task/TaskGetDevices.java index 758c1c06e..f3c3dffb9 100644 --- a/common/src/main/java/com/ultreon/devices/core/network/task/TaskGetDevices.java +++ b/common/src/main/java/com/ultreon/devices/core/network/task/TaskGetDevices.java @@ -5,11 +5,13 @@ import com.ultreon.devices.core.network.NetworkDevice; import com.ultreon.devices.core.network.Router; import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.chunk.LevelChunk; import java.util.Collection; @@ -19,7 +21,7 @@ */ public class TaskGetDevices extends Task { private BlockPos devicePos; - private Class targetDeviceClass; + private BlockEntityType targetType; private Collection foundDevices; private String reason; @@ -33,39 +35,45 @@ public TaskGetDevices(BlockPos devicePos) { this.devicePos = devicePos; } - public TaskGetDevices(BlockPos devicePos, Class targetDeviceClass) { + public TaskGetDevices(BlockPos devicePos, BlockEntityType targetType) { + this(); + this.devicePos = devicePos; + this.targetType = targetType; + } + + /** + * @deprecated Use {@link #TaskGetDevices(BlockPos, BlockEntityType)} instead. + */ + @Deprecated(forRemoval = true) + public TaskGetDevices(BlockPos devicePos, Class ignoredTargetDeviceClass) { this(); this.devicePos = devicePos; - this.targetDeviceClass = targetDeviceClass; } @Override public void prepareRequest(CompoundTag tag) { tag.putLong("devicePos", devicePos.asLong()); - if (targetDeviceClass != null) { - tag.putString("targetClass", targetDeviceClass.getName()); + if (targetType != null) { + tag.putInt("targetType", BuiltInRegistries.BLOCK_ENTITY_TYPE.getId(targetType)); } } @Override - @SuppressWarnings("unchecked") public void processRequest(CompoundTag tag, Level level, Player player) { BlockPos devicePos = BlockPos.of(tag.getLong("devicePos")); - Class targetDeviceClass = null; - try { - Class targetClass = Class.forName(tag.getString("targetClass")); - if (NetworkDeviceBlockEntity.class.isAssignableFrom(targetClass)) { - targetDeviceClass = (Class) targetClass; - } - } catch (ClassNotFoundException e) { - e.printStackTrace(); + BlockEntityType targetType; + int typeId = tag.getInt("targetType"); + if (typeId < 0) { + this.reason = "Invalid target ID received: " + typeId; return; } + targetType = BuiltInRegistries.BLOCK_ENTITY_TYPE.byId(typeId); + BlockEntity tileEntity = level.getChunkAt(devicePos).getBlockEntity(devicePos, LevelChunk.EntityCreationType.IMMEDIATE); if (!(tileEntity instanceof NetworkDeviceBlockEntity tileEntityNetworkDevice)) { - this.reason = "No network driver found"; + this.reason = "Not a network device"; return; } @@ -80,11 +88,7 @@ public void processRequest(CompoundTag tag, Level level, Player player) { return; } - if (targetDeviceClass != null) { - this.foundDevices = router.getConnectedDevices(level, targetDeviceClass); - } else { - this.foundDevices = router.getConnectedDevices(level); - } + this.foundDevices = targetType != null ? router.getConnectedDevices(level) : router.getConnectedDevices(level, targetType); this.setSuccessful(); } @@ -101,6 +105,6 @@ public void prepareResponse(CompoundTag tag) { @Override public void processResponse(CompoundTag tag) { - + // Does not need response processing } } diff --git a/common/src/main/java/com/ultreon/devices/init/ModTags.java b/common/src/main/java/com/ultreon/devices/init/ModTags.java index ee659d369..29e1c752f 100644 --- a/common/src/main/java/com/ultreon/devices/init/ModTags.java +++ b/common/src/main/java/com/ultreon/devices/init/ModTags.java @@ -5,9 +5,10 @@ import net.minecraft.tags.TagKey; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.entity.BlockEntityType; /** - * @author Qboi + * @author XyperCode */ public final class ModTags { public static final class Items { @@ -30,4 +31,12 @@ private static TagKey createTag(String name) { return TagKey.create(Registries.BLOCK, Devices.id(name)); } } + + public static final class BlockEntityTypes { + public static final TagKey> NETWORK_DEVICES = createTag("network_devices"); + + private static TagKey> createTag(String name) { + return TagKey.create(Registries.BLOCK_ENTITY_TYPE, Devices.id(name)); + } + } }