-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a new mechanism for attaching additional objects to a computer, allowing them to be queried by other mods. This is primarily designed for mods which add external APIs, allowing them to add APIs which depend on the computer's position or can interact with the turtle inventory. I will stress that the use-cases for custom APIs are few and far between. Almost all the time a peripheral would be the better option, and I am wary that this PR will encourage misuse of APIs. However, there are some legitimate use-cases, and I think we should enable them. - Add a new "ComputerComponent" class, and several built-in components (for turtle, pocket and command computers). - Add a method to `IComputerSystem` to read a component from the computer. We also add methods to get the level and position of the computer. - Move all our existing APIs (built-in turtle, pocket, command) to use the public API.
- Loading branch information
Showing
16 changed files
with
327 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
projects/common-api/src/main/java/dan200/computercraft/api/component/AdminComputer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers | ||
// | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dan200.computercraft.api.component; | ||
|
||
import net.minecraft.commands.CommandSourceStack; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
/** | ||
* A computer which has permission to perform administrative/op commands, such as the command computer. | ||
*/ | ||
@ApiStatus.NonExtendable | ||
public interface AdminComputer { | ||
/** | ||
* The permission level that this computer can operate at. | ||
* | ||
* @return The permission level for this computer. | ||
* @see CommandSourceStack#hasPermission(int) | ||
*/ | ||
default int permissionLevel() { | ||
return 2; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
projects/common-api/src/main/java/dan200/computercraft/api/component/ComputerComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers | ||
// | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dan200.computercraft.api.component; | ||
|
||
import dan200.computercraft.api.lua.IComputerSystem; | ||
import dan200.computercraft.api.lua.ILuaAPIFactory; | ||
|
||
/** | ||
* A component attached to a computer. | ||
* <p> | ||
* Components provide a mechanism to attach additional data to a computer, that can then be queried with | ||
* {@link IComputerSystem#getComponent(ComputerComponent)}. | ||
* <p> | ||
* This is largely designed for {@linkplain ILuaAPIFactory custom APIs}, allowing APIs to read additional properties | ||
* of the computer, such as its position. | ||
* | ||
* @param <T> The type of this component. | ||
* @see ComputerComponents The built-in components. | ||
*/ | ||
@SuppressWarnings("UnusedTypeParameter") | ||
public final class ComputerComponent<T> { | ||
private final String id; | ||
|
||
private ComputerComponent(String id) { | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Create a new computer component. | ||
* <p> | ||
* Mods typically will not need to create their own components. | ||
* | ||
* @param namespace The namespace of this component. This should be the mod id. | ||
* @param id The unique id of this component. | ||
* @param <T> The component | ||
* @return The newly created component. | ||
*/ | ||
public static <T> ComputerComponent<T> create(String namespace, String id) { | ||
return new ComputerComponent<>(namespace + ":" + id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ComputerComponent(" + id + ")"; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
projects/common-api/src/main/java/dan200/computercraft/api/component/ComputerComponents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers | ||
// | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dan200.computercraft.api.component; | ||
|
||
import dan200.computercraft.api.ComputerCraftAPI; | ||
import dan200.computercraft.api.pocket.IPocketAccess; | ||
import dan200.computercraft.api.turtle.ITurtleAccess; | ||
|
||
/** | ||
* The {@link ComputerComponent}s provided by ComputerCraft. | ||
*/ | ||
public class ComputerComponents { | ||
/** | ||
* The {@link ITurtleAccess} associated with a turtle. | ||
*/ | ||
public static final ComputerComponent<ITurtleAccess> TURTLE = ComputerComponent.create(ComputerCraftAPI.MOD_ID, "turtle"); | ||
|
||
/** | ||
* The {@link IPocketAccess} associated with a pocket computer. | ||
*/ | ||
public static final ComputerComponent<IPocketAccess> POCKET = ComputerComponent.create(ComputerCraftAPI.MOD_ID, "pocket"); | ||
|
||
/** | ||
* This component is only present on "command computers", and other computers with admin capabilities. | ||
*/ | ||
public static final ComputerComponent<AdminComputer> ADMIN_COMPUTER = ComputerComponent.create(ComputerCraftAPI.MOD_ID, "admin_computer"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.