mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-15 22:17:39 +00:00
Compare commits
129 Commits
v1.80pr1.3
...
v1.80pr1.7
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3ac8dde779 | ||
![]() |
17dace979a | ||
![]() |
d405316a4b | ||
![]() |
7e18f2cead | ||
![]() |
000786a1a7 | ||
![]() |
0bf13562b9 | ||
![]() |
45a189e834 | ||
![]() |
0ce6f34a09 | ||
![]() |
4d984dc5ee | ||
![]() |
984d358930 | ||
![]() |
a95893b823 | ||
![]() |
81aaead032 | ||
![]() |
a2083bcff1 | ||
![]() |
052f2a16dc | ||
![]() |
fd10ed6f62 | ||
![]() |
c0bdd4ff1d | ||
![]() |
5f0addbc3e | ||
![]() |
c9589ad0e7 | ||
![]() |
b21c495815 | ||
![]() |
18429c50f9 | ||
![]() |
7ec8ddcf7d | ||
![]() |
5bf9f9e3c5 | ||
![]() |
e4164ee9a1 | ||
![]() |
b522af3075 | ||
![]() |
8775052dee | ||
![]() |
c0c5d57e10 | ||
![]() |
4c2e97b1af | ||
![]() |
f17df15117 | ||
![]() |
bfbb18bdfc | ||
![]() |
cac65ef755 | ||
![]() |
a42793024b | ||
![]() |
7dbc4e6455 | ||
![]() |
a0d71cb3ad | ||
![]() |
83546d0acb | ||
![]() |
e2f9ddd534 | ||
![]() |
911e404bfa | ||
![]() |
bfeafe163f | ||
![]() |
6cf32f1f74 | ||
![]() |
04f162ef25 | ||
![]() |
b2aa390ae1 | ||
![]() |
6ca61f000f | ||
![]() |
20a47a7f88 | ||
![]() |
e2e6946c92 | ||
![]() |
abe917cd54 | ||
![]() |
a1d77ab8e7 | ||
![]() |
c8db671409 | ||
![]() |
52641b7bea | ||
![]() |
3e751ee94a | ||
![]() |
2b28cc3558 | ||
![]() |
f9761388b1 | ||
![]() |
d28694eb57 | ||
![]() |
d758895578 | ||
![]() |
043d5f00ca | ||
![]() |
36878e75b7 | ||
![]() |
ebb50cba48 | ||
![]() |
7c218361d9 | ||
![]() |
bb2eab0bed | ||
![]() |
e8c0cf3857 | ||
![]() |
db825a7aab | ||
![]() |
dbcae810f0 | ||
![]() |
914df8b0c7 | ||
![]() |
f753513289 | ||
![]() |
7bb8efed1d | ||
![]() |
0cec4aee8c | ||
![]() |
e0c9dc24e7 | ||
![]() |
244907a39a | ||
![]() |
9be61abd6b | ||
![]() |
922f424a78 | ||
![]() |
5c7828dd79 | ||
![]() |
74f5093d2a | ||
![]() |
4651e362c9 | ||
![]() |
a2e2a5cb37 | ||
![]() |
15a3882016 | ||
![]() |
d3ecd5214b | ||
![]() |
0a8a8a742e | ||
![]() |
ecff23d027 | ||
![]() |
20dcb32bae | ||
![]() |
678462d2db | ||
![]() |
61fdfec09b | ||
![]() |
5eadf5533d | ||
![]() |
2d3cd5dc80 | ||
![]() |
5208ad0b98 | ||
![]() |
662fb96beb | ||
![]() |
4c14431a3d | ||
![]() |
5ae38a3f18 | ||
![]() |
94e10d1f67 | ||
![]() |
0a50676884 | ||
![]() |
41cce78fcb | ||
![]() |
4c0fa1fabe | ||
![]() |
54e1dafa3f | ||
![]() |
3ac76bc05b | ||
![]() |
83030df3ee | ||
![]() |
07d15caf6f | ||
![]() |
3298efe652 | ||
![]() |
01d9919a3e | ||
![]() |
80b1170b63 | ||
![]() |
2e7302e654 | ||
![]() |
ca7fb8a0b4 | ||
![]() |
c9b0894f26 | ||
![]() |
c3454a195d | ||
![]() |
3b3dd8071b | ||
![]() |
0d28c67534 | ||
![]() |
d0af85754a | ||
![]() |
3e265c27ff | ||
![]() |
8d356f50c4 | ||
![]() |
f30c4f16c0 | ||
![]() |
8bb8caa315 | ||
![]() |
0f17a3d72e | ||
![]() |
7647369e2d | ||
![]() |
4b4208e724 | ||
![]() |
2a16a1df85 | ||
![]() |
25f7c58400 | ||
![]() |
c3db91f11f | ||
![]() |
8c66ce03d4 | ||
![]() |
2be2a0625e | ||
![]() |
c904d5041b | ||
![]() |
632762768e | ||
![]() |
c69ba205f8 | ||
![]() |
019f4dbea9 | ||
![]() |
259ea41ce3 | ||
![]() |
11290f7204 | ||
![]() |
4fb0240a36 | ||
![]() |
f20a7afa7f | ||
![]() |
5be2202b2e | ||
![]() |
b8630f739a | ||
![]() |
1ef7c8e8db | ||
![]() |
1415dd0dae | ||
![]() |
5989d021c7 | ||
![]() |
90c4ebd208 |
36
README.md
36
README.md
@@ -6,26 +6,42 @@ features of the mod. For a more stable experience, I recommend checking out the
|
||||
[original mod](https://github.com/dan200/ComputerCraft).
|
||||
|
||||
## What?
|
||||
CC: Tweaked does not aim to create a competing fork of ComputerCraft, nor am I planning to take it in in a vastly
|
||||
different direction to the original mod. In fact, CC: Tweaked aims to be a nurturing ground for various features, with
|
||||
a pull request against the original mod being the end goal.
|
||||
CC: Tweaked (or CC:T for short) does not aim to create a competing fork of ComputerCraft, nor am I planning to take it
|
||||
in in a vastly different direction to the original mod. In fact, CC:T aims to be a nurturing ground for various
|
||||
features, with a pull request against the original mod being the end goal.
|
||||
|
||||
CC: Tweaked also includes many pull requests from the community which have not yet been merged, offering a large number
|
||||
CC:T also includes many pull requests from the community which have not yet been merged, offering a large number
|
||||
of additional bug fixes and features over the original mod.
|
||||
|
||||
## Features
|
||||
CC: Tweaked contains the all features of the latest alpha, as well as numerous fixes, performance improvements and
|
||||
several additional features. I'd recommend checking out [the releases page](https://github.com/SquidDev-CC/CC-Tweaked/releases)
|
||||
to see the full changes, but here's a couple of the more interesting changes:
|
||||
|
||||
- Replace LuaJ with Cobalt.
|
||||
- Allow running multiple computers at the same time.
|
||||
- Websocket support in the HTTP library.
|
||||
- Wired modems and cables act more like multiparts.
|
||||
- Add map-like rendering for pocket computers and printed pages/books.
|
||||
- Adds the `/computercraft` command, offering various diagnostic tools for server owners. This allows operators to
|
||||
track which computers are hogging resources, turn on and shutdown multiple computers at once and interact with
|
||||
computers remotely.
|
||||
- Add full-block wired modems, allowing one to wrap non-solid peripherals (such as turtles, or chests if Plethora is
|
||||
installed).
|
||||
|
||||
## Relation to CCTweaks?
|
||||
This mod has nothing to do with CCTweaks, though there is no denying the name is a throwback to it. However, I do plan
|
||||
to migrate some features of CCTweaks into CC: Tweaked.
|
||||
This mod has nothing to do with CCTweaks, though there is no denying the name is a throwback to it. That being said,
|
||||
several features have been included, such as full block modems, the Cobalt runtime and map-like rendering for pocket
|
||||
computers.
|
||||
|
||||
## Contributing
|
||||
Any contribution is welcome, be that using the mod, reporting bugs or contributing code. If you do wish to contribute
|
||||
code, do consider submitting it to the ComputerCraft repository instead.
|
||||
|
||||
That being said, in order to start helping develop CC: Tweaked, you'll need to follow these steps:
|
||||
That being said, in order to start helping develop CC:T, you'll need to follow these steps:
|
||||
|
||||
- **Clone the repository:** `git clone https://github.com/SquidDev-CC/CC-Tweaked.git && cd CC-Tweaked`
|
||||
- **Setup Forge:** `./gradlew setupDecompWorkspace`
|
||||
- **Test your changes:** `./gradlew runClient` (or run the `GradleStart` class from your IDE).
|
||||
|
||||
If you want to run CC: Tweaked in a normal Minecraft instance, run `./gradlew build` and copy the `.jar` from
|
||||
`build/libs`.
|
||||
|
||||
If you want to run CC:T in a normal Minecraft instance, run `./gradlew build` and copy the `.jar` from `build/libs`.
|
||||
|
75
build.gradle
75
build.gradle
@@ -15,19 +15,20 @@ buildscript {
|
||||
}
|
||||
|
||||
plugins {
|
||||
id 'com.matthewprenger.cursegradle' version '1.0.9'
|
||||
id 'com.matthewprenger.cursegradle' version '1.0.10'
|
||||
}
|
||||
|
||||
apply plugin: 'net.minecraftforge.gradle.forge'
|
||||
apply plugin: 'org.ajoberstar.grgit'
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'maven'
|
||||
|
||||
|
||||
version = "1.80pr1.3"
|
||||
version = "1.80pr1.7"
|
||||
group = "org.squiddev"
|
||||
archivesBaseName = "cc-tweaked"
|
||||
|
||||
minecraft {
|
||||
version = "1.12-14.21.1.2387"
|
||||
version = "1.12.2-14.23.2.2634"
|
||||
runDir = "run"
|
||||
replace '${version}', project.version
|
||||
|
||||
@@ -36,7 +37,7 @@ minecraft {
|
||||
// stable_# stables are built at the discretion of the MCP team.
|
||||
// Use non-default mappings at your own risk. they may not allways work.
|
||||
// simply re-run your setup task after changing the mappings to update your workspace.
|
||||
mappings = "snapshot_20170629"
|
||||
mappings = "snapshot_20180324"
|
||||
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
|
||||
}
|
||||
|
||||
@@ -54,12 +55,17 @@ repositories {
|
||||
configurations {
|
||||
shade
|
||||
compile.extendsFrom shade
|
||||
deployerJars
|
||||
}
|
||||
|
||||
dependencies {
|
||||
deobfProvided "mezz.jei:jei_1.12:4.7.5.86:api"
|
||||
runtime "mezz.jei:jei_1.12:4.7.5.86"
|
||||
shade 'org.squiddev:Cobalt:0.3.0'
|
||||
deobfProvided "mezz.jei:jei_1.12.2:4.8.5.159:api"
|
||||
runtime "mezz.jei:jei_1.12.2:4.8.5.159"
|
||||
shade 'org.squiddev:Cobalt:0.3.1'
|
||||
|
||||
testCompile 'junit:junit:4.11'
|
||||
|
||||
deployerJars "org.apache.maven.wagon:wagon-ssh:3.0.0"
|
||||
}
|
||||
|
||||
javadoc {
|
||||
@@ -124,6 +130,59 @@ curseforge {
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
from components.java
|
||||
artifact sourceJar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uploadArchives {
|
||||
repositories {
|
||||
if(project.hasProperty('mavenUploadUrl')) {
|
||||
mavenDeployer {
|
||||
configuration = configurations.deployerJars
|
||||
|
||||
repository(url: project.property('mavenUploadUrl')) {
|
||||
authentication(
|
||||
userName: project.property('mavenUploadUser'),
|
||||
privateKey: project.property('mavenUploadKey'))
|
||||
}
|
||||
|
||||
pom.project {
|
||||
name 'CC: Tweaked'
|
||||
packaging 'jar'
|
||||
description 'A fork of ComputerCraft which aims to provide earlier access to the more experimental and in-development features of the mod.'
|
||||
url 'https://github.com/SquidDev-CC/CC-Tweaked'
|
||||
|
||||
scm {
|
||||
url 'https://github.com/dan200/ComputerCraft.git'
|
||||
}
|
||||
|
||||
issueManagement {
|
||||
system 'github'
|
||||
url 'https://github.com/dan200/ComputerCraft/issues'
|
||||
}
|
||||
|
||||
licenses {
|
||||
license {
|
||||
name 'ComputerCraft Public License, Version 1.0'
|
||||
url 'https://github.com/dan200/ComputerCraft/blob/master/LICENSE'
|
||||
distribution 'repo'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pom.whenConfigured { pom ->
|
||||
pom.dependencies.clear()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gradle.projectsEvaluated {
|
||||
tasks.withType(JavaCompile) {
|
||||
options.compilerArgs << "-Xlint"
|
||||
|
1
settings.gradle
Normal file
1
settings.gradle
Normal file
@@ -0,0 +1 @@
|
||||
rootProject.name = 'cc-tweaked'
|
@@ -7,23 +7,28 @@
|
||||
package dan200.computercraft;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import com.google.common.base.Converter;
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.api.media.IMediaProvider;
|
||||
import dan200.computercraft.api.network.IPacketNetwork;
|
||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||
import dan200.computercraft.api.permissions.ITurtlePermissionProvider;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import dan200.computercraft.api.turtle.event.TurtleAction;
|
||||
import dan200.computercraft.core.apis.AddressPredicate;
|
||||
import dan200.computercraft.core.filesystem.ComboMount;
|
||||
import dan200.computercraft.core.filesystem.FileMount;
|
||||
import dan200.computercraft.core.filesystem.JarMount;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
import dan200.computercraft.shared.command.CommandComputer;
|
||||
import dan200.computercraft.shared.command.CommandComputerCraft;
|
||||
import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider;
|
||||
@@ -42,6 +47,7 @@ import dan200.computercraft.shared.network.ComputerCraftPacket;
|
||||
import dan200.computercraft.shared.network.PacketHandler;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockCable;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockWiredModemFull;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
|
||||
import dan200.computercraft.shared.peripheral.modem.BlockAdvancedModem;
|
||||
import dan200.computercraft.shared.peripheral.modem.WirelessNetwork;
|
||||
@@ -55,6 +61,8 @@ import dan200.computercraft.shared.turtle.blocks.BlockTurtle;
|
||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||
import dan200.computercraft.shared.turtle.upgrades.*;
|
||||
import dan200.computercraft.shared.util.*;
|
||||
import dan200.computercraft.shared.wired.CapabilityWiredElement;
|
||||
import dan200.computercraft.shared.wired.WiredNode;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
@@ -62,11 +70,13 @@ import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.config.ConfigCategory;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
@@ -88,6 +98,7 @@ import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
@@ -140,6 +151,7 @@ public class ComputerCraft
|
||||
public static int advancedTurtleFuelLimit = 100000;
|
||||
public static boolean turtlesObeyBlockProtection = true;
|
||||
public static boolean turtlesCanPush = true;
|
||||
public static EnumSet<TurtleAction> turtleDisabledActions = EnumSet.noneOf( TurtleAction.class );
|
||||
|
||||
public static final int terminalWidth_computer = 51;
|
||||
public static final int terminalHeight_computer = 19;
|
||||
@@ -172,6 +184,7 @@ public class ComputerCraft
|
||||
public static BlockTurtle turtleAdvanced;
|
||||
public static BlockCommandComputer commandComputer;
|
||||
public static BlockAdvancedModem advancedModem;
|
||||
public static BlockWiredModemFull wiredModemFull;
|
||||
}
|
||||
|
||||
public static class Items
|
||||
@@ -222,6 +235,7 @@ public class ComputerCraft
|
||||
public static Property advancedTurtleFuelLimit;
|
||||
public static Property turtlesObeyBlockProtection;
|
||||
public static Property turtlesCanPush;
|
||||
public static Property turtleDisabledActions;
|
||||
|
||||
public static Property modem_range;
|
||||
public static Property modem_highAltitudeRange;
|
||||
@@ -277,6 +291,18 @@ public class ComputerCraft
|
||||
|
||||
// Load config
|
||||
Config.config = new Configuration( event.getSuggestedConfigurationFile() );
|
||||
loadConfig();
|
||||
|
||||
// Setup network
|
||||
networkEventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel( "CC" );
|
||||
networkEventChannel.register( new PacketHandler() );
|
||||
|
||||
proxy.preInit();
|
||||
turtleProxy.preInit();
|
||||
}
|
||||
|
||||
public static void loadConfig()
|
||||
{
|
||||
Config.config.load();
|
||||
|
||||
Config.http_enable = Config.config.get( Configuration.CATEGORY_GENERAL, "http_enable", http_enable );
|
||||
@@ -365,6 +391,9 @@ public class ComputerCraft
|
||||
Config.turtlesCanPush = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesCanPush", turtlesCanPush );
|
||||
Config.turtlesCanPush.setComment( "If set to true, Turtles will push entities out of the way instead of stopping if there is space to do so" );
|
||||
|
||||
Config.turtleDisabledActions = Config.config.get( Configuration.CATEGORY_GENERAL, "turtle_disabled_actions", new String[ 0 ] );
|
||||
Config.turtleDisabledActions.setComment( "A list of turtle actions which are disabled." );
|
||||
|
||||
Config.maxNotesPerTick = Config.config.get( Configuration.CATEGORY_GENERAL, "maxNotesPerTick", maxNotesPerTick );
|
||||
Config.maxNotesPerTick.setComment( "Maximum amount of notes a speaker can play at once" );
|
||||
|
||||
@@ -374,13 +403,6 @@ public class ComputerCraft
|
||||
}
|
||||
|
||||
syncConfig();
|
||||
|
||||
// Setup network
|
||||
networkEventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel( "CC" );
|
||||
networkEventChannel.register( new PacketHandler() );
|
||||
|
||||
proxy.preInit();
|
||||
turtleProxy.preInit();
|
||||
}
|
||||
|
||||
public static void syncConfig() {
|
||||
@@ -412,6 +434,20 @@ public class ComputerCraft
|
||||
turtlesObeyBlockProtection = Config.turtlesObeyBlockProtection.getBoolean();
|
||||
turtlesCanPush = Config.turtlesCanPush.getBoolean();
|
||||
|
||||
turtleDisabledActions.clear();
|
||||
Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo( CaseFormat.UPPER_UNDERSCORE );
|
||||
for( String value : Config.turtleDisabledActions.getStringList() )
|
||||
{
|
||||
try
|
||||
{
|
||||
turtleDisabledActions.add( TurtleAction.valueOf( converter.convert( value ) ) );
|
||||
}
|
||||
catch( IllegalArgumentException e )
|
||||
{
|
||||
ComputerCraft.log.error( "Unknown turtle action " + value );
|
||||
}
|
||||
}
|
||||
|
||||
maxNotesPerTick = Math.max(1, Config.maxNotesPerTick.getInt());
|
||||
|
||||
Config.config.save();
|
||||
@@ -427,8 +463,7 @@ public class ComputerCraft
|
||||
@Mod.EventHandler
|
||||
public void onServerStarting( FMLServerStartingEvent event )
|
||||
{
|
||||
event.registerServerCommand( new CommandComputer() );
|
||||
event.registerServerCommand( new CommandComputerCraft() );
|
||||
proxy.initServer( event.getServer() );
|
||||
}
|
||||
|
||||
@Mod.EventHandler
|
||||
@@ -438,6 +473,7 @@ public class ComputerCraft
|
||||
{
|
||||
ComputerCraft.serverComputerRegistry.reset();
|
||||
WirelessNetwork.resetNetworks();
|
||||
Tracking.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -448,6 +484,7 @@ public class ComputerCraft
|
||||
{
|
||||
ComputerCraft.serverComputerRegistry.reset();
|
||||
WirelessNetwork.resetNetworks();
|
||||
Tracking.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,11 +508,6 @@ public class ComputerCraft
|
||||
return proxy.getRenderFrame();
|
||||
}
|
||||
|
||||
public static void deleteDisplayLists( int list, int range )
|
||||
{
|
||||
proxy.deleteDisplayLists( list, range );
|
||||
}
|
||||
|
||||
public static Object getFixedWidthFontRenderer()
|
||||
{
|
||||
return proxy.getFixedWidthFontRenderer();
|
||||
@@ -704,6 +736,11 @@ public class ComputerCraft
|
||||
}
|
||||
}
|
||||
|
||||
public static IWiredNode createWiredNodeForElement( IWiredElement element )
|
||||
{
|
||||
return new WiredNode( element );
|
||||
}
|
||||
|
||||
public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side )
|
||||
{
|
||||
// Try the handlers in order:
|
||||
@@ -725,6 +762,14 @@ public class ComputerCraft
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IWiredElement getWiredElementAt( IBlockAccess world, BlockPos pos, EnumFacing side )
|
||||
{
|
||||
TileEntity tile = world.getTileEntity( pos );
|
||||
return tile != null && tile.hasCapability( CapabilityWiredElement.CAPABILITY, side )
|
||||
? tile.getCapability( CapabilityWiredElement.CAPABILITY, side )
|
||||
: null;
|
||||
}
|
||||
|
||||
public static int getDefaultBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side )
|
||||
{
|
||||
if( WorldUtil.isBlockInWorld( world, pos ) )
|
||||
@@ -826,7 +871,7 @@ public class ComputerCraft
|
||||
return upgrades;
|
||||
}
|
||||
|
||||
public IPacketNetwork getWirelessNetwork()
|
||||
public static IPacketNetwork getWirelessNetwork()
|
||||
{
|
||||
return WirelessNetwork.getUniversal();
|
||||
}
|
||||
@@ -1080,13 +1125,18 @@ public class ComputerCraft
|
||||
turtleProxy.addAllUpgradedTurtles( list );
|
||||
}
|
||||
|
||||
public static void setEntityDropConsumer( Entity entity, IEntityDropConsumer consumer )
|
||||
public static void setDropConsumer( Entity entity, Consumer<ItemStack> consumer )
|
||||
{
|
||||
turtleProxy.setEntityDropConsumer( entity, consumer );
|
||||
turtleProxy.setDropConsumer( entity, consumer );
|
||||
}
|
||||
|
||||
public static void clearEntityDropConsumer( Entity entity )
|
||||
public static void setDropConsumer( World world, BlockPos pos, Consumer<ItemStack> consumer )
|
||||
{
|
||||
turtleProxy.clearEntityDropConsumer( entity );
|
||||
turtleProxy.setDropConsumer( world, pos, consumer );
|
||||
}
|
||||
|
||||
public static void clearDropConsumer( )
|
||||
{
|
||||
turtleProxy.clearDropConsumer();
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,8 @@ import dan200.computercraft.api.lua.ILuaAPIFactory;
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.api.media.IMediaProvider;
|
||||
import dan200.computercraft.api.network.IPacketNetwork;
|
||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.peripheral.IPeripheralProvider;
|
||||
@@ -21,6 +23,7 @@ import dan200.computercraft.api.redstone.IBundledRedstoneProvider;
|
||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -328,6 +331,61 @@ public final class ComputerCraftAPI
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new wired node for a given wired element
|
||||
*
|
||||
* @param element The element to construct it for
|
||||
* @return The element's node
|
||||
* @see IWiredElement#getNode()
|
||||
*/
|
||||
@Nonnull
|
||||
public static IWiredNode createWiredNodeForElement( @Nonnull IWiredElement element )
|
||||
{
|
||||
findCC();
|
||||
if( computerCraft_createWiredNodeForElement != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
return (IWiredNode) computerCraft_createWiredNodeForElement.invoke( null, element );
|
||||
}
|
||||
catch( ReflectiveOperationException e )
|
||||
{
|
||||
throw new IllegalStateException( "Error creating wired node", e );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException( "ComputerCraft cannot be found" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the wired network element for a block in world
|
||||
*
|
||||
* @param world The world the block exists in
|
||||
* @param pos The position the block exists in
|
||||
* @param side The side to extract the network element from
|
||||
* @return The element's node
|
||||
* @see IWiredElement#getNode()
|
||||
*/
|
||||
@Nullable
|
||||
public static IWiredElement getWiredElementAt( @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
|
||||
{
|
||||
findCC();
|
||||
if( computerCraft_getWiredElementAt != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
return (IWiredElement) computerCraft_getWiredElementAt.invoke( null, world, pos, side );
|
||||
}
|
||||
catch( ReflectiveOperationException ignored )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// The functions below here are private, and are used to interface with the non-API ComputerCraft classes.
|
||||
// Reflection is used here so you can develop your mod without decompiling ComputerCraft and including
|
||||
// it in your solution, and so your mod won't crash if ComputerCraft is installed.
|
||||
@@ -374,6 +432,12 @@ public final class ComputerCraftAPI
|
||||
computerCraft_registerAPIFactory = findCCMethod( "registerAPIFactory", new Class<?>[] {
|
||||
ILuaAPIFactory.class
|
||||
} );
|
||||
computerCraft_createWiredNodeForElement = findCCMethod( "createWiredNodeForElement", new Class<?>[] {
|
||||
IWiredElement.class
|
||||
} );
|
||||
computerCraft_getWiredElementAt = findCCMethod( "getWiredElementAt", new Class<?>[]{
|
||||
IBlockAccess.class, BlockPos.class, EnumFacing.class
|
||||
} );
|
||||
} catch( Exception e ) {
|
||||
System.out.println( "ComputerCraftAPI: ComputerCraft not found." );
|
||||
} finally {
|
||||
@@ -411,4 +475,6 @@ public final class ComputerCraftAPI
|
||||
private static Method computerCraft_registerPocketUpgrade = null;
|
||||
private static Method computerCraft_getWirelessNetwork = null;
|
||||
private static Method computerCraft_registerAPIFactory = null;
|
||||
private static Method computerCraft_createWiredNodeForElement = null;
|
||||
private static Method computerCraft_getWiredElementAt = null;
|
||||
}
|
||||
|
@@ -0,0 +1,29 @@
|
||||
package dan200.computercraft.api.network.wired;
|
||||
|
||||
import dan200.computercraft.api.ComputerCraftAPI;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* An object which may be part of a wired network.
|
||||
*
|
||||
* Elements should construct a node using {@link ComputerCraftAPI#createWiredNodeForElement(IWiredElement)}. This acts
|
||||
* as a proxy for all network objects. Whilst the node may change networks, an element's node should remain constant
|
||||
* for its lifespan.
|
||||
*
|
||||
* Elements are generally tied to a block or tile entity in world. In such as case, one should provide the
|
||||
* {@link IWiredElement} capability for the appropriate sides.
|
||||
*/
|
||||
public interface IWiredElement extends IWiredSender
|
||||
{
|
||||
/**
|
||||
* Called when objects on the network change. This may occur when network nodes are added or removed, or when
|
||||
* peripherals change.
|
||||
*
|
||||
* @param change The change which occurred.
|
||||
* @see IWiredNetworkChange
|
||||
*/
|
||||
default void networkChanged( @Nonnull IWiredNetworkChange change )
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package dan200.computercraft.api.network.wired;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A wired network is composed of one of more {@link IWiredNode}s, a set of connections between them, and a series
|
||||
* of peripherals.
|
||||
*
|
||||
* Networks from a connected graph. This means there is some path between all nodes on the network. Further more, if
|
||||
* there is some path between two nodes then they must be on the same network. {@link IWiredNetwork} will automatically
|
||||
* handle the merging and splitting of networks (and thus changing of available nodes and peripherals) as connections
|
||||
* change.
|
||||
*
|
||||
* This does mean one can not rely on the network remaining consistent between subsequent operations. Consequently,
|
||||
* it is generally preferred to use the methods provided by {@link IWiredNode}.
|
||||
*
|
||||
* @see IWiredNode#getNetwork()
|
||||
*/
|
||||
public interface IWiredNetwork
|
||||
{
|
||||
/**
|
||||
* Create a connection between two nodes.
|
||||
*
|
||||
* This should only be used on the server thread.
|
||||
*
|
||||
* @param left The first node to connect
|
||||
* @param right The second node to connect
|
||||
* @return {@code true} if a connection was created or {@code false} if the connection already exists.
|
||||
* @throws IllegalStateException If neither node is on the network.
|
||||
* @throws IllegalArgumentException If {@code left} and {@code right} are equal.
|
||||
* @see IWiredNode#connectTo(IWiredNode)
|
||||
* @see IWiredNetwork#connect(IWiredNode, IWiredNode)
|
||||
*/
|
||||
boolean connect( @Nonnull IWiredNode left, @Nonnull IWiredNode right );
|
||||
|
||||
/**
|
||||
* Destroy a connection between this node and another.
|
||||
*
|
||||
* This should only be used on the server thread.
|
||||
*
|
||||
* @param left The first node in the connection.
|
||||
* @param right The second node in the connection.
|
||||
* @return {@code true} if a connection was destroyed or {@code false} if no connection exists.
|
||||
* @throws IllegalArgumentException If either node is not on the network.
|
||||
* @throws IllegalArgumentException If {@code left} and {@code right} are equal.
|
||||
* @see IWiredNode#disconnectFrom(IWiredNode)
|
||||
* @see IWiredNetwork#connect(IWiredNode, IWiredNode)
|
||||
*/
|
||||
boolean disconnect( @Nonnull IWiredNode left, @Nonnull IWiredNode right );
|
||||
|
||||
/**
|
||||
* Sever all connections this node has, removing it from this network.
|
||||
*
|
||||
* This should only be used on the server thread. You should only call this on nodes
|
||||
* that your network element owns.
|
||||
*
|
||||
* @param node The node to remove
|
||||
* @return Whether this node was removed from the network. One cannot remove a node from a network where it is the
|
||||
* only element.
|
||||
* @throws IllegalArgumentException If the node is not in the network.
|
||||
* @see IWiredNode#remove()
|
||||
*/
|
||||
boolean remove( @Nonnull IWiredNode node );
|
||||
|
||||
/**
|
||||
* Update the peripherals a node provides.
|
||||
*
|
||||
* This should only be used on the server thread. You should only call this on nodes
|
||||
* that your network element owns.
|
||||
*
|
||||
* @param node The node to attach peripherals for.
|
||||
* @param peripherals The new peripherals for this node.
|
||||
* @throws IllegalArgumentException If the node is not in the network.
|
||||
* @see IWiredNode#updatePeripherals(Map)
|
||||
*/
|
||||
void updatePeripherals( @Nonnull IWiredNode node, @Nonnull Map<String, IPeripheral> peripherals );
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package dan200.computercraft.api.network.wired;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents a change to the objects on a wired network.
|
||||
*
|
||||
* @see IWiredElement#networkChanged(IWiredNetworkChange)
|
||||
*/
|
||||
public interface IWiredNetworkChange
|
||||
{
|
||||
/**
|
||||
* A set of peripherals which have been removed. Note that there may be entries with the same name
|
||||
* in the added and removed set, but with a different peripheral.
|
||||
*
|
||||
* @return The set of removed peripherals.
|
||||
*/
|
||||
@Nonnull
|
||||
Map<String, IPeripheral> peripheralsRemoved();
|
||||
|
||||
/**
|
||||
* A set of peripherals which have been added. Note that there may be entries with the same name
|
||||
* in the added and removed set, but with a different peripheral.
|
||||
*
|
||||
* @return The set of added peripherals.
|
||||
*/
|
||||
@Nonnull
|
||||
Map<String, IPeripheral> peripheralsAdded();
|
||||
}
|
@@ -0,0 +1,103 @@
|
||||
package dan200.computercraft.api.network.wired;
|
||||
|
||||
import dan200.computercraft.api.network.IPacketNetwork;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Wired nodes act as a layer between {@link IWiredElement}s and {@link IWiredNetwork}s.
|
||||
*
|
||||
* Firstly, a node acts as a packet network, capable of sending and receiving modem messages to connected nodes. These
|
||||
* methods may be safely used on any thread.
|
||||
*
|
||||
* When sending a packet, the system will attempt to find the shortest path between the two nodes based on their
|
||||
* element's position. Note that packet senders and receivers can have different locations from their associated
|
||||
* element: the distance between the two will be added to the total packet's distance.
|
||||
*
|
||||
* Wired nodes also provide several convenience methods for interacting with a wired network. These should only ever
|
||||
* be used on the main server thread.
|
||||
*/
|
||||
public interface IWiredNode extends IPacketNetwork
|
||||
{
|
||||
/**
|
||||
* The associated element for this network node.
|
||||
*
|
||||
* @return This node's element.
|
||||
*/
|
||||
@Nonnull
|
||||
IWiredElement getElement();
|
||||
|
||||
/**
|
||||
* The network this node is currently connected to. Note that this may change
|
||||
* after any network operation, so it should not be cached.
|
||||
*
|
||||
* This should only be used on the server thread.
|
||||
*
|
||||
* @return This node's network.
|
||||
*/
|
||||
@Nonnull
|
||||
IWiredNetwork getNetwork();
|
||||
|
||||
/**
|
||||
* Create a connection from this node to another.
|
||||
*
|
||||
* This should only be used on the server thread.
|
||||
*
|
||||
* @param node The other node to connect to.
|
||||
* @return {@code true} if a connection was created or {@code false} if the connection already exists.
|
||||
* @see IWiredNetwork#connect(IWiredNode, IWiredNode)
|
||||
* @see IWiredNode#disconnectFrom(IWiredNode)
|
||||
*/
|
||||
default boolean connectTo( @Nonnull IWiredNode node )
|
||||
{
|
||||
return getNetwork().connect( this, node );
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy a connection between this node and another.
|
||||
*
|
||||
* This should only be used on the server thread.
|
||||
*
|
||||
* @param node The other node to disconnect from.
|
||||
* @return {@code true} if a connection was destroyed or {@code false} if no connection exists.
|
||||
* @throws IllegalArgumentException If {@code node} is not on the same network.
|
||||
* @see IWiredNetwork#disconnect(IWiredNode, IWiredNode)
|
||||
* @see IWiredNode#connectTo(IWiredNode)
|
||||
*/
|
||||
default boolean disconnectFrom( @Nonnull IWiredNode node )
|
||||
{
|
||||
return getNetwork().disconnect( this, node );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sever all connections this node has, removing it from this network.
|
||||
*
|
||||
* This should only be used on the server thread. You should only call this on nodes
|
||||
* that your network element owns.
|
||||
*
|
||||
* @return Whether this node was removed from the network. One cannot remove a node from a network where it is the
|
||||
* only element.
|
||||
* @throws IllegalArgumentException If the node is not in the network.
|
||||
* @see IWiredNetwork#remove(IWiredNode)
|
||||
*/
|
||||
default boolean remove()
|
||||
{
|
||||
return getNetwork().remove( this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark this node's peripherals as having changed.
|
||||
*
|
||||
* This should only be used on the server thread. You should only call this on nodes
|
||||
* that your network element owns.
|
||||
*
|
||||
* @param peripherals The new peripherals for this node.
|
||||
* @see IWiredNetwork#updatePeripherals(IWiredNode, Map)
|
||||
*/
|
||||
default void updatePeripherals( @Nonnull Map<String, IPeripheral> peripherals )
|
||||
{
|
||||
getNetwork().updatePeripherals( this, peripherals );
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package dan200.computercraft.api.network.wired;
|
||||
|
||||
import dan200.computercraft.api.network.IPacketSender;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* An object on a {@link IWiredNetwork} capable of sending packets.
|
||||
*
|
||||
* Unlike a regular {@link IPacketSender}, this must be associated with the node you are attempting to
|
||||
* to send the packet from.
|
||||
*/
|
||||
public interface IWiredSender extends IPacketSender
|
||||
{
|
||||
/**
|
||||
* The node in the network representing this object.
|
||||
*
|
||||
* This should be used as a proxy for the main network. One should send packets
|
||||
* and register receivers through this object.
|
||||
*
|
||||
* @return The node for this element.
|
||||
*/
|
||||
@Nonnull
|
||||
IWiredNode getNode();
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
@API( owner="ComputerCraft", provides="ComputerCraft|API|Network|Wired", apiVersion="${version}" )
|
||||
package dan200.computercraft.api.network.wired;
|
||||
|
||||
import net.minecraftforge.fml.common.API;
|
@@ -13,6 +13,8 @@ import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The interface passed to peripherals by computers or turtles, providing methods
|
||||
@@ -154,4 +156,33 @@ public interface IComputerAccess
|
||||
*/
|
||||
@Nonnull
|
||||
String getAttachmentName();
|
||||
|
||||
/**
|
||||
* Get a set of peripherals that this computer access can "see", along with their attachment name.
|
||||
*
|
||||
* This may include other peripherals on the wired network or peripherals on other sides of the computer.
|
||||
*
|
||||
* @return All reachable peripherals
|
||||
* @see #getAttachmentName()
|
||||
* @see #getAvailablePeripheral(String)
|
||||
*/
|
||||
@Nonnull
|
||||
default Map<String, IPeripheral> getAvailablePeripherals()
|
||||
{
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a reachable peripheral with the given attachement name. This is a equivalent to
|
||||
* {@link #getAvailablePeripherals()}{@code .get(name)}, though may be more performant.
|
||||
*
|
||||
* @param name The peripheral's attached name
|
||||
* @return The reachable peripheral, or {@code null} if none can be found.
|
||||
* @see #getAvailablePeripherals()
|
||||
*/
|
||||
@Nullable
|
||||
default IPeripheral getAvailablePeripheral( @Nonnull String name )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -114,6 +114,18 @@ public interface IPeripheral
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the object that this peripheral provides methods for. This will generally be the tile entity
|
||||
* or block, but may be an inventory, entity, etc...
|
||||
*
|
||||
* @return The object this peripheral targets
|
||||
*/
|
||||
@Nonnull
|
||||
default Object getTarget()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether this peripheral is equivalent to another one.
|
||||
*
|
||||
|
@@ -6,6 +6,7 @@
|
||||
|
||||
package dan200.computercraft.api.turtle;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
@@ -135,6 +136,14 @@ public interface ITurtleAccess
|
||||
*/
|
||||
int getColour();
|
||||
|
||||
/**
|
||||
* Get the player who owns this turtle, namely whoever placed it.
|
||||
*
|
||||
* @return This turtle's owner.
|
||||
*/
|
||||
@Nonnull
|
||||
GameProfile getOwningPlayer();
|
||||
|
||||
/**
|
||||
* Get the inventory of this turtle
|
||||
*
|
||||
@@ -148,7 +157,7 @@ public interface ITurtleAccess
|
||||
* Get the inventory of this turtle as an {@link IItemHandlerModifiable}.
|
||||
*
|
||||
* @return This turtle's inventory
|
||||
* @see #getInventory()
|
||||
* @see #getInventory()
|
||||
* @see IItemHandlerModifiable
|
||||
* @see net.minecraftforge.items.CapabilityItemHandler#ITEM_HANDLER_CAPABILITY
|
||||
*/
|
||||
|
@@ -8,11 +8,15 @@ package dan200.computercraft.api.turtle;
|
||||
|
||||
import dan200.computercraft.api.ComputerCraftAPI;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.turtle.event.TurtleAttackEvent;
|
||||
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
|
||||
import net.minecraft.client.renderer.block.model.IBakedModel;
|
||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.event.entity.player.AttackEntityEvent;
|
||||
import net.minecraftforge.event.world.BlockEvent;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
@@ -100,6 +104,9 @@ public interface ITurtleUpgrade
|
||||
* Will only be called for Tool turtle. Called when turtle.dig() or turtle.attack() is called
|
||||
* by the turtle, and the tool is required to do some work.
|
||||
*
|
||||
* Conforming implementations should fire {@link BlockEvent.BreakEvent} and {@link TurtleBlockEvent.Dig}for digging,
|
||||
* {@link AttackEntityEvent} and {@link TurtleAttackEvent} for attacking.
|
||||
*
|
||||
* @param turtle Access to the turtle that the tool resides on.
|
||||
* @param side Which side of the turtle (left or right) the tool resides on.
|
||||
* @param verb Which action (dig or attack) the turtle is being called on to perform.
|
||||
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
/**
|
||||
* A basic action that a turtle may perform, as accessed by the {@code turtle} API.
|
||||
*
|
||||
* @see TurtleActionEvent
|
||||
*/
|
||||
public enum TurtleAction
|
||||
{
|
||||
/**
|
||||
* A turtle moves to a new position.
|
||||
*
|
||||
* @see TurtleBlockEvent.Move
|
||||
*/
|
||||
MOVE,
|
||||
|
||||
/**
|
||||
* A turtle turns in a specific direction.
|
||||
*/
|
||||
TURN,
|
||||
|
||||
/**
|
||||
* A turtle attempts to dig a block.
|
||||
*
|
||||
* @see TurtleBlockEvent.Dig
|
||||
*/
|
||||
DIG,
|
||||
|
||||
/**
|
||||
* A turtle attempts to place a block or item in the world.
|
||||
*
|
||||
* @see TurtleBlockEvent.Place
|
||||
*/
|
||||
PLACE,
|
||||
|
||||
/**
|
||||
* A turtle attempts to attack an entity.
|
||||
*
|
||||
* @see TurtleActionEvent
|
||||
*/
|
||||
ATTACK,
|
||||
|
||||
/**
|
||||
* Drop an item into an inventory/the world.
|
||||
*
|
||||
* @see TurtleInventoryEvent.Drop
|
||||
*/
|
||||
DROP,
|
||||
|
||||
/**
|
||||
* Suck an item from an inventory or the world.
|
||||
*
|
||||
* @see TurtleInventoryEvent.Suck
|
||||
*/
|
||||
SUCK,
|
||||
|
||||
/**
|
||||
* Refuel the turtle's fuel levels.
|
||||
*/
|
||||
REFUEL,
|
||||
|
||||
/**
|
||||
* Equip or unequip an item.
|
||||
*/
|
||||
EQUIP,
|
||||
|
||||
/**
|
||||
* Inspect a block in world
|
||||
*
|
||||
* @see TurtleBlockEvent.Inspect
|
||||
*/
|
||||
INSPECT,
|
||||
|
||||
/**
|
||||
* Gather metdata about an item in the turtle's inventory.
|
||||
*/
|
||||
INSPECT_ITEM,
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import dan200.computercraft.api.turtle.TurtleCommandResult;
|
||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* An event fired when a turtle is performing a known action.
|
||||
*/
|
||||
@Cancelable
|
||||
public class TurtleActionEvent extends TurtleEvent
|
||||
{
|
||||
private final TurtleAction action;
|
||||
private String failureMessage;
|
||||
|
||||
public TurtleActionEvent( @Nonnull ITurtleAccess turtle, @Nonnull TurtleAction action )
|
||||
{
|
||||
super( turtle );
|
||||
|
||||
Preconditions.checkNotNull( action, "action cannot be null" );
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public TurtleAction getAction()
|
||||
{
|
||||
return action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the cancellation state of this action.
|
||||
*
|
||||
* If {@code cancel} is {@code true}, this action will not be carried out.
|
||||
*
|
||||
* @param cancel The new canceled value.
|
||||
* @see TurtleCommandResult#failure()
|
||||
* @deprecated Use {@link #setCanceled(boolean, String)} instead.
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public void setCanceled( boolean cancel )
|
||||
{
|
||||
setCanceled( cancel, null );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cancellation state of this action, setting a failure message if required.
|
||||
*
|
||||
* If {@code cancel} is {@code true}, this action will not be carried out.
|
||||
*
|
||||
* @param cancel The new canceled value.
|
||||
* @param failureMessage The message to return to the user explaining the failure.
|
||||
* @see TurtleCommandResult#failure(String)
|
||||
*/
|
||||
public void setCanceled( boolean cancel, @Nullable String failureMessage )
|
||||
{
|
||||
super.setCanceled( cancel );
|
||||
this.failureMessage = cancel ? failureMessage : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message with which this will fail.
|
||||
*
|
||||
* @return The failure message.
|
||||
* @see TurtleCommandResult#failure()
|
||||
* @see #setCanceled(boolean, String)
|
||||
*/
|
||||
@Nullable
|
||||
public String getFailureMessage()
|
||||
{
|
||||
return failureMessage;
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import dan200.computercraft.api.turtle.TurtleSide;
|
||||
import dan200.computercraft.api.turtle.TurtleVerb;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
import net.minecraftforge.event.entity.player.AttackEntityEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to attack an entity.
|
||||
*
|
||||
* This must be fired by {@link ITurtleUpgrade#useTool(ITurtleAccess, TurtleSide, TurtleVerb, EnumFacing)},
|
||||
* as the base {@code turtle.attack()} command does not fire it.
|
||||
*
|
||||
* Note that such commands should also fire {@link AttackEntityEvent}, so you do not need to listen to both.
|
||||
*
|
||||
* @see TurtleAction#ATTACK
|
||||
*/
|
||||
public class TurtleAttackEvent extends TurtlePlayerEvent
|
||||
{
|
||||
private final Entity target;
|
||||
private final ITurtleUpgrade upgrade;
|
||||
private final TurtleSide side;
|
||||
|
||||
public TurtleAttackEvent( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull Entity target, @Nonnull ITurtleUpgrade upgrade, @Nonnull TurtleSide side )
|
||||
{
|
||||
super( turtle, TurtleAction.ATTACK, player );
|
||||
Preconditions.checkNotNull( target, "target cannot be null" );
|
||||
Preconditions.checkNotNull( upgrade, "upgrade cannot be null" );
|
||||
Preconditions.checkNotNull( side, "side cannot be null" );
|
||||
this.target = target;
|
||||
this.upgrade = upgrade;
|
||||
this.side = side;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entity being attacked by this turtle.
|
||||
*
|
||||
* @return The entity being attacked.
|
||||
*/
|
||||
@Nonnull
|
||||
public Entity getTarget()
|
||||
{
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the upgrade responsible for attacking.
|
||||
*
|
||||
* @return The upgrade responsible for attacking.
|
||||
*/
|
||||
@Nonnull
|
||||
public ITurtleUpgrade getUpgrade()
|
||||
{
|
||||
return upgrade;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the side the attacking upgrade is on.
|
||||
*
|
||||
* @return The upgrade's side.
|
||||
*/
|
||||
@Nonnull
|
||||
public TurtleSide getSide()
|
||||
{
|
||||
return side;
|
||||
}
|
||||
}
|
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import dan200.computercraft.api.turtle.TurtleSide;
|
||||
import dan200.computercraft.api.turtle.TurtleVerb;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
import net.minecraftforge.event.world.BlockEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A general event for when a turtle interacts with a block or region.
|
||||
*
|
||||
* You should generally listen to one of the sub-events instead, cancelling them where
|
||||
* appropriate.
|
||||
*
|
||||
* Note that you are not guaranteed to receive this event, if it has been cancelled by other
|
||||
* mechanisms, such as block protection systems.
|
||||
*
|
||||
* Be aware that some events (such as {@link TurtleInventoryEvent}) do not necessarily interact
|
||||
* with a block, simply objects within that block space.
|
||||
*/
|
||||
@Cancelable
|
||||
public abstract class TurtleBlockEvent extends TurtlePlayerEvent
|
||||
{
|
||||
private final World world;
|
||||
private final BlockPos pos;
|
||||
|
||||
protected TurtleBlockEvent( @Nonnull ITurtleAccess turtle, @Nonnull TurtleAction action, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos )
|
||||
{
|
||||
super( turtle, action, player );
|
||||
|
||||
Preconditions.checkNotNull( world, "world cannot be null" );
|
||||
Preconditions.checkNotNull( pos, "pos cannot be null" );
|
||||
this.world = world;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the world the turtle is interacting in.
|
||||
*
|
||||
* @return The world the turtle is interacting in.
|
||||
*/
|
||||
public World getWorld()
|
||||
{
|
||||
return world;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the position the turtle is interacting with. Note that this is different
|
||||
* to {@link ITurtleAccess#getPosition()}.
|
||||
*
|
||||
* @return The position the turtle is interacting with.
|
||||
*/
|
||||
public BlockPos getPos()
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to dig a block.
|
||||
*
|
||||
* This must be fired by {@link ITurtleUpgrade#useTool(ITurtleAccess, TurtleSide, TurtleVerb, EnumFacing)},
|
||||
* as the base {@code turtle.dig()} command does not fire it.
|
||||
*
|
||||
* Note that such commands should also fire {@link BlockEvent.BreakEvent}, so you do not need to listen to both.
|
||||
*
|
||||
* @see TurtleAction#DIG
|
||||
*/
|
||||
@Cancelable
|
||||
public static class Dig extends TurtleBlockEvent
|
||||
{
|
||||
private final IBlockState block;
|
||||
private final ITurtleUpgrade upgrade;
|
||||
private final TurtleSide side;
|
||||
|
||||
public Dig( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState block, @Nonnull ITurtleUpgrade upgrade, @Nonnull TurtleSide side )
|
||||
{
|
||||
super( turtle, TurtleAction.DIG, player, world, pos );
|
||||
|
||||
Preconditions.checkNotNull( block, "block cannot be null" );
|
||||
Preconditions.checkNotNull( upgrade, "upgrade cannot be null" );
|
||||
Preconditions.checkNotNull( side, "side cannot be null" );
|
||||
this.block = block;
|
||||
this.upgrade = upgrade;
|
||||
this.side = side;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the block which is about to be broken.
|
||||
*
|
||||
* @return The block which is going to be broken.
|
||||
*/
|
||||
@Nonnull
|
||||
public IBlockState getBlock()
|
||||
{
|
||||
return block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the upgrade doing the digging
|
||||
*
|
||||
* @return The upgrade doing the digging.
|
||||
*/
|
||||
@Nonnull
|
||||
public ITurtleUpgrade getUpgrade()
|
||||
{
|
||||
return upgrade;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the side the upgrade doing the digging is on.
|
||||
*
|
||||
* @return The upgrade's side.
|
||||
*/
|
||||
@Nonnull
|
||||
public TurtleSide getSide()
|
||||
{
|
||||
return side;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to move into a block.
|
||||
*
|
||||
* @see TurtleAction#MOVE
|
||||
*/
|
||||
@Cancelable
|
||||
public static class Move extends TurtleBlockEvent
|
||||
{
|
||||
public Move( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos )
|
||||
{
|
||||
super( turtle, TurtleAction.MOVE, player, world, pos );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to place a block in the world.
|
||||
*
|
||||
* @see TurtleAction#PLACE
|
||||
*/
|
||||
@Cancelable
|
||||
public static class Place extends TurtleBlockEvent
|
||||
{
|
||||
private final ItemStack stack;
|
||||
|
||||
public Place( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull ItemStack stack )
|
||||
{
|
||||
super( turtle, TurtleAction.PLACE, player, world, pos );
|
||||
|
||||
Preconditions.checkNotNull( stack, "stack cannot be null" );
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the item stack that will be placed. This should not be modified.
|
||||
*
|
||||
* @return The item stack to be placed.
|
||||
*/
|
||||
@Nonnull
|
||||
public ItemStack getStack()
|
||||
{
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when a turtle gathers data on a block in world.
|
||||
*
|
||||
* You may prevent blocks being inspected, or add additional information to the result.
|
||||
*
|
||||
* @see TurtleAction#INSPECT
|
||||
*/
|
||||
@Cancelable
|
||||
public static class Inspect extends TurtleBlockEvent
|
||||
{
|
||||
private final IBlockState state;
|
||||
private final Map<String, Object> data;
|
||||
|
||||
public Inspect( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Map<String, Object> data )
|
||||
{
|
||||
super( turtle, TurtleAction.INSPECT, player, world, pos );
|
||||
|
||||
Preconditions.checkNotNull( state, "state cannot be null" );
|
||||
Preconditions.checkNotNull( data, "data cannot be null" );
|
||||
this.data = data;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the block state which is being inspected.
|
||||
*
|
||||
* @return The inspected block state.
|
||||
*/
|
||||
@Nonnull
|
||||
public IBlockState getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "inspection data" from this block, which will be returned to the user.
|
||||
*
|
||||
* @return This block's inspection data.
|
||||
*/
|
||||
@Nonnull
|
||||
public Map<String, Object> getData()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new information to the inspection result. Note this will override fields with the same name.
|
||||
*
|
||||
* @param newData The data to add. Note all values should be convertable to Lua (see
|
||||
* {@link dan200.computercraft.api.peripheral.IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}).
|
||||
*/
|
||||
public void addData( @Nonnull Map<String, ?> newData )
|
||||
{
|
||||
Preconditions.checkNotNull( newData, "newData cannot be null" );
|
||||
data.putAll( newData );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* A base class for all events concerning a turtle. This will only ever constructed and fired on the server side,
|
||||
* so sever specific methods on {@link ITurtleAccess} are safe to use.
|
||||
*
|
||||
* You should generally not need to subscribe to this event, preferring one of the more specific classes.
|
||||
*
|
||||
* @see TurtleActionEvent
|
||||
*/
|
||||
public abstract class TurtleEvent extends Event
|
||||
{
|
||||
private final ITurtleAccess turtle;
|
||||
|
||||
protected TurtleEvent( @Nonnull ITurtleAccess turtle )
|
||||
{
|
||||
Preconditions.checkNotNull( turtle, "turtle cannot be null" );
|
||||
this.turtle = turtle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the turtle which is performing this action.
|
||||
*
|
||||
* @return The access for this turtle.
|
||||
*/
|
||||
@Nonnull
|
||||
public ITurtleAccess getTurtle()
|
||||
{
|
||||
return turtle;
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to interact with an inventory.
|
||||
*/
|
||||
@Cancelable
|
||||
public abstract class TurtleInventoryEvent extends TurtleBlockEvent
|
||||
{
|
||||
private final IItemHandler handler;
|
||||
|
||||
protected TurtleInventoryEvent( @Nonnull ITurtleAccess turtle, @Nonnull TurtleAction action, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nullable IItemHandler handler )
|
||||
{
|
||||
super( turtle, action, player, world, pos );
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the inventory being interacted with
|
||||
*
|
||||
* @return The inventory being interacted with, {@code null} if the item will be dropped to/sucked from the world.
|
||||
*/
|
||||
@Nullable
|
||||
public IItemHandler getItemHandler()
|
||||
{
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to suck from an inventory.
|
||||
*
|
||||
* @see TurtleAction#SUCK
|
||||
*/
|
||||
@Cancelable
|
||||
public static class Suck extends TurtleInventoryEvent
|
||||
{
|
||||
public Suck( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nullable IItemHandler handler )
|
||||
{
|
||||
super( turtle, TurtleAction.SUCK, player, world, pos, handler );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when a turtle attempts to drop an item into an inventory.
|
||||
*
|
||||
* @see TurtleAction#DROP
|
||||
*/
|
||||
@Cancelable
|
||||
public static class Drop extends TurtleInventoryEvent
|
||||
{
|
||||
private final ItemStack stack;
|
||||
|
||||
public Drop( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nullable IItemHandler handler, @Nonnull ItemStack stack )
|
||||
{
|
||||
super( turtle, TurtleAction.DROP, player, world, pos, handler );
|
||||
|
||||
Preconditions.checkNotNull( stack, "stack cannot be null" );
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* The item which will be inserted into the inventory/dropped on the ground.
|
||||
*
|
||||
* Note that this is a copy of the original stack, and so should not be modified, as that will have no effect.
|
||||
*
|
||||
* @return The item stack which will be dropped.
|
||||
*/
|
||||
public ItemStack getStack()
|
||||
{
|
||||
return stack.copy();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* An action done by a turtle which is normally done by a player.
|
||||
*
|
||||
* {@link #getPlayer()} may be used to modify the player's attributes or perform permission checks.
|
||||
*/
|
||||
public abstract class TurtlePlayerEvent extends TurtleActionEvent
|
||||
{
|
||||
private final FakePlayer player;
|
||||
|
||||
protected TurtlePlayerEvent( @Nonnull ITurtleAccess turtle, @Nonnull TurtleAction action, @Nonnull FakePlayer player )
|
||||
{
|
||||
super( turtle, action );
|
||||
|
||||
Preconditions.checkNotNull( player, "player cannot be null" );
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* A fake player, representing this turtle.
|
||||
*
|
||||
* This may be used for triggering permission checks.
|
||||
*
|
||||
* @return A {@link FakePlayer} representing this turtle.
|
||||
*/
|
||||
@Nonnull
|
||||
public FakePlayer getPlayer()
|
||||
{
|
||||
return player;
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* This file is part of the public ComputerCraft API - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. This API may be redistributed unmodified and in full only.
|
||||
* For help using the API, and posting your mods, visit the forums at computercraft.info.
|
||||
*/
|
||||
|
||||
@API(owner = "ComputerCraft", provides = "ComputerCraft|API|Turtle|Event", apiVersion = "${version}")
|
||||
package dan200.computercraft.api.turtle.event;
|
||||
|
||||
import net.minecraftforge.fml.common.API;
|
@@ -6,25 +6,19 @@
|
||||
|
||||
package dan200.computercraft.client.gui;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.core.terminal.TextBuffer;
|
||||
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
|
||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||
import dan200.computercraft.shared.util.Palette;
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import org.lwjgl.input.Mouse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static dan200.computercraft.client.render.PrintoutRenderer.*;
|
||||
|
||||
public class GuiPrintout extends GuiContainer
|
||||
{
|
||||
private static final ResourceLocation background = new ResourceLocation( "computercraft", "textures/gui/printout.png" );
|
||||
|
||||
private static final int xSize = 172;
|
||||
private static final int ySize = 209;
|
||||
|
||||
private final boolean m_book;
|
||||
private final int m_pages;
|
||||
private final TextBuffer[] m_text;
|
||||
@@ -34,23 +28,18 @@ public class GuiPrintout extends GuiContainer
|
||||
public GuiPrintout( ContainerHeldItem container )
|
||||
{
|
||||
super( container );
|
||||
m_book = (ItemPrintout.getType( container.getStack() ) == ItemPrintout.Type.Book);
|
||||
|
||||
String[] text = ItemPrintout.getText( container.getStack() );
|
||||
m_text = new TextBuffer[ text.length ];
|
||||
for( int i=0; i<m_text.length; ++i )
|
||||
{
|
||||
m_text[i] = new TextBuffer( text[i] );
|
||||
}
|
||||
for( int i = 0; i < m_text.length; ++i ) m_text[ i ] = new TextBuffer( text[ i ] );
|
||||
|
||||
String[] colours = ItemPrintout.getColours( container.getStack() );
|
||||
m_colours = new TextBuffer[ colours.length ];
|
||||
for( int i=0; i<m_colours.length; ++i )
|
||||
{
|
||||
m_colours[i] = new TextBuffer( colours[i] );
|
||||
}
|
||||
for( int i = 0; i < m_colours.length; ++i ) m_colours[ i ] = new TextBuffer( colours[ i ] );
|
||||
|
||||
m_pages = Math.max( m_text.length / ItemPrintout.LINES_PER_PAGE, 1 );
|
||||
m_page = 0;
|
||||
m_pages = Math.max( m_text.length / ItemPrintout.LINES_PER_PAGE, 1 );
|
||||
m_book = ItemPrintout.getType( container.getStack() ) == ItemPrintout.Type.Book;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -78,25 +67,19 @@ public class GuiPrintout extends GuiContainer
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void keyTyped(char c, int k) throws IOException
|
||||
protected void keyTyped( char c, int k ) throws IOException
|
||||
{
|
||||
super.keyTyped( c, k );
|
||||
|
||||
if( k == 205 )
|
||||
{
|
||||
// Right
|
||||
if( m_page < m_pages - 1 )
|
||||
{
|
||||
m_page = m_page + 1;
|
||||
}
|
||||
if( m_page < m_pages - 1 ) m_page++;
|
||||
}
|
||||
else if( k == 203 )
|
||||
{
|
||||
// Left
|
||||
if( m_page > 0 )
|
||||
{
|
||||
m_page = m_page - 1;
|
||||
}
|
||||
// Left
|
||||
if( m_page > 0 ) m_page--;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,21 +89,15 @@ public class GuiPrintout extends GuiContainer
|
||||
super.handleMouseInput();
|
||||
|
||||
int mouseWheelChange = Mouse.getEventDWheel();
|
||||
if (mouseWheelChange < 0)
|
||||
if( mouseWheelChange < 0 )
|
||||
{
|
||||
// Up
|
||||
if( m_page < m_pages - 1 )
|
||||
{
|
||||
m_page = m_page + 1;
|
||||
}
|
||||
if( m_page < m_pages - 1 ) m_page++;
|
||||
}
|
||||
else if (mouseWheelChange > 0)
|
||||
else if( mouseWheelChange > 0 )
|
||||
{
|
||||
// Down
|
||||
if( m_page > 0 )
|
||||
{
|
||||
m_page = m_page - 1;
|
||||
}
|
||||
if( m_page > 0 ) m_page--;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,78 +112,20 @@ public class GuiPrintout extends GuiContainer
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawScreen(int mouseX, int mouseY, float f)
|
||||
public void drawScreen( int mouseX, int mouseY, float f )
|
||||
{
|
||||
// Draw background
|
||||
zLevel = zLevel - 1;
|
||||
drawDefaultBackground();
|
||||
|
||||
zLevel = zLevel + 1;
|
||||
|
||||
// Draw the printout
|
||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||
this.mc.getTextureManager().bindTexture( background );
|
||||
|
||||
int startY = (height - ySize) / 2;
|
||||
//int startX = (width - xSize) / 2 - (m_page * 8);
|
||||
int startX = (width - (xSize + (m_pages - 1)*8)) / 2;
|
||||
|
||||
if( m_book )
|
||||
{
|
||||
// Border
|
||||
drawTexturedModalRect( startX - 8, startY - 8, xSize + 48, 0, 12, ySize + 24);
|
||||
drawTexturedModalRect( startX + xSize + (m_pages - 1)*8 - 4, startY - 8, xSize + 48 + 12, 0, 12, ySize + 24);
|
||||
|
||||
drawTexturedModalRect( startX, startY - 8, 0, ySize, xSize, 12);
|
||||
drawTexturedModalRect( startX, startY + ySize - 4, 0, ySize + 12, xSize, 12);
|
||||
for( int n=1; n<m_pages; ++n )
|
||||
{
|
||||
drawTexturedModalRect( startX + xSize + (n-1)*8, startY - 8, 0, ySize, 8, 12);
|
||||
drawTexturedModalRect( startX + xSize + (n-1)*8, startY + ySize - 4, 0, ySize + 12, 8, 12);
|
||||
}
|
||||
}
|
||||
|
||||
// Left half
|
||||
if( m_page == 0 )
|
||||
{
|
||||
drawTexturedModalRect( startX, startY, 24, 0, xSize / 2, ySize);
|
||||
drawTexturedModalRect( startX, startY, 0, 0, 12, ySize);
|
||||
}
|
||||
else
|
||||
{
|
||||
drawTexturedModalRect( startX, startY, 0, 0, 12, ySize);
|
||||
for( int n=1; n<m_page; ++n )
|
||||
{
|
||||
drawTexturedModalRect( startX + n*8, startY, 12, 0, 12, ySize);
|
||||
}
|
||||
drawTexturedModalRect( startX + m_page*8, startY, 24, 0, xSize / 2, ySize);
|
||||
}
|
||||
|
||||
// Right half
|
||||
if( m_page == (m_pages - 1) )
|
||||
{
|
||||
drawTexturedModalRect( startX + m_page*8 + xSize/2, startY, 24 + xSize / 2, 0, xSize / 2, ySize);
|
||||
drawTexturedModalRect( startX + m_page*8 + (xSize - 12), startY, 24 + xSize + 12, 0, 12, ySize);
|
||||
}
|
||||
else
|
||||
{
|
||||
drawTexturedModalRect( startX + (m_pages - 1)*8 + (xSize - 12), startY, 24 + xSize + 12, 0, 12, ySize);
|
||||
for( int n=m_pages-2; n>=m_page; --n )
|
||||
{
|
||||
drawTexturedModalRect( startX + n*8 + (xSize - 12), startY, 24 + xSize, 0, 12, ySize);
|
||||
}
|
||||
drawTexturedModalRect( startX + m_page*8 + xSize/2, startY, 24 + xSize / 2, 0, xSize / 2, ySize);
|
||||
}
|
||||
|
||||
// Draw the text
|
||||
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer)ComputerCraft.getFixedWidthFontRenderer();
|
||||
int x = startX + m_page * 8 + 13;
|
||||
int y = startY + 11;
|
||||
for( int line=0; line<ItemPrintout.LINES_PER_PAGE; ++line )
|
||||
{
|
||||
int lineIdx = ItemPrintout.LINES_PER_PAGE * m_page + line;
|
||||
if( lineIdx >= 0 && lineIdx < m_text.length )
|
||||
{
|
||||
fontRenderer.drawString( m_text[lineIdx], x, y, m_colours[lineIdx], null, 0, 0, false, Palette.DEFAULT );
|
||||
}
|
||||
y = y + FixedWidthFontRenderer.FONT_HEIGHT;
|
||||
}
|
||||
int startY = (height - Y_SIZE) / 2;
|
||||
int startX = (width - X_SIZE) / 2;
|
||||
|
||||
drawBorder( startX, startY, zLevel, m_page, m_pages, m_book );
|
||||
drawText( startX + X_TEXT_MARGIN, startY + Y_TEXT_MARGIN, ItemPrintout.LINES_PER_PAGE * m_page, m_text, m_colours );
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@ import net.minecraft.client.renderer.block.model.IBakedModel;
|
||||
import net.minecraft.client.renderer.block.model.ModelBakery;
|
||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
||||
import net.minecraft.client.renderer.color.IItemColor;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.resources.SimpleReloadableResourceManager;
|
||||
@@ -115,8 +116,23 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon
|
||||
MinecraftForge.EVENT_BUS.register( handlers );
|
||||
}
|
||||
|
||||
public class ForgeHandlers
|
||||
public static class ForgeHandlers
|
||||
{
|
||||
private static final String[] TURTLE_UPGRADES = {
|
||||
"turtle_modem_off_left",
|
||||
"turtle_modem_on_left",
|
||||
"turtle_modem_off_right",
|
||||
"turtle_modem_on_right",
|
||||
"turtle_crafting_table_left",
|
||||
"turtle_crafting_table_right",
|
||||
"advanced_turtle_modem_off_left",
|
||||
"advanced_turtle_modem_on_left",
|
||||
"advanced_turtle_modem_off_right",
|
||||
"advanced_turtle_modem_on_right",
|
||||
"turtle_speaker_upgrade_left",
|
||||
"turtle_speaker_upgrade_right",
|
||||
};
|
||||
|
||||
private TurtleSmartItemModel m_turtleSmartItemModel;
|
||||
|
||||
public ForgeHandlers()
|
||||
@@ -142,24 +158,27 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon
|
||||
@SubscribeEvent
|
||||
public void onTextureStitchEvent( TextureStitchEvent.Pre event )
|
||||
{
|
||||
event.getMap().registerSprite( new ResourceLocation( "computercraft", "blocks/crafty_upgrade" ) );
|
||||
// Load all textures for upgrades
|
||||
TextureMap map = event.getMap();
|
||||
for( String upgrade : TURTLE_UPGRADES )
|
||||
{
|
||||
IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( "computercraft", "block/" + upgrade ) );
|
||||
for( ResourceLocation texture : model.getTextures() )
|
||||
{
|
||||
map.registerSprite( texture );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onModelBakeEvent( ModelBakeEvent event )
|
||||
{
|
||||
loadModel( event, "turtle_modem_off_left" );
|
||||
loadModel( event, "turtle_modem_on_left" );
|
||||
loadModel( event, "turtle_modem_off_right" );
|
||||
loadModel( event, "turtle_modem_on_right" );
|
||||
loadModel( event, "turtle_crafting_table_left" );
|
||||
loadModel( event, "turtle_crafting_table_right" );
|
||||
loadModel( event, "advanced_turtle_modem_off_left" );
|
||||
loadModel( event, "advanced_turtle_modem_on_left" );
|
||||
loadModel( event, "advanced_turtle_modem_off_right" );
|
||||
loadModel( event, "advanced_turtle_modem_on_right" );
|
||||
loadModel( event, "turtle_speaker_upgrade_left" );
|
||||
loadModel( event, "turtle_speaker_upgrade_right" );
|
||||
// Load all upgrade models
|
||||
for( String upgrade : TURTLE_UPGRADES )
|
||||
{
|
||||
loadModel( event, upgrade );
|
||||
}
|
||||
|
||||
loadSmartModel( event, "turtle_dynamic", m_turtleSmartItemModel );
|
||||
}
|
||||
|
||||
@@ -191,7 +210,7 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon
|
||||
private static class TurtleItemColour implements IItemColor
|
||||
{
|
||||
@Override
|
||||
public int getColorFromItemstack( @Nonnull ItemStack stack, int tintIndex )
|
||||
public int colorMultiplier( @Nonnull ItemStack stack, int tintIndex )
|
||||
{
|
||||
if( tintIndex == 0 )
|
||||
{
|
||||
|
@@ -9,6 +9,7 @@ package dan200.computercraft.client.proxy;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.*;
|
||||
import dan200.computercraft.client.render.ItemPocketRenderer;
|
||||
import dan200.computercraft.client.render.ItemPrintoutRenderer;
|
||||
import dan200.computercraft.client.render.RenderOverlayCable;
|
||||
import dan200.computercraft.client.render.TileEntityCableRenderer;
|
||||
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
|
||||
@@ -25,6 +26,7 @@ import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||
import dan200.computercraft.shared.network.ComputerCraftPacket;
|
||||
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
|
||||
import dan200.computercraft.shared.peripheral.modem.TileCable;
|
||||
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
|
||||
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
|
||||
import dan200.computercraft.shared.peripheral.printer.TilePrinter;
|
||||
import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
|
||||
@@ -33,9 +35,10 @@ import dan200.computercraft.shared.proxy.ComputerCraftProxyCommon;
|
||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
||||
import dan200.computercraft.shared.turtle.entity.TurtleVisionCamera;
|
||||
import dan200.computercraft.shared.util.Colour;
|
||||
import gnu.trove.map.hash.TIntIntHashMap;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.gui.GuiNewChat;
|
||||
import net.minecraft.client.renderer.ItemMeshDefinition;
|
||||
import net.minecraft.client.renderer.block.model.ModelBakery;
|
||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
||||
@@ -50,6 +53,7 @@ import net.minecraft.util.IThreadListener;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.client.event.ModelRegistryEvent;
|
||||
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
||||
@@ -57,6 +61,7 @@ import net.minecraftforge.client.event.RenderHandEvent;
|
||||
import net.minecraftforge.client.event.RenderPlayerEvent;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||
@@ -70,6 +75,8 @@ import java.util.List;
|
||||
|
||||
public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
{
|
||||
private static TIntIntHashMap lastCounts = new TIntIntHashMap();
|
||||
|
||||
private long m_tick;
|
||||
private long m_renderFrame;
|
||||
private FixedWidthFontRenderer m_fixedWidthFontRenderer;
|
||||
@@ -115,6 +122,7 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
registerItemModel( ComputerCraft.Blocks.commandComputer, "command_computer" );
|
||||
registerItemModel( ComputerCraft.Blocks.advancedModem, "advanced_modem" );
|
||||
registerItemModel( ComputerCraft.Blocks.peripheral, 5, "speaker" );
|
||||
registerItemModel( ComputerCraft.Blocks.wiredModemFull, "wired_modem_full" );
|
||||
|
||||
registerItemModel( ComputerCraft.Items.disk, "disk" );
|
||||
registerItemModel( ComputerCraft.Items.diskExpanded, "disk_expanded" );
|
||||
@@ -296,12 +304,6 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
return m_renderFrame;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteDisplayLists( int list, int range )
|
||||
{
|
||||
GlStateManager.glDeleteLists( list, range );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getFixedWidthFontRenderer()
|
||||
{
|
||||
@@ -388,6 +390,7 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
case ComputerCraftPacket.ComputerTerminalChanged:
|
||||
case ComputerCraftPacket.ComputerDeleted:
|
||||
case ComputerCraftPacket.PlayRecord:
|
||||
case ComputerCraftPacket.PostChat:
|
||||
{
|
||||
// Packet from Server to Client
|
||||
IThreadListener listener = Minecraft.getMinecraft();
|
||||
@@ -454,6 +457,36 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
{
|
||||
mc.world.playRecord( pos, null );
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ComputerCraftPacket.PostChat:
|
||||
{
|
||||
/*
|
||||
This allows us to send delete chat messages of the same "category" as the previous one.
|
||||
It's used by the various /computercraft commands to avoid filling the chat with repetitive
|
||||
messages.
|
||||
*/
|
||||
|
||||
int id = packet.m_dataInt[0];
|
||||
ITextComponent[] components = new ITextComponent[packet.m_dataString.length];
|
||||
for( int i = 0; i < packet.m_dataString.length; i++ )
|
||||
{
|
||||
components[i] = ITextComponent.Serializer.jsonToComponent( packet.m_dataString[i] );
|
||||
}
|
||||
|
||||
GuiNewChat chat = Minecraft.getMinecraft().ingameGUI.getChatGUI();
|
||||
|
||||
// Keep track of how many lines we wrote last time, deleting any extra ones.
|
||||
int lastCount = lastCounts.get( id );
|
||||
for( int i = components.length; i < lastCount; i++ ) chat.deleteChatLine( i + id );
|
||||
lastCounts.put( id, components.length );
|
||||
|
||||
// Add new lines
|
||||
for( int i = 0; i < components.length; i++ )
|
||||
{
|
||||
chat.printChatMessageWithOptionalDeletion( components[i], id + i );
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -465,6 +498,7 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
MinecraftForge.EVENT_BUS.register( handlers );
|
||||
MinecraftForge.EVENT_BUS.register( new RenderOverlayCable() );
|
||||
MinecraftForge.EVENT_BUS.register( new ItemPocketRenderer() );
|
||||
MinecraftForge.EVENT_BUS.register( new ItemPrintoutRenderer() );
|
||||
}
|
||||
|
||||
public class ForgeHandlers
|
||||
@@ -551,6 +585,15 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
m_renderFrame++;
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onWorldUnload( WorldEvent.Unload event )
|
||||
{
|
||||
if( event.getWorld().isRemote )
|
||||
{
|
||||
ClientMonitor.destroyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@@ -564,7 +607,7 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColorFromItemstack( @Nonnull ItemStack stack, int layer )
|
||||
public int colorMultiplier( @Nonnull ItemStack stack, int layer )
|
||||
{
|
||||
return layer == 0 ? 0xFFFFFF : disk.getColour( stack );
|
||||
}
|
||||
|
@@ -0,0 +1,197 @@
|
||||
package dan200.computercraft.client.render;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.ItemRenderer;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.EnumHandSide;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraftforge.client.event.RenderItemInFrameEvent;
|
||||
import net.minecraftforge.client.event.RenderSpecificHandEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH;
|
||||
import static dan200.computercraft.client.render.PrintoutRenderer.*;
|
||||
import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE;
|
||||
import static dan200.computercraft.shared.media.items.ItemPrintout.LINE_MAX_LENGTH;
|
||||
|
||||
public class ItemPrintoutRenderer
|
||||
{
|
||||
@SubscribeEvent
|
||||
public void onRenderInHand( RenderSpecificHandEvent event )
|
||||
{
|
||||
ItemStack stack = event.getItemStack();
|
||||
if( stack.getItem() != ComputerCraft.Items.printout ) return;
|
||||
|
||||
event.setCanceled( true );
|
||||
|
||||
EntityPlayer player = Minecraft.getMinecraft().player;
|
||||
|
||||
GlStateManager.pushMatrix();
|
||||
if( event.getHand() == EnumHand.MAIN_HAND && player.getHeldItemOffhand().isEmpty() )
|
||||
{
|
||||
renderPrintoutFirstPersonCentre(
|
||||
event.getInterpolatedPitch(),
|
||||
event.getEquipProgress(),
|
||||
event.getSwingProgress(),
|
||||
stack
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderPrintoutFirstPersonSide(
|
||||
event.getHand() == EnumHand.MAIN_HAND ? player.getPrimaryHand() : player.getPrimaryHand().opposite(),
|
||||
event.getEquipProgress(),
|
||||
event.getSwingProgress(),
|
||||
stack
|
||||
);
|
||||
}
|
||||
GlStateManager.popMatrix();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a pocket computer to one side of the player.
|
||||
*
|
||||
* @param side The side to render on
|
||||
* @param equipProgress The equip progress of this item
|
||||
* @param swingProgress The swing progress of this item
|
||||
* @param stack The stack to render
|
||||
* @see ItemRenderer#renderMapFirstPersonSide(float, EnumHandSide, float, ItemStack)
|
||||
*/
|
||||
private void renderPrintoutFirstPersonSide( EnumHandSide side, float equipProgress, float swingProgress, ItemStack stack )
|
||||
{
|
||||
Minecraft minecraft = Minecraft.getMinecraft();
|
||||
float offset = side == EnumHandSide.RIGHT ? 1f : -1f;
|
||||
GlStateManager.translate( offset * 0.125f, -0.125f, 0f );
|
||||
|
||||
// If the player is not invisible then render a single arm
|
||||
if( !minecraft.player.isInvisible() )
|
||||
{
|
||||
GlStateManager.pushMatrix();
|
||||
GlStateManager.rotate( offset * 10f, 0f, 0f, 1f );
|
||||
minecraft.getItemRenderer().renderArmFirstPerson( equipProgress, swingProgress, side );
|
||||
GlStateManager.popMatrix();
|
||||
}
|
||||
|
||||
// Setup the appropriate transformations. This is just copied from the
|
||||
// corresponding method in ItemRenderer.
|
||||
GlStateManager.pushMatrix();
|
||||
GlStateManager.translate( offset * 0.51f, -0.08f + equipProgress * -1.2f, -0.75f );
|
||||
float f1 = MathHelper.sqrt( swingProgress );
|
||||
float f2 = MathHelper.sin( f1 * (float) Math.PI );
|
||||
float f3 = -0.5f * f2;
|
||||
float f4 = 0.4f * MathHelper.sin( f1 * ((float) Math.PI * 2f) );
|
||||
float f5 = -0.3f * MathHelper.sin( swingProgress * (float) Math.PI );
|
||||
GlStateManager.translate( offset * f3, f4 - 0.3f * f2, f5 );
|
||||
GlStateManager.rotate( f2 * -45f, 1f, 0f, 0f );
|
||||
GlStateManager.rotate( offset * f2 * -30f, 0f, 1f, 0f );
|
||||
|
||||
renderPrintoutFirstPerson( stack );
|
||||
|
||||
GlStateManager.popMatrix();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render an item in the middle of the screen
|
||||
*
|
||||
* @param pitch The pitch of the player
|
||||
* @param equipProgress The equip progress of this item
|
||||
* @param swingProgress The swing progress of this item
|
||||
* @param stack The stack to render
|
||||
* @see ItemRenderer#renderMapFirstPerson(float, float, float)
|
||||
*/
|
||||
private void renderPrintoutFirstPersonCentre( float pitch, float equipProgress, float swingProgress, ItemStack stack )
|
||||
{
|
||||
ItemRenderer itemRenderer = Minecraft.getMinecraft().getItemRenderer();
|
||||
|
||||
// Setup the appropriate transformations. This is just copied from the
|
||||
// corresponding method in ItemRenderer.
|
||||
float swingRt = MathHelper.sqrt( swingProgress );
|
||||
float tX = -0.2f * MathHelper.sin( swingProgress * (float) Math.PI );
|
||||
float tZ = -0.4f * MathHelper.sin( swingRt * (float) Math.PI );
|
||||
GlStateManager.translate( 0f, -tX / 2f, tZ );
|
||||
float pitchAngle = itemRenderer.getMapAngleFromPitch( pitch );
|
||||
GlStateManager.translate( 0f, 0.04f + equipProgress * -1.2f + pitchAngle * -0.5f, -0.72f );
|
||||
GlStateManager.rotate( pitchAngle * -85f, 1f, 0f, 0f );
|
||||
itemRenderer.renderArms();
|
||||
float rX = MathHelper.sin( swingRt * (float) Math.PI );
|
||||
GlStateManager.rotate( rX * 20f, 1f, 0f, 0f );
|
||||
GlStateManager.scale( 2f, 2f, 2f );
|
||||
|
||||
renderPrintoutFirstPerson( stack );
|
||||
}
|
||||
|
||||
|
||||
private static void renderPrintoutFirstPerson( ItemStack stack )
|
||||
{
|
||||
// Setup various transformations. Note that these are partially adapated from the corresponding method
|
||||
// in ItemRenderer.renderMapFirstPerson
|
||||
GlStateManager.disableLighting();
|
||||
|
||||
GlStateManager.rotate( 180f, 0f, 1f, 0f );
|
||||
GlStateManager.rotate( 180f, 0f, 0f, 1f );
|
||||
GlStateManager.scale( 0.42f, 0.42f, -0.42f );
|
||||
GlStateManager.translate( -0.5f, -0.48f, 0.0f );
|
||||
|
||||
drawPrintout( stack );
|
||||
|
||||
GlStateManager.enableLighting();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onRenderInFrame( RenderItemInFrameEvent event )
|
||||
{
|
||||
ItemStack stack = event.getItem();
|
||||
if( stack.getItem() != ComputerCraft.Items.printout ) return;
|
||||
|
||||
event.setCanceled( true );
|
||||
|
||||
GlStateManager.disableLighting();
|
||||
|
||||
// Move a little bit forward to ensure we're not clipping with the frame
|
||||
GlStateManager.translate( 0.0f, 0.0f, -0.001f );
|
||||
GlStateManager.rotate( 180f, 0f, 0f, 1f );
|
||||
GlStateManager.scale( 0.95f, 0.95f, -0.95f );
|
||||
GlStateManager.translate( -0.5f, -0.5f, 0.0f );
|
||||
|
||||
drawPrintout( stack );
|
||||
|
||||
GlStateManager.enableLighting();
|
||||
}
|
||||
|
||||
private static void drawPrintout( ItemStack stack )
|
||||
{
|
||||
int pages = ItemPrintout.getPageCount( stack );
|
||||
boolean book = ItemPrintout.getType( stack ) == ItemPrintout.Type.Book;
|
||||
|
||||
double width = LINE_MAX_LENGTH * FONT_WIDTH + X_TEXT_MARGIN * 2;
|
||||
double height = LINES_PER_PAGE * FONT_HEIGHT + Y_TEXT_MARGIN * 2;
|
||||
|
||||
// Non-books will be left aligned
|
||||
if( !book ) width += offsetAt( pages );
|
||||
|
||||
double visualWidth = width, visualHeight = height;
|
||||
|
||||
// Meanwhile books will be centred
|
||||
if( book )
|
||||
{
|
||||
visualWidth += 2 * COVER_SIZE + 2 * offsetAt( pages );
|
||||
visualHeight += 2 * COVER_SIZE;
|
||||
}
|
||||
|
||||
double max = Math.max( visualHeight, visualWidth );
|
||||
|
||||
// Scale the printout to fit correctly.
|
||||
double scale = 1.0 / max;
|
||||
GlStateManager.scale( scale, scale, scale );
|
||||
GlStateManager.translate( (max - width) / 2.0f, (max - height) / 2.0f, 0.0f );
|
||||
|
||||
drawBorder( 0, 0, -0.01, 0, pages, book );
|
||||
drawText( X_TEXT_MARGIN, Y_TEXT_MARGIN, 0, ItemPrintout.getText( stack ), ItemPrintout.getColours( stack ) );
|
||||
}
|
||||
}
|
@@ -189,7 +189,7 @@ public final class ModelTransformer
|
||||
private BakedQuadBuilder( VertexFormat format )
|
||||
{
|
||||
this.format = format;
|
||||
this.vertexData = new int[ format.getNextOffset() ];
|
||||
this.vertexData = new int[ format.getSize() ];
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@@ -0,0 +1,162 @@
|
||||
package dan200.computercraft.client.render;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.FixedWidthFontRenderer;
|
||||
import dan200.computercraft.core.terminal.TextBuffer;
|
||||
import dan200.computercraft.shared.util.Palette;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.BufferBuilder;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
|
||||
import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE;
|
||||
|
||||
public class PrintoutRenderer
|
||||
{
|
||||
private static final ResourceLocation BG = new ResourceLocation( "computercraft", "textures/gui/printout.png" );
|
||||
private static final double BG_SIZE = 256.0;
|
||||
|
||||
/**
|
||||
* Width of a page
|
||||
*/
|
||||
public static final int X_SIZE = 172;
|
||||
|
||||
/**
|
||||
* Height of a page
|
||||
*/
|
||||
public static final int Y_SIZE = 209;
|
||||
|
||||
/**
|
||||
* Padding between the left and right of a page and the text
|
||||
*/
|
||||
public static final int X_TEXT_MARGIN = 13;
|
||||
|
||||
/**
|
||||
* Padding between the top and bottom of a page and the text
|
||||
*/
|
||||
public static final int Y_TEXT_MARGIN = 11;
|
||||
|
||||
/**
|
||||
* Width of the extra page texture
|
||||
*/
|
||||
private static final int X_FOLD_SIZE = 12;
|
||||
|
||||
/**
|
||||
* Size of the leather cover
|
||||
*/
|
||||
public static final int COVER_SIZE = 12;
|
||||
|
||||
private static final int COVER_Y = Y_SIZE;
|
||||
private static final int COVER_X = X_SIZE + 4 * X_FOLD_SIZE;
|
||||
|
||||
public static void drawText( int x, int y, int start, TextBuffer[] text, TextBuffer[] colours )
|
||||
{
|
||||
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer) ComputerCraft.getFixedWidthFontRenderer();
|
||||
|
||||
for( int line = 0; line < LINES_PER_PAGE && line < text.length; ++line )
|
||||
{
|
||||
fontRenderer.drawString( text[ start + line ], x, y + line * FONT_HEIGHT, colours[ start + line ], null, 0, 0, false, Palette.DEFAULT );
|
||||
}
|
||||
}
|
||||
|
||||
public static void drawText( int x, int y, int start, String[] text, String[] colours )
|
||||
{
|
||||
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer) ComputerCraft.getFixedWidthFontRenderer();
|
||||
|
||||
for( int line = 0; line < LINES_PER_PAGE && line < text.length; ++line )
|
||||
{
|
||||
fontRenderer.drawString( new TextBuffer( text[ start + line ] ), x, y + line * FONT_HEIGHT, new TextBuffer( colours[ start + line ] ), null, 0, 0, false, Palette.DEFAULT );
|
||||
}
|
||||
}
|
||||
|
||||
public static void drawBorder( double x, double y, double z, int page, int pages, boolean isBook )
|
||||
{
|
||||
GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||
Minecraft.getMinecraft().getTextureManager().bindTexture( BG );
|
||||
|
||||
Tessellator tessellator = Tessellator.getInstance();
|
||||
BufferBuilder buffer = tessellator.getBuffer();
|
||||
buffer.begin( GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX );
|
||||
|
||||
int leftPages = page;
|
||||
int rightPages = pages - page - 1;
|
||||
|
||||
if( isBook )
|
||||
{
|
||||
// Border
|
||||
double offset = offsetAt( pages );
|
||||
final double left = x - 4 - offset;
|
||||
final double right = x + X_SIZE + offset - 4;
|
||||
|
||||
// Left and right border
|
||||
drawTexture( buffer, left - 4, y - 8, z - 0.02, COVER_X, 0, COVER_SIZE, Y_SIZE + COVER_SIZE * 2 );
|
||||
drawTexture( buffer, right, y - 8, z - 0.02, COVER_X + COVER_SIZE, 0, COVER_SIZE, Y_SIZE + COVER_SIZE * 2 );
|
||||
|
||||
// Draw centre panel (just stretched texture, sorry).
|
||||
drawTexture( buffer,
|
||||
x - offset, y, z - 0.02, X_SIZE + offset * 2, Y_SIZE,
|
||||
COVER_X + COVER_SIZE / 2, COVER_SIZE, COVER_SIZE, Y_SIZE
|
||||
);
|
||||
|
||||
double borderX = left;
|
||||
while( borderX < right )
|
||||
{
|
||||
double thisWidth = Math.min( right - borderX, X_SIZE );
|
||||
drawTexture( buffer, borderX, y - 8, z - 0.02, 0, COVER_Y, thisWidth, COVER_SIZE );
|
||||
drawTexture( buffer, borderX, y + Y_SIZE - 4, z - 0.02, 0, COVER_Y + COVER_SIZE, thisWidth, COVER_SIZE );
|
||||
borderX += thisWidth;
|
||||
}
|
||||
}
|
||||
|
||||
// Left half
|
||||
drawTexture( buffer, x, y, z, X_FOLD_SIZE * 2, 0, X_SIZE / 2, Y_SIZE );
|
||||
for( int n = 0; n <= leftPages; n++ )
|
||||
{
|
||||
drawTexture( buffer,
|
||||
x - offsetAt( n ), y, z - 1e-3 * n,
|
||||
// Use the left "bold" fold for the outermost page
|
||||
n == leftPages ? 0 : X_FOLD_SIZE, 0,
|
||||
X_FOLD_SIZE, Y_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
// Right half
|
||||
drawTexture( buffer, x + X_SIZE / 2, y, z, X_FOLD_SIZE * 2 + X_SIZE / 2, 0, X_SIZE / 2, Y_SIZE );
|
||||
for( int n = 0; n <= rightPages; n++ )
|
||||
{
|
||||
drawTexture( buffer,
|
||||
x + (X_SIZE - X_FOLD_SIZE) + offsetAt( n ), y, z - 1e-3 * n,
|
||||
// Two folds, then the main page. Use the right "bold" fold for the outermost page.
|
||||
X_FOLD_SIZE * 2 + X_SIZE + (n == rightPages ? X_FOLD_SIZE : 0), 0,
|
||||
X_FOLD_SIZE, Y_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
tessellator.draw();
|
||||
}
|
||||
|
||||
private static void drawTexture( BufferBuilder buffer, double x, double y, double z, double u, double v, double width, double height )
|
||||
{
|
||||
buffer.pos( x, y + height, z ).tex( u / BG_SIZE, (v + height) / BG_SIZE ).endVertex();
|
||||
buffer.pos( x + width, y + height, z ).tex( (u + width) / BG_SIZE, (v + height) / BG_SIZE ).endVertex();
|
||||
buffer.pos( x + width, y, z ).tex( (u + width) / BG_SIZE, v / BG_SIZE ).endVertex();
|
||||
buffer.pos( x, y, z ).tex( u / BG_SIZE, v / BG_SIZE ).endVertex();
|
||||
}
|
||||
|
||||
private static void drawTexture( BufferBuilder buffer, double x, double y, double z, double width, double height, double u, double v, double tWidth, double tHeight )
|
||||
{
|
||||
buffer.pos( x, y + height, z ).tex( u / BG_SIZE, (v + tHeight) / BG_SIZE ).endVertex();
|
||||
buffer.pos( x + width, y + height, z ).tex( (u + tWidth) / BG_SIZE, (v + tHeight) / BG_SIZE ).endVertex();
|
||||
buffer.pos( x + width, y, z ).tex( (u + tWidth) / BG_SIZE, v / BG_SIZE ).endVertex();
|
||||
buffer.pos( x, y, z ).tex( u / BG_SIZE, v / BG_SIZE ).endVertex();
|
||||
}
|
||||
|
||||
public static double offsetAt( int page )
|
||||
{
|
||||
return 32 * (1 - Math.pow( 1.2, -page ));
|
||||
}
|
||||
}
|
@@ -54,8 +54,6 @@ public class RenderOverlayCable
|
||||
GlStateManager.depthMask( false );
|
||||
GlStateManager.pushMatrix();
|
||||
|
||||
EnumFacing direction = type != PeripheralType.Cable ? cable.getDirection() : null;
|
||||
|
||||
{
|
||||
EntityPlayer player = event.getPlayer();
|
||||
double x = player.lastTickPosX + (player.posX - player.lastTickPosX) * event.getPartialTicks();
|
||||
@@ -78,7 +76,7 @@ public class RenderOverlayCable
|
||||
|
||||
for( EnumFacing facing : EnumFacing.VALUES )
|
||||
{
|
||||
if( direction == facing || BlockCable.isCable( world, pos.offset( facing ) ) )
|
||||
if( BlockCable.doesConnectVisually( state, world, pos, facing ) )
|
||||
{
|
||||
flags |= 1 << facing.ordinal();
|
||||
|
||||
|
@@ -10,7 +10,7 @@ import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.client.gui.FixedWidthFontRenderer;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.core.terminal.TextBuffer;
|
||||
import dan200.computercraft.shared.common.ClientTerminal;
|
||||
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
|
||||
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
|
||||
import dan200.computercraft.shared.util.Colour;
|
||||
import dan200.computercraft.shared.util.DirectionUtil;
|
||||
@@ -18,6 +18,7 @@ import dan200.computercraft.shared.util.Palette;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.BufferBuilder;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.OpenGlHelper;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
|
||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
|
||||
@@ -43,25 +44,24 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
private void renderMonitorAt( TileMonitor monitor, double posX, double posY, double posZ, float f, int i )
|
||||
{
|
||||
// Render from the origin monitor
|
||||
TileMonitor origin = monitor.getOrigin();
|
||||
if( origin == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
ClientMonitor originTerminal = monitor.getClientMonitor();
|
||||
|
||||
// Ensure each monitor is rendered only once
|
||||
long renderFrame = ComputerCraft.getRenderFrame();
|
||||
if( origin.m_lastRenderFrame == renderFrame )
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
origin.m_lastRenderFrame = renderFrame;
|
||||
}
|
||||
|
||||
boolean redraw = origin.pollChanged();
|
||||
if( originTerminal == null ) return;
|
||||
TileMonitor origin = originTerminal.getOrigin();
|
||||
BlockPos monitorPos = monitor.getPos();
|
||||
|
||||
// Ensure each monitor terminal is rendered only once. We allow rendering a specific tile
|
||||
// multiple times in a single frame to ensure compatibility with shaders which may run a
|
||||
// pass multiple times.
|
||||
long renderFrame = ComputerCraft.getRenderFrame();
|
||||
if( originTerminal.lastRenderFrame == renderFrame && !monitorPos.equals( originTerminal.lastRenderPos ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
originTerminal.lastRenderFrame = renderFrame;
|
||||
originTerminal.lastRenderPos = monitorPos;
|
||||
|
||||
BlockPos originPos = origin.getPos();
|
||||
posX += originPos.getX() - monitorPos.getX();
|
||||
posY += originPos.getY() - monitorPos.getY();
|
||||
@@ -94,34 +94,34 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
BufferBuilder renderer = tessellator.getBuffer();
|
||||
|
||||
// Get terminal
|
||||
ClientTerminal clientTerminal = (ClientTerminal)origin.getTerminal();
|
||||
Terminal terminal = (clientTerminal != null) ? clientTerminal.getTerminal() : null;
|
||||
redraw = redraw || (clientTerminal != null && clientTerminal.hasTerminalChanged());
|
||||
boolean redraw = originTerminal.pollTerminalChanged();
|
||||
|
||||
// Draw the contents
|
||||
GlStateManager.depthMask( false );
|
||||
OpenGlHelper.setLightmapTextureCoords( OpenGlHelper.lightmapTexUnit, 0xFF, 0xFF );
|
||||
GlStateManager.disableLighting();
|
||||
mc.entityRenderer.disableLightmap();
|
||||
try
|
||||
{
|
||||
Terminal terminal = originTerminal.getTerminal();
|
||||
if( terminal != null )
|
||||
{
|
||||
Palette palette = terminal.getPalette();
|
||||
|
||||
// Allocate display lists
|
||||
if( origin.m_renderDisplayList < 0 )
|
||||
if( originTerminal.renderDisplayLists == null )
|
||||
{
|
||||
origin.m_renderDisplayList = GlStateManager.glGenLists( 3 );
|
||||
originTerminal.createLists();
|
||||
redraw = true;
|
||||
}
|
||||
|
||||
// Draw a terminal
|
||||
boolean greyscale = !clientTerminal.isColour();
|
||||
boolean greyscale = !originTerminal.isColour();
|
||||
int width = terminal.getWidth();
|
||||
int height = terminal.getHeight();
|
||||
int cursorX = terminal.getCursorX();
|
||||
int cursorY = terminal.getCursorY();
|
||||
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer)ComputerCraft.getFixedWidthFontRenderer();
|
||||
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer) ComputerCraft.getFixedWidthFontRenderer();
|
||||
|
||||
GlStateManager.pushMatrix();
|
||||
try
|
||||
@@ -135,7 +135,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
if( redraw )
|
||||
{
|
||||
// Build background display list
|
||||
GlStateManager.glNewList( origin.m_renderDisplayList, GL11.GL_COMPILE );
|
||||
GlStateManager.glNewList( originTerminal.renderDisplayLists[0], GL11.GL_COMPILE );
|
||||
try
|
||||
{
|
||||
double marginXSize = TileMonitor.RENDER_MARGIN / xScale;
|
||||
@@ -149,7 +149,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
GlStateManager.scale( 1.0, marginSquash, 1.0 );
|
||||
GlStateManager.translate( 0.0, -marginYSize / marginSquash, 0.0 );
|
||||
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( 0 ), marginXSize, marginXSize, greyscale, palette );
|
||||
GlStateManager.translate( 0.0, ( marginYSize + height * FixedWidthFontRenderer.FONT_HEIGHT ) / marginSquash, 0.0 );
|
||||
GlStateManager.translate( 0.0, (marginYSize + height * FixedWidthFontRenderer.FONT_HEIGHT) / marginSquash, 0.0 );
|
||||
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( height - 1 ), marginXSize, marginXSize, greyscale, palette );
|
||||
}
|
||||
finally
|
||||
@@ -174,7 +174,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
GlStateManager.glEndList();
|
||||
}
|
||||
}
|
||||
GlStateManager.callList( origin.m_renderDisplayList );
|
||||
GlStateManager.callList( originTerminal.renderDisplayLists[0] );
|
||||
GlStateManager.resetColor();
|
||||
|
||||
// Draw text
|
||||
@@ -182,7 +182,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
if( redraw )
|
||||
{
|
||||
// Build text display list
|
||||
GlStateManager.glNewList( origin.m_renderDisplayList + 1, GL11.GL_COMPILE );
|
||||
GlStateManager.glNewList( originTerminal.renderDisplayLists[1], GL11.GL_COMPILE );
|
||||
try
|
||||
{
|
||||
// Lines
|
||||
@@ -202,7 +202,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
GlStateManager.glEndList();
|
||||
}
|
||||
}
|
||||
GlStateManager.callList( origin.m_renderDisplayList + 1 );
|
||||
GlStateManager.callList( originTerminal.renderDisplayLists[1] );
|
||||
GlStateManager.resetColor();
|
||||
|
||||
// Draw cursor
|
||||
@@ -210,7 +210,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
if( redraw )
|
||||
{
|
||||
// Build cursor display list
|
||||
GlStateManager.glNewList( origin.m_renderDisplayList + 2, GL11.GL_COMPILE );
|
||||
GlStateManager.glNewList( originTerminal.renderDisplayLists[2], GL11.GL_COMPILE );
|
||||
try
|
||||
{
|
||||
// Cursor
|
||||
@@ -236,7 +236,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
|
||||
}
|
||||
if( ComputerCraft.getGlobalCursorBlink() )
|
||||
{
|
||||
GlStateManager.callList( origin.m_renderDisplayList + 2 );
|
||||
GlStateManager.callList( originTerminal.renderDisplayLists[2] );
|
||||
GlStateManager.resetColor();
|
||||
}
|
||||
}
|
||||
|
@@ -167,7 +167,7 @@ public class TurtleSmartItemModel implements IBakedModel, IResourceManagerReload
|
||||
ModelResourceLocation baseModelLocation = TileEntityTurtleRenderer.getTurtleModel( combo.m_family, combo.m_colour );
|
||||
ModelResourceLocation overlayModelLocation = TileEntityTurtleRenderer.getTurtleOverlayModel( combo.m_family, combo.m_overlay, combo.m_christmas );
|
||||
IBakedModel baseModel = modelManager.getModel( baseModelLocation );
|
||||
IBakedModel overlayModel = (overlayModelLocation != null) ? modelManager.getModel( baseModelLocation ) : null;
|
||||
IBakedModel overlayModel = (overlayModelLocation != null) ? modelManager.getModel( overlayModelLocation ) : null;
|
||||
Matrix4f transform = combo.m_flip ? s_flip : s_identity;
|
||||
Pair<IBakedModel, Matrix4f> leftModel = (combo.m_leftUpgrade != null) ? combo.m_leftUpgrade.getModel( null, TurtleSide.Left ) : null;
|
||||
Pair<IBakedModel, Matrix4f> rightModel = (combo.m_rightUpgrade != null) ? combo.m_rightUpgrade.getModel( null, TurtleSide.Right ) : null;
|
||||
|
@@ -3,14 +3,17 @@ package dan200.computercraft.core.apis;
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.computer.IComputerOwned;
|
||||
import dan200.computercraft.core.filesystem.FileSystem;
|
||||
import dan200.computercraft.core.filesystem.FileSystemException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class ComputerAccess implements IComputerAccess
|
||||
public abstract class ComputerAccess implements IComputerAccess, IComputerOwned
|
||||
{
|
||||
private final IAPIEnvironment m_environment;
|
||||
private final Set<String> m_mounts = new HashSet<>();
|
||||
@@ -133,6 +136,13 @@ public abstract class ComputerAccess implements IComputerAccess
|
||||
m_environment.queueEvent( event, arguments );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Computer getComputer()
|
||||
{
|
||||
return m_environment.getComputer();
|
||||
}
|
||||
|
||||
private String findFreeLocation( String desiredLoc )
|
||||
{
|
||||
try
|
||||
|
@@ -15,6 +15,7 @@ import dan200.computercraft.core.apis.handles.EncodedInputHandle;
|
||||
import dan200.computercraft.core.apis.handles.EncodedOutputHandle;
|
||||
import dan200.computercraft.core.filesystem.FileSystem;
|
||||
import dan200.computercraft.core.filesystem.FileSystemException;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.InputStream;
|
||||
@@ -88,6 +89,7 @@ public class FSAPI implements ILuaAPI
|
||||
{
|
||||
// list
|
||||
String path = getString( args, 0 );
|
||||
m_env.addTrackingChange( TrackingField.FS_OPS );
|
||||
try {
|
||||
String[] results = m_fileSystem.list( path );
|
||||
Map<Object,Object> table = new HashMap<>();
|
||||
@@ -162,6 +164,7 @@ public class FSAPI implements ILuaAPI
|
||||
// makeDir
|
||||
String path = getString( args, 0 );
|
||||
try {
|
||||
m_env.addTrackingChange( TrackingField.FS_OPS );
|
||||
m_fileSystem.makeDir( path );
|
||||
return null;
|
||||
} catch( FileSystemException e ) {
|
||||
@@ -174,6 +177,7 @@ public class FSAPI implements ILuaAPI
|
||||
String path = getString( args, 0 );
|
||||
String dest = getString( args, 1 );
|
||||
try {
|
||||
m_env.addTrackingChange( TrackingField.FS_OPS );
|
||||
m_fileSystem.move( path, dest );
|
||||
return null;
|
||||
} catch( FileSystemException e ) {
|
||||
@@ -186,6 +190,7 @@ public class FSAPI implements ILuaAPI
|
||||
String path = getString( args, 0 );
|
||||
String dest = getString( args, 1 );
|
||||
try {
|
||||
m_env.addTrackingChange( TrackingField.FS_OPS );
|
||||
m_fileSystem.copy( path, dest );
|
||||
return null;
|
||||
} catch( FileSystemException e ) {
|
||||
@@ -197,6 +202,7 @@ public class FSAPI implements ILuaAPI
|
||||
// delete
|
||||
String path = getString( args, 0 );
|
||||
try {
|
||||
m_env.addTrackingChange( TrackingField.FS_OPS );
|
||||
m_fileSystem.delete( path );
|
||||
return null;
|
||||
} catch( FileSystemException e ) {
|
||||
@@ -208,6 +214,7 @@ public class FSAPI implements ILuaAPI
|
||||
// open
|
||||
String path = getString( args, 0 );
|
||||
String mode = getString( args, 1 );
|
||||
m_env.addTrackingChange( TrackingField.FS_OPS );
|
||||
try {
|
||||
switch( mode )
|
||||
{
|
||||
@@ -288,6 +295,7 @@ public class FSAPI implements ILuaAPI
|
||||
// find
|
||||
String path = getString( args, 0 );
|
||||
try {
|
||||
m_env.addTrackingChange( TrackingField.FS_OPS );
|
||||
String[] results = m_fileSystem.find( path );
|
||||
Map<Object,Object> table = new HashMap<>();
|
||||
for(int i=0; i<results.length; ++i ) {
|
||||
|
@@ -6,6 +6,7 @@
|
||||
|
||||
package dan200.computercraft.core.apis;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.lua.ILuaAPI;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
@@ -21,9 +22,14 @@ import java.util.*;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.*;
|
||||
import static dan200.computercraft.core.apis.TableHelper.*;
|
||||
|
||||
public class HTTPAPI implements ILuaAPI
|
||||
{
|
||||
private static final Set<String> HTTP_METHODS = ImmutableSet.of(
|
||||
"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE"
|
||||
);
|
||||
|
||||
private final IAPIEnvironment m_apiEnvironment;
|
||||
private final List<Future<?>> m_httpTasks;
|
||||
private final Set<Closeable> m_closeables;
|
||||
@@ -38,7 +44,7 @@ public class HTTPAPI implements ILuaAPI
|
||||
@Override
|
||||
public String[] getNames()
|
||||
{
|
||||
return new String[] {
|
||||
return new String[]{
|
||||
"http"
|
||||
};
|
||||
}
|
||||
@@ -59,7 +65,7 @@ public class HTTPAPI implements ILuaAPI
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown( )
|
||||
public void shutdown()
|
||||
{
|
||||
synchronized( m_httpTasks )
|
||||
{
|
||||
@@ -89,7 +95,7 @@ public class HTTPAPI implements ILuaAPI
|
||||
@Override
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
return new String[] {
|
||||
return new String[]{
|
||||
"request",
|
||||
"checkURL",
|
||||
"websocket",
|
||||
@@ -101,52 +107,68 @@ public class HTTPAPI implements ILuaAPI
|
||||
{
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
case 0: // request
|
||||
{
|
||||
// request
|
||||
// Get URL
|
||||
String urlString = getString( args, 0 );
|
||||
String urlString, postString, requestMethod;
|
||||
Map<Object, Object> headerTable;
|
||||
boolean binary, redirect;
|
||||
|
||||
// Get POST
|
||||
String postString = optString( args, 1, null );
|
||||
|
||||
// Get Headers
|
||||
Map<String, String> headers = null;
|
||||
Map<Object, Object> table = optTable( args, 2, null );
|
||||
if( table != null )
|
||||
if( args.length >= 1 && args[0] instanceof Map )
|
||||
{
|
||||
headers = new HashMap<>( table.size() );
|
||||
for( Object key : table.keySet() )
|
||||
Map<?, ?> options = (Map) args[0];
|
||||
urlString = getStringField( options, "url" );
|
||||
postString = optStringField( options, "body", null );
|
||||
headerTable = optTableField( options, "headers", null );
|
||||
binary = optBooleanField( options, "binary", false );
|
||||
requestMethod = optStringField( options, "method", null );
|
||||
redirect = optBooleanField( options, "redirect", true );
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get URL and post information
|
||||
urlString = getString( args, 0 );
|
||||
postString = optString( args, 1, null );
|
||||
headerTable = optTable( args, 2, null );
|
||||
binary = optBoolean( args, 3, false );
|
||||
requestMethod = null;
|
||||
redirect = true;
|
||||
}
|
||||
|
||||
Map<String, String> headers = null;
|
||||
if( headerTable != null )
|
||||
{
|
||||
headers = new HashMap<>( headerTable.size() );
|
||||
for( Object key : headerTable.keySet() )
|
||||
{
|
||||
Object value = table.get( key );
|
||||
Object value = headerTable.get( key );
|
||||
if( key instanceof String && value instanceof String )
|
||||
{
|
||||
headers.put( (String)key, (String)value );
|
||||
headers.put( (String) key, (String) value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get binary
|
||||
boolean binary = false;
|
||||
if( args.length >= 4 )
|
||||
|
||||
if( requestMethod != null && !HTTP_METHODS.contains( requestMethod ) )
|
||||
{
|
||||
binary = args[ 3 ] != null && !args[ 3 ].equals( Boolean.FALSE );
|
||||
throw new LuaException( "Unsupported HTTP method" );
|
||||
}
|
||||
|
||||
// Make the request
|
||||
try
|
||||
{
|
||||
URL url = HTTPRequest.checkURL( urlString );
|
||||
HTTPRequest request = new HTTPRequest( m_apiEnvironment, urlString, url, postString, headers, binary );
|
||||
HTTPRequest request = new HTTPRequest( m_apiEnvironment, urlString, url, postString, headers, binary, requestMethod, redirect );
|
||||
synchronized( m_httpTasks )
|
||||
{
|
||||
m_httpTasks.add( HTTPExecutor.EXECUTOR.submit( request ) );
|
||||
}
|
||||
return new Object[] { true };
|
||||
return new Object[]{ true };
|
||||
}
|
||||
catch( HTTPRequestException e )
|
||||
{
|
||||
return new Object[] { false, e.getMessage() };
|
||||
return new Object[]{ false, e.getMessage() };
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
@@ -164,11 +186,11 @@ public class HTTPAPI implements ILuaAPI
|
||||
{
|
||||
m_httpTasks.add( HTTPExecutor.EXECUTOR.submit( check ) );
|
||||
}
|
||||
return new Object[] { true };
|
||||
return new Object[]{ true };
|
||||
}
|
||||
catch( HTTPRequestException e )
|
||||
{
|
||||
return new Object[] { false, e.getMessage() };
|
||||
return new Object[]{ false, e.getMessage() };
|
||||
}
|
||||
}
|
||||
case 2: // websocket
|
||||
@@ -201,11 +223,11 @@ public class HTTPAPI implements ILuaAPI
|
||||
{
|
||||
m_httpTasks.add( connector );
|
||||
}
|
||||
return new Object[] { true };
|
||||
return new Object[]{ true };
|
||||
}
|
||||
catch( HTTPRequestException e )
|
||||
{
|
||||
return new Object[] { false, e.getMessage() };
|
||||
return new Object[]{ false, e.getMessage() };
|
||||
}
|
||||
}
|
||||
default:
|
||||
|
@@ -9,16 +9,19 @@ package dan200.computercraft.core.apis;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.computer.IComputerEnvironment;
|
||||
import dan200.computercraft.core.computer.IComputerOwned;
|
||||
import dan200.computercraft.core.filesystem.FileSystem;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
|
||||
public interface IAPIEnvironment
|
||||
public interface IAPIEnvironment extends IComputerOwned
|
||||
{
|
||||
interface IPeripheralChangeListener
|
||||
{
|
||||
void onPeripheralChanged( int side, IPeripheral newPeripheral );
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
Computer getComputer();
|
||||
int getComputerID();
|
||||
IComputerEnvironment getComputerEnvironment();
|
||||
@@ -42,4 +45,11 @@ public interface IAPIEnvironment
|
||||
|
||||
String getLabel();
|
||||
void setLabel( String label );
|
||||
|
||||
void addTrackingChange( TrackingField field, long change );
|
||||
|
||||
default void addTrackingChange( TrackingField field )
|
||||
{
|
||||
addTrackingChange( field, 1 );
|
||||
}
|
||||
}
|
||||
|
@@ -15,10 +15,12 @@ import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.computer.ComputerThread;
|
||||
import dan200.computercraft.core.computer.ITask;
|
||||
import dan200.computercraft.core.filesystem.FileSystem;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -108,6 +110,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
||||
}
|
||||
if( method >= 0 )
|
||||
{
|
||||
m_environment.addTrackingChange( TrackingField.PERIPHERAL_OPS );
|
||||
return m_peripheral.callMethod( this, context, method, arguments );
|
||||
}
|
||||
else
|
||||
@@ -180,10 +183,49 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
||||
}
|
||||
return m_side;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Map<String, IPeripheral> getAvailablePeripherals()
|
||||
{
|
||||
if( !m_attached )
|
||||
{
|
||||
throw new RuntimeException( "You are not attached to this Computer" );
|
||||
}
|
||||
|
||||
Map<String, IPeripheral> peripherals = new HashMap<>();
|
||||
for( PeripheralWrapper wrapper : m_peripherals )
|
||||
{
|
||||
if( wrapper != null && wrapper.isAttached() )
|
||||
{
|
||||
peripherals.put( wrapper.getAttachmentName(), wrapper.getPeripheral() );
|
||||
}
|
||||
}
|
||||
|
||||
return Collections.unmodifiableMap( peripherals );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IPeripheral getAvailablePeripheral( @Nonnull String name )
|
||||
{
|
||||
if( !m_attached )
|
||||
{
|
||||
throw new RuntimeException( "You are not attached to this Computer" );
|
||||
}
|
||||
|
||||
for( PeripheralWrapper wrapper : m_peripherals )
|
||||
{
|
||||
if( wrapper != null && wrapper.isAttached() && wrapper.getAttachmentName().equals( name ) )
|
||||
{
|
||||
return wrapper.getPeripheral();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private final IAPIEnvironment m_environment;
|
||||
private FileSystem m_fileSystem;
|
||||
private final PeripheralWrapper[] m_peripherals;
|
||||
private boolean m_running;
|
||||
|
||||
@@ -285,7 +327,6 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
||||
{
|
||||
synchronized( m_peripherals )
|
||||
{
|
||||
m_fileSystem = m_environment.getFileSystem();
|
||||
m_running = true;
|
||||
for( int i=0; i<6; ++i )
|
||||
{
|
||||
@@ -312,7 +353,6 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
||||
wrapper.detach();
|
||||
}
|
||||
}
|
||||
m_fileSystem = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
214
src/main/java/dan200/computercraft/core/apis/TableHelper.java
Normal file
214
src/main/java/dan200/computercraft/core/apis/TableHelper.java
Normal file
@@ -0,0 +1,214 @@
|
||||
package dan200.computercraft.core.apis;
|
||||
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Various helpers for tables
|
||||
*/
|
||||
public final class TableHelper
|
||||
{
|
||||
private TableHelper()
|
||||
{
|
||||
throw new IllegalStateException( "Cannot instantiate singleton " + getClass().getName() );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static LuaException badKey( @Nonnull String key, @Nonnull String expected, @Nullable Object actual )
|
||||
{
|
||||
return badKey( key, expected, ArgumentHelper.getType( actual ) );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static LuaException badKey( @Nonnull String key, @Nonnull String expected, @Nonnull String actual )
|
||||
{
|
||||
return new LuaException( "bad field '" + key + "' (" + expected + " expected, got " + actual + ")" );
|
||||
}
|
||||
|
||||
public static double getNumberField( @Nonnull Map<?, ?> table, @Nonnull String key ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value instanceof Number )
|
||||
{
|
||||
return ((Number) value).doubleValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "number", value );
|
||||
}
|
||||
}
|
||||
|
||||
public static int getIntField( @Nonnull Map<?, ?> table, @Nonnull String key ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value instanceof Number )
|
||||
{
|
||||
return (int) ((Number) value).longValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "number", value );
|
||||
}
|
||||
}
|
||||
|
||||
public static double getRealField( @Nonnull Map<?, ?> table, @Nonnull String key ) throws LuaException
|
||||
{
|
||||
return checkReal( key, getNumberField( table, key ) );
|
||||
}
|
||||
|
||||
public static boolean getBooleanField( @Nonnull Map<?, ?> table, @Nonnull String key ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value instanceof Boolean )
|
||||
{
|
||||
return (Boolean) value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "boolean", value );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static String getStringField( @Nonnull Map<?, ?> table, @Nonnull String key ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value instanceof String )
|
||||
{
|
||||
return (String) value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "string", value );
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings( "unchecked" )
|
||||
@Nonnull
|
||||
public static Map<Object, Object> getTableField( @Nonnull Map<?, ?> table, @Nonnull String key ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value instanceof Map )
|
||||
{
|
||||
return (Map<Object, Object>) value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "table", value );
|
||||
}
|
||||
}
|
||||
|
||||
public static double optNumberField( @Nonnull Map<?, ?> table, @Nonnull String key, double def ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value == null )
|
||||
{
|
||||
return def;
|
||||
}
|
||||
else if( value instanceof Number )
|
||||
{
|
||||
return ((Number) value).doubleValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "number", value );
|
||||
}
|
||||
}
|
||||
|
||||
public static int optIntField( @Nonnull Map<?, ?> table, @Nonnull String key, int def ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value == null )
|
||||
{
|
||||
return def;
|
||||
}
|
||||
else if( value instanceof Number )
|
||||
{
|
||||
return (int) ((Number) value).longValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "number", value );
|
||||
}
|
||||
}
|
||||
|
||||
public static double optRealField( @Nonnull Map<?, ?> table, @Nonnull String key, double def ) throws LuaException
|
||||
{
|
||||
return checkReal( key, optNumberField( table, key, def ) );
|
||||
}
|
||||
|
||||
public static boolean optBooleanField( @Nonnull Map<?, ?> table, @Nonnull String key, boolean def ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value == null )
|
||||
{
|
||||
return def;
|
||||
}
|
||||
else if( value instanceof Boolean )
|
||||
{
|
||||
return (Boolean) value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "boolean", value );
|
||||
}
|
||||
}
|
||||
|
||||
public static String optStringField( @Nonnull Map<?, ?> table, @Nonnull String key, String def ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value == null )
|
||||
{
|
||||
return def;
|
||||
}
|
||||
else if( value instanceof String )
|
||||
{
|
||||
return (String) value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "string", value );
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings( "unchecked" )
|
||||
public static Map<Object, Object> optTableField( @Nonnull Map<?, ?> table, @Nonnull String key, Map<Object, Object> def ) throws LuaException
|
||||
{
|
||||
Object value = table.get( key );
|
||||
if( value == null )
|
||||
{
|
||||
return def;
|
||||
}
|
||||
else if( value instanceof Map )
|
||||
{
|
||||
return (Map<Object, Object>) value;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw badKey( key, "table", value );
|
||||
}
|
||||
}
|
||||
|
||||
private static double checkReal( @Nonnull String key, double value ) throws LuaException
|
||||
{
|
||||
if( Double.isNaN( value ) )
|
||||
{
|
||||
throw badKey( key, "number", "nan" );
|
||||
}
|
||||
else if( value == Double.POSITIVE_INFINITY )
|
||||
{
|
||||
throw badKey( key, "number", "inf" );
|
||||
}
|
||||
else if( value == Double.NEGATIVE_INFINITY )
|
||||
{
|
||||
throw badKey( key, "number", "-inf" );
|
||||
}
|
||||
else
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
@@ -13,6 +14,8 @@ import static dan200.computercraft.core.apis.ArgumentHelper.getInt;
|
||||
|
||||
public class BinaryInputHandle extends HandleGeneric
|
||||
{
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
private final InputStream m_stream;
|
||||
|
||||
public BinaryInputHandle( InputStream reader )
|
||||
@@ -45,16 +48,46 @@ public class BinaryInputHandle extends HandleGeneric
|
||||
if( args.length > 0 && args[ 0 ] != null )
|
||||
{
|
||||
int count = getInt( args, 0 );
|
||||
if( count <= 0 || count >= 1024 * 16 )
|
||||
if( count < 0 )
|
||||
{
|
||||
throw new LuaException( "Count out of range" );
|
||||
// Whilst this may seem absurd to allow reading 0 bytes, PUC Lua it so
|
||||
// it seems best to remain somewhat consistent.
|
||||
throw new LuaException( "Cannot read a negative number of bytes" );
|
||||
}
|
||||
else if( count <= BUFFER_SIZE )
|
||||
{
|
||||
// If we've got a small count, then allocate that and read it.
|
||||
byte[] bytes = new byte[ count ];
|
||||
int read = m_stream.read( bytes );
|
||||
|
||||
byte[] bytes = new byte[ count ];
|
||||
count = m_stream.read( bytes );
|
||||
if( count < 0 ) return null;
|
||||
if( count < bytes.length ) bytes = Arrays.copyOf( bytes, count );
|
||||
return new Object[] { bytes };
|
||||
if( read < 0 ) return null;
|
||||
if( read < count ) bytes = Arrays.copyOf( bytes, read );
|
||||
return new Object[] { bytes };
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] buffer = new byte[ BUFFER_SIZE ];
|
||||
|
||||
// Read the initial set of bytes, failing if none are read.
|
||||
int read = m_stream.read( buffer, 0, Math.min( buffer.length, count ) );
|
||||
if( read == -1 ) return null;
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream( read );
|
||||
count -= read;
|
||||
out.write( buffer, 0, read );
|
||||
|
||||
// Otherwise read until we either reach the limit or we no longer consume
|
||||
// the full buffer.
|
||||
while( read >= buffer.length && count > 0 )
|
||||
{
|
||||
read = m_stream.read( buffer, 0, Math.min( BUFFER_SIZE, count ) );
|
||||
if( read == -1 ) break;
|
||||
count -= read;
|
||||
out.write( buffer, 0, read );
|
||||
}
|
||||
|
||||
return new Object[] { out.toByteArray() };
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@@ -6,10 +6,12 @@ import dan200.computercraft.api.lua.LuaException;
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.*;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.*;
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.optInt;
|
||||
|
||||
public class EncodedInputHandle extends HandleGeneric
|
||||
{
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
private final BufferedReader m_reader;
|
||||
|
||||
public EncodedInputHandle( BufferedReader reader )
|
||||
@@ -111,15 +113,45 @@ public class EncodedInputHandle extends HandleGeneric
|
||||
try
|
||||
{
|
||||
int count = optInt( args, 0, 1 );
|
||||
if( count <= 0 || count >= 1024 * 16 )
|
||||
if( count < 0 )
|
||||
{
|
||||
throw new LuaException( "Count out of range" );
|
||||
// Whilst this may seem absurd to allow reading 0 characters, PUC Lua it so
|
||||
// it seems best to remain somewhat consistent.
|
||||
throw new LuaException( "Cannot read a negative number of characters" );
|
||||
}
|
||||
else if( count <= BUFFER_SIZE )
|
||||
{
|
||||
// If we've got a small count, then allocate that and read it.
|
||||
char[] chars = new char[ count ];
|
||||
int read = m_reader.read( chars );
|
||||
|
||||
return read < 0 ? null : new Object[] { new String( chars, 0, read ) };
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we've got a large count, read in bunches of 8192.
|
||||
char[] buffer = new char[ BUFFER_SIZE ];
|
||||
|
||||
// Read the initial set of characters, failing if none are read.
|
||||
int read = m_reader.read( buffer, 0, Math.min( buffer.length, count ) );
|
||||
if( read == -1 ) return null;
|
||||
|
||||
StringBuilder out = new StringBuilder( read );
|
||||
count -= read;
|
||||
out.append( buffer, 0, read );
|
||||
|
||||
// Otherwise read until we either reach the limit or we no longer consume
|
||||
// the full buffer.
|
||||
while( read >= BUFFER_SIZE && count > 0 )
|
||||
{
|
||||
read = m_reader.read( buffer, 0, Math.min( BUFFER_SIZE, count ) );
|
||||
if( read == -1 ) break;
|
||||
count -= read;
|
||||
out.append( buffer, 0, read );
|
||||
}
|
||||
|
||||
return new Object[] { out.toString() };
|
||||
}
|
||||
char[] bytes = new char[ count ];
|
||||
count = m_reader.read( bytes );
|
||||
if( count < 0 ) return null;
|
||||
String str = new String( bytes, 0, count );
|
||||
return new Object[] { str };
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
|
@@ -15,6 +15,7 @@ import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.core.apis.IAPIEnvironment;
|
||||
import dan200.computercraft.core.apis.handles.BinaryInputHandle;
|
||||
import dan200.computercraft.core.apis.handles.EncodedInputHandle;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.*;
|
||||
@@ -78,8 +79,10 @@ public class HTTPRequest implements Runnable
|
||||
private final String m_postText;
|
||||
private final Map<String, String> m_headers;
|
||||
private boolean m_binary;
|
||||
private final String m_method;
|
||||
private final boolean m_followRedirects;
|
||||
|
||||
public HTTPRequest( IAPIEnvironment environment, String urlString, URL url, final String postText, final Map<String, String> headers, boolean binary ) throws HTTPRequestException
|
||||
public HTTPRequest( IAPIEnvironment environment, String urlString, URL url, final String postText, final Map<String, String> headers, boolean binary, final String method, final boolean followRedirects ) throws HTTPRequestException
|
||||
{
|
||||
m_environment = environment;
|
||||
m_urlString = urlString;
|
||||
@@ -87,6 +90,8 @@ public class HTTPRequest implements Runnable
|
||||
m_binary = binary;
|
||||
m_postText = postText;
|
||||
m_headers = headers;
|
||||
m_method = method;
|
||||
m_followRedirects = followRedirects;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -101,7 +106,7 @@ public class HTTPRequest implements Runnable
|
||||
{
|
||||
// Queue the failure event if not.
|
||||
String error = e.getMessage();
|
||||
m_environment.queueEvent( "http_failure", new Object[] { m_urlString, error == null ? "Could not connect" : error, null } );
|
||||
m_environment.queueEvent( "http_failure", new Object[]{ m_urlString, error == null ? "Could not connect" : error, null } );
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -119,6 +124,8 @@ public class HTTPRequest implements Runnable
|
||||
{
|
||||
connection.setRequestMethod( "GET" );
|
||||
}
|
||||
if( m_method != null ) connection.setRequestMethod( m_method );
|
||||
connection.setInstanceFollowRedirects( m_followRedirects );
|
||||
|
||||
// Set headers
|
||||
connection.setRequestProperty( "accept-charset", "UTF-8" );
|
||||
@@ -134,6 +141,11 @@ public class HTTPRequest implements Runnable
|
||||
}
|
||||
}
|
||||
|
||||
// Add request size and count to the tracker before opening the connection
|
||||
m_environment.addTrackingChange( TrackingField.HTTP_REQUESTS );
|
||||
m_environment.addTrackingChange( TrackingField.HTTP_UPLOAD,
|
||||
getHeaderSize( connection.getRequestProperties() ) + (m_postText == null ? 0 : m_postText.length()) );
|
||||
|
||||
// Send POST text
|
||||
if( m_postText != null )
|
||||
{
|
||||
@@ -178,6 +190,9 @@ public class HTTPRequest implements Runnable
|
||||
headers.put( header.getKey(), joiner.join( header.getValue() ) );
|
||||
}
|
||||
|
||||
m_environment.addTrackingChange( TrackingField.HTTP_DOWNLOAD,
|
||||
getHeaderSize( connection.getHeaderFields() ) + result.length );
|
||||
|
||||
InputStream contents = new ByteArrayInputStream( result );
|
||||
ILuaObject stream = wrapStream(
|
||||
m_binary ? new BinaryInputHandle( contents ) : new EncodedInputHandle( contents, connection.getContentEncoding() ),
|
||||
@@ -189,17 +204,17 @@ public class HTTPRequest implements Runnable
|
||||
// Queue the appropriate event.
|
||||
if( responseSuccess )
|
||||
{
|
||||
m_environment.queueEvent( "http_success", new Object[] { m_urlString, stream } );
|
||||
m_environment.queueEvent( "http_success", new Object[]{ m_urlString, stream } );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_environment.queueEvent( "http_failure", new Object[] { m_urlString, "Could not connect", stream } );
|
||||
m_environment.queueEvent( "http_failure", new Object[]{ m_urlString, "Could not connect", stream } );
|
||||
}
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
// There was an error
|
||||
m_environment.queueEvent( "http_failure", new Object[] { m_urlString, "Could not connect", null } );
|
||||
m_environment.queueEvent( "http_failure", new Object[]{ m_urlString, "Could not connect", null } );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,8 +224,8 @@ public class HTTPRequest implements Runnable
|
||||
final int methodOffset = oldMethods.length;
|
||||
|
||||
final String[] newMethods = Arrays.copyOf( oldMethods, oldMethods.length + 2 );
|
||||
newMethods[ methodOffset + 0 ] = "getResponseCode";
|
||||
newMethods[ methodOffset + 1 ] = "getResponseHeaders";
|
||||
newMethods[methodOffset + 0] = "getResponseCode";
|
||||
newMethods[methodOffset + 1] = "getResponseHeaders";
|
||||
|
||||
return new ILuaObject()
|
||||
{
|
||||
@@ -233,12 +248,12 @@ public class HTTPRequest implements Runnable
|
||||
case 0:
|
||||
{
|
||||
// getResponseCode
|
||||
return new Object[] { responseCode };
|
||||
return new Object[]{ responseCode };
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
// getResponseHeaders
|
||||
return new Object[] { responseHeaders };
|
||||
return new Object[]{ responseHeaders };
|
||||
}
|
||||
default:
|
||||
{
|
||||
@@ -248,4 +263,15 @@ public class HTTPRequest implements Runnable
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static long getHeaderSize( Map<String, List<String>> headers )
|
||||
{
|
||||
long size = 0;
|
||||
for( Map.Entry<String, List<String>> header : headers.entrySet() )
|
||||
{
|
||||
size += header.getKey() == null ? 0 : header.getKey().length();
|
||||
for( String value : header.getValue() ) size += value == null ? 0 : value.length() + 1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,7 @@ import dan200.computercraft.api.lua.ILuaObject;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.core.apis.HTTPAPI;
|
||||
import dan200.computercraft.core.apis.IAPIEnvironment;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@@ -71,7 +72,7 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
private void onClosed()
|
||||
{
|
||||
close( true );
|
||||
computer.queueEvent( CLOSE_EVENT, new Object[] { url } );
|
||||
computer.queueEvent( CLOSE_EVENT, new Object[]{ url } );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,7 +103,7 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
if( !handshaker.isHandshakeComplete() )
|
||||
{
|
||||
handshaker.finishHandshake( ch, (FullHttpResponse) msg );
|
||||
computer.queueEvent( SUCCESS_EVENT, new Object[] { url, this } );
|
||||
computer.queueEvent( SUCCESS_EVENT, new Object[]{ url, this } );
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -115,14 +116,19 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
WebSocketFrame frame = (WebSocketFrame) msg;
|
||||
if( frame instanceof TextWebSocketFrame )
|
||||
{
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[] { url, ((TextWebSocketFrame) frame).text() } );
|
||||
String data = ((TextWebSocketFrame) frame).text();
|
||||
|
||||
computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, data.length() );
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[]{ url, data } );
|
||||
}
|
||||
else if( frame instanceof BinaryWebSocketFrame )
|
||||
{
|
||||
ByteBuf data = frame.content();
|
||||
byte[] converted = new byte[ data.readableBytes() ];
|
||||
byte[] converted = new byte[data.readableBytes()];
|
||||
data.readBytes( converted );
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[] { url, data } );
|
||||
|
||||
computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, converted.length );
|
||||
computer.queueEvent( MESSAGE_EVENT, new Object[]{ url, converted } );
|
||||
}
|
||||
else if( frame instanceof CloseWebSocketFrame )
|
||||
{
|
||||
@@ -135,7 +141,7 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause )
|
||||
{
|
||||
ctx.close();
|
||||
computer.queueEvent( FAILURE_EVENT, new Object[] {
|
||||
computer.queueEvent( FAILURE_EVENT, new Object[]{
|
||||
url,
|
||||
cause instanceof WebSocketHandshakeException ? cause.getMessage() : "Could not connect"
|
||||
} );
|
||||
@@ -145,7 +151,7 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
@Override
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
return new String[] { "receive", "send", "close" };
|
||||
return new String[]{ "receive", "send", "close" };
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -159,15 +165,16 @@ public class WebsocketConnection extends SimpleChannelInboundHandler<Object> imp
|
||||
{
|
||||
checkOpen();
|
||||
Object[] event = context.pullEvent( MESSAGE_EVENT );
|
||||
if( event.length >= 3 && Objects.equal( event[ 1 ], url ) )
|
||||
if( event.length >= 3 && Objects.equal( event[1], url ) )
|
||||
{
|
||||
return new Object[] { event[ 2 ] };
|
||||
return new Object[]{ event[2] };
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
checkOpen();
|
||||
String text = arguments.length > 0 && arguments[ 0 ] != null ? arguments[ 0 ].toString() : "";
|
||||
String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";
|
||||
computer.addTrackingChange( TrackingField.WEBSOCKET_OUTGOING, text.length() );
|
||||
channel.writeAndFlush( new TextWebSocketFrame( text ) );
|
||||
return null;
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
|
||||
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;
|
||||
import io.netty.handler.ssl.SslContext;
|
||||
import io.netty.handler.ssl.SslContextBuilder;
|
||||
|
||||
@@ -34,7 +35,7 @@ import java.security.KeyStore;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/*
|
||||
/**
|
||||
* Provides functionality to verify and connect to a remote websocket.
|
||||
*/
|
||||
public final class WebsocketConnector
|
||||
@@ -177,7 +178,7 @@ public final class WebsocketConnector
|
||||
httpHeaders.add( header.getKey(), header.getValue() );
|
||||
}
|
||||
|
||||
WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker( uri, WebSocketVersion.V13, null, false, httpHeaders );
|
||||
WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker( uri, WebSocketVersion.V13, null, true, httpHeaders );
|
||||
final WebsocketConnection connection = new WebsocketConnection( environment, api, handshaker, address );
|
||||
|
||||
new Bootstrap()
|
||||
@@ -190,7 +191,12 @@ public final class WebsocketConnector
|
||||
{
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
if( ssl != null ) p.addLast( ssl.newHandler( ch.alloc(), uri.getHost(), port ) );
|
||||
p.addLast( new HttpClientCodec(), new HttpObjectAggregator( 8192 ), connection );
|
||||
p.addLast(
|
||||
new HttpClientCodec(),
|
||||
new HttpObjectAggregator( 8192 ),
|
||||
WebSocketClientCompressionHandler.INSTANCE,
|
||||
connection
|
||||
);
|
||||
}
|
||||
} )
|
||||
.remoteAddress( socketAddress )
|
||||
|
@@ -20,6 +20,8 @@ import dan200.computercraft.core.filesystem.FileSystemException;
|
||||
import dan200.computercraft.core.lua.CobaltLuaMachine;
|
||||
import dan200.computercraft.core.lua.ILuaMachine;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
@@ -167,6 +169,12 @@ public class Computer
|
||||
m_computer.setLabel( label );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTrackingChange( TrackingField field, long change )
|
||||
{
|
||||
Tracking.addValue( m_computer, field, change );
|
||||
}
|
||||
|
||||
public void onPeripheralChanged( int side, IPeripheral peripheral )
|
||||
{
|
||||
synchronized( m_computer.m_peripherals )
|
||||
|
@@ -7,6 +7,7 @@
|
||||
package dan200.computercraft.core.computer;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -68,17 +69,17 @@ public class ComputerThread
|
||||
s_stopped = false;
|
||||
if( s_threads == null || s_threads.length != ComputerCraft.computer_threads )
|
||||
{
|
||||
s_threads = new Thread[ ComputerCraft.computer_threads ];
|
||||
s_threads = new Thread[ComputerCraft.computer_threads];
|
||||
}
|
||||
|
||||
SecurityManager manager = System.getSecurityManager();
|
||||
final ThreadGroup group = manager == null ? Thread.currentThread().getThreadGroup() : manager.getThreadGroup();
|
||||
for( int i = 0; i < s_threads.length; i++ )
|
||||
{
|
||||
Thread thread = s_threads[ i ];
|
||||
Thread thread = s_threads[i];
|
||||
if( thread == null || !thread.isAlive() )
|
||||
{
|
||||
thread = s_threads[ i ] = new Thread( group, new TaskExecutor(), "ComputerCraft-Computer-Manager-" + s_ManagerCounter.getAndIncrement() );
|
||||
thread = s_threads[i] = new Thread( group, new TaskExecutor(), "ComputerCraft-Computer-Manager-" + s_ManagerCounter.getAndIncrement() );
|
||||
thread.setDaemon( true );
|
||||
thread.start();
|
||||
}
|
||||
@@ -195,6 +196,8 @@ public class ComputerThread
|
||||
thread.start();
|
||||
}
|
||||
|
||||
long start = System.nanoTime();
|
||||
|
||||
// Execute the task
|
||||
runner.submit( task );
|
||||
|
||||
@@ -221,6 +224,23 @@ public class ComputerThread
|
||||
// Interrupt the thread
|
||||
if( !done )
|
||||
{
|
||||
StringBuilder builder = new StringBuilder( "Terminating " );
|
||||
if( computer != null )
|
||||
{
|
||||
builder.append( "computer " ).append( computer.getID() );
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.append( "unknown computer" );
|
||||
}
|
||||
|
||||
builder.append( ". Thread is currently running" );
|
||||
for( StackTraceElement element : thread.getStackTrace() )
|
||||
{
|
||||
builder.append( "\n at " ).append( element );
|
||||
}
|
||||
ComputerCraft.log.error( builder.toString() );
|
||||
|
||||
thread.interrupt();
|
||||
thread = null;
|
||||
runner = null;
|
||||
@@ -229,6 +249,10 @@ public class ComputerThread
|
||||
}
|
||||
finally
|
||||
{
|
||||
long stop = System.nanoTime();
|
||||
Computer computer = task.getOwner();
|
||||
if( computer != null ) Tracking.addTaskTiming( computer, stop - start );
|
||||
|
||||
// Re-add it back onto the queue or remove it
|
||||
synchronized( s_taskLock )
|
||||
{
|
||||
|
@@ -0,0 +1,9 @@
|
||||
package dan200.computercraft.core.computer;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public interface IComputerOwned
|
||||
{
|
||||
@Nullable
|
||||
Computer getComputer();
|
||||
}
|
@@ -6,14 +6,17 @@
|
||||
|
||||
package dan200.computercraft.core.computer;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
|
||||
public class MainThread
|
||||
{
|
||||
private static final int MAX_TASKS_PER_TICK = 1000;
|
||||
private static final int MAX_TASKS_TOTAL = 50000;
|
||||
|
||||
private static final LinkedList<ITask> m_outstandingTasks = new LinkedList<>();
|
||||
private static final Queue<ITask> m_outstandingTasks = new ArrayDeque<>();
|
||||
private static final Object m_nextUnusedTaskIDLock = new Object();
|
||||
private static long m_nextUnusedTaskID = 0;
|
||||
|
||||
@@ -31,7 +34,7 @@ public class MainThread
|
||||
{
|
||||
if( m_outstandingTasks.size() < MAX_TASKS_TOTAL )
|
||||
{
|
||||
m_outstandingTasks.addLast( task );
|
||||
m_outstandingTasks.offer( task );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -46,14 +49,17 @@ public class MainThread
|
||||
ITask task = null;
|
||||
synchronized( m_outstandingTasks )
|
||||
{
|
||||
if( m_outstandingTasks.size() > 0 )
|
||||
{
|
||||
task = m_outstandingTasks.removeFirst();
|
||||
}
|
||||
task = m_outstandingTasks.poll();
|
||||
}
|
||||
if( task != null )
|
||||
{
|
||||
long start = System.nanoTime();
|
||||
task.execute();
|
||||
|
||||
long stop = System.nanoTime();
|
||||
Computer computer = task.getOwner();
|
||||
if( computer != null ) Tracking.addServerTiming( computer, stop - start );
|
||||
|
||||
++tasksThisTick;
|
||||
}
|
||||
else
|
||||
|
@@ -56,7 +56,7 @@ public class Terminal
|
||||
m_palette = new Palette();
|
||||
}
|
||||
|
||||
public void reset()
|
||||
public synchronized void reset()
|
||||
{
|
||||
m_cursorColour = 0;
|
||||
m_cursorBackgroundColour = 15;
|
||||
@@ -76,7 +76,7 @@ public class Terminal
|
||||
return m_height;
|
||||
}
|
||||
|
||||
public void resize( int width, int height )
|
||||
public synchronized void resize( int width, int height )
|
||||
{
|
||||
if( width == m_width && height == m_height )
|
||||
{
|
||||
@@ -189,7 +189,7 @@ public class Terminal
|
||||
return m_palette;
|
||||
}
|
||||
|
||||
public void blit( String text, String textColour, String backgroundColour )
|
||||
public synchronized void blit( String text, String textColour, String backgroundColour )
|
||||
{
|
||||
int x = m_cursorX;
|
||||
int y = m_cursorY;
|
||||
@@ -202,7 +202,7 @@ public class Terminal
|
||||
}
|
||||
}
|
||||
|
||||
public void write( String text )
|
||||
public synchronized void write( String text )
|
||||
{
|
||||
int x = m_cursorX;
|
||||
int y = m_cursorY;
|
||||
@@ -215,7 +215,7 @@ public class Terminal
|
||||
}
|
||||
}
|
||||
|
||||
public void scroll( int yDiff )
|
||||
public synchronized void scroll( int yDiff )
|
||||
{
|
||||
if( yDiff != 0 )
|
||||
{
|
||||
@@ -245,7 +245,7 @@ public class Terminal
|
||||
}
|
||||
}
|
||||
|
||||
public void clear()
|
||||
public synchronized void clear()
|
||||
{
|
||||
for( int y = 0; y < m_height; ++y )
|
||||
{
|
||||
@@ -256,7 +256,7 @@ public class Terminal
|
||||
m_changed = true;
|
||||
}
|
||||
|
||||
public void clearLine()
|
||||
public synchronized void clearLine()
|
||||
{
|
||||
int y = m_cursorY;
|
||||
if( y >= 0 && y < m_height )
|
||||
@@ -268,7 +268,7 @@ public class Terminal
|
||||
}
|
||||
}
|
||||
|
||||
public TextBuffer getLine( int y )
|
||||
public synchronized TextBuffer getLine( int y )
|
||||
{
|
||||
if( y >= 0 && y < m_height )
|
||||
{
|
||||
@@ -277,7 +277,7 @@ public class Terminal
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setLine( int y, String text, String textColour, String backgroundColour )
|
||||
public synchronized void setLine( int y, String text, String textColour, String backgroundColour )
|
||||
{
|
||||
m_text[y].write( text );
|
||||
m_textColour[y].write( textColour );
|
||||
@@ -285,7 +285,7 @@ public class Terminal
|
||||
m_changed = true;
|
||||
}
|
||||
|
||||
public TextBuffer getTextColourLine( int y )
|
||||
public synchronized TextBuffer getTextColourLine( int y )
|
||||
{
|
||||
if( y>=0 && y<m_height )
|
||||
{
|
||||
@@ -294,7 +294,7 @@ public class Terminal
|
||||
return null;
|
||||
}
|
||||
|
||||
public TextBuffer getBackgroundColourLine( int y )
|
||||
public synchronized TextBuffer getBackgroundColourLine( int y )
|
||||
{
|
||||
if( y>=0 && y<m_height )
|
||||
{
|
||||
@@ -318,7 +318,7 @@ public class Terminal
|
||||
m_changed = false;
|
||||
}
|
||||
|
||||
public NBTTagCompound writeToNBT( NBTTagCompound nbttagcompound )
|
||||
public synchronized NBTTagCompound writeToNBT( NBTTagCompound nbttagcompound )
|
||||
{
|
||||
nbttagcompound.setInteger( "term_cursorX", m_cursorX );
|
||||
nbttagcompound.setInteger( "term_cursorY", m_cursorY );
|
||||
@@ -338,7 +338,7 @@ public class Terminal
|
||||
return nbttagcompound;
|
||||
}
|
||||
|
||||
public void readFromNBT( NBTTagCompound nbttagcompound )
|
||||
public synchronized void readFromNBT( NBTTagCompound nbttagcompound )
|
||||
{
|
||||
m_cursorX = nbttagcompound.getInteger( "term_cursorX" );
|
||||
m_cursorY = nbttagcompound.getInteger( "term_cursorY" );
|
||||
|
@@ -0,0 +1,117 @@
|
||||
package dan200.computercraft.core.tracking;
|
||||
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import gnu.trove.map.hash.TObjectLongHashMap;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class ComputerTracker
|
||||
{
|
||||
private final WeakReference<Computer> computer;
|
||||
private final int computerId;
|
||||
|
||||
private long tasks;
|
||||
private long totalTime;
|
||||
private long maxTime;
|
||||
|
||||
private long serverCount;
|
||||
private long serverTime;
|
||||
|
||||
private final TObjectLongHashMap<TrackingField> fields;
|
||||
|
||||
public ComputerTracker( Computer computer )
|
||||
{
|
||||
this.computer = new WeakReference<>( computer );
|
||||
this.computerId = computer.getID();
|
||||
this.fields = new TObjectLongHashMap<>();
|
||||
}
|
||||
|
||||
ComputerTracker( ComputerTracker timings )
|
||||
{
|
||||
this.computer = timings.computer;
|
||||
this.computerId = timings.computerId;
|
||||
|
||||
this.tasks = timings.tasks;
|
||||
this.totalTime = timings.totalTime;
|
||||
this.maxTime = timings.maxTime;
|
||||
|
||||
this.serverCount = timings.serverCount;
|
||||
this.serverTime = timings.serverTime;
|
||||
|
||||
this.fields = new TObjectLongHashMap<>( timings.fields );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Computer getComputer()
|
||||
{
|
||||
return computer.get();
|
||||
}
|
||||
|
||||
public int getComputerId()
|
||||
{
|
||||
return computerId;
|
||||
}
|
||||
|
||||
public long getTasks()
|
||||
{
|
||||
return tasks;
|
||||
}
|
||||
|
||||
public long getTotalTime()
|
||||
{
|
||||
return totalTime;
|
||||
}
|
||||
|
||||
public long getMaxTime()
|
||||
{
|
||||
return maxTime;
|
||||
}
|
||||
|
||||
public long getAverage()
|
||||
{
|
||||
return totalTime / tasks;
|
||||
}
|
||||
|
||||
void addTaskTiming( long time )
|
||||
{
|
||||
tasks++;
|
||||
totalTime += time;
|
||||
if( time > maxTime ) maxTime = time;
|
||||
}
|
||||
|
||||
void addMainTiming( long time )
|
||||
{
|
||||
serverCount++;
|
||||
serverTime += time;
|
||||
}
|
||||
|
||||
void addValue( TrackingField field, long change )
|
||||
{
|
||||
synchronized( fields )
|
||||
{
|
||||
fields.adjustOrPutValue( field, change, change );
|
||||
}
|
||||
}
|
||||
|
||||
public long get( TrackingField field )
|
||||
{
|
||||
if( field == TrackingField.TASKS ) return tasks;
|
||||
if( field == TrackingField.MAX_TIME ) return maxTime;
|
||||
if( field == TrackingField.TOTAL_TIME ) return totalTime;
|
||||
if( field == TrackingField.AVERAGE_TIME ) return tasks == 0 ? 0 : totalTime / tasks;
|
||||
|
||||
if( field == TrackingField.SERVER_COUNT ) return serverCount;
|
||||
if( field == TrackingField.SERVER_TIME ) return serverTime;
|
||||
|
||||
synchronized( fields )
|
||||
{
|
||||
return fields.get( field );
|
||||
}
|
||||
}
|
||||
|
||||
public String getFormatted( TrackingField field )
|
||||
{
|
||||
return field.format( get( field ) );
|
||||
}
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
package dan200.computercraft.core.tracking;
|
||||
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
|
||||
public interface Tracker
|
||||
{
|
||||
@Deprecated
|
||||
default void addTiming( Computer computer, long time )
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Report how long a task executed on the computer thread took.
|
||||
*
|
||||
* Computer thread tasks include events or a computer being turned on/off.
|
||||
*
|
||||
* @param computer The computer processing this task
|
||||
* @param time The time taken for this task.
|
||||
*/
|
||||
default void addTaskTiming( Computer computer, long time )
|
||||
{
|
||||
//noinspection deprecation
|
||||
addTiming( computer, time );
|
||||
}
|
||||
|
||||
/**
|
||||
* Report how long a task executed on the server thread took.
|
||||
*
|
||||
* Server tasks include actions performed by peripherals.
|
||||
*
|
||||
* @param computer The computer processing this task
|
||||
* @param time The time taken for this task.
|
||||
*/
|
||||
default void addServerTiming( Computer computer, long time )
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment an arbitrary field by some value. Implementations may track how often this is called
|
||||
* as well as the change, to compute some level of "average".
|
||||
*
|
||||
* @param computer The computer to increment
|
||||
* @param field The field to increment.
|
||||
* @param change The amount to increment said field by.
|
||||
*/
|
||||
default void addValue( Computer computer, TrackingField field, long change )
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
package dan200.computercraft.core.tracking;
|
||||
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Tracking
|
||||
{
|
||||
static final AtomicInteger tracking = new AtomicInteger( 0 );
|
||||
|
||||
private static final Object lock = new Object();
|
||||
private static final HashMap<UUID, TrackingContext> contexts = new HashMap<>();
|
||||
private static final List<Tracker> trackers = new ArrayList<>();
|
||||
|
||||
public static TrackingContext getContext( UUID uuid )
|
||||
{
|
||||
synchronized( lock )
|
||||
{
|
||||
TrackingContext context = contexts.get( uuid );
|
||||
if( context == null ) contexts.put( uuid, context = new TrackingContext() );
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
public static void add( Tracker tracker )
|
||||
{
|
||||
synchronized( lock )
|
||||
{
|
||||
trackers.add( tracker );
|
||||
tracking.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
||||
public static void addTaskTiming( Computer computer, long time )
|
||||
{
|
||||
if( tracking.get() == 0 ) return;
|
||||
|
||||
synchronized( contexts )
|
||||
{
|
||||
for( TrackingContext context : contexts.values() ) context.addTaskTiming( computer, time );
|
||||
for( Tracker tracker : trackers ) tracker.addTaskTiming( computer, time );
|
||||
}
|
||||
}
|
||||
|
||||
public static void addServerTiming( Computer computer, long time )
|
||||
{
|
||||
if( tracking.get() == 0 ) return;
|
||||
|
||||
synchronized( contexts )
|
||||
{
|
||||
for( TrackingContext context : contexts.values() ) context.addServerTiming( computer, time );
|
||||
for( Tracker tracker : trackers ) tracker.addServerTiming( computer, time );
|
||||
}
|
||||
}
|
||||
|
||||
public static void addValue( Computer computer, TrackingField field, long change )
|
||||
{
|
||||
if( tracking.get() == 0 ) return;
|
||||
|
||||
synchronized( lock )
|
||||
{
|
||||
for( TrackingContext context : contexts.values() ) context.addValue( computer, field, change );
|
||||
for( Tracker tracker : trackers ) tracker.addValue( computer, field, change );
|
||||
}
|
||||
}
|
||||
|
||||
public static void reset()
|
||||
{
|
||||
synchronized( lock )
|
||||
{
|
||||
contexts.clear();
|
||||
trackers.clear();
|
||||
tracking.set( 0 );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,111 @@
|
||||
package dan200.computercraft.core.tracking;
|
||||
|
||||
import com.google.common.collect.MapMaker;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Tracks timing information about computers, including how long they ran for
|
||||
* and the number of events they handled.
|
||||
*
|
||||
* Note that this <em>will</em> track computers which have been deleted (hence
|
||||
* the presence of {@link #timingLookup} and {@link #timings}
|
||||
*/
|
||||
public class TrackingContext implements Tracker
|
||||
{
|
||||
private boolean tracking = false;
|
||||
|
||||
private final List<ComputerTracker> timings = new ArrayList<>();
|
||||
private final Map<Computer, ComputerTracker> timingLookup = new MapMaker().weakKeys().makeMap();
|
||||
|
||||
public synchronized void start()
|
||||
{
|
||||
if( !tracking ) Tracking.tracking.incrementAndGet();
|
||||
tracking = true;
|
||||
|
||||
timings.clear();
|
||||
timingLookup.clear();
|
||||
}
|
||||
|
||||
public synchronized boolean stop()
|
||||
{
|
||||
if( !tracking ) return false;
|
||||
|
||||
Tracking.tracking.decrementAndGet();
|
||||
tracking = false;
|
||||
timingLookup.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
public synchronized List<ComputerTracker> getImmutableTimings()
|
||||
{
|
||||
ArrayList<ComputerTracker> timings = new ArrayList<>( this.timings.size() );
|
||||
for( ComputerTracker timing : this.timings ) timings.add( new ComputerTracker( timing ) );
|
||||
return timings;
|
||||
}
|
||||
|
||||
public synchronized List<ComputerTracker> getTimings()
|
||||
{
|
||||
return new ArrayList<>( timings );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTaskTiming( Computer computer, long time )
|
||||
{
|
||||
if( !tracking ) return;
|
||||
|
||||
synchronized( this )
|
||||
{
|
||||
ComputerTracker computerTimings = timingLookup.get( computer );
|
||||
if( computerTimings == null )
|
||||
{
|
||||
computerTimings = new ComputerTracker( computer );
|
||||
timingLookup.put( computer, computerTimings );
|
||||
timings.add( computerTimings );
|
||||
}
|
||||
|
||||
computerTimings.addTaskTiming( time );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addServerTiming( Computer computer, long time )
|
||||
{
|
||||
if( !tracking ) return;
|
||||
|
||||
synchronized( this )
|
||||
{
|
||||
ComputerTracker computerTimings = timingLookup.get( computer );
|
||||
if( computerTimings == null )
|
||||
{
|
||||
computerTimings = new ComputerTracker( computer );
|
||||
timingLookup.put( computer, computerTimings );
|
||||
timings.add( computerTimings );
|
||||
}
|
||||
|
||||
computerTimings.addMainTiming( time );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addValue( Computer computer, TrackingField field, long change )
|
||||
{
|
||||
if( !tracking ) return;
|
||||
|
||||
synchronized( this )
|
||||
{
|
||||
ComputerTracker computerTimings = timingLookup.get( computer );
|
||||
if( computerTimings == null )
|
||||
{
|
||||
computerTimings = new ComputerTracker( computer );
|
||||
timingLookup.put( computer, computerTimings );
|
||||
timings.add( computerTimings );
|
||||
}
|
||||
|
||||
computerTimings.addValue( field, change );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,88 @@
|
||||
package dan200.computercraft.core.tracking;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
public class TrackingField
|
||||
{
|
||||
private static final Map<String, TrackingField> fields = new HashMap<>();
|
||||
|
||||
public static final TrackingField TASKS = TrackingField.of( "tasks", "Tasks", x -> String.format( "%4d", x ) );
|
||||
public static final TrackingField TOTAL_TIME = TrackingField.of( "total", "Total time", x -> String.format( "%7.1fms", x / 1e6 ) );
|
||||
public static final TrackingField AVERAGE_TIME = TrackingField.of( "average", "Average time", x -> String.format( "%4.1fms", x / 1e6 ) );
|
||||
public static final TrackingField MAX_TIME = TrackingField.of( "max", "Max time", x -> String.format( "%5.1fms", x / 1e6 ) );
|
||||
|
||||
public static final TrackingField SERVER_COUNT = TrackingField.of( "server_count", "Server task count", x -> String.format( "%4d", x ) );
|
||||
public static final TrackingField SERVER_TIME = TrackingField.of( "server_time", "Server task time", x -> String.format( "%7.1fms", x / 1e6 ) );
|
||||
|
||||
public static final TrackingField PERIPHERAL_OPS = TrackingField.of( "peripheral", "Peripheral calls", TrackingField::formatDefault );
|
||||
public static final TrackingField FS_OPS = TrackingField.of( "fs", "Filesystem operations", TrackingField::formatDefault );
|
||||
public static final TrackingField TURTLE_OPS = TrackingField.of( "turtle", "Turtle operations", TrackingField::formatDefault );
|
||||
|
||||
public static final TrackingField HTTP_REQUESTS = TrackingField.of( "http", "HTTP requests", TrackingField::formatDefault );
|
||||
public static final TrackingField HTTP_UPLOAD = TrackingField.of( "http_upload", "HTTP upload", TrackingField::formatBytes );
|
||||
public static final TrackingField HTTP_DOWNLOAD = TrackingField.of( "http_download", "HTTT download", TrackingField::formatBytes );
|
||||
|
||||
public static final TrackingField WEBSOCKET_INCOMING = TrackingField.of( "websocket_incoming", "Websocket incoming", TrackingField::formatBytes );
|
||||
public static final TrackingField WEBSOCKET_OUTGOING = TrackingField.of( "websocket_outgoing", "Websocket outgoing", TrackingField::formatBytes );
|
||||
|
||||
private final String id;
|
||||
private final String displayName;
|
||||
private final LongFunction<String> format;
|
||||
|
||||
public String id()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public String displayName()
|
||||
{
|
||||
return displayName;
|
||||
}
|
||||
|
||||
private TrackingField( String id, String displayName, LongFunction<String> format )
|
||||
{
|
||||
this.id = id;
|
||||
this.displayName = displayName;
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String format( long value )
|
||||
{
|
||||
return format.apply( value );
|
||||
}
|
||||
|
||||
public static TrackingField of( String id, String displayName, LongFunction<String> format )
|
||||
{
|
||||
TrackingField field = new TrackingField( id, displayName, format );
|
||||
fields.put( id, field );
|
||||
return field;
|
||||
}
|
||||
|
||||
public static Map<String, TrackingField> fields()
|
||||
{
|
||||
return Collections.unmodifiableMap( fields );
|
||||
}
|
||||
|
||||
private static String formatDefault( long value )
|
||||
{
|
||||
return String.format( "%6d", value );
|
||||
}
|
||||
|
||||
/**
|
||||
* So technically a kibibyte, but let's not argue here.
|
||||
*/
|
||||
private static final int KILOBYTE_SIZE = 1024;
|
||||
|
||||
private static final String SI_PREFIXES = "KMGT";
|
||||
|
||||
private static String formatBytes( long bytes )
|
||||
{
|
||||
if( bytes < 1024 ) return String.format( "%10d B", bytes );
|
||||
int exp = (int) (Math.log( bytes ) / Math.log( KILOBYTE_SIZE ));
|
||||
if( exp > SI_PREFIXES.length() ) exp = SI_PREFIXES.length();
|
||||
return String.format( "%10.1f %siB", bytes / Math.pow( KILOBYTE_SIZE, exp ), SI_PREFIXES.charAt( exp - 1 ) );
|
||||
}
|
||||
}
|
@@ -1,27 +1,39 @@
|
||||
package dan200.computercraft.shared.command;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.tracking.ComputerTracker;
|
||||
import dan200.computercraft.core.tracking.Tracking;
|
||||
import dan200.computercraft.core.tracking.TrackingContext;
|
||||
import dan200.computercraft.core.tracking.TrackingField;
|
||||
import dan200.computercraft.shared.command.framework.*;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.command.CommandBase;
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import static dan200.computercraft.shared.command.framework.ChatHelpers.*;
|
||||
|
||||
public final class CommandComputerCraft extends CommandDelegate
|
||||
{
|
||||
public static final UUID SYSTEM_UUID = new UUID( 0, 0 );
|
||||
|
||||
private static final int DUMP_LIST_ID = 5373952;
|
||||
private static final int DUMP_SINGLE_ID = 1844510720;
|
||||
private static final int TRACK_ID = 373882880;
|
||||
|
||||
public CommandComputerCraft()
|
||||
{
|
||||
super( create() );
|
||||
@@ -37,8 +49,8 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
|
||||
root.register( new SubCommandBase(
|
||||
"dump", "[id]", "Display the status of computers.", UserLevel.OWNER_OP,
|
||||
"Display the status of all computers or specific information about one computer. You can either specify the computer's instance " +
|
||||
"id (e.g. 123) or computer id (e.g #123)."
|
||||
"Display the status of all computers or specific information about one computer. You can specify the " +
|
||||
"computer's instance id (e.g. 123), computer id (e.g #123) or label (e.g. \"@My Computer\")."
|
||||
)
|
||||
{
|
||||
@Override
|
||||
@@ -46,19 +58,43 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
{
|
||||
if( arguments.size() == 0 )
|
||||
{
|
||||
TextTable table = new TextTable( "Instance", "Id", "On", "Position" );
|
||||
TextTable table = new TextTable( DUMP_LIST_ID, "Computer", "On", "Position" );
|
||||
|
||||
int max = 50;
|
||||
for( ServerComputer computer : ComputerCraft.serverComputerRegistry.getComputers() )
|
||||
List<ServerComputer> computers = new ArrayList<>( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
|
||||
// Unless we're on a server, limit the number of rows we can send.
|
||||
if( !(context.getSender() instanceof MinecraftServer) )
|
||||
{
|
||||
World world = context.getSender().getEntityWorld();
|
||||
BlockPos pos = context.getSender().getPosition();
|
||||
|
||||
computers.sort( ( a, b ) -> {
|
||||
if( a.getWorld() == b.getWorld() && a.getWorld() == world )
|
||||
{
|
||||
return Double.compare( a.getPosition().distanceSq( pos ), b.getPosition().distanceSq( pos ) );
|
||||
}
|
||||
else if( a.getWorld() == world )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if( b.getWorld() == world )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Integer.compare( a.getInstanceID(), b.getInstanceID() );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
table.addRow(
|
||||
linkComputer( computer ),
|
||||
text( Integer.toString( computer.getID() ) ),
|
||||
linkComputer( context, computer, computer.getID() ),
|
||||
bool( computer.isOn() ),
|
||||
linkPosition( context, computer )
|
||||
);
|
||||
|
||||
if( max-- < 0 ) break;
|
||||
}
|
||||
|
||||
table.displayTo( context.getSender() );
|
||||
@@ -67,7 +103,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
{
|
||||
ServerComputer computer = ComputerSelector.getComputer( arguments.get( 0 ) );
|
||||
|
||||
TextTable table = new TextTable();
|
||||
TextTable table = new TextTable( DUMP_SINGLE_ID );
|
||||
table.addRow( header( "Instance" ), text( Integer.toString( computer.getInstanceID() ) ) );
|
||||
table.addRow( header( "Id" ), text( Integer.toString( computer.getID() ) ) );
|
||||
table.addRow( header( "Label" ), text( computer.getLabel() ) );
|
||||
@@ -80,7 +116,7 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
IPeripheral peripheral = computer.getPeripheral( i );
|
||||
if( peripheral != null )
|
||||
{
|
||||
table.addRow( header( "Peripheral " + Computer.s_sideNames[ i ] ), text( peripheral.getType() ) );
|
||||
table.addRow( header( "Peripheral " + Computer.s_sideNames[i] ), text( peripheral.getType() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,25 +134,25 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
{
|
||||
return arguments.size() == 1
|
||||
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
||||
: Collections.<String>emptyList();
|
||||
: Collections.emptyList();
|
||||
}
|
||||
} );
|
||||
|
||||
root.register( new SubCommandBase(
|
||||
"shutdown", "[ids...]", "Shutdown computers remotely.", UserLevel.OWNER_OP,
|
||||
"Shutdown the listed computers or all if none are specified. You can either specify the computer's instance " +
|
||||
"id (e.g. 123) or computer id (e.g #123)."
|
||||
"Shutdown the listed computers or all if none are specified. You can specify the computer's instance id " +
|
||||
"(e.g. 123), computer id (e.g #123) or label (e.g. \"@My Computer\")."
|
||||
)
|
||||
{
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||
{
|
||||
List<ServerComputer> computers = Lists.newArrayList();
|
||||
Set<ServerComputer> computers = Sets.newHashSet();
|
||||
if( arguments.size() > 0 )
|
||||
{
|
||||
for( String arg : arguments )
|
||||
{
|
||||
computers.add( ComputerSelector.getComputer( arg ) );
|
||||
computers.addAll( ComputerSelector.getComputers( arg ) );
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -138,7 +174,48 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
||||
{
|
||||
return arguments.size() == 0
|
||||
? Collections.<String>emptyList()
|
||||
? Collections.emptyList()
|
||||
: ComputerSelector.completeComputer( arguments.get( arguments.size() - 1 ) );
|
||||
}
|
||||
} );
|
||||
|
||||
root.register( new SubCommandBase(
|
||||
"turn-on", "ids...", "Turn computers on remotely.", UserLevel.OWNER_OP,
|
||||
"Turn on the listed computers. You can specify the computer's instance id (e.g. 123), computer id (e.g #123) " +
|
||||
"or label (e.g. \"@My Computer\")."
|
||||
)
|
||||
{
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||
{
|
||||
Set<ServerComputer> computers = Sets.newHashSet();
|
||||
if( arguments.size() > 0 )
|
||||
{
|
||||
for( String arg : arguments )
|
||||
{
|
||||
computers.addAll( ComputerSelector.getComputers( arg ) );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
computers.addAll( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
}
|
||||
|
||||
int on = 0;
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
if( !computer.isOn() ) on++;
|
||||
computer.turnOn();
|
||||
}
|
||||
context.getSender().sendMessage( text( "Turned on " + on + " / " + computers.size() + " computers" ) );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
||||
{
|
||||
return arguments.size() == 0
|
||||
? Collections.emptyList()
|
||||
: ComputerSelector.completeComputer( arguments.get( arguments.size() - 1 ) );
|
||||
}
|
||||
} );
|
||||
@@ -194,48 +271,179 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
{
|
||||
return arguments.size() == 1
|
||||
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
||||
: Collections.<String>emptyList();
|
||||
: Collections.emptyList();
|
||||
}
|
||||
} );
|
||||
|
||||
root.register(new SubCommandBase(
|
||||
root.register( new SubCommandBase(
|
||||
"view", "<id>", "View the terminal of a computer.", UserLevel.OP,
|
||||
"Open the terminal of a computer, allowing remote control of a computer. This does not provide access to " +
|
||||
"turtle's inventories. You can either specify the computer's instance id (e.g. 123) or computer id (e.g #123)."
|
||||
) {
|
||||
)
|
||||
{
|
||||
@Override
|
||||
public void execute(@Nonnull CommandContext context, @Nonnull List<String> arguments) throws CommandException {
|
||||
if (arguments.size() != 1) throw new CommandException(context.getFullUsage());
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||
{
|
||||
if( arguments.size() != 1 ) throw new CommandException( context.getFullUsage() );
|
||||
|
||||
ICommandSender sender = context.getSender();
|
||||
if (!(sender instanceof EntityPlayerMP)) {
|
||||
throw new CommandException("Cannot open terminal for non-player");
|
||||
if( !(sender instanceof EntityPlayerMP) )
|
||||
{
|
||||
throw new CommandException( "Cannot open terminal for non-player" );
|
||||
}
|
||||
|
||||
ServerComputer computer = ComputerSelector.getComputer(arguments.get(0));
|
||||
ServerComputer computer = ComputerSelector.getComputer( arguments.get( 0 ) );
|
||||
ComputerCraft.openComputerGUI( (EntityPlayerMP) sender, computer );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getCompletion(@Nonnull CommandContext context, @Nonnull List<String> arguments) {
|
||||
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
||||
{
|
||||
return arguments.size() == 1
|
||||
? ComputerSelector.completeComputer( arguments.get( 0 ) )
|
||||
: Collections.emptyList();
|
||||
}
|
||||
});
|
||||
} );
|
||||
|
||||
CommandRoot track = new CommandRoot( "track", "Track execution times for computers.",
|
||||
"Track how long computers execute for, as well as how many events they handle. This presents information in " +
|
||||
"a similar way to /forge track and can be useful for diagnosing lag." );
|
||||
root.register( track );
|
||||
|
||||
track.register( new SubCommandBase(
|
||||
"start", "Start tracking all computers", UserLevel.OWNER_OP,
|
||||
"Start tracking all computers' execution times and event counts. This will discard the results of previous runs."
|
||||
)
|
||||
{
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
||||
{
|
||||
getTimingContext( context ).start();
|
||||
|
||||
String stopCommand = "/" + context.parent().getFullPath() + " stop";
|
||||
context.getSender().sendMessage( list(
|
||||
text( "Run " ),
|
||||
link( text( stopCommand ), stopCommand, "Click to stop tracking" ),
|
||||
text( " to stop tracking and view the results" )
|
||||
) );
|
||||
}
|
||||
} );
|
||||
|
||||
track.register( new SubCommandBase(
|
||||
"stop", "Stop tracking all computers", UserLevel.OWNER_OP,
|
||||
"Stop tracking all computers' events and execution times"
|
||||
)
|
||||
{
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||
{
|
||||
TrackingContext timings = getTimingContext( context );
|
||||
if( !timings.stop() ) throw new CommandException( "Tracking not enabled" );
|
||||
displayTimings( context, timings.getImmutableTimings(), TrackingField.AVERAGE_TIME );
|
||||
}
|
||||
} );
|
||||
|
||||
track.register( new SubCommandBase(
|
||||
"dump", "[kind]", "Dump the latest track results", UserLevel.OWNER_OP,
|
||||
"Dump the latest results of computer tracking."
|
||||
)
|
||||
{
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments ) throws CommandException
|
||||
{
|
||||
TrackingField field = TrackingField.AVERAGE_TIME;
|
||||
if( arguments.size() >= 1 )
|
||||
{
|
||||
field = TrackingField.fields().get( arguments.get( 0 ) );
|
||||
if( field == null ) throw new CommandException( "Unknown field '" + arguments.get( 0 ) + "'" );
|
||||
}
|
||||
|
||||
displayTimings( context, getTimingContext( context ).getImmutableTimings(), field );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> getCompletion( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
||||
{
|
||||
if( arguments.size() == 1 )
|
||||
{
|
||||
String match = arguments.get( 0 );
|
||||
|
||||
List<String> out = new ArrayList<>();
|
||||
for( String key : TrackingField.fields().keySet() )
|
||||
{
|
||||
if( CommandBase.doesStringStartWith( match, key ) ) out.add( key );
|
||||
}
|
||||
|
||||
out.sort( Comparator.naturalOrder() );
|
||||
return out;
|
||||
}
|
||||
else
|
||||
{
|
||||
return super.getCompletion( context, arguments );
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
root.register( new SubCommandBase(
|
||||
"reload", "Reload the ComputerCraft config file", UserLevel.OWNER_OP,
|
||||
"Reload the ComputerCraft config file"
|
||||
)
|
||||
{
|
||||
@Override
|
||||
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments )
|
||||
{
|
||||
ComputerCraft.loadConfig();
|
||||
ComputerCraft.syncConfig();
|
||||
context.getSender().sendMessage( new TextComponentString( "Reloaded config" ) );
|
||||
}
|
||||
} );
|
||||
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private static ITextComponent linkComputer( ServerComputer computer )
|
||||
private static ITextComponent linkComputer( CommandContext context, ServerComputer serverComputer, int computerId )
|
||||
{
|
||||
return link(
|
||||
text( Integer.toString( computer.getInstanceID() ) ),
|
||||
"/computercraft dump " + computer.getInstanceID(),
|
||||
"View more info about this computer"
|
||||
);
|
||||
ITextComponent out = new TextComponentString( "" );
|
||||
|
||||
// Append the computer instance
|
||||
if( serverComputer == null )
|
||||
{
|
||||
out.appendSibling( text( "?" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
out.appendSibling( link(
|
||||
text( Integer.toString( serverComputer.getInstanceID() ) ),
|
||||
"/computercraft dump " + serverComputer.getInstanceID(),
|
||||
"View more info about this computer"
|
||||
) );
|
||||
}
|
||||
|
||||
// And ID
|
||||
out.appendText( " (id " + computerId + ")" );
|
||||
|
||||
// And, if we're a player, some useful links
|
||||
if( serverComputer != null && UserLevel.OP.canExecute( context ) && context.fromPlayer() )
|
||||
{
|
||||
out
|
||||
.appendText( " " )
|
||||
.appendSibling( link(
|
||||
text( "\u261b" ),
|
||||
"/computercraft tp " + serverComputer.getInstanceID(),
|
||||
"Teleport to this computer"
|
||||
) )
|
||||
.appendText( " " )
|
||||
.appendSibling( link(
|
||||
text( "\u20e2" ),
|
||||
"/computercraft view " + serverComputer.getInstanceID(),
|
||||
"View this computer"
|
||||
) );
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
private static ITextComponent linkPosition( CommandContext context, ServerComputer computer )
|
||||
@@ -253,4 +461,69 @@ public final class CommandComputerCraft extends CommandDelegate
|
||||
return position( computer.getPosition() );
|
||||
}
|
||||
}
|
||||
|
||||
private static TrackingContext getTimingContext( CommandContext context )
|
||||
{
|
||||
Entity entity = context.getSender().getCommandSenderEntity();
|
||||
if( entity instanceof EntityPlayerMP )
|
||||
{
|
||||
return Tracking.getContext( entity.getUniqueID() );
|
||||
}
|
||||
else
|
||||
{
|
||||
return Tracking.getContext( SYSTEM_UUID );
|
||||
}
|
||||
}
|
||||
|
||||
private static void displayTimings( CommandContext context, List<ComputerTracker> timings, TrackingField field ) throws CommandException
|
||||
{
|
||||
if( timings.isEmpty() ) throw new CommandException( "No timings available" );
|
||||
|
||||
Map<Computer, ServerComputer> lookup = new HashMap<>();
|
||||
int maxId = 0, maxInstance = 0;
|
||||
for( ServerComputer server : ComputerCraft.serverComputerRegistry.getComputers() )
|
||||
{
|
||||
lookup.put( server.getComputer(), server );
|
||||
|
||||
if( server.getInstanceID() > maxInstance ) maxInstance = server.getInstanceID();
|
||||
if( server.getID() > maxId ) maxId = server.getID();
|
||||
}
|
||||
|
||||
ICommandSender sender = context.getSender();
|
||||
|
||||
timings.sort( Comparator.<ComputerTracker, Long>comparing( x -> x.get( field ) ).reversed() );
|
||||
|
||||
boolean defaultLayout = field == TrackingField.TASKS || field == TrackingField.TOTAL_TIME
|
||||
|| field == TrackingField.AVERAGE_TIME || field == TrackingField.MAX_TIME;
|
||||
|
||||
|
||||
TextTable table = defaultLayout
|
||||
? new TextTable( TRACK_ID, "Computer", "Tasks", "Total", "Average", "Maximum" )
|
||||
: new TextTable( TRACK_ID, "Computer", field.displayName() );
|
||||
|
||||
for( ComputerTracker entry : timings )
|
||||
{
|
||||
Computer computer = entry.getComputer();
|
||||
ServerComputer serverComputer = computer == null ? null : lookup.get( computer );
|
||||
|
||||
ITextComponent computerComponent = linkComputer( context, serverComputer, entry.getComputerId() );
|
||||
|
||||
if( defaultLayout )
|
||||
{
|
||||
table.addRow(
|
||||
computerComponent,
|
||||
text( entry.getFormatted( TrackingField.TASKS ) ),
|
||||
text( entry.getFormatted( TrackingField.TOTAL_TIME ) ),
|
||||
text( entry.getFormatted( TrackingField.AVERAGE_TIME ) ),
|
||||
text( entry.getFormatted( TrackingField.MAX_TIME ) )
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
table.addRow( computerComponent, text( entry.getFormatted( field ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
table.displayTo( context.getSender() );
|
||||
}
|
||||
}
|
||||
|
@@ -3,15 +3,59 @@ package dan200.computercraft.shared.command;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.command.CommandException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public final class ComputerSelector
|
||||
{
|
||||
private static List<ServerComputer> getComputers( Predicate<ServerComputer> predicate, String selector ) throws CommandException
|
||||
{
|
||||
// We copy it to prevent concurrent modifications.
|
||||
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
List<ServerComputer> candidates = Lists.newArrayList();
|
||||
for( ServerComputer searchComputer : computers )
|
||||
{
|
||||
if( predicate.test( searchComputer ) ) candidates.add( searchComputer );
|
||||
}
|
||||
|
||||
if( candidates.isEmpty() )
|
||||
{
|
||||
throw new CommandException( "No computer matching " + selector );
|
||||
}
|
||||
else
|
||||
{
|
||||
return candidates;
|
||||
}
|
||||
}
|
||||
|
||||
public static ServerComputer getComputer( String selector ) throws CommandException
|
||||
{
|
||||
List<ServerComputer> computers = getComputers( selector );
|
||||
if( computers.size() == 1 )
|
||||
{
|
||||
return computers.get( 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
StringBuilder builder = new StringBuilder( "Multiple computers matching " )
|
||||
.append( selector ).append( " (instances " );
|
||||
|
||||
for( int i = 0; i < computers.size(); i++ )
|
||||
{
|
||||
if( i > 0 ) builder.append( ", " );
|
||||
builder.append( computers.get( i ).getInstanceID() );
|
||||
}
|
||||
builder.append( ")" );
|
||||
|
||||
throw new CommandException( builder.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
public static List<ServerComputer> getComputers( String selector ) throws CommandException
|
||||
{
|
||||
if( selector.length() > 0 && selector.charAt( 0 ) == '#' )
|
||||
{
|
||||
@@ -27,49 +71,17 @@ public final class ComputerSelector
|
||||
throw new CommandException( "'" + selector + "' is not a valid number" );
|
||||
}
|
||||
|
||||
// We copy it to prevent concurrent modifications.
|
||||
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
List<ServerComputer> candidates = Lists.newArrayList();
|
||||
for( ServerComputer searchComputer : computers )
|
||||
{
|
||||
if( searchComputer.getID() == id )
|
||||
{
|
||||
candidates.add( searchComputer );
|
||||
}
|
||||
}
|
||||
|
||||
if( candidates.size() == 0 )
|
||||
{
|
||||
throw new CommandException( "No such computer for id " + id );
|
||||
}
|
||||
else if( candidates.size() == 1 )
|
||||
{
|
||||
return candidates.get( 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
StringBuilder builder = new StringBuilder( "Multiple computers with id " )
|
||||
.append( id ).append( " (instances " );
|
||||
|
||||
boolean first = true;
|
||||
for( ServerComputer computer : candidates )
|
||||
{
|
||||
if( first )
|
||||
{
|
||||
first = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.append( ", " );
|
||||
}
|
||||
|
||||
builder.append( computer.getInstanceID() );
|
||||
}
|
||||
|
||||
builder.append( ")" );
|
||||
|
||||
throw new CommandException( builder.toString() );
|
||||
}
|
||||
return getComputers( x -> x.getID() == id, selector );
|
||||
}
|
||||
else if( selector.length() > 0 && selector.charAt( 0 ) == '@' )
|
||||
{
|
||||
String label = selector.substring( 1 );
|
||||
return getComputers( x -> Objects.equals( label, x.getLabel() ), selector );
|
||||
}
|
||||
else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
|
||||
{
|
||||
String familyName = selector.substring( 1 );
|
||||
return getComputers( x -> x.getFamily().name().equalsIgnoreCase( familyName ), selector );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -90,14 +102,14 @@ public final class ComputerSelector
|
||||
}
|
||||
else
|
||||
{
|
||||
return computer;
|
||||
return Collections.singletonList( computer );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> completeComputer( String selector )
|
||||
{
|
||||
Set<String> options = Sets.newHashSet();
|
||||
TreeSet<String> options = Sets.newTreeSet();
|
||||
|
||||
// We copy it to prevent concurrent modifications.
|
||||
List<ServerComputer> computers = Lists.newArrayList( ComputerCraft.serverComputerRegistry.getComputers() );
|
||||
@@ -112,6 +124,26 @@ public final class ComputerSelector
|
||||
if( id.startsWith( selector ) ) options.add( "#" + id );
|
||||
}
|
||||
}
|
||||
else if( selector.length() > 0 && selector.charAt( 0 ) == '@' )
|
||||
{
|
||||
String label = selector.substring( 1 );
|
||||
for( ServerComputer computer : computers )
|
||||
{
|
||||
String thisLabel = computer.getLabel();
|
||||
if( thisLabel != null && thisLabel.startsWith( label ) ) options.add( "@" + thisLabel );
|
||||
}
|
||||
}
|
||||
else if( selector.length() > 0 && selector.charAt( 0 ) == '~' )
|
||||
{
|
||||
String familyName = selector.substring( 1 ).toLowerCase( Locale.ENGLISH );
|
||||
for( ComputerFamily family : ComputerFamily.values() )
|
||||
{
|
||||
if( family.name().toLowerCase( Locale.ENGLISH ).startsWith( familyName ) )
|
||||
{
|
||||
options.add( "~" + family.name() );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for( ServerComputer computer : computers )
|
||||
@@ -121,6 +153,20 @@ public final class ComputerSelector
|
||||
}
|
||||
}
|
||||
|
||||
return Lists.newArrayList( options );
|
||||
if( options.size() > 100 )
|
||||
{
|
||||
ArrayList<String> result = Lists.newArrayListWithCapacity( 100 );
|
||||
for( String element : options )
|
||||
{
|
||||
if( result.size() > 100 ) break;
|
||||
result.add( element );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Lists.newArrayList( options );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,9 @@ package dan200.computercraft.shared.command.framework;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -90,4 +92,9 @@ public final class CommandContext
|
||||
{
|
||||
return sender;
|
||||
}
|
||||
|
||||
public boolean fromPlayer()
|
||||
{
|
||||
return sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
|
||||
}
|
||||
}
|
||||
|
@@ -36,7 +36,7 @@ public class CommandDelegate implements ICommand
|
||||
@Override
|
||||
public String getUsage( @Nonnull ICommandSender sender )
|
||||
{
|
||||
return "/" + command.getName() + " " + command.getUsage( new CommandContext( sender.getServer(), sender, command ) );
|
||||
return new CommandContext( sender.getServer(), sender, command ).getFullUsage();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
@@ -86,7 +86,7 @@ public class CommandRoot implements ISubCommand
|
||||
{
|
||||
for( ISubCommand command : subCommands.values() )
|
||||
{
|
||||
if( command.checkPermission( context ) ) return true;
|
||||
if( !(command instanceof SubCommandHelp) && command.checkPermission( context ) ) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public class CommandRoot implements ISubCommand
|
||||
ISubCommand command = subCommands.get( arguments.get( 0 ) );
|
||||
if( command == null || !command.checkPermission( context ) )
|
||||
{
|
||||
throw new CommandException( getName() + " " + getUsage( context ) );
|
||||
throw new CommandException( context.getFullUsage() );
|
||||
}
|
||||
|
||||
command.execute( context.enter( command ), arguments.subList( 1, arguments.size() ) );
|
||||
|
@@ -0,0 +1,147 @@
|
||||
package dan200.computercraft.shared.command.framework;
|
||||
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static dan200.computercraft.shared.command.framework.ChatHelpers.coloured;
|
||||
|
||||
/**
|
||||
* Adapated from Sponge's PaginationCalculator
|
||||
*/
|
||||
public class TextFormatter
|
||||
{
|
||||
private static final char PADDING_CHAR = '\u02cc';
|
||||
|
||||
/**
|
||||
* Yoinked from FontRenderer
|
||||
*
|
||||
* @see net.minecraft.client.gui.FontRenderer#charWidth
|
||||
* @see net.minecraft.client.gui.FontRenderer#getCharWidth(char)
|
||||
*/
|
||||
private static final String CHARACTERS = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000";
|
||||
private static final int[] CHAR_WIDTHS = new int[]{
|
||||
6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 4,
|
||||
4, 6, 7, 6, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6,
|
||||
7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6,
|
||||
3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6,
|
||||
6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6,
|
||||
6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2, 6, 6,
|
||||
8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6,
|
||||
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
||||
9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 9, 5, 9, 9,
|
||||
8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7,
|
||||
7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1
|
||||
};
|
||||
|
||||
private static final int[] EXTRA_CHARS = new int[]{
|
||||
'\u20e2', '\u261b',
|
||||
};
|
||||
|
||||
private static final byte[] EXTRA_WIDTHS = new byte[]{
|
||||
8, 4,
|
||||
};
|
||||
|
||||
public static int getWidth( int codePoint )
|
||||
{
|
||||
// Escape codes
|
||||
if( codePoint == 167 ) return -1;
|
||||
|
||||
// Space and non-breaking space
|
||||
if( codePoint == 32 || codePoint == 160 ) return 4;
|
||||
|
||||
// Built-in characters
|
||||
int nonUnicodeIdx = CHARACTERS.indexOf( codePoint );
|
||||
if( codePoint > 0 && nonUnicodeIdx != -1 ) return CHAR_WIDTHS[nonUnicodeIdx];
|
||||
|
||||
// Other special characters we use.
|
||||
int extraIdx = Arrays.binarySearch( EXTRA_CHARS, codePoint );
|
||||
if( extraIdx >= 0 ) return EXTRA_WIDTHS[extraIdx];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int getWidth( ITextComponent component )
|
||||
{
|
||||
int total = 0;
|
||||
if( component instanceof TextComponentString )
|
||||
{
|
||||
String contents = component.getUnformattedComponentText();
|
||||
|
||||
int bold = component.getStyle().getBold() ? 1 : 0;
|
||||
for( int i = 0; i < contents.length(); i++ )
|
||||
{
|
||||
int cp = contents.charAt( i );
|
||||
assert cp != '\n';
|
||||
int width = getWidth( cp );
|
||||
if( width < 0 )
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
total += width + bold;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for( ITextComponent child : component.getSiblings() )
|
||||
{
|
||||
total += getWidth( child );
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
public static boolean isPlayer( ICommandSender sender )
|
||||
{
|
||||
return sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
|
||||
}
|
||||
|
||||
public static int getMaxWidth( ICommandSender sender )
|
||||
{
|
||||
return isPlayer( sender ) ? 320 : 80;
|
||||
}
|
||||
|
||||
public static int getWidthFor( ITextComponent component, ICommandSender sender )
|
||||
{
|
||||
return isPlayer( sender ) ? getWidth( component ) : component.getUnformattedText().length();
|
||||
}
|
||||
|
||||
public static int getWidthFor( int codepoint, ICommandSender sender )
|
||||
{
|
||||
return isPlayer( sender ) ? getWidth( codepoint ) : 1;
|
||||
}
|
||||
|
||||
public static void appendFixedWidth( ITextComponent out, ICommandSender sender, ITextComponent entry, int maxWidth )
|
||||
{
|
||||
out.appendSibling( entry );
|
||||
|
||||
int width = getWidthFor( entry, sender );
|
||||
int delta = maxWidth - width;
|
||||
|
||||
int spaceWidth = getWidthFor( ' ', sender );
|
||||
int spaces = delta / spaceWidth;
|
||||
int extra = delta % spaces;
|
||||
|
||||
// Append a fixed number of spaces
|
||||
if( spaces > 0 ) out.appendSibling( new TextComponentString( StringUtils.repeat( ' ', spaces ) ) );
|
||||
|
||||
// Append several minor characters to pad to a full string
|
||||
if( extra > 0 )
|
||||
{
|
||||
out.appendSibling( coloured( StringUtils.repeat( PADDING_CHAR, extra ), TextFormatting.GRAY ) );
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,117 +1,53 @@
|
||||
package dan200.computercraft.shared.command.framework;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.network.ComputerCraftPacket;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static dan200.computercraft.shared.command.framework.ChatHelpers.coloured;
|
||||
import static dan200.computercraft.shared.command.framework.ChatHelpers.text;
|
||||
import static dan200.computercraft.shared.command.framework.TextFormatter.*;
|
||||
|
||||
public class TextTable
|
||||
{
|
||||
private static final String CHARACTERS = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000";
|
||||
private static final int[] CHAR_WIDTHS = new int[] {
|
||||
6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 4,
|
||||
4, 6, 7, 6, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6,
|
||||
7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6,
|
||||
3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6,
|
||||
6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6,
|
||||
6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2, 6, 6,
|
||||
8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6,
|
||||
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
|
||||
9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 9, 5, 9, 9,
|
||||
8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7,
|
||||
7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1
|
||||
};
|
||||
|
||||
private static final ITextComponent SEPARATOR = coloured( " | ", TextFormatting.GRAY );
|
||||
private static final ITextComponent SEPARATOR = coloured( "| ", TextFormatting.GRAY );
|
||||
private static final ITextComponent LINE = text( "\n" );
|
||||
|
||||
private static int getWidth( char character, ICommandSender sender )
|
||||
{
|
||||
if( sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer) )
|
||||
{
|
||||
// Use font widths here.
|
||||
if( character == 167 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if( character == 32 )
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
else if( CHARACTERS.indexOf( character ) != -1 )
|
||||
{
|
||||
return CHAR_WIDTHS[ character ];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Eh, close enough.
|
||||
return 6;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
private static int getWidth( ITextComponent text, ICommandSender sender )
|
||||
{
|
||||
int sum = 0;
|
||||
String chars = text.getUnformattedText();
|
||||
for( int i = 0; i < chars.length(); i++ )
|
||||
{
|
||||
sum += getWidth( chars.charAt( i ), sender );
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static boolean isPlayer( ICommandSender sender )
|
||||
{
|
||||
return sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
|
||||
}
|
||||
|
||||
private static int getMaxWidth( ICommandSender sender )
|
||||
{
|
||||
return isPlayer( sender ) ? 320 : 80;
|
||||
}
|
||||
|
||||
private final int id;
|
||||
private int columns = -1;
|
||||
private final ITextComponent[] header;
|
||||
private final List<ITextComponent[]> rows = Lists.newArrayList();
|
||||
|
||||
public TextTable( @Nonnull ITextComponent... header )
|
||||
public TextTable( int id, @Nonnull ITextComponent... header )
|
||||
{
|
||||
this.id = id;
|
||||
this.header = header;
|
||||
this.columns = header.length;
|
||||
}
|
||||
|
||||
public TextTable()
|
||||
public TextTable( int id )
|
||||
{
|
||||
header = null;
|
||||
this.id = id;
|
||||
this.header = null;
|
||||
}
|
||||
|
||||
public TextTable( @Nonnull String... header )
|
||||
public TextTable( int id, @Nonnull String... header )
|
||||
{
|
||||
this.header = new ITextComponent[ header.length ];
|
||||
this.id = id;
|
||||
this.header = new ITextComponent[header.length];
|
||||
for( int i = 0; i < header.length; i++ )
|
||||
{
|
||||
this.header[ i ] = ChatHelpers.header( header[ i ] );
|
||||
this.header[i] = ChatHelpers.header( header[i] );
|
||||
}
|
||||
this.columns = header.length;
|
||||
}
|
||||
@@ -134,117 +70,101 @@ public class TextTable
|
||||
{
|
||||
if( columns <= 0 ) return;
|
||||
|
||||
final int maxWidth = getMaxWidth( sender );
|
||||
|
||||
int[] minWidths = new int[ columns ];
|
||||
int[] maxWidths = new int[ columns ];
|
||||
int[] rowWidths = new int[ columns ];
|
||||
int[] maxWidths = new int[columns];
|
||||
|
||||
if( header != null )
|
||||
{
|
||||
for( int i = 0; i < columns; i++ )
|
||||
{
|
||||
maxWidths[ i ] = minWidths[ i ] = getWidth( header[ i ], sender );
|
||||
maxWidths[i] = getWidthFor( header[i], sender );
|
||||
}
|
||||
}
|
||||
|
||||
for( ITextComponent[] row : rows )
|
||||
// Limit the number of rows to fit within a single chat window on default Minecraft
|
||||
// options.
|
||||
int height = isPlayer( sender ) ? 18 : 100;
|
||||
int limit = rows.size() <= height ? rows.size() : height - 1;
|
||||
|
||||
for( int y = 0; y < limit; y++ )
|
||||
{
|
||||
ITextComponent[] row = rows.get( y );
|
||||
for( int i = 0; i < row.length; i++ )
|
||||
{
|
||||
int width = getWidth( row[ i ], sender );
|
||||
rowWidths[ i ] += width;
|
||||
if( width > maxWidths[ i ] )
|
||||
{
|
||||
maxWidths[ i ] = width;
|
||||
}
|
||||
int width = getWidthFor( row[i], sender );
|
||||
if( width > maxWidths[i] ) maxWidths[i] = width;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate the average width
|
||||
for( int i = 0; i < columns; i++ )
|
||||
{
|
||||
rowWidths[ i ] = Math.max( rowWidths[ i ], rows.size() );
|
||||
}
|
||||
// Add a small amount of extra padding. This defaults to 3 spaces for players
|
||||
// and 1 for everyone else.
|
||||
int padding = isPlayer( sender ) ? getWidth( ' ' ) * 3 : 1;
|
||||
for( int i = 0; i < maxWidths.length; i++ ) maxWidths[i] += padding;
|
||||
|
||||
int totalWidth = (columns - 1) * getWidth( SEPARATOR, sender );
|
||||
int totalWidth = (columns - 1) * getWidthFor( SEPARATOR, sender );
|
||||
for( int x : maxWidths ) totalWidth += x;
|
||||
|
||||
// TODO: Limit the widths of some entries if totalWidth > maxWidth
|
||||
|
||||
ITextComponent out = new TextComponentString( "" );
|
||||
List<ITextComponent> out = new ArrayList<>();
|
||||
|
||||
if( header != null )
|
||||
{
|
||||
for( int i = 0; i < columns; i++ )
|
||||
TextComponentString line = new TextComponentString( "" );
|
||||
for( int i = 0; i < columns - 1; i++ )
|
||||
{
|
||||
if( i != 0 ) out.appendSibling( SEPARATOR );
|
||||
appendFixed( out, sender, header[ i ], maxWidths[ i ] );
|
||||
appendFixedWidth( line, sender, header[i], maxWidths[i] );
|
||||
line.appendSibling( SEPARATOR );
|
||||
}
|
||||
out.appendSibling( LINE );
|
||||
line.appendSibling( header[columns - 1] );
|
||||
out.add( line );
|
||||
|
||||
// Round the width up rather than down
|
||||
int rowCharWidth = getWidth( '=', sender );
|
||||
int rowCharWidth = getWidthFor( '=', sender );
|
||||
int rowWidth = totalWidth / rowCharWidth + (totalWidth % rowCharWidth == 0 ? 0 : 1);
|
||||
out.appendSibling( coloured( StringUtils.repeat( '=', rowWidth ), TextFormatting.GRAY ) );
|
||||
out.appendSibling( LINE );
|
||||
out.add( coloured( StringUtils.repeat( '=', rowWidth ), TextFormatting.GRAY ) );
|
||||
}
|
||||
|
||||
for( int i = 0; i < rows.size(); i++ )
|
||||
for( int i = 0; i < limit; i++ )
|
||||
{
|
||||
TextComponentString line = new TextComponentString( "" );
|
||||
ITextComponent[] row = rows.get( i );
|
||||
if( i != 0 ) out.appendSibling( LINE );
|
||||
for( int j = 0; j < columns; j++ )
|
||||
for( int j = 0; j < columns - 1; j++ )
|
||||
{
|
||||
if( j != 0 ) out.appendSibling( SEPARATOR );
|
||||
appendFixed( out, sender, row[ j ], maxWidths[ j ] );
|
||||
appendFixedWidth( line, sender, row[j], maxWidths[j] );
|
||||
line.appendSibling( SEPARATOR );
|
||||
}
|
||||
line.appendSibling( row[columns - 1] );
|
||||
out.add( line );
|
||||
}
|
||||
|
||||
sender.sendMessage( out );
|
||||
}
|
||||
|
||||
private static void appendFixed( ITextComponent out, ICommandSender sender, ITextComponent entry, int maxWidth )
|
||||
{
|
||||
int length = getWidth( entry, sender );
|
||||
int delta = length - maxWidth;
|
||||
if( delta < 0 )
|
||||
if( rows.size() > limit )
|
||||
{
|
||||
// Convert to overflow;
|
||||
delta = -delta;
|
||||
out.add( coloured( (rows.size() - limit) + " additional rows...", TextFormatting.AQUA ) );
|
||||
}
|
||||
|
||||
// We have to remove some padding as there is a padding added between formatted and unformatted text
|
||||
if( !entry.getStyle().isEmpty() && isPlayer( sender ) ) delta -= 1;
|
||||
if( isPlayer( sender ) && id != 0 )
|
||||
{
|
||||
ComputerCraftPacket packet = new ComputerCraftPacket();
|
||||
packet.m_packetType = ComputerCraftPacket.PostChat;
|
||||
packet.m_dataInt = new int[]{ id };
|
||||
|
||||
out.appendSibling( entry );
|
||||
|
||||
int spaceWidth = getWidth( ' ', sender );
|
||||
|
||||
int spaces = delta / spaceWidth;
|
||||
int missing = delta % spaceWidth;
|
||||
spaces -= missing;
|
||||
|
||||
ITextComponent component = new TextComponentString( StringUtils.repeat( ' ', spaces < 0 ? 0 : spaces ) );
|
||||
if( missing > 0 )
|
||||
String[] lines = packet.m_dataString = new String[out.size()];
|
||||
for( int i = 0; i < out.size(); i++ )
|
||||
{
|
||||
ITextComponent bold = new TextComponentString( StringUtils.repeat( ' ', missing ) );
|
||||
bold.getStyle().setBold( true );
|
||||
component.appendSibling( bold );
|
||||
lines[i] = ITextComponent.Serializer.componentToJson( out.get( i ) );
|
||||
}
|
||||
|
||||
out.appendSibling( component );
|
||||
}
|
||||
else if( delta > 0 )
|
||||
{
|
||||
out.appendSibling( entry );
|
||||
ComputerCraft.sendToPlayer( (EntityPlayerMP) sender, packet );
|
||||
}
|
||||
else
|
||||
{
|
||||
out.appendSibling( entry );
|
||||
|
||||
// We have to add some padding as we expect a padding between formatted and unformatted text
|
||||
// and there won't be.
|
||||
if( entry.getStyle().isEmpty() && isPlayer( sender ) ) out.appendText( " " );
|
||||
ITextComponent result = new TextComponentString( "" );
|
||||
for( int i = 0; i < out.size(); i++ )
|
||||
{
|
||||
if( i > 0 ) result.appendSibling( LINE );
|
||||
result.appendSibling( out.get( i ) );
|
||||
}
|
||||
sender.sendMessage( result );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -36,7 +36,7 @@ public abstract class BlockGeneric extends Block implements
|
||||
protected BlockGeneric( Material material )
|
||||
{
|
||||
super( material );
|
||||
this.isBlockContainer = true;
|
||||
this.hasTileEntity = true;
|
||||
}
|
||||
|
||||
protected abstract IBlockState getDefaultBlockState( int damage, EnumFacing placedSide );
|
||||
|
@@ -14,29 +14,28 @@ public class ClientTerminal implements ITerminal
|
||||
private boolean m_colour;
|
||||
private Terminal m_terminal;
|
||||
private boolean m_terminalChanged;
|
||||
private boolean m_terminalChangedLastFrame;
|
||||
|
||||
public ClientTerminal( boolean colour )
|
||||
{
|
||||
m_colour = colour;
|
||||
m_terminal = null;
|
||||
m_terminalChanged = false;
|
||||
m_terminalChangedLastFrame = false;
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
m_terminalChangedLastFrame = m_terminalChanged || (m_terminal != null && m_terminal.getChanged());
|
||||
if( m_terminal != null )
|
||||
{
|
||||
m_terminalChanged |= m_terminal.getChanged();
|
||||
m_terminal.clearChanged();
|
||||
}
|
||||
m_terminalChanged = false;
|
||||
}
|
||||
|
||||
public boolean hasTerminalChanged()
|
||||
public boolean pollTerminalChanged()
|
||||
{
|
||||
return m_terminalChangedLastFrame;
|
||||
boolean changed = m_terminalChanged;
|
||||
m_terminalChanged = false;
|
||||
return changed;
|
||||
}
|
||||
|
||||
// ITerminal implementation
|
||||
|
@@ -86,7 +86,7 @@ public class ColourableRecipe extends IForgeRegistryEntry.Impl<IRecipe> implemen
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHidden()
|
||||
public boolean isDynamic()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
@@ -54,6 +54,11 @@ public class ServerTerminal implements ITerminal
|
||||
}
|
||||
}
|
||||
|
||||
protected void markTerminalChanged()
|
||||
{
|
||||
m_terminalChanged = true;
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
m_terminalChangedLastFrame = m_terminalChanged || (m_terminal != null && m_terminal.getChanged());
|
||||
|
@@ -97,4 +97,11 @@ public class ComputerPeripheral
|
||||
{
|
||||
return (other != null && other.getClass() == this.getClass());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Object getTarget()
|
||||
{
|
||||
return m_computer.getTile();
|
||||
}
|
||||
}
|
||||
|
@@ -94,6 +94,11 @@ public class ServerComputer extends ServerTerminal
|
||||
return m_computer.getAPIEnvironment();
|
||||
}
|
||||
|
||||
public Computer getComputer()
|
||||
{
|
||||
return m_computer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
{
|
||||
@@ -149,7 +154,7 @@ public class ServerComputer extends ServerTerminal
|
||||
return packet;
|
||||
}
|
||||
|
||||
private ComputerCraftPacket createTerminalPacket() {
|
||||
protected ComputerCraftPacket createTerminalPacket() {
|
||||
ComputerCraftPacket packet = new ComputerCraftPacket();
|
||||
packet.m_packetType = ComputerCraftPacket.ComputerTerminalChanged;
|
||||
packet.m_dataInt = new int[] { getInstanceID() };
|
||||
@@ -460,7 +465,7 @@ public class ServerComputer extends ServerTerminal
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInteracting( EntityPlayer player )
|
||||
protected boolean isInteracting( EntityPlayer player )
|
||||
{
|
||||
if( player == null ) return false;
|
||||
|
||||
|
@@ -16,4 +16,5 @@ public interface IComputerItem
|
||||
int getComputerID( @Nonnull ItemStack stack );
|
||||
String getLabel( @Nonnull ItemStack stack );
|
||||
ComputerFamily getFamily( @Nonnull ItemStack stack );
|
||||
ItemStack withFamily(@Nonnull ItemStack stack, @Nonnull ComputerFamily family);
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ import javax.annotation.Nullable;
|
||||
public class ItemComputer extends ItemComputerBase
|
||||
{
|
||||
public static int HIGHEST_DAMAGE_VALUE_ID = 16382;
|
||||
|
||||
|
||||
public ItemComputer( Block block )
|
||||
{
|
||||
super( block );
|
||||
@@ -87,7 +87,7 @@ public class ItemComputer extends ItemComputerBase
|
||||
TileEntity tile = world.getTileEntity( pos );
|
||||
if( tile != null && tile instanceof IComputerTile )
|
||||
{
|
||||
IComputerTile computer = (IComputerTile)tile;
|
||||
IComputerTile computer = (IComputerTile) tile;
|
||||
setupComputerAfterPlacement( stack, computer );
|
||||
}
|
||||
return true;
|
||||
@@ -146,10 +146,16 @@ public class ItemComputer extends ItemComputerBase
|
||||
else
|
||||
{
|
||||
int damage = stack.getItemDamage() & 0x3fff;
|
||||
return ( damage - 1 );
|
||||
return (damage - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack withFamily( @Nonnull ItemStack stack, @Nonnull ComputerFamily family )
|
||||
{
|
||||
return ComputerItemFactory.create( getComputerID( stack ), getLabel( stack ), family );
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComputerFamily getFamily( int damage )
|
||||
{
|
||||
|
@@ -0,0 +1,66 @@
|
||||
package dan200.computercraft.shared.computer.recipe;
|
||||
|
||||
import dan200.computercraft.shared.computer.items.IComputerItem;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.item.crafting.ShapedRecipes;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Represents a recipe which converts a computer from one form into another.
|
||||
*/
|
||||
public abstract class ComputerConvertRecipe extends ShapedRecipes
|
||||
{
|
||||
public ComputerConvertRecipe( String group, @Nonnull CraftingHelper.ShapedPrimer primer, @Nonnull ItemStack result )
|
||||
{
|
||||
super( group, primer.width, primer.height, primer.input, result );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
protected abstract ItemStack convert( @Nonnull ItemStack stack );
|
||||
|
||||
@Override
|
||||
public boolean matches( @Nonnull InventoryCrafting inventory, @Nonnull World world )
|
||||
{
|
||||
// See if we match the recipe, and extract the input computercraft ID
|
||||
ItemStack computerStack = null;
|
||||
for( int y = 0; y < 3; ++y )
|
||||
{
|
||||
for( int x = 0; x < 3; ++x )
|
||||
{
|
||||
ItemStack stack = inventory.getStackInRowAndColumn( x, y );
|
||||
Ingredient target = getIngredients().get( x + y * 3 );
|
||||
|
||||
// First verify we match the ingredient
|
||||
if( !target.apply( stack ) ) return false;
|
||||
|
||||
// We want to ensure we have a computer item somewhere in the recipe
|
||||
if( stack.getItem() instanceof IComputerItem ) computerStack = stack;
|
||||
}
|
||||
}
|
||||
|
||||
return computerStack != null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ItemStack getCraftingResult( @Nonnull InventoryCrafting inventory )
|
||||
{
|
||||
for( int y = 0; y < 3; ++y )
|
||||
{
|
||||
for( int x = 0; x < 3; ++x )
|
||||
{
|
||||
ItemStack item = inventory.getStackInRowAndColumn( x, y );
|
||||
|
||||
// If we're a computer, convert!
|
||||
if( item.getItem() instanceof IComputerItem ) return convert( item );
|
||||
}
|
||||
}
|
||||
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package dan200.computercraft.shared.computer.recipe;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.items.IComputerItem;
|
||||
import dan200.computercraft.shared.util.RecipeUtil;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.util.JsonUtils;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.IRecipeFactory;
|
||||
import net.minecraftforge.common.crafting.JsonContext;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ComputerFamilyRecipe extends ComputerConvertRecipe
|
||||
{
|
||||
private final ComputerFamily family;
|
||||
|
||||
public ComputerFamilyRecipe( String group, @Nonnull CraftingHelper.ShapedPrimer primer, @Nonnull ItemStack result, ComputerFamily family )
|
||||
{
|
||||
super( group, primer, result );
|
||||
this.family = family;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
protected ItemStack convert( @Nonnull ItemStack stack )
|
||||
{
|
||||
return ((IComputerItem) stack.getItem()).withFamily( stack, family );
|
||||
}
|
||||
|
||||
public static class Factory implements IRecipeFactory
|
||||
{
|
||||
@Override
|
||||
public IRecipe parse( JsonContext context, JsonObject json )
|
||||
{
|
||||
String group = JsonUtils.getString( json, "group", "" );
|
||||
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
|
||||
|
||||
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
|
||||
ItemStack result = deserializeItem( JsonUtils.getJsonObject( json, "result" ), false );
|
||||
|
||||
return new ComputerFamilyRecipe( group, primer, result, family );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package dan200.computercraft.shared.computer.recipe;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.items.IComputerItem;
|
||||
import dan200.computercraft.shared.util.RecipeUtil;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.util.JsonUtils;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.IIngredientFactory;
|
||||
import net.minecraftforge.common.crafting.JsonContext;
|
||||
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Represents an ingredient which requires an item to have a specific
|
||||
* computer family. This allows us to have operations which only work
|
||||
* on normal or
|
||||
*/
|
||||
public class ComputerIngredient extends Ingredient
|
||||
{
|
||||
private final IComputerItem item;
|
||||
private final ComputerFamily family;
|
||||
|
||||
public <T extends Item & IComputerItem> ComputerIngredient( T item, int data, ComputerFamily family )
|
||||
{
|
||||
super( new ItemStack( item, 1, data ) );
|
||||
|
||||
this.item = item;
|
||||
this.family = family;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply( @Nullable ItemStack stack )
|
||||
{
|
||||
return stack != null && stack.getItem() == item && item.getFamily( stack ) == family;
|
||||
}
|
||||
|
||||
public static class Factory implements IIngredientFactory
|
||||
{
|
||||
@Nonnull
|
||||
@Override
|
||||
public Ingredient parse( JsonContext context, JsonObject json )
|
||||
{
|
||||
String itemName = context.appendModId( JsonUtils.getString( json, "item" ) );
|
||||
int data = JsonUtils.getInt( json, "data", 0 );
|
||||
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
|
||||
|
||||
Item item = ForgeRegistries.ITEMS.getValue( new ResourceLocation( itemName ) );
|
||||
|
||||
if( item == null ) throw new JsonSyntaxException( "Unknown item '" + itemName + "'" );
|
||||
if( !(item instanceof IComputerItem) )
|
||||
{
|
||||
throw new JsonSyntaxException( "Item '" + itemName + "' is not a computer item" );
|
||||
}
|
||||
|
||||
|
||||
return new ComputerIngredient( (Item & IComputerItem) item, data, family );
|
||||
}
|
||||
}
|
||||
}
|
@@ -54,15 +54,7 @@ public class ItemDiskLegacy extends Item
|
||||
@Nonnull
|
||||
public static ItemStack createFromIDAndColour( int id, String label, int colour )
|
||||
{
|
||||
if( colour != Colour.Blue.getHex() )
|
||||
{
|
||||
return ItemDiskExpanded.createFromIDAndColour( id, label, colour );
|
||||
}
|
||||
|
||||
ItemStack stack = new ItemStack( ComputerCraft.Items.disk, 1 );
|
||||
ComputerCraft.Items.disk.setDiskID( stack, id );
|
||||
ComputerCraft.Items.disk.setLabel( stack, label );
|
||||
return stack;
|
||||
return ItemDiskExpanded.createFromIDAndColour( id, label, colour );
|
||||
}
|
||||
|
||||
public int getDiskID( @Nonnull ItemStack stack )
|
||||
|
@@ -10,19 +10,23 @@ import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
||||
import dan200.computercraft.shared.util.Colour;
|
||||
import dan200.computercraft.shared.util.ColourTracker;
|
||||
import dan200.computercraft.shared.util.ColourUtils;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.ForgeHooks;
|
||||
import net.minecraftforge.oredict.OreIngredient;
|
||||
import net.minecraftforge.registries.IForgeRegistryEntry;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class DiskRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe
|
||||
{
|
||||
private final Ingredient paper = new OreIngredient( "paper" );
|
||||
private final Ingredient redstone = new OreIngredient( "dustRedstone" );
|
||||
|
||||
@Override
|
||||
public boolean matches( @Nonnull InventoryCrafting inv, @Nonnull World world )
|
||||
{
|
||||
@@ -35,12 +39,12 @@ public class DiskRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRe
|
||||
|
||||
if( !stack.isEmpty() )
|
||||
{
|
||||
if( stack.getItem() == Items.PAPER )
|
||||
if( paper.apply( stack ) )
|
||||
{
|
||||
if( paperFound ) return false;
|
||||
paperFound = true;
|
||||
}
|
||||
else if( stack.getItem() == Items.REDSTONE )
|
||||
else if( redstone.apply( stack ) )
|
||||
{
|
||||
if( redstoneFound ) return false;
|
||||
redstoneFound = true;
|
||||
@@ -66,8 +70,8 @@ public class DiskRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRe
|
||||
ItemStack stack = inv.getStackInSlot( i );
|
||||
|
||||
if( stack.isEmpty() ) continue;
|
||||
|
||||
if( stack.getItem() != Items.PAPER && stack.getItem() != Items.REDSTONE )
|
||||
|
||||
if( !paper.apply( stack ) && !redstone.apply( stack ) )
|
||||
{
|
||||
int index = ColourUtils.getStackColour( stack );
|
||||
if( index < 0 ) continue;
|
||||
@@ -87,7 +91,7 @@ public class DiskRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRe
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHidden()
|
||||
public boolean isDynamic()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
@@ -7,23 +7,24 @@
|
||||
package dan200.computercraft.shared.media.recipes;
|
||||
|
||||
import dan200.computercraft.shared.media.items.ItemPrintout;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.ForgeHooks;
|
||||
import net.minecraftforge.oredict.OreIngredient;
|
||||
import net.minecraftforge.registries.IForgeRegistryEntry;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class PrintoutRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe
|
||||
{
|
||||
public PrintoutRecipe( )
|
||||
{
|
||||
}
|
||||
private final Ingredient paper = new OreIngredient( "paper" );
|
||||
private final Ingredient leather = new OreIngredient( "leather" );
|
||||
private final Ingredient string = new OreIngredient( "string" );
|
||||
|
||||
@Override
|
||||
public boolean canFit( int x, int y )
|
||||
@@ -32,7 +33,7 @@ public class PrintoutRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHidden()
|
||||
public boolean isDynamic()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -68,8 +69,7 @@ public class PrintoutRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements
|
||||
ItemStack stack = inventory.getStackInRowAndColumn(x, y);
|
||||
if( !stack.isEmpty() )
|
||||
{
|
||||
Item item = stack.getItem();
|
||||
if( item instanceof ItemPrintout && ItemPrintout.getType( stack ) != ItemPrintout.Type.Book )
|
||||
if( stack.getItem() instanceof ItemPrintout && ItemPrintout.getType( stack ) != ItemPrintout.Type.Book )
|
||||
{
|
||||
if( printouts == null )
|
||||
{
|
||||
@@ -80,7 +80,7 @@ public class PrintoutRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements
|
||||
numPrintouts++;
|
||||
printoutFound = true;
|
||||
}
|
||||
else if( item == Items.PAPER )
|
||||
else if( paper.apply( stack ) )
|
||||
{
|
||||
if( printouts == null )
|
||||
{
|
||||
@@ -90,11 +90,11 @@ public class PrintoutRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements
|
||||
numPages++;
|
||||
numPrintouts++;
|
||||
}
|
||||
else if( item == Items.STRING && !stringFound )
|
||||
else if( string.apply( stack ) && !stringFound )
|
||||
{
|
||||
stringFound = true;
|
||||
}
|
||||
else if( item == Items.LEATHER && !leatherFound )
|
||||
else if( leather.apply( stack ) && !leatherFound )
|
||||
{
|
||||
leatherFound = true;
|
||||
}
|
||||
|
@@ -33,6 +33,7 @@ public class ComputerCraftPacket
|
||||
public static final byte ComputerTerminalChanged = 8;
|
||||
public static final byte ComputerDeleted = 9;
|
||||
public static final byte PlayRecord = 10;
|
||||
public static final byte PostChat = 11;
|
||||
|
||||
// Packet class
|
||||
public byte m_packetType;
|
||||
|
@@ -21,7 +21,8 @@ public enum PeripheralType implements IStringSerializable
|
||||
Cable( "cable" ),
|
||||
WiredModemWithCable( "wired_modem_with_cable" ),
|
||||
AdvancedModem( "advanced_modem" ),
|
||||
Speaker( "speaker" );
|
||||
Speaker( "speaker" ),
|
||||
WiredModemFull( "wired_modem_full" );
|
||||
|
||||
private String m_name;
|
||||
|
||||
|
@@ -11,7 +11,6 @@ import dan200.computercraft.shared.common.TileGeneric;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.modem.TileCable;
|
||||
import dan200.computercraft.shared.util.WorldUtil;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.state.BlockFaceShape;
|
||||
@@ -51,23 +50,6 @@ public class BlockCable extends BlockPeripheralBase
|
||||
public static final PropertyBool DOWN = PropertyBool.create( "down" );
|
||||
}
|
||||
|
||||
public static boolean isCable( IBlockAccess world, BlockPos pos )
|
||||
{
|
||||
Block block = world.getBlockState( pos ).getBlock();
|
||||
if( block == ComputerCraft.Blocks.cable )
|
||||
{
|
||||
switch( ComputerCraft.Blocks.cable.getPeripheralType( world, pos ) )
|
||||
{
|
||||
case Cable:
|
||||
case WiredModemWithCable:
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Members
|
||||
|
||||
public BlockCable()
|
||||
@@ -175,20 +157,17 @@ public class BlockCable extends BlockPeripheralBase
|
||||
}
|
||||
}
|
||||
|
||||
private boolean doesConnect( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing dir )
|
||||
public static boolean canConnectIn( IBlockState state, EnumFacing direction )
|
||||
{
|
||||
if( state.getValue( Properties.CABLE ) == BlockCableCableVariant.NONE )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if( state.getValue( Properties.MODEM ).getFacing() == dir )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return isCable( world, pos.offset( dir ) );
|
||||
}
|
||||
return state.getValue( BlockCable.Properties.CABLE ) != BlockCableCableVariant.NONE
|
||||
&& state.getValue( BlockCable.Properties.MODEM ).getFacing() != direction;
|
||||
}
|
||||
|
||||
public static boolean doesConnectVisually( IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction )
|
||||
{
|
||||
if( state.getValue( Properties.CABLE ) == BlockCableCableVariant.NONE ) return false;
|
||||
if( state.getValue( Properties.MODEM ).getFacing() == direction ) return true;
|
||||
return ComputerCraft.getWiredElementAt( world, pos.offset( direction ), direction.getOpposite() ) != null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -196,12 +175,12 @@ public class BlockCable extends BlockPeripheralBase
|
||||
@Deprecated
|
||||
public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos )
|
||||
{
|
||||
state = state.withProperty( Properties.NORTH, doesConnect( state, world, pos, EnumFacing.NORTH ) );
|
||||
state = state.withProperty( Properties.SOUTH, doesConnect( state, world, pos, EnumFacing.SOUTH ) );
|
||||
state = state.withProperty( Properties.EAST, doesConnect( state, world, pos, EnumFacing.EAST ) );
|
||||
state = state.withProperty( Properties.WEST, doesConnect( state, world, pos, EnumFacing.WEST ) );
|
||||
state = state.withProperty( Properties.UP, doesConnect( state, world, pos, EnumFacing.UP ) );
|
||||
state = state.withProperty( Properties.DOWN, doesConnect( state, world, pos, EnumFacing.DOWN ) );
|
||||
state = state.withProperty( Properties.NORTH, doesConnectVisually( state, world, pos, EnumFacing.NORTH ) );
|
||||
state = state.withProperty( Properties.SOUTH, doesConnectVisually( state, world, pos, EnumFacing.SOUTH ) );
|
||||
state = state.withProperty( Properties.EAST, doesConnectVisually( state, world, pos, EnumFacing.EAST ) );
|
||||
state = state.withProperty( Properties.WEST, doesConnectVisually( state, world, pos, EnumFacing.WEST ) );
|
||||
state = state.withProperty( Properties.UP, doesConnectVisually( state, world, pos, EnumFacing.UP ) );
|
||||
state = state.withProperty( Properties.DOWN, doesConnectVisually( state, world, pos, EnumFacing.DOWN ) );
|
||||
|
||||
if( state.getValue( Properties.CABLE ) != BlockCableCableVariant.NONE )
|
||||
{
|
||||
@@ -345,7 +324,6 @@ public class BlockCable extends BlockPeripheralBase
|
||||
if( WorldUtil.isVecInsideInclusive( bb, hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
|
||||
{
|
||||
world.setBlockState( pos, state.withProperty( Properties.MODEM, BlockCableModemVariant.None ), 3 );
|
||||
cable.modemChanged();
|
||||
item = PeripheralItemFactory.create( PeripheralType.WiredModem, null, 1 );
|
||||
}
|
||||
else
|
||||
@@ -354,7 +332,8 @@ public class BlockCable extends BlockPeripheralBase
|
||||
item = PeripheralItemFactory.create( PeripheralType.Cable, null, 1 );
|
||||
}
|
||||
|
||||
cable.networkChanged();
|
||||
cable.modemChanged();
|
||||
cable.connectionsChanged();
|
||||
if( !world.isRemote && !player.capabilities.isCreativeMode ) dropItem( world, pos, item );
|
||||
|
||||
return false;
|
||||
@@ -365,6 +344,7 @@ public class BlockCable extends BlockPeripheralBase
|
||||
return super.removedByPlayer( state, world, pos, player, willHarvest );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ItemStack getPickBlock( @Nonnull IBlockState state, RayTraceResult hit, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player )
|
||||
{
|
||||
@@ -373,7 +353,7 @@ public class BlockCable extends BlockPeripheralBase
|
||||
{
|
||||
TileCable cable = (TileCable) tile;
|
||||
PeripheralType type = getPeripheralType( state );
|
||||
|
||||
|
||||
if( type == PeripheralType.WiredModemWithCable )
|
||||
{
|
||||
if( hit == null || WorldUtil.isVecInsideInclusive( cable.getModemBounds(), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
|
||||
@@ -403,7 +383,7 @@ public class BlockCable extends BlockPeripheralBase
|
||||
TileCable cable = (TileCable) tile;
|
||||
if( cable.getPeripheralType() != PeripheralType.WiredModem )
|
||||
{
|
||||
cable.networkChanged();
|
||||
cable.connectionsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -631,6 +631,13 @@ public class BlockPeripheral extends BlockPeripheralBase
|
||||
return isOpaqueCube( state );
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean isFullBlock( IBlockState state )
|
||||
{
|
||||
return isOpaqueCube( state );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
@@ -638,4 +645,20 @@ public class BlockPeripheral extends BlockPeripheralBase
|
||||
{
|
||||
return isOpaqueCube( state ) ? BlockFaceShape.SOLID : BlockFaceShape.UNDEFINED;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean causesSuffocation(IBlockState state)
|
||||
{
|
||||
// This normally uses the default state
|
||||
return blockMaterial.blocksMovement() && state.isOpaqueCube();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public int getLightOpacity( IBlockState state )
|
||||
{
|
||||
// This normally uses the default state
|
||||
return isOpaqueCube( state ) ? 255 : 0;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.peripheral.common;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.modem.TileWiredModemFull;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class BlockWiredModemFull extends BlockPeripheralBase
|
||||
{
|
||||
// Statics
|
||||
|
||||
public static class Properties
|
||||
{
|
||||
public static final PropertyBool MODEM_ON = PropertyBool.create( "modem" );
|
||||
public static final PropertyBool PERIPHERAL_ON = PropertyBool.create( "peripheral" );
|
||||
}
|
||||
|
||||
// Members
|
||||
|
||||
public BlockWiredModemFull()
|
||||
{
|
||||
setHardness( 1.5f );
|
||||
setUnlocalizedName( "computercraft:wired_modem_full" );
|
||||
setCreativeTab( ComputerCraft.mainCreativeTab );
|
||||
setDefaultState( blockState.getBaseState()
|
||||
.withProperty( Properties.MODEM_ON, false )
|
||||
.withProperty( Properties.PERIPHERAL_ON, false )
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IBlockState getDefaultBlockState( PeripheralType type, EnumFacing placedSide )
|
||||
{
|
||||
return getDefaultState();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
protected BlockStateContainer createBlockState()
|
||||
{
|
||||
return new BlockStateContainer( this,
|
||||
Properties.MODEM_ON,
|
||||
Properties.PERIPHERAL_ON
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetaFromState( IBlockState state )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
@Deprecated
|
||||
public IBlockState getActualState( @Nonnull IBlockState state, IBlockAccess world, BlockPos pos )
|
||||
{
|
||||
TileEntity te = world.getTileEntity( pos );
|
||||
if( te instanceof TileWiredModemFull )
|
||||
{
|
||||
TileWiredModemFull modem = (TileWiredModemFull) te;
|
||||
int anim = modem.getAnim();
|
||||
state = state
|
||||
.withProperty( Properties.MODEM_ON, (anim & 1) != 0 )
|
||||
.withProperty( Properties.PERIPHERAL_ON, (anim & 2) != 0 );
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PeripheralType getPeripheralType( int damage )
|
||||
{
|
||||
return PeripheralType.WiredModemFull;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PeripheralType getPeripheralType( IBlockState state )
|
||||
{
|
||||
return PeripheralType.WiredModemFull;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TilePeripheralBase createTile( PeripheralType type )
|
||||
{
|
||||
return new TileWiredModemFull();
|
||||
}
|
||||
}
|
@@ -98,8 +98,8 @@ public class ItemCable extends ItemPeripheralBase
|
||||
TileEntity tile = world.getTileEntity( pos );
|
||||
if( tile != null && tile instanceof TileCable )
|
||||
{
|
||||
TileCable cable = (TileCable)tile;
|
||||
cable.networkChanged();
|
||||
TileCable cable = (TileCable) tile;
|
||||
cable.connectionsChanged();
|
||||
}
|
||||
return EnumActionResult.SUCCESS;
|
||||
}
|
||||
@@ -131,7 +131,8 @@ public class ItemCable extends ItemPeripheralBase
|
||||
if( tile != null && tile instanceof TileCable )
|
||||
{
|
||||
TileCable cable = (TileCable)tile;
|
||||
cable.networkChanged();
|
||||
cable.modemChanged();
|
||||
cable.connectionsChanged();
|
||||
}
|
||||
return EnumActionResult.SUCCESS;
|
||||
}
|
||||
@@ -153,7 +154,8 @@ public class ItemCable extends ItemPeripheralBase
|
||||
if( tile != null && tile instanceof TileCable )
|
||||
{
|
||||
TileCable cable = (TileCable)tile;
|
||||
cable.networkChanged();
|
||||
cable.modemChanged();
|
||||
cable.connectionsChanged();
|
||||
}
|
||||
return EnumActionResult.SUCCESS;
|
||||
}
|
||||
|
@@ -11,8 +11,8 @@ import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -102,6 +102,8 @@ public abstract class ItemPeripheralBase extends ItemBlock implements IPeriphera
|
||||
{
|
||||
return "tile.computercraft:speaker";
|
||||
}
|
||||
case WiredModemFull:
|
||||
return "tile.computercraft:wired_modem";
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package dan200.computercraft.shared.peripheral.common;
|
||||
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import net.minecraft.block.Block;
|
||||
|
||||
public class ItemWiredModemFull extends ItemPeripheralBase
|
||||
{
|
||||
public ItemWiredModemFull( Block block )
|
||||
{
|
||||
super( block );
|
||||
}
|
||||
|
||||
@Override
|
||||
public PeripheralType getPeripheralType( int damage )
|
||||
{
|
||||
return PeripheralType.WiredModemFull;
|
||||
}
|
||||
}
|
@@ -47,6 +47,8 @@ public class PeripheralItemFactory
|
||||
{
|
||||
return advancedModem.create( type, label, quantity );
|
||||
}
|
||||
case WiredModemFull:
|
||||
return new ItemStack( ComputerCraft.Blocks.wiredModemFull, quantity );
|
||||
}
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ public abstract class TilePeripheralBase extends TileGeneric
|
||||
{
|
||||
// Statics
|
||||
|
||||
private EnumFacing m_dir;
|
||||
protected EnumFacing m_dir;
|
||||
private int m_anim;
|
||||
private boolean m_changed;
|
||||
|
||||
@@ -97,6 +97,11 @@ public abstract class TilePeripheralBase extends TileGeneric
|
||||
return m_dir;
|
||||
}
|
||||
|
||||
public EnumFacing getCachedDirection()
|
||||
{
|
||||
return m_dir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDirection( EnumFacing dir )
|
||||
{
|
||||
|
@@ -62,9 +62,9 @@ public class DiskDrivePeripheral implements IPeripheral
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
// isPresent
|
||||
// isDiskPresent
|
||||
return new Object[] {
|
||||
m_diskDrive.getDiskStack() != null
|
||||
!m_diskDrive.getDiskStack().isEmpty()
|
||||
};
|
||||
}
|
||||
case 1:
|
||||
@@ -196,4 +196,11 @@ public class DiskDrivePeripheral implements IPeripheral
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Object getTarget()
|
||||
{
|
||||
return m_diskDrive;
|
||||
}
|
||||
}
|
||||
|
@@ -422,6 +422,7 @@ public class TileDiskDrive extends TilePeripheralBase
|
||||
return new DiskDrivePeripheral( this );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public ItemStack getDiskStack()
|
||||
{
|
||||
synchronized( this )
|
||||
|
@@ -8,8 +8,8 @@ package dan200.computercraft.shared.peripheral.modem;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class TileAdvancedModem extends TileModemBase
|
||||
@Override
|
||||
public Vec3d getPosition()
|
||||
{
|
||||
BlockPos pos = m_entity.getPos().offset( m_entity.getDirection() );
|
||||
BlockPos pos = m_entity.getPos().offset( m_entity.getCachedDirection() );
|
||||
return new Vec3d( pos.getX(), pos.getY(), pos.getZ() );
|
||||
}
|
||||
|
||||
@@ -57,9 +57,40 @@ public class TileAdvancedModem extends TileModemBase
|
||||
}
|
||||
|
||||
// Members
|
||||
private boolean m_hasDirection = false;
|
||||
|
||||
public TileAdvancedModem()
|
||||
{
|
||||
m_dir = EnumFacing.DOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad()
|
||||
{
|
||||
super.onLoad();
|
||||
updateDirection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateContainingBlockInfo()
|
||||
{
|
||||
m_hasDirection = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
{
|
||||
super.update();
|
||||
updateDirection();
|
||||
}
|
||||
|
||||
private void updateDirection()
|
||||
{
|
||||
if( !m_hasDirection )
|
||||
{
|
||||
m_hasDirection = true;
|
||||
m_dir = getDirection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,425 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.peripheral.modem;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockCable;
|
||||
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
|
||||
import dan200.computercraft.shared.wired.CapabilityWiredElement;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.AxisAlignedBB;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.util.text.TextComponentTranslation;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
public class TileWiredModemFull extends TilePeripheralBase
|
||||
{
|
||||
private static class FullElement extends WiredModemElement
|
||||
{
|
||||
private final TileWiredModemFull m_entity;
|
||||
|
||||
private FullElement( TileWiredModemFull m_entity )
|
||||
{
|
||||
this.m_entity = m_entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void attachPeripheral( String name, IPeripheral peripheral )
|
||||
{
|
||||
for( int i = 0; i < 6; i++ )
|
||||
{
|
||||
WiredModemPeripheral modem = m_entity.m_modems[i];
|
||||
if( modem != null ) modem.attachPeripheral( name, peripheral );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void detachPeripheral( String name )
|
||||
{
|
||||
for( int i = 0; i < 6; i++ )
|
||||
{
|
||||
WiredModemPeripheral modem = m_entity.m_modems[i];
|
||||
if( modem != null ) modem.detachPeripheral( name );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public World getWorld()
|
||||
{
|
||||
return m_entity.getWorld();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Vec3d getPosition()
|
||||
{
|
||||
BlockPos pos = m_entity.getPos();
|
||||
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
|
||||
}
|
||||
}
|
||||
|
||||
private WiredModemPeripheral[] m_modems = new WiredModemPeripheral[6];
|
||||
|
||||
private boolean m_peripheralAccessAllowed = false;
|
||||
private WiredModemLocalPeripheral[] m_peripherals = new WiredModemLocalPeripheral[6];
|
||||
|
||||
private boolean m_destroyed = false;
|
||||
private boolean m_connectionsFormed = false;
|
||||
|
||||
private final WiredModemElement m_element = new FullElement( this );
|
||||
private final IWiredNode m_node = m_element.getNode();
|
||||
|
||||
public TileWiredModemFull()
|
||||
{
|
||||
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i] = new WiredModemLocalPeripheral();
|
||||
}
|
||||
|
||||
private void remove()
|
||||
{
|
||||
if( world == null || !world.isRemote )
|
||||
{
|
||||
m_node.remove();
|
||||
m_connectionsFormed = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
if( !m_destroyed )
|
||||
{
|
||||
m_destroyed = true;
|
||||
remove();
|
||||
}
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnload()
|
||||
{
|
||||
super.onChunkUnload();
|
||||
remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate()
|
||||
{
|
||||
super.invalidate();
|
||||
remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumFacing getDirection()
|
||||
{
|
||||
return EnumFacing.NORTH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDirection( EnumFacing dir )
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNeighbourChange()
|
||||
{
|
||||
if( !world.isRemote && m_peripheralAccessAllowed )
|
||||
{
|
||||
boolean hasChanged = false;
|
||||
for( EnumFacing facing : EnumFacing.VALUES )
|
||||
{
|
||||
hasChanged |= m_peripherals[facing.ordinal()].attach( world, getPos(), facing );
|
||||
}
|
||||
|
||||
if( hasChanged ) updateConnectedPeripherals();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNeighbourTileEntityChange( @Nonnull BlockPos neighbour )
|
||||
{
|
||||
if( !world.isRemote && m_peripheralAccessAllowed )
|
||||
{
|
||||
for( EnumFacing facing : EnumFacing.VALUES )
|
||||
{
|
||||
if( getPos().offset( facing ).equals( neighbour ) )
|
||||
{
|
||||
WiredModemLocalPeripheral peripheral = m_peripherals[facing.ordinal()];
|
||||
if( peripheral.attach( world, getPos(), facing ) ) updateConnectedPeripherals();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public AxisAlignedBB getBounds()
|
||||
{
|
||||
return BlockCable.FULL_BLOCK_AABB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActivate( EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ )
|
||||
{
|
||||
if( !getWorld().isRemote )
|
||||
{
|
||||
// On server, we interacted if a peripheral was found
|
||||
Set<String> oldPeriphName = getConnectedPeripheralNames();
|
||||
togglePeripheralAccess();
|
||||
Set<String> periphName = getConnectedPeripheralNames();
|
||||
|
||||
if( !Objects.equal( periphName, oldPeriphName ) )
|
||||
{
|
||||
if( !oldPeriphName.isEmpty() )
|
||||
{
|
||||
List<String> names = new ArrayList<>( oldPeriphName );
|
||||
names.sort( Comparator.naturalOrder() );
|
||||
|
||||
player.sendMessage(
|
||||
new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_disconnected", String.join( ", ", names ) )
|
||||
);
|
||||
}
|
||||
if( !periphName.isEmpty() )
|
||||
{
|
||||
List<String> names = new ArrayList<>( periphName );
|
||||
names.sort( Comparator.naturalOrder() );
|
||||
player.sendMessage(
|
||||
new TextComponentTranslation( "gui.computercraft:wired_modem.peripheral_connected", String.join( ", ", names ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// On client, we can't know this, so we assume so to be safe
|
||||
// The server will correct us if we're wrong
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT( NBTTagCompound tag )
|
||||
{
|
||||
super.readFromNBT( tag );
|
||||
m_peripheralAccessAllowed = tag.getBoolean( "peripheralAccess" );
|
||||
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].readNBT( tag, "_" + i );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public NBTTagCompound writeToNBT( NBTTagCompound tag )
|
||||
{
|
||||
tag = super.writeToNBT( tag );
|
||||
tag.setBoolean( "peripheralAccess", m_peripheralAccessAllowed );
|
||||
for( int i = 0; i < m_peripherals.length; i++ ) m_peripherals[i].writeNBT( tag, "_" + i );
|
||||
return tag;
|
||||
}
|
||||
|
||||
protected void updateAnim()
|
||||
{
|
||||
int anim = 0;
|
||||
for( WiredModemPeripheral modem : m_modems )
|
||||
{
|
||||
if( modem != null && modem.isActive() )
|
||||
{
|
||||
anim += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( m_peripheralAccessAllowed )
|
||||
{
|
||||
anim += 2;
|
||||
}
|
||||
setAnim( anim );
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void readDescription( @Nonnull NBTTagCompound tag )
|
||||
{
|
||||
super.readDescription( tag );
|
||||
updateBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
{
|
||||
if( !getWorld().isRemote )
|
||||
{
|
||||
boolean changed = false;
|
||||
for( WiredModemPeripheral peripheral : m_modems )
|
||||
{
|
||||
if( peripheral != null && peripheral.pollChanged() ) changed = true;
|
||||
}
|
||||
if( changed ) updateAnim();
|
||||
|
||||
if( !m_connectionsFormed )
|
||||
{
|
||||
m_connectionsFormed = true;
|
||||
|
||||
connectionsChanged();
|
||||
if( m_peripheralAccessAllowed )
|
||||
{
|
||||
for( EnumFacing facing : EnumFacing.VALUES )
|
||||
{
|
||||
m_peripherals[facing.ordinal()].attach( world, getPos(), facing );
|
||||
}
|
||||
updateConnectedPeripherals();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.update();
|
||||
}
|
||||
|
||||
private void connectionsChanged()
|
||||
{
|
||||
if( getWorld().isRemote ) return;
|
||||
|
||||
World world = getWorld();
|
||||
BlockPos current = getPos();
|
||||
for( EnumFacing facing : EnumFacing.VALUES )
|
||||
{
|
||||
BlockPos offset = current.offset( facing );
|
||||
if( !world.isBlockLoaded( offset ) ) continue;
|
||||
|
||||
IWiredElement element = ComputerCraft.getWiredElementAt( world, offset, facing.getOpposite() );
|
||||
if( element == null ) continue;
|
||||
|
||||
// If we can connect to it then do so
|
||||
m_node.connectTo( element.getNode() );
|
||||
}
|
||||
}
|
||||
|
||||
// private stuff
|
||||
private void togglePeripheralAccess()
|
||||
{
|
||||
if( !m_peripheralAccessAllowed )
|
||||
{
|
||||
boolean hasAny = false;
|
||||
for( EnumFacing facing : EnumFacing.VALUES )
|
||||
{
|
||||
WiredModemLocalPeripheral peripheral = m_peripherals[facing.ordinal()];
|
||||
peripheral.attach( world, getPos(), facing );
|
||||
hasAny |= peripheral.hasPeripheral();
|
||||
}
|
||||
|
||||
if( !hasAny ) return;
|
||||
|
||||
m_peripheralAccessAllowed = true;
|
||||
m_node.updatePeripherals( getConnectedPeripherals() );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_peripheralAccessAllowed = false;
|
||||
|
||||
for( WiredModemLocalPeripheral peripheral : m_peripherals ) peripheral.detach();
|
||||
m_node.updatePeripherals( Collections.emptyMap() );
|
||||
}
|
||||
|
||||
updateAnim();
|
||||
}
|
||||
|
||||
private Set<String> getConnectedPeripheralNames()
|
||||
{
|
||||
if( !m_peripheralAccessAllowed ) return Collections.emptySet();
|
||||
|
||||
Set<String> peripherals = new HashSet<>( 6 );
|
||||
for( WiredModemLocalPeripheral m_peripheral : m_peripherals )
|
||||
{
|
||||
String name = m_peripheral.getConnectedName();
|
||||
if( name != null ) peripherals.add( name );
|
||||
}
|
||||
return peripherals;
|
||||
}
|
||||
|
||||
private Map<String, IPeripheral> getConnectedPeripherals()
|
||||
{
|
||||
if( !m_peripheralAccessAllowed ) return Collections.emptyMap();
|
||||
|
||||
Map<String, IPeripheral> peripherals = new HashMap<>( 6 );
|
||||
for( WiredModemLocalPeripheral m_peripheral : m_peripherals ) m_peripheral.extendMap( peripherals );
|
||||
return peripherals;
|
||||
}
|
||||
|
||||
private void updateConnectedPeripherals()
|
||||
{
|
||||
Map<String, IPeripheral> peripherals = getConnectedPeripherals();
|
||||
if( peripherals.isEmpty() )
|
||||
{
|
||||
// If there are no peripherals then disable access and update the display state.
|
||||
m_peripheralAccessAllowed = false;
|
||||
updateAnim();
|
||||
}
|
||||
|
||||
m_node.updatePeripherals( peripherals );
|
||||
}
|
||||
|
||||
// IWiredElementTile
|
||||
|
||||
@Override
|
||||
public boolean hasCapability( @Nonnull Capability<?> capability, @Nullable EnumFacing facing )
|
||||
{
|
||||
return capability == CapabilityWiredElement.CAPABILITY || super.hasCapability( capability, facing );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getCapability( @Nonnull Capability<T> capability, @Nullable EnumFacing facing )
|
||||
{
|
||||
if( capability == CapabilityWiredElement.CAPABILITY )
|
||||
{
|
||||
return CapabilityWiredElement.CAPABILITY.cast( m_element );
|
||||
}
|
||||
|
||||
return super.getCapability( capability, facing );
|
||||
}
|
||||
|
||||
// IPeripheralTile
|
||||
|
||||
@Override
|
||||
public IPeripheral getPeripheral( EnumFacing side )
|
||||
{
|
||||
WiredModemPeripheral peripheral = m_modems[side.ordinal()];
|
||||
if( peripheral == null )
|
||||
{
|
||||
WiredModemLocalPeripheral localPeripheral = m_peripherals[side.ordinal()];
|
||||
peripheral = m_modems[side.ordinal()] = new WiredModemPeripheral( m_element )
|
||||
{
|
||||
@Nonnull
|
||||
@Override
|
||||
protected WiredModemLocalPeripheral getLocalPeripheral()
|
||||
{
|
||||
return localPeripheral;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Vec3d getPosition()
|
||||
{
|
||||
BlockPos pos = getPos().offset( side );
|
||||
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
|
||||
}
|
||||
};
|
||||
}
|
||||
return peripheral;
|
||||
}
|
||||
}
|
@@ -12,8 +12,8 @@ import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheralVariant;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class TileWirelessModem extends TileModemBase
|
||||
private static class Peripheral extends WirelessModemPeripheral
|
||||
{
|
||||
private TileModemBase m_entity;
|
||||
|
||||
|
||||
public Peripheral( TileModemBase entity )
|
||||
{
|
||||
super( false );
|
||||
@@ -39,12 +39,12 @@ public class TileWirelessModem extends TileModemBase
|
||||
{
|
||||
return m_entity.getWorld();
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Vec3d getPosition()
|
||||
{
|
||||
BlockPos pos = m_entity.getPos().offset( m_entity.getDirection() );
|
||||
BlockPos pos = m_entity.getPos().offset( m_entity.getCachedDirection() );
|
||||
return new Vec3d( pos.getX(), pos.getY(), pos.getZ() );
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class TileWirelessModem extends TileModemBase
|
||||
{
|
||||
if( other instanceof Peripheral )
|
||||
{
|
||||
Peripheral otherModem = (Peripheral)other;
|
||||
Peripheral otherModem = (Peripheral) other;
|
||||
return otherModem.m_entity == m_entity;
|
||||
}
|
||||
return false;
|
||||
@@ -62,8 +62,40 @@ public class TileWirelessModem extends TileModemBase
|
||||
|
||||
// Members
|
||||
|
||||
private boolean m_hasDirection = false;
|
||||
|
||||
public TileWirelessModem()
|
||||
{
|
||||
m_dir = EnumFacing.DOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad()
|
||||
{
|
||||
super.onLoad();
|
||||
updateDirection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateContainingBlockInfo()
|
||||
{
|
||||
m_hasDirection = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update()
|
||||
{
|
||||
super.update();
|
||||
updateDirection();
|
||||
}
|
||||
|
||||
private void updateDirection()
|
||||
{
|
||||
if( !m_hasDirection )
|
||||
{
|
||||
m_hasDirection = true;
|
||||
m_dir = getDirection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,59 @@
|
||||
package dan200.computercraft.shared.peripheral.modem;
|
||||
|
||||
import dan200.computercraft.api.network.wired.IWiredElement;
|
||||
import dan200.computercraft.api.network.wired.IWiredNetworkChange;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.wired.WiredNode;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class WiredModemElement implements IWiredElement
|
||||
{
|
||||
private final IWiredNode node = new WiredNode( this );
|
||||
private final Map<String, IPeripheral> remotePeripherals = new HashMap<>();
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public IWiredNode getNode()
|
||||
{
|
||||
return node;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getSenderID()
|
||||
{
|
||||
return "modem";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void networkChanged( @Nonnull IWiredNetworkChange change )
|
||||
{
|
||||
synchronized( remotePeripherals )
|
||||
{
|
||||
remotePeripherals.keySet().removeAll( change.peripheralsRemoved().keySet() );
|
||||
for( String name : change.peripheralsRemoved().keySet() )
|
||||
{
|
||||
detachPeripheral( name );
|
||||
}
|
||||
|
||||
for( Map.Entry<String, IPeripheral> peripheral : change.peripheralsAdded().entrySet() )
|
||||
{
|
||||
attachPeripheral( peripheral.getKey(), peripheral.getValue() );
|
||||
}
|
||||
remotePeripherals.putAll( change.peripheralsAdded() );
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, IPeripheral> getRemotePeripherals()
|
||||
{
|
||||
return remotePeripherals;
|
||||
}
|
||||
|
||||
protected abstract void attachPeripheral( String name, IPeripheral peripheral );
|
||||
|
||||
protected abstract void detachPeripheral( String name );
|
||||
}
|
@@ -0,0 +1,139 @@
|
||||
package dan200.computercraft.shared.peripheral.modem;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.shared.util.IDAssigner;
|
||||
import dan200.computercraft.shared.util.PeripheralUtil;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents a local peripheral exposed on the wired network
|
||||
*
|
||||
* This is responsible for getting the peripheral in world, tracking id and type and determining whether
|
||||
* it has changed.
|
||||
*/
|
||||
public final class WiredModemLocalPeripheral
|
||||
{
|
||||
private int id;
|
||||
private String type;
|
||||
|
||||
private IPeripheral peripheral;
|
||||
|
||||
/**
|
||||
* Attach a new peripheral from the world
|
||||
*
|
||||
* @param world The world to search in
|
||||
* @param origin The position to search from
|
||||
* @param direction The direction so search in
|
||||
* @return Whether the peripheral changed.
|
||||
*/
|
||||
public boolean attach( @Nonnull World world, @Nonnull BlockPos origin, @Nonnull EnumFacing direction )
|
||||
{
|
||||
IPeripheral oldPeripheral = this.peripheral;
|
||||
IPeripheral peripheral = this.peripheral = getPeripheralFrom( world, origin, direction );
|
||||
|
||||
if( peripheral == null )
|
||||
{
|
||||
return oldPeripheral != null;
|
||||
}
|
||||
else
|
||||
{
|
||||
String type = peripheral.getType();
|
||||
int id = this.id;
|
||||
|
||||
if( id > 0 && this.type == null )
|
||||
{
|
||||
// If we had an ID but no type, then just set the type.
|
||||
this.type = type;
|
||||
}
|
||||
else if( id < 0 || !type.equals( this.type ) )
|
||||
{
|
||||
this.type = type;
|
||||
this.id = IDAssigner.getNextIDFromFile( new File(
|
||||
ComputerCraft.getWorldDir( world ),
|
||||
"computer/lastid_" + type + ".txt"
|
||||
) );
|
||||
}
|
||||
|
||||
return oldPeripheral == null || !oldPeripheral.equals( peripheral );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detach the current peripheral
|
||||
*
|
||||
* @return Whether the peripheral changed
|
||||
*/
|
||||
public boolean detach()
|
||||
{
|
||||
if( peripheral == null ) return false;
|
||||
peripheral = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getConnectedName()
|
||||
{
|
||||
return peripheral != null ? type + "_" + id : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public IPeripheral getPeripheral()
|
||||
{
|
||||
return peripheral;
|
||||
}
|
||||
|
||||
public boolean hasPeripheral()
|
||||
{
|
||||
return peripheral != null;
|
||||
}
|
||||
|
||||
public void extendMap( @Nonnull Map<String, IPeripheral> peripherals )
|
||||
{
|
||||
if( peripheral != null ) peripherals.put( type + "_" + id, peripheral );
|
||||
}
|
||||
|
||||
public Map<String, IPeripheral> toMap()
|
||||
{
|
||||
return peripheral == null
|
||||
? Collections.emptyMap()
|
||||
: Collections.singletonMap( type + "_" + id, peripheral );
|
||||
}
|
||||
|
||||
public void writeNBT( @Nonnull NBTTagCompound tag, @Nonnull String suffix )
|
||||
{
|
||||
if( id >= 0 ) tag.setInteger( "peripheralID" + suffix, id );
|
||||
if( type != null ) tag.setString( "peripheralType" + suffix, type );
|
||||
}
|
||||
|
||||
public void readNBT( @Nonnull NBTTagCompound tag, @Nonnull String suffix )
|
||||
{
|
||||
id = tag.hasKey( "peripheralID" + suffix, Constants.NBT.TAG_ANY_NUMERIC )
|
||||
? tag.getInteger( "peripheralID" + suffix ) : -1;
|
||||
|
||||
type = tag.hasKey( "peripheralType" + suffix, Constants.NBT.TAG_STRING )
|
||||
? tag.getString( "peripheralType" + suffix ) : null;
|
||||
}
|
||||
|
||||
private static IPeripheral getPeripheralFrom( World world, BlockPos pos, EnumFacing direction )
|
||||
{
|
||||
BlockPos offset = pos.offset( direction );
|
||||
|
||||
Block block = world.getBlockState( offset ).getBlock();
|
||||
if( block == ComputerCraft.Blocks.wiredModemFull || block == ComputerCraft.Blocks.cable ) return null;
|
||||
|
||||
IPeripheral peripheral = PeripheralUtil.getPeripheral( world, offset, direction.getOpposite() );
|
||||
return peripheral instanceof WiredModemPeripheral ? null : peripheral;
|
||||
}
|
||||
}
|
@@ -0,0 +1,421 @@
|
||||
package dan200.computercraft.shared.peripheral.modem;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import dan200.computercraft.api.filesystem.IMount;
|
||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
import dan200.computercraft.api.network.IPacketNetwork;
|
||||
import dan200.computercraft.api.network.wired.IWiredNode;
|
||||
import dan200.computercraft.api.network.wired.IWiredSender;
|
||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.core.computer.Computer;
|
||||
import dan200.computercraft.core.computer.IComputerOwned;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
|
||||
|
||||
public abstract class WiredModemPeripheral extends ModemPeripheral implements IWiredSender
|
||||
{
|
||||
private final WiredModemElement modem;
|
||||
|
||||
private final Map<String, RemotePeripheralWrapper> peripheralWrappers = new HashMap<>();
|
||||
|
||||
public WiredModemPeripheral( WiredModemElement modem )
|
||||
{
|
||||
this.modem = modem;
|
||||
}
|
||||
|
||||
//region IPacketSender implementation
|
||||
@Override
|
||||
public boolean isInterdimensional()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getRange()
|
||||
{
|
||||
return 256.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IPacketNetwork getNetwork()
|
||||
{
|
||||
return modem.getNode();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public World getWorld()
|
||||
{
|
||||
return modem.getWorld();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
protected abstract WiredModemLocalPeripheral getLocalPeripheral();
|
||||
//endregion
|
||||
|
||||
//region IPeripheral
|
||||
@Nonnull
|
||||
@Override
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
String[] methods = super.getMethodNames();
|
||||
String[] newMethods = new String[methods.length + 6];
|
||||
System.arraycopy( methods, 0, newMethods, 0, methods.length );
|
||||
newMethods[methods.length] = "getNamesRemote";
|
||||
newMethods[methods.length + 1] = "isPresentRemote";
|
||||
newMethods[methods.length + 2] = "getTypeRemote";
|
||||
newMethods[methods.length + 3] = "getMethodsRemote";
|
||||
newMethods[methods.length + 4] = "callRemote";
|
||||
newMethods[methods.length + 5] = "getNameLocal";
|
||||
return newMethods;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException
|
||||
{
|
||||
String[] methods = super.getMethodNames();
|
||||
switch( method - methods.length )
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
// getNamesRemote
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
int idx = 1;
|
||||
Map<Object, Object> table = new HashMap<>();
|
||||
for( String name : peripheralWrappers.keySet() )
|
||||
{
|
||||
table.put( idx++, name );
|
||||
}
|
||||
return new Object[]{ table };
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
// isPresentRemote
|
||||
String type = getTypeRemote( getString( arguments, 0 ) );
|
||||
return new Object[]{ type != null };
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
// getTypeRemote
|
||||
String type = getTypeRemote( getString( arguments, 0 ) );
|
||||
if( type != null )
|
||||
{
|
||||
return new Object[]{ type };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
// getMethodsRemote
|
||||
String[] methodNames = getMethodNamesRemote( getString( arguments, 0 ) );
|
||||
if( methodNames != null )
|
||||
{
|
||||
Map<Object, Object> table = new HashMap<>();
|
||||
for( int i = 0; i < methodNames.length; ++i )
|
||||
{
|
||||
table.put( i + 1, methodNames[i] );
|
||||
}
|
||||
return new Object[]{ table };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
// callRemote
|
||||
String remoteName = getString( arguments, 0 );
|
||||
String methodName = getString( arguments, 1 );
|
||||
Object[] methodArgs = new Object[arguments.length - 2];
|
||||
System.arraycopy( arguments, 2, methodArgs, 0, arguments.length - 2 );
|
||||
return callMethodRemote( remoteName, context, methodName, methodArgs );
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
// getNameLocal
|
||||
String local = getLocalPeripheral().getConnectedName();
|
||||
return local == null ? null : new Object[]{ local };
|
||||
}
|
||||
default:
|
||||
{
|
||||
// The regular modem methods
|
||||
return super.callMethod( computer, context, method, arguments );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attach( @Nonnull IComputerAccess computer )
|
||||
{
|
||||
super.attach( computer );
|
||||
synchronized( modem.getRemotePeripherals() )
|
||||
{
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
for( Map.Entry<String, IPeripheral> entry : modem.getRemotePeripherals().entrySet() )
|
||||
{
|
||||
attachPeripheralImpl( entry.getKey(), entry.getValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void detach( @Nonnull IComputerAccess computer )
|
||||
{
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
for( RemotePeripheralWrapper wrapper : peripheralWrappers.values() )
|
||||
{
|
||||
wrapper.detach();
|
||||
}
|
||||
peripheralWrappers.clear();
|
||||
}
|
||||
super.detach( computer );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( IPeripheral other )
|
||||
{
|
||||
if( other instanceof WiredModemPeripheral )
|
||||
{
|
||||
WiredModemPeripheral otherModem = (WiredModemPeripheral) other;
|
||||
return otherModem.modem == modem;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//endregion
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public IWiredNode getNode()
|
||||
{
|
||||
return modem.getNode();
|
||||
}
|
||||
|
||||
public void attachPeripheral( String name, IPeripheral peripheral )
|
||||
{
|
||||
if( getComputer() == null ) return;
|
||||
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
attachPeripheralImpl( name, peripheral );
|
||||
}
|
||||
}
|
||||
|
||||
public void detachPeripheral( String name )
|
||||
{
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
RemotePeripheralWrapper wrapper = peripheralWrappers.get( name );
|
||||
if( wrapper != null )
|
||||
{
|
||||
peripheralWrappers.remove( name );
|
||||
wrapper.detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void attachPeripheralImpl( String periphName, IPeripheral peripheral )
|
||||
{
|
||||
if( !peripheralWrappers.containsKey( periphName ) && !periphName.equals( getLocalPeripheral().getConnectedName() ) )
|
||||
{
|
||||
RemotePeripheralWrapper wrapper = new RemotePeripheralWrapper( modem, peripheral, getComputer(), periphName );
|
||||
peripheralWrappers.put( periphName, wrapper );
|
||||
wrapper.attach();
|
||||
}
|
||||
}
|
||||
|
||||
private String getTypeRemote( String remoteName )
|
||||
{
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
RemotePeripheralWrapper wrapper = peripheralWrappers.get( remoteName );
|
||||
if( wrapper != null )
|
||||
{
|
||||
return wrapper.getType();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String[] getMethodNamesRemote( String remoteName )
|
||||
{
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
RemotePeripheralWrapper wrapper = peripheralWrappers.get( remoteName );
|
||||
if( wrapper != null )
|
||||
{
|
||||
return wrapper.getMethodNames();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Object[] callMethodRemote( String remoteName, ILuaContext context, String method, Object[] arguments ) throws LuaException, InterruptedException
|
||||
{
|
||||
RemotePeripheralWrapper wrapper;
|
||||
synchronized( peripheralWrappers )
|
||||
{
|
||||
wrapper = peripheralWrappers.get( remoteName );
|
||||
}
|
||||
if( wrapper != null )
|
||||
{
|
||||
return wrapper.callMethod( context, method, arguments );
|
||||
}
|
||||
throw new LuaException( "No peripheral: " + remoteName );
|
||||
}
|
||||
|
||||
private static class RemotePeripheralWrapper implements IComputerAccess, IComputerOwned
|
||||
{
|
||||
private final WiredModemElement m_element;
|
||||
private final IPeripheral m_peripheral;
|
||||
private final IComputerAccess m_computer;
|
||||
private final String m_name;
|
||||
|
||||
private final String m_type;
|
||||
private final String[] m_methods;
|
||||
private final Map<String, Integer> m_methodMap;
|
||||
|
||||
public RemotePeripheralWrapper( WiredModemElement element, IPeripheral peripheral, IComputerAccess computer, String name )
|
||||
{
|
||||
m_element = element;
|
||||
m_peripheral = peripheral;
|
||||
m_computer = computer;
|
||||
m_name = name;
|
||||
|
||||
m_type = peripheral.getType();
|
||||
m_methods = peripheral.getMethodNames();
|
||||
assert (m_type != null);
|
||||
assert (m_methods != null);
|
||||
|
||||
m_methodMap = new HashMap<>();
|
||||
for( int i = 0; i < m_methods.length; ++i )
|
||||
{
|
||||
if( m_methods[i] != null )
|
||||
{
|
||||
m_methodMap.put( m_methods[i], i );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void attach()
|
||||
{
|
||||
m_peripheral.attach( this );
|
||||
m_computer.queueEvent( "peripheral", new Object[]{ getAttachmentName() } );
|
||||
}
|
||||
|
||||
public void detach()
|
||||
{
|
||||
m_peripheral.detach( this );
|
||||
m_computer.queueEvent( "peripheral_detach", new Object[]{ getAttachmentName() } );
|
||||
}
|
||||
|
||||
public String getType()
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
public String[] getMethodNames()
|
||||
{
|
||||
return m_methods;
|
||||
}
|
||||
|
||||
public Object[] callMethod( ILuaContext context, String methodName, Object[] arguments ) throws LuaException, InterruptedException
|
||||
{
|
||||
if( m_methodMap.containsKey( methodName ) )
|
||||
{
|
||||
int method = m_methodMap.get( methodName );
|
||||
return m_peripheral.callMethod( this, context, method, arguments );
|
||||
}
|
||||
throw new LuaException( "No such method " + methodName );
|
||||
}
|
||||
|
||||
// IComputerAccess implementation
|
||||
|
||||
@Override
|
||||
public String mount( @Nonnull String desiredLocation, @Nonnull IMount mount )
|
||||
{
|
||||
return m_computer.mount( desiredLocation, mount, m_name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String mount( @Nonnull String desiredLocation, @Nonnull IMount mount, @Nonnull String driveName )
|
||||
{
|
||||
return m_computer.mount( desiredLocation, mount, driveName );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String mountWritable( @Nonnull String desiredLocation, @Nonnull IWritableMount mount )
|
||||
{
|
||||
return m_computer.mountWritable( desiredLocation, mount, m_name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String mountWritable( @Nonnull String desiredLocation, @Nonnull IWritableMount mount, @Nonnull String driveName )
|
||||
{
|
||||
return m_computer.mountWritable( desiredLocation, mount, driveName );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unmount( String location )
|
||||
{
|
||||
m_computer.unmount( location );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getID()
|
||||
{
|
||||
return m_computer.getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queueEvent( @Nonnull String event, Object[] arguments )
|
||||
{
|
||||
m_computer.queueEvent( event, arguments );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getAttachmentName()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Map<String, IPeripheral> getAvailablePeripherals()
|
||||
{
|
||||
synchronized( m_element.getRemotePeripherals() )
|
||||
{
|
||||
return ImmutableMap.copyOf( m_element.getRemotePeripherals() );
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IPeripheral getAvailablePeripheral( @Nonnull String name )
|
||||
{
|
||||
synchronized( m_element.getRemotePeripherals() )
|
||||
{
|
||||
return m_element.getRemotePeripherals().get( name );
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Computer getComputer()
|
||||
{
|
||||
return m_computer instanceof IComputerOwned ? ((IComputerOwned) m_computer).getComputer() : null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,88 @@
|
||||
package dan200.computercraft.shared.peripheral.monitor;
|
||||
|
||||
import dan200.computercraft.shared.common.ClientTerminal;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
public class ClientMonitor extends ClientTerminal
|
||||
{
|
||||
private static final Set<ClientMonitor> allMonitors = new HashSet<>();
|
||||
|
||||
private final TileMonitor origin;
|
||||
|
||||
public long lastRenderFrame = -1;
|
||||
public BlockPos lastRenderPos = null;
|
||||
public int[] renderDisplayLists = null;
|
||||
|
||||
public ClientMonitor( boolean colour, TileMonitor origin )
|
||||
{
|
||||
super( colour );
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
public TileMonitor getOrigin()
|
||||
{
|
||||
return origin;
|
||||
}
|
||||
|
||||
public void createLists()
|
||||
{
|
||||
if( renderDisplayLists == null )
|
||||
{
|
||||
renderDisplayLists = new int[3];
|
||||
|
||||
for( int i = 0; i < renderDisplayLists.length; i++ )
|
||||
{
|
||||
renderDisplayLists[i] = GlStateManager.glGenLists( 1 );
|
||||
}
|
||||
|
||||
synchronized( allMonitors )
|
||||
{
|
||||
allMonitors.add( this );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy()
|
||||
{
|
||||
if( renderDisplayLists != null )
|
||||
{
|
||||
synchronized( allMonitors )
|
||||
{
|
||||
allMonitors.remove( this );
|
||||
}
|
||||
|
||||
for( int list : renderDisplayLists )
|
||||
{
|
||||
GlStateManager.glDeleteLists( list, 1 );
|
||||
}
|
||||
|
||||
renderDisplayLists = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void destroyAll()
|
||||
{
|
||||
synchronized( allMonitors )
|
||||
{
|
||||
for( Iterator<ClientMonitor> iterator = allMonitors.iterator(); iterator.hasNext(); )
|
||||
{
|
||||
ClientMonitor monitor = iterator.next();
|
||||
if( monitor.renderDisplayLists != null )
|
||||
{
|
||||
for( int list : monitor.renderDisplayLists )
|
||||
{
|
||||
GlStateManager.glDeleteLists( list, 1 );
|
||||
}
|
||||
monitor.renderDisplayLists = null;
|
||||
}
|
||||
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -73,6 +73,12 @@ public class MonitorPeripheral implements IPeripheral
|
||||
@Override
|
||||
public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object args[] ) throws LuaException
|
||||
{
|
||||
ServerMonitor monitor = m_monitor.getCachedServerMonitor();
|
||||
if( monitor == null ) throw new LuaException( "Monitor has been detatched" );
|
||||
|
||||
Terminal terminal = monitor.getTerminal();
|
||||
if( terminal == null ) throw new LuaException( "Monitor has been detatched" );
|
||||
|
||||
switch( method )
|
||||
{
|
||||
case 0:
|
||||
@@ -84,7 +90,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
} else {
|
||||
text = "";
|
||||
}
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.write( text );
|
||||
terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() );
|
||||
return null;
|
||||
@@ -93,7 +98,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
{
|
||||
// scroll
|
||||
int value = getInt( args, 0 );
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.scroll( value );
|
||||
return null;
|
||||
}
|
||||
@@ -102,7 +106,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
// setCursorPos
|
||||
int x = getInt( args, 0 ) - 1;
|
||||
int y = getInt( args, 1 ) - 1;
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.setCursorPos( x, y );
|
||||
return null;
|
||||
}
|
||||
@@ -110,14 +113,12 @@ public class MonitorPeripheral implements IPeripheral
|
||||
{
|
||||
// setCursorBlink
|
||||
boolean blink = getBoolean( args, 0 );
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.setCursorBlink( blink );
|
||||
return null;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
// getCursorPos
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
return new Object[] {
|
||||
terminal.getCursorX() + 1,
|
||||
terminal.getCursorY() + 1
|
||||
@@ -126,7 +127,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
case 5:
|
||||
{
|
||||
// getSize
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
return new Object[] {
|
||||
terminal.getWidth(),
|
||||
terminal.getHeight()
|
||||
@@ -135,14 +135,12 @@ public class MonitorPeripheral implements IPeripheral
|
||||
case 6:
|
||||
{
|
||||
// clear
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.clear();
|
||||
return null;
|
||||
}
|
||||
case 7:
|
||||
{
|
||||
// clearLine
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.clearLine();
|
||||
return null;
|
||||
}
|
||||
@@ -154,7 +152,7 @@ public class MonitorPeripheral implements IPeripheral
|
||||
{
|
||||
throw new LuaException( "Expected number in range 0.5-5" );
|
||||
}
|
||||
m_monitor.setTextScale( scale );
|
||||
monitor.setTextScale( scale );
|
||||
return null;
|
||||
}
|
||||
case 9:
|
||||
@@ -162,7 +160,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
{
|
||||
// setTextColour/setTextColor
|
||||
int colour = TermAPI.parseColour( args );
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.setTextColour( colour );
|
||||
return null;
|
||||
}
|
||||
@@ -171,7 +168,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
{
|
||||
// setBackgroundColour/setBackgroundColor
|
||||
int colour = TermAPI.parseColour( args );
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.setBackgroundColour( colour );
|
||||
return null;
|
||||
}
|
||||
@@ -180,21 +176,19 @@ public class MonitorPeripheral implements IPeripheral
|
||||
{
|
||||
// isColour/isColor
|
||||
return new Object[] {
|
||||
m_monitor.getTerminal().isColour()
|
||||
monitor.isColour()
|
||||
};
|
||||
}
|
||||
case 15:
|
||||
case 16:
|
||||
{
|
||||
// getTextColour/getTextColor
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
return TermAPI.encodeColour( terminal.getTextColour() );
|
||||
}
|
||||
case 17:
|
||||
case 18:
|
||||
{
|
||||
// getBackgroundColour/getBackgroundColor
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
return TermAPI.encodeColour( terminal.getBackgroundColour() );
|
||||
}
|
||||
case 19:
|
||||
@@ -208,7 +202,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
throw new LuaException( "Arguments must be the same length" );
|
||||
}
|
||||
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
terminal.blit( text, textColour, backgroundColour );
|
||||
terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() );
|
||||
return null;
|
||||
@@ -217,8 +210,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
case 21:
|
||||
{
|
||||
// setPaletteColour/setPaletteColor
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
|
||||
int colour = 15 - TermAPI.parseColour( args );
|
||||
if( args.length == 2 )
|
||||
{
|
||||
@@ -239,7 +230,6 @@ public class MonitorPeripheral implements IPeripheral
|
||||
case 23:
|
||||
{
|
||||
// getPaletteColour/getPaletteColor
|
||||
Terminal terminal = m_monitor.getTerminal().getTerminal();
|
||||
Palette palette = terminal.getPalette();
|
||||
|
||||
int colour = 15 - TermAPI.parseColour( args );
|
||||
@@ -253,7 +243,7 @@ public class MonitorPeripheral implements IPeripheral
|
||||
case 24:
|
||||
{
|
||||
// getTextScale
|
||||
return new Object[] { m_monitor.getTextScale() };
|
||||
return new Object[] { monitor.getTextScale() / 2.0 };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@@ -0,0 +1,64 @@
|
||||
package dan200.computercraft.shared.peripheral.monitor;
|
||||
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.shared.common.ServerTerminal;
|
||||
|
||||
public class ServerMonitor extends ServerTerminal
|
||||
{
|
||||
private final TileMonitor origin;
|
||||
private int textScale = 2;
|
||||
private boolean resized;
|
||||
|
||||
public ServerMonitor( boolean colour, TileMonitor origin )
|
||||
{
|
||||
super( colour );
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
public synchronized void rebuild()
|
||||
{
|
||||
Terminal oldTerm = getTerminal();
|
||||
int oldWidth = oldTerm == null ? -1 : oldTerm.getWidth();
|
||||
int oldHeight = oldTerm == null ? -1 : oldTerm.getHeight();
|
||||
|
||||
double textScale = this.textScale * 0.5;
|
||||
int termWidth = (int) Math.max(
|
||||
Math.round( (origin.getWidth() - 2.0 * (TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN)) / (textScale * 6.0 * TileMonitor.RENDER_PIXEL_SCALE) ),
|
||||
1.0
|
||||
);
|
||||
int termHeight = (int) Math.max(
|
||||
Math.round( (origin.getHeight() - 2.0 * (TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN)) / (textScale * 9.0 * TileMonitor.RENDER_PIXEL_SCALE) ),
|
||||
1.0
|
||||
);
|
||||
|
||||
resize( termWidth, termHeight );
|
||||
if( oldWidth != termWidth || oldHeight != termHeight )
|
||||
{
|
||||
getTerminal().clear();
|
||||
resized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public int getTextScale()
|
||||
{
|
||||
return textScale;
|
||||
}
|
||||
|
||||
public synchronized void setTextScale( int textScale )
|
||||
{
|
||||
if( this.textScale == textScale ) return;
|
||||
this.textScale = textScale;
|
||||
rebuild();
|
||||
}
|
||||
|
||||
public synchronized boolean pollResized()
|
||||
{
|
||||
if( resized )
|
||||
{
|
||||
resized = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -10,19 +10,17 @@ import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.core.terminal.Terminal;
|
||||
import dan200.computercraft.shared.common.ClientTerminal;
|
||||
import dan200.computercraft.shared.common.ITerminal;
|
||||
import dan200.computercraft.shared.common.ITerminalTile;
|
||||
import dan200.computercraft.shared.common.ServerTerminal;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
|
||||
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.AxisAlignedBB;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -30,7 +28,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class TileMonitor extends TilePeripheralBase
|
||||
implements ITerminalTile
|
||||
{
|
||||
// Statics
|
||||
|
||||
@@ -42,26 +39,22 @@ public class TileMonitor extends TilePeripheralBase
|
||||
private static final int MAX_HEIGHT = 6;
|
||||
|
||||
// Members
|
||||
|
||||
private ServerTerminal m_serverTerminal;
|
||||
private ClientTerminal m_clientTerminal;
|
||||
private ServerMonitor m_serverMonitor;
|
||||
private ClientMonitor m_clientMonitor;
|
||||
private MonitorPeripheral m_peripheral;
|
||||
private final Set<IComputerAccess> m_computers;
|
||||
|
||||
public long m_lastRenderFrame = -1; // For rendering use only
|
||||
public int m_renderDisplayList = -1; // For rendering use only
|
||||
|
||||
private boolean m_destroyed;
|
||||
private boolean m_ignoreMe;
|
||||
private boolean m_changed;
|
||||
|
||||
private int m_textScale;
|
||||
private int m_width;
|
||||
private int m_height;
|
||||
private int m_xIndex;
|
||||
private int m_yIndex;
|
||||
|
||||
private int m_dir;
|
||||
private boolean m_sizeChangedQueued;
|
||||
|
||||
private boolean m_advanced;
|
||||
|
||||
public TileMonitor()
|
||||
{
|
||||
@@ -69,17 +62,23 @@ public class TileMonitor extends TilePeripheralBase
|
||||
|
||||
m_destroyed = false;
|
||||
m_ignoreMe = false;
|
||||
m_textScale = 2;
|
||||
|
||||
m_width = 1;
|
||||
m_height = 1;
|
||||
m_xIndex = 0;
|
||||
m_yIndex = 0;
|
||||
m_changed = false;
|
||||
|
||||
m_dir = 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad()
|
||||
{
|
||||
super.onLoad();
|
||||
m_advanced = getBlockState().getValue( BlockPeripheral.Properties.VARIANT )
|
||||
.getPeripheralType() == PeripheralType.AdvancedMonitor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
@@ -91,11 +90,20 @@ public class TileMonitor extends TilePeripheralBase
|
||||
contractNeighbours();
|
||||
}
|
||||
}
|
||||
if( m_renderDisplayList >= 0 )
|
||||
{
|
||||
ComputerCraft.deleteDisplayLists( m_renderDisplayList, 3 );
|
||||
m_renderDisplayList = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate()
|
||||
{
|
||||
super.invalidate();
|
||||
if( m_clientMonitor != null && m_xIndex == 0 && m_yIndex == 0 ) m_clientMonitor.destroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnload()
|
||||
{
|
||||
super.onChunkUnload();
|
||||
if( m_clientMonitor != null && m_xIndex == 0 && m_yIndex == 0 ) m_clientMonitor.destroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -143,41 +151,38 @@ public class TileMonitor extends TilePeripheralBase
|
||||
|
||||
if( !getWorld().isRemote )
|
||||
{
|
||||
if( m_sizeChangedQueued )
|
||||
if( m_xIndex == 0 && m_yIndex == 0 && m_serverMonitor != null )
|
||||
{
|
||||
for( IComputerAccess computer : m_computers )
|
||||
if( m_serverMonitor.pollResized() )
|
||||
{
|
||||
computer.queueEvent( "monitor_resize", new Object[] {
|
||||
computer.getAttachmentName()
|
||||
} );
|
||||
}
|
||||
m_sizeChangedQueued = false;
|
||||
}
|
||||
for( int x = 0; x < m_width; x++ )
|
||||
{
|
||||
for( int y = 0; y < m_height; y++ )
|
||||
{
|
||||
TileMonitor monitor = getNeighbour( x, y );
|
||||
if( monitor == null ) continue;
|
||||
|
||||
if( m_serverTerminal != null )
|
||||
{
|
||||
m_serverTerminal.update();
|
||||
if( m_serverTerminal.hasTerminalChanged() )
|
||||
{
|
||||
updateBlock();
|
||||
for( IComputerAccess computer : monitor.m_computers )
|
||||
{
|
||||
computer.queueEvent( "monitor_resize", new Object[] {
|
||||
computer.getAttachmentName()
|
||||
} );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( m_clientTerminal != null )
|
||||
{
|
||||
m_clientTerminal.update();
|
||||
m_serverMonitor.update();
|
||||
if( m_serverMonitor.hasTerminalChanged() ) updateBlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean pollChanged()
|
||||
{
|
||||
if( m_changed )
|
||||
else
|
||||
{
|
||||
m_changed = false;
|
||||
return true;
|
||||
if( m_xIndex == 0 && m_yIndex == 0 && m_clientMonitor != null )
|
||||
{
|
||||
m_clientMonitor.update();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// IPeripheralTile implementation
|
||||
@@ -185,24 +190,72 @@ public class TileMonitor extends TilePeripheralBase
|
||||
@Override
|
||||
public IPeripheral getPeripheral( EnumFacing side )
|
||||
{
|
||||
return new MonitorPeripheral( this );
|
||||
createServerMonitor(); // Ensure the monitor is created before doing anything else.
|
||||
|
||||
if( m_peripheral == null ) m_peripheral = new MonitorPeripheral( this );
|
||||
return m_peripheral;
|
||||
}
|
||||
|
||||
public void setTextScale( int scale )
|
||||
public ServerMonitor getCachedServerMonitor()
|
||||
{
|
||||
return m_serverMonitor;
|
||||
}
|
||||
|
||||
private ServerMonitor getServerMonitor()
|
||||
{
|
||||
if( m_serverMonitor != null ) return m_serverMonitor;
|
||||
|
||||
TileMonitor origin = getOrigin();
|
||||
if( origin != null )
|
||||
if( origin == null ) return null;
|
||||
|
||||
return m_serverMonitor = origin.m_serverMonitor;
|
||||
}
|
||||
|
||||
private ServerMonitor createServerMonitor()
|
||||
{
|
||||
if( m_serverMonitor != null )
|
||||
{
|
||||
synchronized( origin )
|
||||
return m_serverMonitor;
|
||||
}
|
||||
else if( m_xIndex == 0 && m_yIndex == 0 )
|
||||
{
|
||||
// If we're the origin, set up the new monitor
|
||||
m_serverMonitor = new ServerMonitor( m_advanced, this );
|
||||
m_serverMonitor.rebuild();
|
||||
|
||||
// And propagate it to child monitors
|
||||
for( int x = 0; x < m_width; x++ )
|
||||
{
|
||||
if( origin.m_textScale != scale )
|
||||
for( int y = 0; y < m_height; y++ )
|
||||
{
|
||||
origin.m_textScale = scale;
|
||||
origin.rebuildTerminal();
|
||||
origin.updateBlock();
|
||||
TileMonitor monitor = getNeighbour( x, y );
|
||||
if( monitor != null ) monitor.m_serverMonitor = m_serverMonitor;
|
||||
}
|
||||
}
|
||||
|
||||
return m_serverMonitor;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise fetch the origin and attempt to get its monitor
|
||||
// Note this may load chunks, but we don't really have a choice here.
|
||||
BlockPos pos = getPos();
|
||||
TileEntity te = world.getTileEntity( pos.offset( getRight(), -m_xIndex ).offset( getDown(), -m_yIndex ) );
|
||||
if( !(te instanceof TileMonitor) ) return null;
|
||||
|
||||
return m_serverMonitor = ((TileMonitor) te).createServerMonitor();
|
||||
}
|
||||
}
|
||||
|
||||
public ClientMonitor getClientMonitor()
|
||||
{
|
||||
if( m_clientMonitor != null ) return m_clientMonitor;
|
||||
|
||||
BlockPos pos = getPos();
|
||||
TileEntity te = world.getTileEntity( pos.offset( getRight(), -m_xIndex ).offset( getDown(), -m_yIndex ) );
|
||||
if( !(te instanceof TileMonitor) ) return null;
|
||||
|
||||
return m_clientMonitor = ((TileMonitor) te).m_clientMonitor;
|
||||
}
|
||||
|
||||
// Networking stuff
|
||||
@@ -215,9 +268,12 @@ public class TileMonitor extends TilePeripheralBase
|
||||
nbttagcompound.setInteger( "yIndex", m_yIndex );
|
||||
nbttagcompound.setInteger( "width", m_width );
|
||||
nbttagcompound.setInteger( "height", m_height );
|
||||
nbttagcompound.setInteger( "textScale", m_textScale );
|
||||
nbttagcompound.setInteger( "monitorDir", m_dir );
|
||||
((ServerTerminal)getLocalTerminal()).writeDescription( nbttagcompound );
|
||||
|
||||
if( m_xIndex == 0 && m_yIndex == 0 && m_serverMonitor != null )
|
||||
{
|
||||
m_serverMonitor.writeDescription( nbttagcompound );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -229,110 +285,39 @@ public class TileMonitor extends TilePeripheralBase
|
||||
int oldYIndex = m_yIndex;
|
||||
int oldWidth = m_width;
|
||||
int oldHeight = m_height;
|
||||
int oldTextScale = m_textScale;
|
||||
int oldDir = m_dir;
|
||||
|
||||
m_xIndex = nbttagcompound.getInteger( "xIndex" );
|
||||
m_yIndex = nbttagcompound.getInteger( "yIndex" );
|
||||
m_width = nbttagcompound.getInteger( "width" );
|
||||
m_height = nbttagcompound.getInteger( "height" );
|
||||
m_textScale = nbttagcompound.getInteger( "textScale" );
|
||||
m_dir = nbttagcompound.getInteger( "monitorDir" );
|
||||
((ClientTerminal)getLocalTerminal()).readDescription( nbttagcompound );
|
||||
m_changed = true;
|
||||
|
||||
if( oldXIndex != m_xIndex || oldYIndex != m_yIndex )
|
||||
{
|
||||
// If our index has changed then it's possible the origin monitor has changed. Thus
|
||||
// we'll clear our cache. If we're the origin then we'll need to remove the glList as well.
|
||||
if( oldXIndex == 0 && oldYIndex == 0 && m_clientMonitor != null ) m_clientMonitor.destroy();
|
||||
m_clientMonitor = null;
|
||||
}
|
||||
|
||||
if( m_xIndex == 0 && m_yIndex == 0 )
|
||||
{
|
||||
// If we're the origin terminal then read the description
|
||||
if( m_clientMonitor == null ) m_clientMonitor = new ClientMonitor( m_advanced, this );
|
||||
m_clientMonitor.readDescription( nbttagcompound );
|
||||
}
|
||||
|
||||
if( oldXIndex != m_xIndex || oldYIndex != m_yIndex ||
|
||||
oldWidth != m_width || oldHeight != m_height ||
|
||||
oldTextScale != m_textScale || oldDir != m_dir )
|
||||
oldDir != m_dir )
|
||||
{
|
||||
// One of our properties has changed, so ensure we redraw the block
|
||||
updateBlock();
|
||||
}
|
||||
}
|
||||
|
||||
// ITerminalTile implementation
|
||||
|
||||
@Override
|
||||
public ITerminal getTerminal()
|
||||
{
|
||||
TileMonitor origin = getOrigin();
|
||||
if( origin != null )
|
||||
{
|
||||
return origin.getLocalTerminal();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ITerminal getLocalTerminal()
|
||||
{
|
||||
if( !getWorld().isRemote )
|
||||
{
|
||||
if( m_serverTerminal == null )
|
||||
{
|
||||
m_serverTerminal = new ServerTerminal(
|
||||
getPeripheralType() == PeripheralType.AdvancedMonitor
|
||||
);
|
||||
}
|
||||
return m_serverTerminal;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( m_clientTerminal == null )
|
||||
{
|
||||
m_clientTerminal = new ClientTerminal(
|
||||
getPeripheralType() == PeripheralType.AdvancedMonitor
|
||||
);
|
||||
}
|
||||
return m_clientTerminal;
|
||||
}
|
||||
}
|
||||
|
||||
// Sizing and placement stuff
|
||||
|
||||
public double getTextScale()
|
||||
{
|
||||
TileMonitor origin = getOrigin();
|
||||
return (origin == null ? m_textScale : origin.m_textScale) * 0.5;
|
||||
}
|
||||
|
||||
private void rebuildTerminal()
|
||||
{
|
||||
Terminal oldTerm = getTerminal().getTerminal();
|
||||
int oldWidth = (oldTerm != null) ? oldTerm.getWidth() : -1;
|
||||
int oldHeight = (oldTerm != null) ? oldTerm.getHeight() : -1;
|
||||
|
||||
double textScale = getTextScale();
|
||||
int termWidth = (int)Math.max(
|
||||
Math.round( (m_width - 2.0 * ( TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN )) / (textScale * 6.0 * TileMonitor.RENDER_PIXEL_SCALE) ),
|
||||
1.0
|
||||
);
|
||||
int termHeight = (int)Math.max(
|
||||
Math.round( (m_height - 2.0 * ( TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN )) / (textScale * 9.0 * TileMonitor.RENDER_PIXEL_SCALE) ),
|
||||
1.0
|
||||
);
|
||||
((ServerTerminal)getLocalTerminal()).resize( termWidth, termHeight );
|
||||
|
||||
if( oldWidth != termWidth || oldHeight != termHeight )
|
||||
{
|
||||
getLocalTerminal().getTerminal().clear();
|
||||
for( int y=0; y<m_height; ++y )
|
||||
{
|
||||
for( int x=0; x<m_width; ++x )
|
||||
{
|
||||
TileMonitor monitor = getNeighbour( x, y );
|
||||
if( monitor != null )
|
||||
{
|
||||
monitor.queueSizeChangedEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void destroyTerminal()
|
||||
{
|
||||
((ServerTerminal)getLocalTerminal()).delete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumFacing getDirection()
|
||||
{
|
||||
@@ -354,7 +339,6 @@ public class TileMonitor extends TilePeripheralBase
|
||||
public void setDir( int dir )
|
||||
{
|
||||
m_dir = dir;
|
||||
m_changed = true;
|
||||
markDirty();
|
||||
}
|
||||
|
||||
@@ -431,10 +415,9 @@ public class TileMonitor extends TilePeripheralBase
|
||||
TileEntity tile = world.getTileEntity( pos );
|
||||
if( tile != null && tile instanceof TileMonitor )
|
||||
{
|
||||
TileMonitor monitor = (TileMonitor)tile;
|
||||
if( monitor.getDir() == getDir() &&
|
||||
monitor.getLocalTerminal().isColour() == getLocalTerminal().isColour() &&
|
||||
!monitor.m_destroyed && !monitor.m_ignoreMe )
|
||||
TileMonitor monitor = (TileMonitor) tile;
|
||||
if( monitor.getDir() == getDir() && monitor.m_advanced == m_advanced &&
|
||||
!monitor.m_destroyed && !monitor.m_ignoreMe )
|
||||
{
|
||||
return monitor;
|
||||
}
|
||||
@@ -464,34 +447,62 @@ public class TileMonitor extends TilePeripheralBase
|
||||
|
||||
private void resize( int width, int height )
|
||||
{
|
||||
// Update the positions and indexes of the other monitors
|
||||
BlockPos pos = getPos();
|
||||
EnumFacing right = getRight();
|
||||
EnumFacing down = getDown();
|
||||
for( int y=0; y<height; ++y )
|
||||
// If we're not already the origin then we'll need to generate a new terminal.
|
||||
if(m_xIndex != 0 || m_yIndex != 0) m_serverMonitor = null;
|
||||
|
||||
m_xIndex = 0;
|
||||
m_yIndex = 0;
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
|
||||
// Determine if we actually need a monitor. In order to do this, simply check if
|
||||
// any component monitor been wrapped as a peripheral. Whilst this flag may be
|
||||
// out of date,
|
||||
boolean needsTerminal = false;
|
||||
terminalCheck:
|
||||
for( int x = 0; x < width; x++ )
|
||||
{
|
||||
for( int x=0; x<width; ++x )
|
||||
for( int y = 0; y < height; y++ )
|
||||
{
|
||||
TileMonitor monitor = getSimilarMonitorAt(
|
||||
pos.offset( right, x ).offset( down, y )
|
||||
);
|
||||
if( monitor != null )
|
||||
TileMonitor monitor = getNeighbour( x, y );
|
||||
if( monitor != null && monitor.m_peripheral != null )
|
||||
{
|
||||
monitor.m_xIndex = x;
|
||||
monitor.m_yIndex = y;
|
||||
monitor.m_width = width;
|
||||
monitor.m_height = height;
|
||||
monitor.updateBlock();
|
||||
if( x != 0 || y != 0 )
|
||||
{
|
||||
monitor.destroyTerminal();
|
||||
}
|
||||
needsTerminal = true;
|
||||
break terminalCheck;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild this terminal (will invoke resize events)
|
||||
rebuildTerminal();
|
||||
// Either delete the current monitor or sync a new one.
|
||||
if( needsTerminal )
|
||||
{
|
||||
if( m_serverMonitor == null ) m_serverMonitor = new ServerMonitor( m_advanced, this );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_serverMonitor = null;
|
||||
}
|
||||
|
||||
// Update the terminal's width and height and rebuild it. This ensures the monitor
|
||||
// is consistent when syncing it to other monitors.
|
||||
if( m_serverMonitor != null ) m_serverMonitor.rebuild();
|
||||
|
||||
// Update the other monitors, setting coordinates, dimensions and the server terminal
|
||||
for( int x = 0; x < width; x++ )
|
||||
{
|
||||
for( int y = 0; y < height; y++ )
|
||||
{
|
||||
TileMonitor monitor = getNeighbour( x, y );
|
||||
if( monitor == null ) continue;
|
||||
|
||||
monitor.m_xIndex = x;
|
||||
monitor.m_yIndex = y;
|
||||
monitor.m_width = width;
|
||||
monitor.m_height = height;
|
||||
monitor.m_serverMonitor = m_serverMonitor;
|
||||
monitor.updateBlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean mergeLeft()
|
||||
@@ -699,51 +710,36 @@ public class TileMonitor extends TilePeripheralBase
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Terminal originTerminal = getTerminal().getTerminal();
|
||||
if( originTerminal == null )
|
||||
|
||||
ServerTerminal serverTerminal = getServerMonitor();
|
||||
if( serverTerminal == null || !serverTerminal.isColour() ) return;
|
||||
|
||||
Terminal originTerminal = serverTerminal.getTerminal();
|
||||
if( originTerminal == null ) return;
|
||||
|
||||
double xCharWidth = (m_width - ((RENDER_BORDER + RENDER_MARGIN) * 2.0)) / originTerminal.getWidth();
|
||||
double yCharHeight = (m_height - ((RENDER_BORDER + RENDER_MARGIN) * 2.0)) / originTerminal.getHeight();
|
||||
|
||||
int xCharPos = (int) Math.min( originTerminal.getWidth(), Math.max( ((pair.x - RENDER_BORDER - RENDER_MARGIN) / xCharWidth) + 1.0, 1.0 ) );
|
||||
int yCharPos = (int) Math.min( originTerminal.getHeight(), Math.max( ((pair.y - RENDER_BORDER - RENDER_MARGIN) / yCharHeight) + 1.0, 1.0 ) );
|
||||
|
||||
for( int y = 0; y < m_height; ++y )
|
||||
{
|
||||
return;
|
||||
}
|
||||
if( !getTerminal().isColour() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
double xCharWidth = (m_width - ((RENDER_BORDER + RENDER_MARGIN) * 2.0)) / (originTerminal.getWidth());
|
||||
double yCharHeight = (m_height - ((RENDER_BORDER + RENDER_MARGIN) * 2.0)) / (originTerminal.getHeight());
|
||||
|
||||
int xCharPos = (int)Math.min(originTerminal.getWidth(), Math.max(((pair.x - RENDER_BORDER - RENDER_MARGIN) / xCharWidth) + 1.0, 1.0));
|
||||
int yCharPos = (int)Math.min(originTerminal.getHeight(), Math.max(((pair.y - RENDER_BORDER - RENDER_MARGIN) / yCharHeight) + 1.0, 1.0));
|
||||
|
||||
for( int y=0; y<m_height; ++y )
|
||||
{
|
||||
for( int x=0; x<m_width; ++x )
|
||||
for( int x = 0; x < m_width; ++x )
|
||||
{
|
||||
TileMonitor monitor = getNeighbour( x, y );
|
||||
if( monitor != null )
|
||||
if( monitor == null )continue;
|
||||
|
||||
for( IComputerAccess computer : monitor.m_computers )
|
||||
{
|
||||
monitor.queueTouchEvent(xCharPos, yCharPos);
|
||||
computer.queueEvent( "monitor_touch", new Object[] {
|
||||
computer.getAttachmentName(), xCharPos, yCharPos
|
||||
} );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void queueTouchEvent( int xCharPos, int yCharPos )
|
||||
{
|
||||
for( IComputerAccess computer : m_computers )
|
||||
{
|
||||
computer.queueEvent( "monitor_touch", new Object[] {
|
||||
computer.getAttachmentName(), xCharPos, yCharPos
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
private void queueSizeChangedEvent()
|
||||
{
|
||||
m_sizeChangedQueued = true;
|
||||
}
|
||||
|
||||
|
||||
private XYPair convertToXY( float xPos, float yPos, float zPos, int side )
|
||||
{
|
||||
switch (side)
|
||||
@@ -781,18 +777,7 @@ public class TileMonitor extends TilePeripheralBase
|
||||
{
|
||||
synchronized( this )
|
||||
{
|
||||
if( m_computers.size() == 0 )
|
||||
{
|
||||
TileMonitor origin = getOrigin();
|
||||
if( origin != null )
|
||||
{
|
||||
origin.rebuildTerminal();
|
||||
}
|
||||
}
|
||||
if( !m_computers.contains(computer) )
|
||||
{
|
||||
m_computers.add(computer);
|
||||
}
|
||||
m_computers.add( computer );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -800,10 +785,7 @@ public class TileMonitor extends TilePeripheralBase
|
||||
{
|
||||
synchronized( this )
|
||||
{
|
||||
if( m_computers.contains(computer) )
|
||||
{
|
||||
m_computers.remove(computer);
|
||||
}
|
||||
m_computers.remove( computer );
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -145,6 +145,13 @@ public class PrinterPeripheral implements IPeripheral
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Object getTarget()
|
||||
{
|
||||
return m_printer;
|
||||
}
|
||||
|
||||
private Terminal getCurrentPage() throws LuaException
|
||||
{
|
||||
Terminal currentPage = m_printer.getCurrentPage();
|
||||
|
@@ -8,6 +8,7 @@ import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||
import dan200.computercraft.shared.computer.core.ServerComputer;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@@ -138,7 +139,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
||||
{
|
||||
if( this.m_upgrade == upgrade ) return;
|
||||
|
||||
synchronized (this)
|
||||
synchronized( this )
|
||||
{
|
||||
ComputerCraft.Items.pocketComputer.setUpgrade( m_stack, upgrade );
|
||||
if( m_entity instanceof EntityPlayer ) ((EntityPlayer) m_entity).inventory.markDirty();
|
||||
@@ -156,6 +157,9 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
||||
setPosition( entity.getPosition() );
|
||||
}
|
||||
|
||||
// If a new entity has picked it up then rebroadcast the terminal to them
|
||||
if( entity != m_entity && entity instanceof EntityPlayerMP ) markTerminalChanged();
|
||||
|
||||
m_entity = entity;
|
||||
m_stack = stack;
|
||||
|
||||
@@ -165,4 +169,19 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
||||
invalidatePeripheral();
|
||||
}
|
||||
}
|
||||
|
||||
public void broadcastState( boolean force )
|
||||
{
|
||||
super.broadcastState( force );
|
||||
|
||||
if( (hasTerminalChanged() || force) && m_entity instanceof EntityPlayerMP )
|
||||
{
|
||||
// Broadcast the state to the current entity if they're not already interacting with it.
|
||||
EntityPlayerMP player = (EntityPlayerMP) m_entity;
|
||||
if( player.connection != null && !isInteracting( player ) )
|
||||
{
|
||||
ComputerCraft.sendToPlayer( player, createTerminalPacket() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -371,6 +371,15 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia, I
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack withFamily( @Nonnull ItemStack stack, @Nonnull ComputerFamily family )
|
||||
{
|
||||
return PocketComputerItemFactory.create(
|
||||
getComputerID( stack ), getLabel( stack ), getColour( stack ),
|
||||
family, getUpgrade( stack )
|
||||
);
|
||||
}
|
||||
|
||||
// IMedia
|
||||
|
||||
@Override
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user