1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-15 22:17:39 +00:00

Compare commits

...

72 Commits

Author SHA1 Message Date
SquidDev
ebb50cba48 Bump version 2018-03-15 17:43:23 +00:00
SquidDev
7c218361d9 Merge pull request #528 from SquidDev-CC/ComputerCraft/feature/computer-upgrade
Add recipes to upgrade computers
2018-03-15 17:39:17 +00:00
SquidDev
bb2eab0bed Use the ore dictionary for upgrade recipes
See #517 for motivation
2018-03-15 17:34:58 +00:00
SquidDev
e8c0cf3857 Convert TurtleRecipe to use ComputerConvertRecipe
The two recipes are pretty similar, so this allows us to substantially
simplify the code. This now introduces the additional requirement that
computers must be used to create turtles, rather than pocket computers
or another turtle.
2018-03-13 14:21:39 +00:00
SquidDev
db825a7aab Add recipes to convert computer items into their upgraded counterparts
This adds IComputerItem.withFamily(ItemStack, ComputerFamily) as well as
a ComputerFamilyRecipe class. Each type of computer (normal, turtle,
pocket) defines a recipe using this class, as they require a different
number of gold ingots to upgrade.
2018-03-13 14:14:36 +00:00
SquidDev
dbcae810f0 Fix monitor.getTextScale() being doubled
Closes #16. The original method divided by 2 in the getter, but that
was removed in our monitor rewrite.
2018-03-10 22:16:15 +00:00
SquidDev
f753513289 Wrap ComputerCraft fake player with brackets
Hopefully this'll make GriefPrevention play a little nicer
2018-03-01 15:58:09 +00:00
SquidDev
7bb8efed1d Dispose of monitor display lists when unloading worlds
This means all display lists are correctly deleted when changing
dimensions/exiting to main menu.
2018-02-24 20:37:14 +00:00
SquidDev
0cec4aee8c Merge pull request #15 from Lignum/maybe-fix-monitors
Maybe fix monitors
2018-02-24 16:59:21 +00:00
Lignum
e0c9dc24e7 Generate individual display lists instead of relying on their names being contiguous 2018-02-24 17:21:12 +01:00
SquidDev
244907a39a Various cherry picks from world thread safety
- Ensure monitor draw lists are not reused after deleting them.
 - Make terminal methods synchronized, avoding NPEs and IOOBEs when
   resizing.
2018-02-22 21:20:08 +00:00
SquidDev
9be61abd6b Merge pull request #5 from SquidDev-CC/feature/network-api
Well, how badly can this go?
2018-02-21 15:47:15 +00:00
SquidDev
922f424a78 Add full block wired modems
These act similarly to conventional wired modems, but with the advantage
that they are a full block. This means they can be attached to
peripherals which are not solid (such as chests). Further more, as they
do not have a direction, they allow wrapping peripherals on all 6 sides.

It's worth noting that wired modems do not require a cable - they will
automatically form connections to adjacent network elements when placed.
2018-02-21 15:40:08 +00:00
SquidDev
5c7828dd79 Convert TileCable to use the wired network API
There are several important things to note here:

 - The network element is associated with the cable, whilst the
   peripheral (and so packet sender/receiver) is associated with the
   modem. This allows us to have the main element be in the centre of
   the cable block, whilst the modem is in the centre of the adjacent
   computer.

 - Cables will connect to any adjacent network element, not just
   other cables.

 - Rednet messages are now sent on the computer thread, rather than the
   cable tick.
2018-02-21 15:35:38 +00:00
SquidDev
74f5093d2a Add the default implementation of wired networks 2018-02-21 15:29:34 +00:00
SquidDev
4651e362c9 Add an API for wired networks
The API is composed of three primary classes:

- IWiredElement: Represents some physical entity in the network. This
   will generally be a block (such as a cable or modem), but it is not
   required to be.

   Each element can provide a series of peripherals, which will be
   exposed to other elements on the network.

- IWiredNode: Every wired element has a unique wired node. This acts
   as a thread-safe proxy for communicating with the rest of the
   network (such as sending packets). Each node is also its own packet
   network.

- IWiredNetwork: This is responsible for keeping track of nodes and
   peripherals in the network. It provides methods for forming and
   breaking connections, correctly joining and splitting networks where
   needed.

Tiles which wish to be part of a wired network should implement
IWiredElementTile or register a custom IWiredProvider. When loaded into
the world, it should connect to adjacent nodes. Similarly, when removed
(either due to being broken or chunk unloads), it should break those
connections.

There is no method to query the layout of the network, as that offers
greater flexibility in changing or extending the implementation later
on.
2018-02-21 15:26:13 +00:00
SquidDev
a2e2a5cb37 Add the concept of "available peripherals" to IComputerAccess
This provides a mechanism for peripherals to see what else a computer is
connected to - and then interact with those peripherals.

We also add the ability to query what block or tile a peripheral
targets. This allows one to interact with the original block of adjacent
peripherals instead.
2018-02-21 15:25:20 +00:00
SquidDev
15a3882016 Fix monitor clear state being cleared without a redraw 2018-02-21 14:32:53 +00:00
SquidDev
d3ecd5214b Merge remote-tracking branch 'SquidDev-CC-ComputerCraft/feature/turtle-event' 2018-02-16 10:48:43 +00:00
SquidDev
0a8a8a742e Add config options to disable various turtle actions
Mostly intended for those people who don't like .inspect() or
.getItemDetail(), but could allow modpacks to block equipping upgrades,
placing blocks, etc...
2018-02-16 10:37:47 +00:00
SquidDev
ecff23d027 Add turtle events
The main aim of this is to allow for greater extensibility for other
mods. For instance, you can now prevent turtles placing dirt blocks, or
turning when on gravel.
2018-02-16 10:33:32 +00:00
SquidDev
20dcb32bae Add command to reload config from disk
Also bump version number, as we're relatively close to a release and
it's frustrating having to bump it when putting out previews.
2018-02-16 09:33:40 +00:00
SquidDev
678462d2db Minor fixes to the monitor rewrite 2018-02-15 20:49:34 +00:00
SquidDev
61fdfec09b Further overrides for wireless modem blockiness 2018-02-15 18:17:38 +00:00
SquidDev
2d3cd5dc80 Merge pull request #521 from SquidDev-CC/ComputerCraft/hotfix/modem-full-block
Fix wireless modems suffocating entities
2018-02-15 18:00:58 +00:00
SquidDev
5208ad0b98 Fix wireless modems suffocating entities
As of #458, BlockPeripheral will act as a full/opaque block for some
peripherals and a transparent one for others. However, some Block
methods use the default state rather than the current one. This means
modems report being a full block when they are not, leading to
suffocating entities and lighting glitches.
2018-02-15 17:56:08 +00:00
SquidDev
662fb96beb Overhaul monitor's terminal code
This restructures monitor in order to make it thread-safe: namely
removing any world interaction from the computer thread.

Instead of each monitor having their own terminal, resize flag, etc...
we use a monitor "multiblock" object. This is constructed on the origin
monitor and propagated to other monitors when required.

We attempt to construct the multiblock object (and so the corresponding
terminal) as lazily as posible. Consequently, we only create the
terminal when fetching the peripheral (not when attaching, as that is
done on the computer thread).

If a monitor is resized (say due to placing/breaking a monitor) then we
will invalidate all references to the multiblock object, construct a new
one if required, and propagate it to all component monitors.

This commit also fixes several instances of glLists not being deleted
after use. It is not a comprehensive fix, but that is outside the scope
of this commit.
2018-02-14 21:30:07 +00:00
SquidDev
4c14431a3d Various improvements to command system
- Ensure usage is consistent
 - Allow computer selectors to return multiple values
 - Fix commands being marked as usable when it isn't
 - Add /computercraft turn-on, a counter to /computercraft shutdown
2018-02-13 11:45:13 +00:00
SquidDev
5ae38a3f18 Merge pull request #520 from SquidDev-CC/ComputerCraft/hotfix/turtle-world-border
Prevent turtles moving beyond the world border
2018-02-13 11:21:55 +00:00
SquidDev
94e10d1f67 Prevent turtles moving beyond the world border
As tiles outside the world border are not ticked, turtles are rendered
entirely useless. Furthermore, the turtle animation will never progress
resulting in visual glitches.

In order to avoid this, we ensure the target position is within the
world border when moving to it.
2018-02-12 17:50:46 +00:00
SquidDev
0a50676884 Fix turtle owner not being persisted 2018-02-10 16:10:16 +00:00
SquidDev
41cce78fcb Merge pull request #518 from SquidDev-CC/ComputerCraft/feature/owner-tracking
Track which player "owns" a turtle
2018-02-05 11:07:06 +00:00
SquidDev
4c0fa1fabe Track which player "owns" a turtle
When a player places a turtle, they are marked as its owner. Any actions
they perform (such as breaking blocks, moving, etc...) are performed
using this player's game profile.

This allows turtles to work correctly with various permissions mods.
Previously you would have to whitelist all turtles in order for them to
function within a claim.
2018-02-04 21:35:21 +00:00
SquidDev
54e1dafa3f Merge pull request #517 from SquidDev-CC/ComputerCraft/feature/ore-dict
Add ore dictionary support to all recipes
2018-02-04 20:06:34 +00:00
SquidDev
3ac76bc05b Add ore dictionary support to all recipes 2018-02-04 20:02:12 +00:00
SquidDev
83030df3ee Add computer performance monitor 2018-02-02 13:34:27 +00:00
SquidDev
07d15caf6f Bump Cobalt version 2018-02-02 12:26:21 +00:00
SquidDev
3298efe652 Prevent computer dump command sending too much information 2018-01-20 11:07:09 +00:00
SquidDev
01d9919a3e Merge pull request #508 from SquidDev-CC/ComputerCraft/hotfix/turtle-speaker-model
Fix turtle speaker upgrade's missing texture
2018-01-19 13:10:20 +00:00
SquidDev
80b1170b63 Extract required textures from models instead
This ensures we will not get missing texture errors in the future, and
allows resource pack artists to use additional textures.
2018-01-19 13:04:50 +00:00
SquidDev
2e7302e654 Fix turtle speaker upgrade's missing texture
The sprite was not registered into the atlas, meaning it rendered the
missing texture instead.
2018-01-19 12:42:32 +00:00
SquidDev
ca7fb8a0b4 Cache turtle family within the tile
This means one can call .getFamily() in a thread-safe manner, ensuring
turtle.getFuelLimit() does not cause issues. As we use a specialist
TE class for each family this does not require any specialist caching.
2018-01-18 13:06:34 +00:00
SquidDev
c9b0894f26 Cache direction of modems within the tile
This ensures the world is not accessed from another thread.

Closes #410
2018-01-18 13:06:11 +00:00
SquidDev
c3454a195d Merge branch 'master' of https://github.com/dan200/ComputerCraft 2018-01-15 17:18:48 +00:00
Daniel Ratcliffe
3b3dd8071b Merge pull request #506 from Wojbie/Advanced-Monitor-Count-Fix
Fix advanced monitor recipe.
2018-01-15 12:59:35 +00:00
Wojbie
0d28c67534 Fix advanced monitor recipe.
Fix recipe to create 4 monitors.
2018-01-14 23:03:14 +01:00
SquidDev
d0af85754a Merge branch 'master' of https://github.com/dan200/ComputerCraft 2018-01-13 10:49:07 +00:00
Daniel Ratcliffe
3e265c27ff Merge pull request #455 from Wilma456/fileread
Add read() to Filehandle
2018-01-13 00:58:19 +00:00
Daniel Ratcliffe
8d356f50c4 Merge pull request #440 from Wilma456/iomulti
Make io.write() accept multiple args
2018-01-13 00:48:07 +00:00
Daniel Ratcliffe
f30c4f16c0 Merge pull request #411 from Wilma456/copyfixup
Fix Bug in copy.lua, mkdir.lua and rename.lua (updated)
2018-01-13 00:32:55 +00:00
Daniel Ratcliffe
8bb8caa315 Merge pull request #448 from Wilma456/writecheck
Fix check of write()
2018-01-13 00:28:08 +00:00
SquidDev
0f17a3d72e Merge branch 'master' of https://github.com/dan200/ComputerCraft 2018-01-12 14:15:22 +00:00
Daniel Ratcliffe
7647369e2d Merge pull request #446 from Wilma456/moduledir
Add folder /rom/modules
2018-01-12 14:10:50 +00:00
Daniel Ratcliffe
4b4208e724 Merge pull request #476 from SquidDev-CC/hotfix/printer-clear
Fix the printer overwriting the current page
2018-01-12 13:59:51 +00:00
Daniel Ratcliffe
2a16a1df85 Merge pull request #486 from Wilma456/extensionfix
Fix Bug in Paint and Edit
2018-01-12 13:59:15 +00:00
Daniel Ratcliffe
25f7c58400 Merge pull request #494 from SquidDev-CC/hotfix/collision-aabb
Fix getCollisionBoundingBox not using all AABBs
2018-01-12 13:57:50 +00:00
Daniel Ratcliffe
c3db91f11f Merge pull request #485 from Luca0208/patch-1
Removed the "the" that was too much(In /rom/help/cd.txt)
2018-01-12 13:56:48 +00:00
Daniel Ratcliffe
8c66ce03d4 Merge pull request #475 from Wilma456/ioline
Fix io.lines()
2018-01-12 13:56:21 +00:00
Daniel Ratcliffe
2be2a0625e Merge pull request #502 from SquidDev-CC/hotfix/missing-overlay
Fix turtle overlay not being rendered in items
2018-01-12 13:54:59 +00:00
Daniel Ratcliffe
c904d5041b Merge pull request #499 from SquidDev-CC/hotfix/null-network
Fix ComputerCraftAPI.getWirelessNetwork() failing
2018-01-12 13:54:33 +00:00
SquidDev
632762768e Add workaround for incorrect overload of getDrops being overridden
Closes #2
2017-12-30 19:03:32 +00:00
SquidDev
c69ba205f8 Merge pull request #502 from SquidDev-CC/ComputerCraft/hotfix/missing-overlay
Fix turtle overlay not being rendered in items
2017-12-24 21:49:01 +00:00
SquidDev
019f4dbea9 Fix turtle overlay not being rendered in items 2017-12-24 21:44:55 +00:00
SquidDev
259ea41ce3 Merge pull request #499 from SquidDev-CC/ComputerCraft/hotfix/null-network
Fix ComputerCraftAPI.getWirelessNetwork() failing
2017-12-10 15:39:59 +00:00
SquidDev
11290f7204 Fix ComputerCraftAPI.getWirelessNetwork() failing
I've got to admit, it is super embarrassing that a) I didn't notice this
when testing and b) no one else has noticed until now.
2017-12-10 15:37:40 +00:00
Wilma456
4fb0240a36 Changes suggested by SquidDev and update help file 2017-09-24 17:36:20 +02:00
Wilma456 (Jakob0815)
f20a7afa7f Better Code 2017-09-18 15:22:44 +02:00
Wilma456
5be2202b2e Add read() to Filehandle 2017-09-16 16:06:27 +02:00
Wilma456 (Jakob0815)
b8630f739a Add Check requested by dan200 2017-09-13 19:21:17 +02:00
Wilma456
1415dd0dae Changes requested by dan200 2017-09-12 20:43:07 +02:00
Wilma456
5989d021c7 Add folder /rom/modules 2017-09-12 16:44:22 +02:00
Wilma456
90c4ebd208 Fix Bug in copy.lua, mkdir.lua and rename.lua 2017-08-09 19:32:29 +02:00
134 changed files with 5515 additions and 1294 deletions

View File

@@ -21,8 +21,7 @@ plugins {
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'org.ajoberstar.grgit'
version = "1.80pr1.3"
version = "1.80pr1.5"
group = "org.squiddev"
archivesBaseName = "cc-tweaked"
@@ -59,7 +58,9 @@ configurations {
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'
shade 'org.squiddev:Cobalt:0.3.1'
testCompile 'junit:junit:4.11'
}
javadoc {

View File

@@ -7,18 +7,23 @@
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.network.wired.IWiredProvider;
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;
@@ -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,7 @@ 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.WiredNode;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
@@ -67,6 +74,7 @@ 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;
@@ -140,6 +148,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 +181,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 +232,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;
@@ -255,6 +266,7 @@ public class ComputerCraft
private static List<ITurtlePermissionProvider> permissionProviders = new ArrayList<>();
private static final Map<String, IPocketUpgrade> pocketUpgrades = new HashMap<>();
private static final Set<ILuaAPIFactory> apiFactories = new LinkedHashSet<>();
private static final Set<IWiredProvider> wiredProviders = new LinkedHashSet<>();
// Implementation
@Mod.Instance( value = ComputerCraft.MOD_ID )
@@ -277,6 +289,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 +389,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 +401,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 +432,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();
@@ -471,11 +505,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 +733,16 @@ public class ComputerCraft
}
}
public static void registerWiredProvider( IWiredProvider provider )
{
if( provider != null ) wiredProviders.add( provider );
}
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 +764,24 @@ public class ComputerCraft
return null;
}
public static IWiredElement getWiredElementAt( IBlockAccess world, BlockPos pos, EnumFacing side )
{
// Try the handlers in order:
for( IWiredProvider provider : wiredProviders )
{
try
{
IWiredElement element = provider.getElement( world, pos, side );
if( element != null ) return element;
}
catch( Exception e )
{
ComputerCraft.log.error( "Wired element provider " + provider + " errored.", e );
}
}
return null;
}
public static int getDefaultBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side )
{
if( WorldUtil.isBlockInWorld( world, pos ) )
@@ -826,7 +883,7 @@ public class ComputerCraft
return upgrades;
}
public IPacketNetwork getWirelessNetwork()
public static IPacketNetwork getWirelessNetwork()
{
return WirelessNetwork.getUniversal();
}

View File

@@ -12,6 +12,9 @@ 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.network.wired.IWiredProvider;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;
@@ -21,6 +24,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 +332,81 @@ public final class ComputerCraftAPI
}
}
/**
* Registers a peripheral handler to convert blocks into {@link IPeripheral} implementations.
*
* @param handler The peripheral provider to register.
* @see dan200.computercraft.api.peripheral.IPeripheral
* @see dan200.computercraft.api.peripheral.IPeripheralProvider
*/
public static void registerWiredProvider( @Nonnull IWiredProvider handler )
{
findCC();
if ( computerCraft_registerWiredProvider != null)
{
try {
computerCraft_registerWiredProvider.invoke( null, handler );
} catch (Exception e){
// It failed
}
}
}
/**
* 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 +453,15 @@ public final class ComputerCraftAPI
computerCraft_registerAPIFactory = findCCMethod( "registerAPIFactory", new Class<?>[] {
ILuaAPIFactory.class
} );
computerCraft_registerWiredProvider = findCCMethod( "registerWiredProvider", new Class<?>[] {
IWiredProvider.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 +499,7 @@ 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_registerWiredProvider = null;
private static Method computerCraft_createWiredNodeForElement = null;
private static Method computerCraft_getWiredElementAt = null;
}

View File

@@ -0,0 +1,51 @@
package dan200.computercraft.api.network.wired;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;
/**
* 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. One should either register an {@link IWiredProvider}
* or implement {@link IWiredElementTile} on your tile entity.
*
* @see IWiredProvider
* @see ComputerCraftAPI#registerWiredProvider(IWiredProvider)
* @see IWiredElementTile
*/
public interface IWiredElement extends IWiredSender
{
/**
* Fetch the peripherals this network element provides.
*
* This is only called when initially attaching to a network and after a call to {@link IWiredNode#invalidate()}}, so
* one does not <em>need</em> to cache the return value.
*
* @return The peripherals this node provides.
* @see IWiredNode#invalidate()
*/
@Nonnull
default Map<String, IPeripheral> getPeripherals()
{
return Collections.emptyMap();
}
/**
* 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 )
{
}
}

View File

@@ -0,0 +1,22 @@
package dan200.computercraft.api.network.wired;
import net.minecraft.util.EnumFacing;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A {@link net.minecraft.tileentity.TileEntity} which provides a {@link IWiredElement}. This acts
* as a simpler alternative to a full-blown {@link IWiredProvider}.
*/
public interface IWiredElementTile
{
/**
* Get the wired element of this tile for a given side.
*
* @param side The side to get the network element from.
* @return A network element, or {@code null} if there is no element here.
*/
@Nullable
IWiredElement getWiredElement( @Nonnull EnumFacing side );
}

View File

@@ -0,0 +1,74 @@
package dan200.computercraft.api.network.wired;
import javax.annotation.Nonnull;
/**
* 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.
*
* @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 );
/**
* Mark this node's peripherals as having changed.
*
* This should only be used on the server thread.
*
* @param node The node to mark as invalid.
* @throws IllegalArgumentException If the node is not in the network.
* @see IWiredElement#getPeripherals()
*/
void invalidate( @Nonnull IWiredNode node );
}

View File

@@ -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();
}

View File

@@ -0,0 +1,98 @@
package dan200.computercraft.api.network.wired;
import dan200.computercraft.api.network.IPacketNetwork;
import javax.annotation.Nonnull;
/**
* 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.
*
* @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.
*
* @see IWiredElement#getPeripherals()
*/
default void invalidate()
{
getNetwork().invalidate( this );
}
}

View File

@@ -0,0 +1,29 @@
package dan200.computercraft.api.network.wired;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Fetch or create an {@link IWiredElement} for a block at a given position.
*
* @see dan200.computercraft.api.ComputerCraftAPI#registerWiredProvider(IWiredProvider)
* @see IWiredElementTile
*/
@FunctionalInterface
public interface IWiredProvider
{
/**
* Extract a wired network element from a block location.
*
* @param world The world the block is in.
* @param pos The position the block is at.
* @param side The side to get the network element from.
* @return A network element, or {@code null} if there is not an element here you'd like to handle.
*/
@Nullable
IWiredElement getElement( @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing side );
}

View File

@@ -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();
}

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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.
*

View File

@@ -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
*/

View File

@@ -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.

View File

@@ -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,
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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 );
}
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}
}

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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 );
}

View File

@@ -25,6 +25,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;
@@ -35,7 +36,6 @@ import dan200.computercraft.shared.turtle.entity.TurtleVisionCamera;
import dan200.computercraft.shared.util.Colour;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.ModelBakery;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
@@ -57,6 +57,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;
@@ -115,6 +116,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 +298,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()
{
@@ -551,6 +547,15 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
m_renderFrame++;
}
}
@SubscribeEvent
public void onWorldUnload( WorldEvent.Unload event )
{
if( event.getWorld().isRemote )
{
ClientMonitor.destroyAll();
}
}
}
@SideOnly(Side.CLIENT)

View File

@@ -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();

View File

@@ -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;
@@ -43,24 +43,22 @@ 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();
if( originTerminal == null ) return;
TileMonitor origin = originTerminal.getOrigin();
// Ensure each monitor is rendered only once
long renderFrame = ComputerCraft.getRenderFrame();
if( origin.m_lastRenderFrame == renderFrame )
if( originTerminal.lastRenderFrame == renderFrame )
{
return;
}
else
{
origin.m_lastRenderFrame = renderFrame;
originTerminal.lastRenderFrame = renderFrame;
}
boolean redraw = origin.pollChanged();
BlockPos monitorPos = monitor.getPos();
BlockPos originPos = origin.getPos();
posX += originPos.getX() - monitorPos.getX();
@@ -94,9 +92,7 @@ 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 );
@@ -104,24 +100,25 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
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 +132,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 +146,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 +171,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 +179,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 +199,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 +207,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 +233,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
}
if( ComputerCraft.getGlobalCursorBlink() )
{
GlStateManager.callList( origin.m_renderDisplayList + 2 );
GlStateManager.callList( originTerminal.renderDisplayLists[2] );
GlStateManager.resetColor();
}
}

View File

@@ -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;

View File

@@ -15,10 +15,11 @@ 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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -180,10 +181,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 +325,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 +351,6 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
wrapper.detach();
}
}
m_fileSystem = null;
}
}

View File

@@ -195,6 +195,8 @@ public class ComputerThread
thread.start();
}
long start = System.nanoTime();
// Execute the task
runner.submit( task );
@@ -229,6 +231,10 @@ public class ComputerThread
}
finally
{
long stop = System.nanoTime();
Computer computer = task.getOwner();
if( computer != null ) ComputerTimeTracker.addTiming( computer, stop - start );
// Re-add it back onto the queue or remove it
synchronized( s_taskLock )
{

View File

@@ -0,0 +1,116 @@
package dan200.computercraft.core.computer;
import com.google.common.collect.MapMaker;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
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 ComputerTimeTracker
{
public static class Timings
{
private final WeakReference<Computer> computer;
private final int computerId;
private int tasks;
private long totalTime;
private long maxTime;
public Timings( @Nonnull Computer computer )
{
this.computer = new WeakReference<>( computer );
this.computerId = computer.getID();
}
@Nullable
public Computer getComputer()
{
return computer.get();
}
public int getComputerId()
{
return computerId;
}
public int getTasks()
{
return tasks;
}
public long getTotalTime()
{
return totalTime;
}
public long getMaxTime()
{
return maxTime;
}
public double getAverage()
{
return totalTime / (double) tasks;
}
void update( long time )
{
tasks++;
totalTime += time;
if( time > maxTime ) maxTime = time;
}
}
private static boolean tracking;
private static final List<Timings> timings = new ArrayList<>();
private static final Map<Computer, Timings> timingLookup = new MapMaker().weakKeys().makeMap();
public synchronized static void start()
{
tracking = true;
timings.clear();
timingLookup.clear();
}
public synchronized static boolean stop()
{
if( !tracking ) return false;
tracking = false;
timingLookup.clear();
return true;
}
public static synchronized List<Timings> getTimings()
{
return new ArrayList<>( timings );
}
public static synchronized void addTiming( Computer computer, long time )
{
if( !tracking ) return;
Timings timings = ComputerTimeTracker.timingLookup.get( computer );
if( timings == null )
{
timings = new Timings( computer );
timingLookup.put( computer, timings );
ComputerTimeTracker.timings.add( timings );
}
timings.update( time );
}
}

View File

@@ -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" );

View File

@@ -1,22 +1,25 @@
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.computer.ComputerTimeTracker;
import dan200.computercraft.shared.command.framework.*;
import dan200.computercraft.shared.computer.core.ServerComputer;
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 net.minecraftforge.common.util.FakePlayer;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.*;
import static dan200.computercraft.shared.command.framework.ChatHelpers.*;
@@ -37,8 +40,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
@@ -48,8 +51,35 @@ public final class CommandComputerCraft extends CommandDelegate
{
TextTable table = new TextTable( "Instance", "Id", "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 ),
@@ -57,8 +87,6 @@ public final class CommandComputerCraft extends CommandDelegate
bool( computer.isOn() ),
linkPosition( context, computer )
);
if( max-- < 0 ) break;
}
table.displayTo( context.getSender() );
@@ -80,7 +108,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 +126,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 +166,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,36 +263,103 @@ 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 )
{
ComputerTimeTracker.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
{
if( !ComputerTimeTracker.stop() ) throw new CommandException( "Tracking not enabled" );
displayTimings( context );
}
} );
track.register( new SubCommandBase(
"dump", "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
{
displayTimings( context );
}
} );
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;
@@ -253,4 +389,63 @@ public final class CommandComputerCraft extends CommandDelegate
return position( computer.getPosition() );
}
}
private static void displayTimings( CommandContext context ) throws CommandException
{
List<ComputerTimeTracker.Timings> timings = ComputerTimeTracker.getTimings();
if( timings.isEmpty() ) throw new CommandException( "No timings available" );
timings.sort( Comparator.comparing( ComputerTimeTracker.Timings::getAverage ).reversed() );
TextTable table = new TextTable( "Computer", "Tasks", "Total", "Average", "Maximum" );
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();
boolean isPlayer = sender instanceof EntityPlayerMP && !(sender instanceof FakePlayer);
for( ComputerTimeTracker.Timings entry : timings )
{
Computer computer = entry.getComputer();
ServerComputer serverComputer = computer == null ? null : lookup.get( computer );
ITextComponent computerComponent = new TextComponentString( "" )
.appendSibling( serverComputer == null ? text( "?" ) : linkComputer( serverComputer ) )
.appendText( " (id " + entry.getComputerId() + ")" );
if( serverComputer != null && UserLevel.OP.canExecute( context ) && isPlayer )
{
computerComponent
.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"
) );
}
table.addRow(
computerComponent,
formatted( "%4d", entry.getTasks() ),
text( String.format( "%7.1f", entry.getTotalTime() / 1e6 ) + "ms" ),
text( String.format( "%4.1f", entry.getAverage() / 1e6 ) + "ms" ),
text( String.format( "%5.1f", entry.getMaxTime() / 1e6 ) + "ms" )
);
}
table.displayTo( context.getSender() );
}
}

View File

@@ -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 > 1 ) 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 );
}
}
}

View File

@@ -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

View File

@@ -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() ) );

View File

@@ -18,7 +18,7 @@ import static dan200.computercraft.shared.command.framework.ChatHelpers.text;
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[] {
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,
@@ -55,7 +55,7 @@ public class TextTable
}
else if( CHARACTERS.indexOf( character ) != -1 )
{
return CHAR_WIDTHS[ character ];
return CHAR_WIDTHS[character];
}
else
{
@@ -108,10 +108,10 @@ public class TextTable
public TextTable( @Nonnull String... header )
{
this.header = new ITextComponent[ header.length ];
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;
}
@@ -136,27 +136,32 @@ public class TextTable
final int maxWidth = getMaxWidth( sender );
int[] minWidths = new int[ columns ];
int[] maxWidths = new int[ columns ];
int[] rowWidths = new int[ columns ];
int[] minWidths = new int[columns];
int[] maxWidths = new int[columns];
int[] rowWidths = new int[columns];
if( header != null )
{
for( int i = 0; i < columns; i++ )
{
maxWidths[ i ] = minWidths[ i ] = getWidth( header[ i ], sender );
maxWidths[i] = minWidths[i] = getWidth( header[i], sender );
}
}
for( ITextComponent[] row : rows )
// Limit the number of rows to something sensible.
int limit = isPlayer( sender ) ? 30 : 100;
if( limit > rows.size() ) limit = rows.size();
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 ] )
int width = getWidth( row[i], sender );
rowWidths[i] += width;
if( width > maxWidths[i] )
{
maxWidths[ i ] = width;
maxWidths[i] = width;
}
}
}
@@ -164,7 +169,7 @@ public class TextTable
// Calculate the average width
for( int i = 0; i < columns; i++ )
{
rowWidths[ i ] = Math.max( rowWidths[ i ], rows.size() );
rowWidths[i] = Math.max( rowWidths[i], rows.size() );
}
int totalWidth = (columns - 1) * getWidth( SEPARATOR, sender );
@@ -179,7 +184,7 @@ public class TextTable
for( int i = 0; i < columns; i++ )
{
if( i != 0 ) out.appendSibling( SEPARATOR );
appendFixed( out, sender, header[ i ], maxWidths[ i ] );
appendFixed( out, sender, header[i], maxWidths[i] );
}
out.appendSibling( LINE );
@@ -190,17 +195,23 @@ public class TextTable
out.appendSibling( LINE );
}
for( int i = 0; i < rows.size(); i++ )
for( int i = 0; i < limit; i++ )
{
ITextComponent[] row = rows.get( i );
if( i != 0 ) out.appendSibling( LINE );
for( int j = 0; j < columns; j++ )
{
if( j != 0 ) out.appendSibling( SEPARATOR );
appendFixed( out, sender, row[ j ], maxWidths[ j ] );
appendFixed( out, sender, row[j], maxWidths[j] );
}
}
if( limit != rows.size() )
{
out.appendSibling( LINE );
out.appendSibling( coloured( (rows.size() - limit) + " additional rows...", TextFormatting.AQUA ) );
}
sender.sendMessage( out );
}

View File

@@ -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

View File

@@ -97,4 +97,11 @@ public class ComputerPeripheral
{
return (other != null && other.getClass() == this.getClass());
}
@Nonnull
@Override
public Object getTarget()
{
return m_computer.getTile();
}
}

View File

@@ -94,6 +94,11 @@ public class ServerComputer extends ServerTerminal
return m_computer.getAPIEnvironment();
}
public Computer getComputer()
{
return m_computer;
}
@Override
public void update()
{

View File

@@ -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);
}

View File

@@ -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 )
{

View File

@@ -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;
}
}

View File

@@ -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 );
}
}
}

View File

@@ -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 );
}
}
}

View File

@@ -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;

View File

@@ -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 )
@@ -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;
}

View File

@@ -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;

View File

@@ -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
@@ -365,6 +343,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 +352,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() ) ) )

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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";
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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 )
{

View File

@@ -196,4 +196,11 @@ public class DiskDrivePeripheral implements IPeripheral
}
return false;
}
@Nonnull
@Override
public Object getTarget()
{
return m_diskDrive;
}
}

View File

@@ -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

View File

@@ -0,0 +1,416 @@
/*
* 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.IWiredElementTile;
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.util.IDAssigner;
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.util.Constants;
import javax.annotation.Nonnull;
import java.io.File;
import java.util.*;
public class TileWiredModemFull extends TilePeripheralBase implements IWiredElementTile
{
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 && !name.equals( m_entity.getCachedPeripheralName( EnumFacing.VALUES[i] ) ) )
{
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 );
}
@Nonnull
@Override
public Map<String, IPeripheral> getPeripherals()
{
return m_entity.getPeripherals();
}
}
private WiredModemPeripheral[] m_modems = new WiredModemPeripheral[6];
private boolean m_peripheralAccessAllowed = false;
private int[] m_attachedPeripheralIDs = new int[6];
private String[] m_attachedPeripheralTypes = new String[6];
private boolean m_destroyed = false;
private boolean m_connectionsFormed = false;
private final WiredModemElement m_element = new FullElement( this );
private final IWiredNode node = m_element.getNode();
public TileWiredModemFull()
{
Arrays.fill( m_attachedPeripheralIDs, -1 );
}
private void remove()
{
if( world == null || !world.isRemote )
{
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 )
{
Map<String, IPeripheral> updated = getPeripherals();
if( updated.isEmpty() )
{
// If there are no peripherals then disable access and update the display state.
m_peripheralAccessAllowed = false;
updateAnim();
}
// Always invalidate the node: it's more accurate than checking if the peripherals
// have changed
node.invalidate();
}
}
@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 = getPeripherals().keySet();
togglePeripheralAccess();
Set<String> periphName = getPeripherals().keySet();
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_attachedPeripheralIDs.length; i++ )
{
if( tag.hasKey( "peripheralID_" + i, Constants.NBT.TAG_ANY_NUMERIC ) )
{
m_attachedPeripheralIDs[i] = tag.getInteger( "peripheralID_" + i );
}
if( tag.hasKey( "peripheralType_" + i, Constants.NBT.TAG_STRING ) )
{
m_attachedPeripheralTypes[i] = tag.getString( "peripheralType_" + i );
}
}
}
@Nonnull
@Override
public NBTTagCompound writeToNBT( NBTTagCompound tag )
{
tag = super.writeToNBT( tag );
tag.setBoolean( "peripheralAccess", m_peripheralAccessAllowed );
for( int i = 0; i < m_attachedPeripheralIDs.length; i++ )
{
if( m_attachedPeripheralIDs[i] >= 0 )
{
tag.setInteger( "peripheralID_" + i, m_attachedPeripheralIDs[i] );
}
if( m_attachedPeripheralTypes[i] != null )
{
tag.setString( "peripheralType_" + i, m_attachedPeripheralTypes[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 )
{
networkChanged();
m_connectionsFormed = true;
}
}
super.update();
}
private void networkChanged()
{
if( getWorld().isRemote ) return;
World world = getWorld();
BlockPos current = getPos();
for( EnumFacing facing : EnumFacing.VALUES )
{
if( !world.isBlockLoaded( pos ) ) continue;
IWiredElement element = ComputerCraft.getWiredElementAt( world, current.offset( facing ), facing.getOpposite() );
if( element == null ) continue;
// If we can connect to it then do so
node.connectTo( element.getNode() );
}
node.invalidate();
}
// private stuff
private void togglePeripheralAccess()
{
if( !m_peripheralAccessAllowed )
{
m_peripheralAccessAllowed = true;
if( getPeripherals().isEmpty() )
{
m_peripheralAccessAllowed = false;
return;
}
}
else
{
m_peripheralAccessAllowed = false;
}
updateAnim();
node.invalidate();
}
@Nonnull
private Map<String, IPeripheral> getPeripherals()
{
if( !m_peripheralAccessAllowed ) return Collections.emptyMap();
Map<String, IPeripheral> peripherals = new HashMap<>( 6 );
for( EnumFacing facing : EnumFacing.VALUES )
{
BlockPos neighbour = getPos().offset( facing );
IPeripheral peripheral = TileCable.getPeripheral( getWorld(), neighbour, facing.getOpposite() );
if( peripheral != null && !(peripheral instanceof WiredModemPeripheral) )
{
String type = peripheral.getType();
int id = m_attachedPeripheralIDs[facing.ordinal()];
String oldType = m_attachedPeripheralTypes[facing.ordinal()];
if( id < 0 || !type.equals( oldType ) )
{
m_attachedPeripheralTypes[facing.ordinal()] = type;
id = m_attachedPeripheralIDs[facing.ordinal()] = IDAssigner.getNextIDFromFile( new File(
ComputerCraft.getWorldDir( getWorld() ),
"computer/lastid_" + type + ".txt"
) );
}
peripherals.put( type + "_" + id, peripheral );
}
}
return peripherals;
}
private String getCachedPeripheralName( EnumFacing facing )
{
if( !m_peripheralAccessAllowed ) return null;
int id = m_attachedPeripheralIDs[facing.ordinal()];
String type = m_attachedPeripheralTypes[facing.ordinal()];
return id < 0 || type == null ? null : type + "_" + id;
}
// IWiredElementTile
@Nonnull
@Override
public IWiredElement getWiredElement( @Nonnull EnumFacing side )
{
return m_element;
}
// IPeripheralTile
@Override
public IPeripheral getPeripheral( EnumFacing side )
{
WiredModemPeripheral peripheral = m_modems[side.ordinal()];
if( peripheral == null )
{
peripheral = m_modems[side.ordinal()] = new WiredModemPeripheral( m_element )
{
@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;
}
}

View File

@@ -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

View File

@@ -0,0 +1,59 @@
package dan200.computercraft.shared.peripheral.modem;
import dan200.computercraft.api.network.wired.IWiredNetworkChange;
import dan200.computercraft.api.network.wired.IWiredElement;
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 );
}

View File

@@ -0,0 +1,410 @@
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 net.minecraft.util.math.Vec3d;
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 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
@Override
public Vec3d getPosition()
{
return modem.getPosition();
}
//endregion
//region IPeripheral
@Nonnull
@Override
public String[] getMethodNames()
{
String[] methods = super.getMethodNames();
String[] newMethods = new String[methods.length + 5];
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";
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 );
}
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 ) )
{
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
{
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 );
}
}
}
}

View File

@@ -0,0 +1,86 @@
package dan200.computercraft.shared.peripheral.monitor;
import dan200.computercraft.shared.common.ClientTerminal;
import net.minecraft.client.renderer.GlStateManager;
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 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();
}
}
}
}

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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 );
}
}

View File

@@ -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();

View File

@@ -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

View File

@@ -8,6 +8,7 @@ package dan200.computercraft.shared.proxy;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.turtle.blocks.BlockTurtle;
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
@@ -465,6 +466,14 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
{
dispatchEntityDrops( event.getEntity(), event.getDrops() );
}
@SubscribeEvent
public void onTurtleAction( TurtleActionEvent event) {
if( ComputerCraft.turtleDisabledActions.contains( event.getAction() ) )
{
event.setCanceled( true, "Action has been disabled" );
}
}
}
private void dispatchEntityDrops( Entity entity, java.util.List<EntityItem> drops )

View File

@@ -35,10 +35,7 @@ import dan200.computercraft.shared.peripheral.commandblock.CommandBlockPeriphera
import dan200.computercraft.shared.peripheral.common.*;
import dan200.computercraft.shared.peripheral.diskdrive.ContainerDiskDrive;
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
import dan200.computercraft.shared.peripheral.modem.BlockAdvancedModem;
import dan200.computercraft.shared.peripheral.modem.TileAdvancedModem;
import dan200.computercraft.shared.peripheral.modem.TileCable;
import dan200.computercraft.shared.peripheral.modem.TileWirelessModem;
import dan200.computercraft.shared.peripheral.modem.*;
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
import dan200.computercraft.shared.peripheral.printer.ContainerPrinter;
import dan200.computercraft.shared.peripheral.printer.TilePrinter;
@@ -52,6 +49,7 @@ import dan200.computercraft.shared.pocket.recipes.PocketComputerUpgradeRecipe;
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
import dan200.computercraft.shared.util.*;
import dan200.computercraft.shared.wired.DefaultWiredProvider;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
@@ -126,11 +124,6 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
@Override
public abstract long getRenderFrame();
@Override
public void deleteDisplayLists( int list, int range )
{
}
@Override
public abstract Object getFixedWidthFontRenderer();
@@ -272,6 +265,10 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
// Command Computer
ComputerCraft.Blocks.advancedModem = new BlockAdvancedModem();
registry.register( ComputerCraft.Blocks.advancedModem.setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "advanced_modem" ) ) );
// Full block modem
ComputerCraft.Blocks.wiredModemFull = new BlockWiredModemFull();
registry.register( ComputerCraft.Blocks.wiredModemFull.setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "wired_modem_full" ) ) );
}
@SubscribeEvent
@@ -291,9 +288,12 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
// Command Computer
registry.register( new ItemCommandComputer( ComputerCraft.Blocks.commandComputer ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "command_computer" ) ) );
// Command Computer
// Advanced modem
registry.register( new ItemAdvancedModem( ComputerCraft.Blocks.advancedModem ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "advanced_modem" ) ) );
// Full block modem
registry.register( new ItemWiredModemFull( ComputerCraft.Blocks.wiredModemFull ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "wired_modem_full" ) ) );
// Items
// Floppy Disk
ComputerCraft.Items.disk = new ItemDiskLegacy();
@@ -469,6 +469,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
GameRegistry.registerTileEntity( TileCommandComputer.class, ComputerCraft.LOWER_ID + " : " + "command_computer" );
GameRegistry.registerTileEntity( TileAdvancedModem.class, ComputerCraft.LOWER_ID + " : " + "advanced_modem" );
GameRegistry.registerTileEntity( TileSpeaker.class, ComputerCraft.LOWER_ID + " : " + "speaker" );
GameRegistry.registerTileEntity( TileWiredModemFull.class, ComputerCraft.LOWER_ID + " : " + "wired_modem_full" );
// Register peripheral providers
ComputerCraftAPI.registerPeripheralProvider( new DefaultPeripheralProvider() );
@@ -482,6 +483,9 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
// Register media providers
ComputerCraftAPI.registerMediaProvider( new DefaultMediaProvider() );
// Register network providers
ComputerCraftAPI.registerWiredProvider( new DefaultWiredProvider() );
}
private void registerForgeHandlers()

View File

@@ -32,7 +32,6 @@ public interface IComputerCraftProxy
boolean getGlobalCursorBlink();
long getRenderFrame();
void deleteDisplayLists( int list, int range );
Object getFixedWidthFontRenderer();
String getRecordInfo( @Nonnull ItemStack item );

View File

@@ -12,10 +12,13 @@ import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.shared.turtle.core.*;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull;
import java.util.HashMap;
@@ -424,6 +427,13 @@ public class TurtleAPI implements ILuaAPI
table.put( "name", name );
table.put( "damage", damage );
table.put( "count", count );
TurtleActionEvent event = new TurtleActionEvent( m_turtle, TurtleAction.INSPECT_ITEM );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return new Object[] { false, event.getFailureMessage() };
}
return new Object[] { table };
}
else

View File

@@ -17,11 +17,12 @@ import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
@@ -169,6 +170,10 @@ public class BlockTurtle extends BlockComputerBase
{
tile.setWorld( world ); // Not sure why this is necessary
tile.setPos( pos ); // Not sure why this is necessary
if( player instanceof EntityPlayer )
{
((TileTurtle) tile).setOwningPlayer( ((EntityPlayer) player).getGameProfile() );
}
}
// Set direction

View File

@@ -6,6 +6,7 @@
package dan200.computercraft.shared.turtle.blocks;
import com.mojang.authlib.GameProfile;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
@@ -71,14 +72,21 @@ public class TileTurtle extends TileComputerBase
private boolean m_inventoryChanged;
private TurtleBrain m_brain;
private MoveState m_moveState;
private ComputerFamily m_family;
public TileTurtle()
{
this( ComputerFamily.Normal );
}
public TileTurtle(ComputerFamily family)
{
m_inventory = NonNullList.withSize( INVENTORY_SIZE, ItemStack.EMPTY );
m_previousInventory = NonNullList.withSize( INVENTORY_SIZE, ItemStack.EMPTY );
m_inventoryChanged = false;
m_brain = createBrain();
m_moveState = MoveState.NOT_MOVED;
m_family = family;
}
public boolean hasMoved()
@@ -454,6 +462,19 @@ public class TileTurtle extends TileComputerBase
return m_brain.getToolRenderAngle( side, f );
}
// IComputerTile
@Override
public ComputerFamily getFamily()
{
return m_family;
}
public void setOwningPlayer( GameProfile player )
{
m_brain.setOwningPlayer( player );
markDirty();
}
// IInventory
@Override

View File

@@ -6,9 +6,12 @@
package dan200.computercraft.shared.turtle.blocks;
import dan200.computercraft.shared.computer.core.ComputerFamily;
public class TileTurtleAdvanced extends TileTurtle
{
public TileTurtleAdvanced()
{
super( ComputerFamily.Advanced);
}
}

View File

@@ -6,9 +6,12 @@
package dan200.computercraft.shared.turtle.blocks;
import dan200.computercraft.shared.computer.core.ComputerFamily;
public class TileTurtleExpanded extends TileTurtle
{
public TileTurtleExpanded()
{
super( ComputerFamily.Normal);
}
}

View File

@@ -7,6 +7,7 @@
package dan200.computercraft.shared.turtle.core;
import com.google.common.base.Objects;
import com.mojang.authlib.GameProfile;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
@@ -107,6 +108,7 @@ public class TurtleBrain implements ITurtleAccess
private TileTurtle m_owner;
private ComputerProxy m_proxy;
private GameProfile m_owningPlayer;
private LinkedList<TurtleCommandQueueEntry> m_commandQueue;
private int m_commandsIssued;
@@ -233,6 +235,20 @@ public class TurtleBrain implements ITurtleAccess
m_fuelLevel = 0;
}
// Read owner
if( nbttagcompound.hasKey( "owner", Constants.NBT.TAG_COMPOUND ) )
{
NBTTagCompound owner = nbttagcompound.getCompoundTag( "owner" );
m_owningPlayer = new GameProfile(
new UUID( owner.getLong( "upper_id" ), owner.getLong( "lower_id" ) ),
owner.getString( "name" )
);
}
else
{
m_owningPlayer = null;
}
// Read colour
m_colourHex = ColourUtils.getHexColour( nbttagcompound );
@@ -320,6 +336,17 @@ public class TurtleBrain implements ITurtleAccess
nbttagcompound.setInteger( "selectedSlot", m_selectedSlot );
nbttagcompound.setInteger( "fuelLevel", m_fuelLevel );
// Write owner
if( m_owningPlayer != null )
{
NBTTagCompound owner = new NBTTagCompound();
nbttagcompound.setTag( "owner", owner );
owner.setLong( "upper_id", m_owningPlayer.getId().getMostSignificantBits() );
owner.setLong( "lower_id", m_owningPlayer.getId().getLeastSignificantBits() );
owner.setString( "name", m_owningPlayer.getName() );
}
// Write upgrades
String leftUpgradeID = getUpgradeID( getUpgrade( TurtleSide.Left ) );
if( leftUpgradeID != null )
@@ -514,10 +541,11 @@ public class TurtleBrain implements ITurtleAccess
return true;
}
if ( !world.isBlockLoaded( pos ) )
{
return false;
}
// Ensure the chunk is loaded
if( !world.isBlockLoaded( pos ) ) return false;
// Ensure we're inside the world border
if( !world.getWorldBorder().contains( pos ) ) return false;
oldOwner.notifyMoveStart();
@@ -837,6 +865,18 @@ public class TurtleBrain implements ITurtleAccess
return m_colourHex;
}
public void setOwningPlayer( GameProfile profile )
{
m_owningPlayer = profile;
}
@Nonnull
@Override
public GameProfile getOwningPlayer()
{
return m_owningPlayer;
}
@Override
public ITurtleUpgrade getUpgrade( @Nonnull TurtleSide side )
{

View File

@@ -10,12 +10,14 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleAnimation;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleInventoryEvent;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
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.MinecraftForge;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
@@ -59,6 +61,16 @@ public class TurtleDropCommand implements ITurtleCommand
EnumFacing side = direction.getOpposite();
IItemHandler inventory = InventoryUtil.getInventory( world, newPosition, side );
// Fire the event, restoring the inventory and exiting if it is cancelled.
TurtlePlayer player = TurtlePlaceCommand.createPlayer( turtle, oldPosition, direction );
TurtleInventoryEvent.Drop event = new TurtleInventoryEvent.Drop( turtle, player, world, newPosition, inventory, stack );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
InventoryUtil.storeItems( stack, turtle.getItemHandler(), turtle.getSelectedSlot() );
return TurtleCommandResult.failure( event.getFailureMessage() );
}
if( inventory != null )
{
// Drop the item into the inventory

View File

@@ -8,11 +8,14 @@ package dan200.computercraft.shared.turtle.core;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.turtle.*;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.shared.proxy.CCTurtleProxyCommon;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
@@ -63,6 +66,12 @@ public class TurtleEquipCommand implements ITurtleCommand
oldUpgradeStack = null;
}
TurtleActionEvent event = new TurtleActionEvent( turtle, TurtleAction.EQUIP );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return TurtleCommandResult.failure( event.getFailureMessage() );
}
// Do the swapping:
if( newUpgradeStack != null )
{

View File

@@ -10,13 +10,15 @@ import com.google.common.collect.ImmutableMap;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
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.world.World;
import net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull;
import java.util.HashMap;
@@ -46,14 +48,14 @@ public class TurtleInspectCommand implements ITurtleCommand
if( WorldUtil.isBlockInWorld( world, newPosition ) )
{
if( !FAIL_ON_AIR || !world.isAirBlock( newPosition ) )
IBlockState state = world.getBlockState( newPosition );
if( !FAIL_ON_AIR || !state.getBlock().isAir( state, world, newPosition ) )
{
IBlockState state = world.getBlockState( newPosition );
Block block = state.getBlock();
String name = Block.REGISTRY.getNameForObject( block ).toString();
int metadata = block.getMetaFromState( state );
Map<Object, Object> table = new HashMap<>();
Map<String, Object> table = new HashMap<>();
table.put( "name", name );
table.put( "metadata", metadata );
@@ -73,7 +75,15 @@ public class TurtleInspectCommand implements ITurtleCommand
}
table.put( "state", stateTable );
return TurtleCommandResult.success( new Object[]{ table } );
// Fire the event, exiting if it is cancelled
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, oldPosition, direction );
TurtleBlockEvent.Inspect event = new TurtleBlockEvent.Inspect( turtle, turtlePlayer, world, newPosition, state, table );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return TurtleCommandResult.failure( event.getFailureMessage() );
}
return TurtleCommandResult.success( new Object[] { table } );
}
}
@@ -83,7 +93,7 @@ public class TurtleInspectCommand implements ITurtleCommand
table.put( "name", "minecraft:air" );
table.put( "metadata", 0 );
table.put( "state", new HashMap<>() );
return TurtleCommandResult.success( new Object[]{ table } );
return TurtleCommandResult.success( new Object[] { table } );
}
return TurtleCommandResult.failure( "No block to inspect" );
}

View File

@@ -11,13 +11,15 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleAnimation;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
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 net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull;
import java.util.List;
@@ -103,6 +105,12 @@ public class TurtleMoveCommand implements ITurtleCommand
}
}
TurtleBlockEvent.Move moveEvent = new TurtleBlockEvent.Move( turtle, turtlePlayer, oldWorld, newPosition );
if( MinecraftForge.EVENT_BUS.post( moveEvent ) )
{
return TurtleCommandResult.failure( moveEvent.getFailureMessage() );
}
// Check fuel level
if( turtle.isFuelNeeded() && turtle.getFuelLevel() < 1 )
{
@@ -170,6 +178,10 @@ public class TurtleMoveCommand implements ITurtleCommand
{
return TurtleCommandResult.failure( "Cannot leave loaded world" );
}
if( !world.getWorldBorder().contains( position ) )
{
return TurtleCommandResult.failure( "Cannot pass the world border" );
}
return TurtleCommandResult.success();
}
}

View File

@@ -11,6 +11,7 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleAnimation;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
import dan200.computercraft.shared.util.DirectionUtil;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
@@ -31,6 +32,7 @@ import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import org.apache.commons.lang3.tuple.Pair;
@@ -64,6 +66,16 @@ public class TurtlePlaceCommand implements ITurtleCommand
World world = turtle.getWorld();
BlockPos coordinates = WorldUtil.moveCoords( turtle.getPosition(), direction );
// Create a fake player, and orient it appropriately
BlockPos playerPosition = WorldUtil.moveCoords( turtle.getPosition(), direction );
TurtlePlayer turtlePlayer = createPlayer( turtle, playerPosition, direction );
TurtleBlockEvent.Place place = new TurtleBlockEvent.Place( turtle, turtlePlayer, turtle.getWorld(), coordinates, stack );
if( MinecraftForge.EVENT_BUS.post( place ) )
{
return TurtleCommandResult.failure( place.getFailureMessage() );
}
IBlockState previousState;
if( WorldUtil.isBlockInWorld( world, coordinates ) )
{
@@ -75,8 +87,8 @@ public class TurtlePlaceCommand implements ITurtleCommand
}
// Do the deploying
String[] errorMessage = new String[1];
ItemStack remainder = deploy( stack, turtle, direction, m_extraArguments, errorMessage );
String[] errorMessage = new String[ 1 ];
ItemStack remainder = deploy( stack, turtle, turtlePlayer, direction, m_extraArguments, errorMessage );
if( remainder != stack )
{
// Put the remaining items back
@@ -117,6 +129,11 @@ public class TurtlePlaceCommand implements ITurtleCommand
BlockPos playerPosition = WorldUtil.moveCoords( turtle.getPosition(), direction );
TurtlePlayer turtlePlayer = createPlayer( turtle, playerPosition, direction );
return deploy( stack, turtle, turtlePlayer, direction, extraArguments, o_errorMessage );
}
public static ItemStack deploy( @Nonnull ItemStack stack, ITurtleAccess turtle, TurtlePlayer turtlePlayer, EnumFacing direction, Object[] extraArguments, String[] o_errorMessage )
{
// Deploy on an entity
ItemStack remainder = deployOnEntity( stack, turtle, turtlePlayer, direction, extraArguments, o_errorMessage );
if( remainder != stack )
@@ -163,7 +180,7 @@ public class TurtlePlaceCommand implements ITurtleCommand
public static TurtlePlayer createPlayer( ITurtleAccess turtle, BlockPos position, EnumFacing direction )
{
TurtlePlayer turtlePlayer = new TurtlePlayer( (WorldServer)turtle.getWorld() );
TurtlePlayer turtlePlayer = new TurtlePlayer( turtle );
orientPlayer( turtle, turtlePlayer, position, direction );
return turtlePlayer;
}

View File

@@ -20,24 +20,38 @@ import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.UUID;
public class TurtlePlayer extends FakePlayer
{
private final static GameProfile s_profile = new GameProfile(
public final static GameProfile DEFAULT_PROFILE = new GameProfile(
UUID.fromString( "0d0c4ca0-4ff1-11e4-916c-0800200c9a66" ),
"ComputerCraft"
"[ComputerCraft]"
);
@Deprecated
public TurtlePlayer( World world )
{
this( (WorldServer) world );
super( (WorldServer) world, DEFAULT_PROFILE );
}
public TurtlePlayer( ITurtleAccess turtle )
{
super( (WorldServer) turtle.getWorld(), getProfile( turtle.getOwningPlayer() ));
BlockPos position = turtle.getPosition();
posX = position.getX() + 0.5;
posY = position.getY() + 0.5;
posZ = position.getZ() + 0.5;
rotationYaw = turtle.getDirection().getHorizontalAngle();
rotationPitch = 0.0f;
}
public TurtlePlayer( WorldServer world )
private static GameProfile getProfile( @Nullable GameProfile profile )
{
super( world, s_profile );
return profile != null && profile.isComplete() ? profile : DEFAULT_PROFILE;
}
public void loadInventory( @Nonnull ItemStack currentStack )

View File

@@ -10,9 +10,12 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleAnimation;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.shared.util.InventoryUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull;
@@ -71,6 +74,12 @@ public class TurtleRefuelCommand implements ITurtleCommand
return TurtleCommandResult.failure( "Items not combustible" );
}
TurtleActionEvent event = new TurtleActionEvent( turtle, TurtleAction.REFUEL );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return TurtleCommandResult.failure( event.getFailureMessage() );
}
if( !testOnly )
{
// Determine fuel to give and replacement item to leave behind

View File

@@ -10,6 +10,7 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleAnimation;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleInventoryEvent;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.entity.Entity;
@@ -19,6 +20,7 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
@@ -56,6 +58,15 @@ public class TurtleSuckCommand implements ITurtleCommand
EnumFacing side = direction.getOpposite();
IItemHandler inventory = InventoryUtil.getInventory( world, newPosition, side );
// Fire the event, exiting if it is cancelled.
TurtlePlayer player = TurtlePlaceCommand.createPlayer( turtle, oldPosition, direction );
TurtleInventoryEvent.Suck event = new TurtleInventoryEvent.Suck( turtle, player, world, newPosition, inventory );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return TurtleCommandResult.failure( event.getFailureMessage() );
}
if( inventory != null )
{
// Take from inventory of thing in front

View File

@@ -10,7 +10,10 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleAnimation;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.shared.util.DirectionUtil;
import net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull;
@@ -27,6 +30,12 @@ public class TurtleTurnCommand implements ITurtleCommand
@Override
public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
{
TurtleActionEvent event = new TurtleActionEvent( turtle, TurtleAction.TURN );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return TurtleCommandResult.failure( event.getFailureMessage() );
}
switch( m_direction )
{
case Left:

View File

@@ -170,6 +170,17 @@ public abstract class ItemTurtleBase extends ItemComputerBase implements ITurtle
}
}
@Override
public ItemStack withFamily( @Nonnull ItemStack stack, @Nonnull ComputerFamily family )
{
return TurtleItemFactory.create(
getComputerID( stack ), getLabel( stack ),
getColour( stack ), family,
getUpgrade( stack, TurtleSide.Left ), getUpgrade( stack, TurtleSide.Right ),
getFuelLevel( stack ), getOverlay( stack )
);
}
@Override
public ItemStack setColour( ItemStack stack, int colour )
{

View File

@@ -7,82 +7,41 @@
package dan200.computercraft.shared.turtle.recipes;
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.computer.recipe.ComputerConvertRecipe;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.util.RecipeUtil;
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.item.crafting.ShapedRecipes;
import net.minecraft.util.JsonUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.IRecipeFactory;
import net.minecraftforge.common.crafting.JsonContext;
import javax.annotation.Nonnull;
public class TurtleRecipe extends ShapedRecipes
public class TurtleRecipe extends ComputerConvertRecipe
{
private final NonNullList<Ingredient> m_recipe;
private final ComputerFamily m_family;
private final ComputerFamily family;
public TurtleRecipe( String group, int width, int height, NonNullList<Ingredient> recipe, ComputerFamily family )
public TurtleRecipe( String group, @Nonnull CraftingHelper.ShapedPrimer primer, ComputerFamily family )
{
super( group, width, height, recipe, TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null ) );
m_recipe = recipe;
m_family = family;
}
@Override
public boolean matches( @Nonnull InventoryCrafting _inventory, @Nonnull World world )
{
return !getCraftingResult( _inventory ).isEmpty();
super( group, primer, TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null ) );
this.family = family;
}
@Nonnull
@Override
public ItemStack getCraftingResult( @Nonnull InventoryCrafting inventory )
protected ItemStack convert( @Nonnull ItemStack stack )
{
// See if we match the recipe, and extract the input computercraft ID
int computerID = -1;
String label = null;
for( int y = 0; y < 3; ++y )
{
for( int x = 0; x < 3; ++x )
{
ItemStack item = inventory.getStackInRowAndColumn( x, y );
Ingredient target = m_recipe.get( x + y * 3 );
IComputerItem item = (IComputerItem) stack.getItem();
int computerID = item.getComputerID( stack );
String label = item.getLabel( stack );
if( item.getItem() instanceof IComputerItem )
{
IComputerItem itemComputer = (IComputerItem) item.getItem();
if( itemComputer.getFamily( item ) != m_family ) return ItemStack.EMPTY;
if( family == ComputerFamily.Beginners ) computerID = -1;
computerID = itemComputer.getComputerID( item );
label = itemComputer.getLabel( item );
}
else if( !target.apply( item ) )
{
return ItemStack.EMPTY;
}
}
}
// Build a turtle with the same ID the computer had
// Construct the new stack
if( m_family != ComputerFamily.Beginners )
{
return TurtleItemFactory.create( computerID, label, -1, m_family, null, null, 0, null );
}
else
{
return TurtleItemFactory.create( -1, label, -1, m_family, null, null, 0, null );
}
return TurtleItemFactory.create( computerID, label, -1, family, null, null, 0, null );
}
public static class Factory implements IRecipeFactory
@@ -91,20 +50,10 @@ public class TurtleRecipe extends ShapedRecipes
public IRecipe parse( JsonContext context, JsonObject json )
{
String group = JsonUtils.getString( json, "group", "" );
String familyName = JsonUtils.getString( json, "family" );
ComputerFamily family;
try
{
family = ComputerFamily.valueOf( familyName );
}
catch( IllegalArgumentException e )
{
throw new JsonSyntaxException( "Unknown computer family '" + familyName + "'" );
}
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
return new TurtleRecipe( group, primer.width, primer.height, primer.input, family );
return new TurtleRecipe( group, primer, family );
}
}
}

View File

@@ -118,7 +118,7 @@ public class TurtleInventoryCrafting extends InventoryCrafting
}
// Do post-pickup stuff
TurtlePlayer turtlePlayer = new TurtlePlayer( (WorldServer)world );
TurtlePlayer turtlePlayer = new TurtlePlayer( m_turtle );
result.onCrafting( world, turtlePlayer, numToCraft );
results.add( result );

View File

@@ -9,6 +9,8 @@ package dan200.computercraft.shared.turtle.upgrades;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.*;
import dan200.computercraft.api.turtle.event.TurtleAttackEvent;
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
import dan200.computercraft.shared.turtle.core.TurtleBrain;
import dan200.computercraft.shared.turtle.core.TurtlePlaceCommand;
import dan200.computercraft.shared.turtle.core.TurtlePlayer;
@@ -32,7 +34,6 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeEventFactory;
@@ -128,11 +129,11 @@ public class TurtleTool implements ITurtleUpgrade
{
case Attack:
{
return attack( turtle, direction );
return attack( turtle, direction, side );
}
case Dig:
{
return dig( turtle, direction );
return dig( turtle, direction, side );
}
default:
{
@@ -148,10 +149,11 @@ public class TurtleTool implements ITurtleUpgrade
return !block.isAir( state, world, pos ) && block != Blocks.BEDROCK && state.getBlockHardness( world, pos ) > -1.0F;
}
protected boolean canHarvestBlock( World world, BlockPos pos )
protected boolean canHarvestBlock( ITurtleAccess turtleAccess, BlockPos pos )
{
World world = turtleAccess.getWorld();
Block block = world.getBlockState( pos ).getBlock();
TurtlePlayer turtlePlayer = new TurtlePlayer( (WorldServer)world );
TurtlePlayer turtlePlayer = new TurtlePlayer( turtleAccess );
turtlePlayer.loadInventory( m_item.copy() );
return ForgeHooks.canHarvestBlock( block, turtlePlayer, world, pos );
}
@@ -161,7 +163,7 @@ public class TurtleTool implements ITurtleUpgrade
return 3.0f;
}
private TurtleCommandResult attack( final ITurtleAccess turtle, EnumFacing direction )
private TurtleCommandResult attack( final ITurtleAccess turtle, EnumFacing direction, TurtleSide side )
{
// Create a fake player, and orient it appropriately
final World world = turtle.getWorld();
@@ -178,8 +180,21 @@ public class TurtleTool implements ITurtleUpgrade
ItemStack stackCopy = m_item.copy();
turtlePlayer.loadInventory( stackCopy );
// Start claiming entity drops
Entity hitEntity = hit.getKey();
// Fire several events to ensure we have permissions.
if( MinecraftForge.EVENT_BUS.post( new AttackEntityEvent( turtlePlayer, hitEntity ) ) || !hitEntity.canBeAttackedWithItem() )
{
return TurtleCommandResult.failure( "Nothing to attack here" );
}
TurtleAttackEvent attackEvent = new TurtleAttackEvent( turtle, turtlePlayer, hitEntity, this, side );
if( MinecraftForge.EVENT_BUS.post( attackEvent ) )
{
return TurtleCommandResult.failure( attackEvent.getFailureMessage() );
}
// Start claiming entity drops
ComputerCraft.setEntityDropConsumer( hitEntity, ( entity, drop ) ->
{
ItemStack remainder = InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() );
@@ -191,8 +206,7 @@ public class TurtleTool implements ITurtleUpgrade
// Attack the entity
boolean attacked = false;
if( hitEntity.canBeAttackedWithItem() && !hitEntity.hitByEntity( turtlePlayer )
&& !MinecraftForge.EVENT_BUS.post( new AttackEntityEvent( turtlePlayer, hitEntity ) ) )
if( !hitEntity.hitByEntity( turtlePlayer ) )
{
float damage = (float)turtlePlayer.getEntityAttribute( SharedMonsterAttributes.ATTACK_DAMAGE ).getAttributeValue();
damage *= getDamageMultiplier();
@@ -233,7 +247,7 @@ public class TurtleTool implements ITurtleUpgrade
return TurtleCommandResult.failure( "Nothing to attack here" );
}
private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction )
private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction, TurtleSide side )
{
// Get ready to dig
World world = turtle.getWorld();
@@ -266,8 +280,15 @@ public class TurtleTool implements ITurtleUpgrade
return TurtleCommandResult.failure( "Unbreakable block detected" );
}
// Fire the dig event, checking whether it was cancelled.
TurtleBlockEvent.Dig digEvent = new TurtleBlockEvent.Dig( turtle, turtlePlayer, world, newPosition, world.getBlockState( newPosition ), this, side );
if( MinecraftForge.EVENT_BUS.post( digEvent ) )
{
return TurtleCommandResult.failure( digEvent.getFailureMessage() );
}
// Consume the items the block drops
if( canHarvestBlock( world, newPosition ) )
if( canHarvestBlock( turtle, newPosition ) )
{
List<ItemStack> items = getBlockDropped( world, newPosition, turtlePlayer );
if( items != null && items.size() > 0 )
@@ -302,12 +323,13 @@ public class TurtleTool implements ITurtleUpgrade
return TurtleCommandResult.failure( "Nothing to dig here" );
}
@SuppressWarnings("deprecation")
private List<ItemStack> getBlockDropped( World world, BlockPos pos, EntityPlayer player )
{
IBlockState state = world.getBlockState( pos );
Block block = state.getBlock();
NonNullList<ItemStack> drops = NonNullList.create();
block.getDrops( drops, world, pos, world.getBlockState( pos ), 0 );
// Note, we use the deprecated version as some mods override that instead. Those mods are wrong (TM).
List<ItemStack> drops = block.getDrops( world, pos, world.getBlockState( pos ), 0 );
double chance = ForgeEventFactory.fireBlockHarvesting( drops, world, pos, state, 0, 1, false, player );
for( int i = drops.size() - 1; i >= 0; i-- )

View File

@@ -9,6 +9,7 @@ package dan200.computercraft.shared.util;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.*;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.JsonUtils;
import net.minecraft.util.NonNullList;
@@ -102,4 +103,17 @@ public class RecipeUtil
return ings;
}
public static ComputerFamily getFamily( JsonObject json, String name )
{
String familyName = JsonUtils.getString( json, name );
try
{
return ComputerFamily.valueOf( familyName );
}
catch( IllegalArgumentException e )
{
throw new JsonSyntaxException( "Unknown computer family '" + familyName + "' for field " + name );
}
}
}

View File

@@ -0,0 +1,23 @@
package dan200.computercraft.shared.wired;
import dan200.computercraft.api.network.wired.IWiredElement;
import dan200.computercraft.api.network.wired.IWiredElementTile;
import dan200.computercraft.api.network.wired.IWiredProvider;
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;
import javax.annotation.Nullable;
public class DefaultWiredProvider implements IWiredProvider
{
@Nullable
@Override
public IWiredElement getElement( @Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
{
TileEntity te = world.getTileEntity( pos );
return te instanceof IWiredElementTile ? ((IWiredElementTile) te).getWiredElement( side ) : null;
}
}

View File

@@ -0,0 +1,46 @@
package dan200.computercraft.shared.wired;
import dan200.computercraft.ComputerCraft;
/**
* Verifies certain elements of a network are "well formed".
*
* This adds substantial overhead to network modification, and so should only be enabled
* in a development environment.
*/
public class InvariantChecker
{
private static final boolean ENABLED = false;
public static void checkNode( WiredNode node )
{
if( !ENABLED ) return;
WiredNetwork network = node.network;
if( network == null )
{
ComputerCraft.log.error( "Node's network is null", new Exception() );
return;
}
if( network.nodes == null || !network.nodes.contains( node ) )
{
ComputerCraft.log.error( "Node's network does not contain node", new Exception() );
}
for( WiredNode neighbour : node.neighbours )
{
if( !neighbour.neighbours.contains( node ) )
{
ComputerCraft.log.error( "Neighbour is missing node", new Exception() );
}
}
}
public static void checkNetwork( WiredNetwork network )
{
if( !ENABLED ) return;
for( WiredNode node : network.nodes ) checkNode( node );
}
}

View File

@@ -0,0 +1,458 @@
package dan200.computercraft.shared.wired;
import dan200.computercraft.api.network.Packet;
import dan200.computercraft.api.network.wired.IWiredNetwork;
import dan200.computercraft.api.network.wired.IWiredNode;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public final class WiredNetwork implements IWiredNetwork
{
final ReadWriteLock lock = new ReentrantReadWriteLock();
HashSet<WiredNode> nodes;
private HashMap<String, IPeripheral> peripherals = new HashMap<>();
public WiredNetwork( WiredNode node )
{
nodes = new HashSet<>( 1 );
nodes.add( node );
}
private WiredNetwork( HashSet<WiredNode> nodes )
{
this.nodes = nodes;
}
@Override
public boolean connect( @Nonnull IWiredNode nodeU, @Nonnull IWiredNode nodeV )
{
WiredNode wiredU = checkNode( nodeU );
WiredNode wiredV = checkNode( nodeV );
if( nodeU == nodeV ) throw new IllegalArgumentException( "Cannot add a connection to oneself." );
lock.writeLock().lock();
try
{
if( nodes == null ) throw new IllegalStateException( "Cannot add a connection to an empty network." );
boolean hasU = wiredU.network == this;
boolean hasV = wiredV.network == this;
if( !hasU && !hasV ) throw new IllegalArgumentException( "Neither node is in the network." );
// We're going to assimilate a node. Copy across all edges and vertices.
if( !hasU || !hasV )
{
WiredNetwork other = hasU ? wiredV.network : wiredU.network;
other.lock.writeLock().lock();
try
{
// Cache several properties for iterating over later
Map<String, IPeripheral> otherPeripherals = other.peripherals;
Map<String, IPeripheral> thisPeripherals = otherPeripherals.isEmpty() ? peripherals : new HashMap<>( peripherals );
Collection<WiredNode> thisNodes = otherPeripherals.isEmpty() ? nodes : new ArrayList<>( this.nodes );
Collection<WiredNode> otherNodes = other.nodes;
// Move all nodes across into this network, destroying the original nodes.
nodes.addAll( otherNodes );
for( WiredNode node : otherNodes ) node.network = this;
other.nodes = null;
// Move all peripherals across,
other.peripherals = null;
peripherals.putAll( otherPeripherals );
if( !thisPeripherals.isEmpty() )
{
WiredNetworkChange.added( thisPeripherals ).broadcast( otherNodes );
}
if( !otherPeripherals.isEmpty() )
{
WiredNetworkChange.added( otherPeripherals ).broadcast( thisNodes );
}
}
finally
{
other.lock.writeLock().unlock();
}
}
boolean added = wiredU.neighbours.add( wiredV );
if( added ) wiredV.neighbours.add( wiredU );
InvariantChecker.checkNetwork( this );
InvariantChecker.checkNode( wiredU );
InvariantChecker.checkNode( wiredV );
return added;
}
finally
{
lock.writeLock().unlock();
}
}
@Override
public boolean disconnect( @Nonnull IWiredNode nodeU, @Nonnull IWiredNode nodeV )
{
WiredNode wiredU = checkNode( nodeU );
WiredNode wiredV = checkNode( nodeV );
if( nodeU == nodeV ) throw new IllegalArgumentException( "Cannot remove a connection to oneself." );
lock.writeLock().lock();
try
{
boolean hasU = wiredU.network == this;
boolean hasV = wiredV.network == this;
if( !hasU || !hasV ) throw new IllegalArgumentException( "One node is not in the network." );
// If there was no connection to remove then split.
if( !wiredU.neighbours.remove( wiredV ) ) return false;
wiredV.neighbours.remove( wiredU );
// Determine if there is still some connection from u to v.
// Note this is an inlining of reachableNodes which short-circuits
// if all nodes are reachable.
Queue<WiredNode> enqueued = new ArrayDeque<>();
HashSet<WiredNode> reachableU = new HashSet<>();
reachableU.add( wiredU );
enqueued.add( wiredU );
while( !enqueued.isEmpty() )
{
WiredNode node = enqueued.remove();
for( WiredNode neighbour : node.neighbours )
{
// If we can reach wiredV from wiredU then abort.
if( neighbour == wiredV ) return true;
// Otherwise attempt to enqueue this neighbour as well.
if( reachableU.add( neighbour ) ) enqueued.add( neighbour );
}
}
// Create a new network with all U-reachable nodes/edges and remove them
// from the existing graph.
WiredNetwork networkU = new WiredNetwork( reachableU );
networkU.lock.writeLock().lock();
try
{
// Remove nodes from this network
nodes.removeAll( reachableU );
// Set network and transfer peripherals
for( WiredNode node : reachableU )
{
node.network = networkU;
networkU.peripherals.putAll( node.peripherals );
peripherals.keySet().removeAll( node.peripherals.keySet() );
}
// Broadcast changes
if( peripherals.size() != 0 ) WiredNetworkChange.removed( peripherals ).broadcast( networkU.nodes );
if( networkU.peripherals.size() != 0 )
{
WiredNetworkChange.removed( networkU.peripherals ).broadcast( nodes );
}
InvariantChecker.checkNetwork( this );
InvariantChecker.checkNetwork( networkU );
InvariantChecker.checkNode( wiredU );
InvariantChecker.checkNode( wiredV );
return true;
}
finally
{
networkU.lock.writeLock().unlock();
}
}
finally
{
lock.writeLock().unlock();
}
}
@Override
public boolean remove( @Nonnull IWiredNode node )
{
WiredNode wired = checkNode( node );
lock.writeLock().lock();
try
{
// If we're the empty graph then just abort: nodes must have _some_ network.
if( nodes == null ) return false;
if( nodes.size() <= 1 ) return false;
if( wired.network != this ) return false;
HashSet<WiredNode> neighbours = wired.neighbours;
// Remove this node and move into a separate network.
nodes.remove( wired );
for( WiredNode neighbour : neighbours ) neighbour.neighbours.remove( wired );
WiredNetwork wiredNetwork = new WiredNetwork( wired );
// If we're a leaf node in the graph (only one neighbour) then we don't need to
// check for network splitting
if( neighbours.size() == 1 )
{
// Broadcast our simple peripheral changes
removeSingleNode( wired, wiredNetwork );
InvariantChecker.checkNode( wired );
InvariantChecker.checkNetwork( wiredNetwork );
return true;
}
HashSet<WiredNode> reachable = reachableNodes( neighbours.iterator().next() );
// If all nodes are reachable then exit.
if( reachable.size() == nodes.size() )
{
// Broadcast our simple peripheral changes
removeSingleNode( wired, wiredNetwork );
InvariantChecker.checkNode( wired );
InvariantChecker.checkNetwork( wiredNetwork );
return true;
}
// A split may cause 2..neighbours.size() separate networks, so we
// iterate through our neighbour list, generating child networks.
neighbours.removeAll( reachable );
ArrayList<WiredNetwork> maximals = new ArrayList<>( neighbours.size() + 1 );
maximals.add( wiredNetwork );
maximals.add( new WiredNetwork( reachable ) );
while( neighbours.size() > 0 )
{
reachable = reachableNodes( neighbours.iterator().next() );
neighbours.removeAll( reachable );
maximals.add( new WiredNetwork( reachable ) );
}
for( WiredNetwork network : maximals ) network.lock.writeLock().lock();
try
{
// We special case the original node: detaching all peripherals when needed.
wired.network = wiredNetwork;
wired.peripherals = Collections.emptyMap();
// Ensure every network is finalised
for( WiredNetwork network : maximals )
{
for( WiredNode child : network.nodes )
{
child.network = network;
network.peripherals.putAll( child.peripherals );
}
}
for( WiredNetwork network : maximals ) InvariantChecker.checkNetwork( network );
InvariantChecker.checkNode( wired );
// Then broadcast network changes once all nodes are finalised
for( WiredNetwork network : maximals )
{
WiredNetworkChange.changeOf( peripherals, network.peripherals ).broadcast( network.nodes );
}
}
finally
{
for( WiredNetwork network : maximals ) network.lock.writeLock().unlock();
}
nodes.clear();
peripherals.clear();
return true;
}
finally
{
lock.writeLock().unlock();
}
}
@Override
public void invalidate( @Nonnull IWiredNode node )
{
WiredNode wired = checkNode( node );
lock.writeLock().lock();
try
{
if( wired.network != this ) throw new IllegalStateException( "Node is not on this network" );
Map<String, IPeripheral> oldPeripherals = wired.peripherals;
Map<String, IPeripheral> newPeripherals = wired.element.getPeripherals();
WiredNetworkChange change = WiredNetworkChange.changeOf( oldPeripherals, newPeripherals );
if( change.isEmpty() ) return;
wired.peripherals = newPeripherals;
// Detach the old peripherals then remove them.
peripherals.keySet().removeAll( change.peripheralsRemoved().keySet() );
// Add the new peripherals and attach them
peripherals.putAll( change.peripheralsAdded() );
change.broadcast( nodes );
}
finally
{
lock.writeLock().unlock();
}
}
void transmitPacket( WiredNode start, Packet packet, double range, boolean interdimensional )
{
Map<WiredNode, TransmitPoint> points = new HashMap<>();
TreeSet<TransmitPoint> transmitTo = new TreeSet<>();
{
TransmitPoint startEntry = start.element.getWorld() != packet.getSender().getWorld()
? new TransmitPoint( start, Double.POSITIVE_INFINITY, true )
: new TransmitPoint( start, start.element.getPosition().distanceTo( packet.getSender().getPosition() ), false );
points.put( start, startEntry );
transmitTo.add( startEntry );
}
{
TransmitPoint point;
while( (point = transmitTo.pollFirst()) != null )
{
World world = point.node.element.getWorld();
Vec3d position = point.node.element.getPosition();
for( WiredNode neighbour : point.node.neighbours )
{
TransmitPoint neighbourPoint = points.get( neighbour );
boolean newInterdimensional;
double newDistance;
if( world != neighbour.element.getWorld() )
{
newInterdimensional = true;
newDistance = Double.POSITIVE_INFINITY;
}
else
{
newInterdimensional = false;
newDistance = point.distance + position.distanceTo( neighbour.element.getPosition() );
}
if( neighbourPoint == null )
{
neighbourPoint = new TransmitPoint( neighbour, newDistance, newInterdimensional );
points.put( neighbour, neighbourPoint );
transmitTo.add( neighbourPoint );
}
else if( newDistance < neighbourPoint.distance )
{
transmitTo.remove( neighbourPoint );
neighbourPoint.distance = newDistance;
neighbourPoint.interdimensional = newInterdimensional;
transmitTo.add( neighbourPoint );
}
}
}
}
for( TransmitPoint point : points.values() )
{
point.node.tryTransmit( packet, point.distance, point.interdimensional, range, interdimensional );
}
}
private void removeSingleNode( WiredNode wired, WiredNetwork wiredNetwork )
{
wiredNetwork.lock.writeLock().lock();
try
{
// Cache all the old nodes.
Map<String, IPeripheral> wiredPeripherals = new HashMap<>( wired.peripherals );
// Setup the new node's network
// Detach the old peripherals then remove them from the old network
wired.network = wiredNetwork;
wired.neighbours.clear();
wired.peripherals = Collections.emptyMap();
// Broadcast the change
if( !peripherals.isEmpty() ) WiredNetworkChange.removed( peripherals ).broadcast( wired );
// Now remove all peripherals from this network and broadcast the change.
peripherals.keySet().removeAll( wiredPeripherals.keySet() );
if( !wiredPeripherals.isEmpty() ) WiredNetworkChange.removed( wiredPeripherals ).broadcast( nodes );
}
finally
{
wiredNetwork.lock.writeLock().unlock();
}
}
private static class TransmitPoint implements Comparable<TransmitPoint>
{
final WiredNode node;
double distance;
boolean interdimensional;
TransmitPoint( WiredNode node, double distance, boolean interdimensional )
{
this.node = node;
this.distance = distance;
this.interdimensional = interdimensional;
}
@Override
public int compareTo( @Nonnull TransmitPoint o )
{
// Objects with the same distance are not the same object, so we must add an additional layer of ordering.
return distance == o.distance
? Integer.compare( node.hashCode(), o.node.hashCode() )
: Double.compare( distance, o.distance );
}
}
private static WiredNode checkNode( IWiredNode node )
{
if( node instanceof WiredNode )
{
return (WiredNode) node;
}
else
{
throw new IllegalArgumentException( "Unknown implementation of IWiredNode: " + node );
}
}
private static HashSet<WiredNode> reachableNodes( WiredNode start )
{
Queue<WiredNode> enqueued = new ArrayDeque<>();
HashSet<WiredNode> reachable = new HashSet<>();
reachable.add( start );
enqueued.add( start );
WiredNode node;
while( (node = enqueued.poll()) != null )
{
for( WiredNode neighbour : node.neighbours )
{
// Otherwise attempt to enqueue this neighbour as well.
if( reachable.add( neighbour ) ) enqueued.add( neighbour );
}
}
return reachable;
}
}

View File

@@ -0,0 +1,101 @@
package dan200.computercraft.shared.wired;
import dan200.computercraft.api.network.wired.IWiredNetworkChange;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class WiredNetworkChange implements IWiredNetworkChange
{
private final Map<String, IPeripheral> removed;
private final Map<String, IPeripheral> added;
private WiredNetworkChange( Map<String, IPeripheral> removed, Map<String, IPeripheral> added )
{
this.removed = removed;
this.added = added;
}
public static WiredNetworkChange changed( Map<String, IPeripheral> removed, Map<String, IPeripheral> added )
{
return new WiredNetworkChange( Collections.unmodifiableMap( removed ), Collections.unmodifiableMap( added ) );
}
public static WiredNetworkChange added( Map<String, IPeripheral> added )
{
return new WiredNetworkChange( Collections.emptyMap(), Collections.unmodifiableMap( added ) );
}
public static WiredNetworkChange removed( Map<String, IPeripheral> removed )
{
return new WiredNetworkChange( Collections.unmodifiableMap( removed ), Collections.emptyMap() );
}
public static WiredNetworkChange changeOf( Map<String, IPeripheral> oldPeripherals, Map<String, IPeripheral> newPeripherals )
{
Map<String, IPeripheral> added = new HashMap<>( newPeripherals );
Map<String, IPeripheral> removed = new HashMap<>();
for( Map.Entry<String, IPeripheral> entry : oldPeripherals.entrySet() )
{
String oldKey = entry.getKey();
IPeripheral oldValue = entry.getValue();
if( newPeripherals.containsKey( oldKey ) )
{
IPeripheral rightValue = added.get( oldKey );
if( oldValue.equals( rightValue ) )
{
added.remove( oldKey );
}
else
{
removed.put( oldKey, oldValue );
}
}
else
{
removed.put( oldKey, oldValue );
}
}
return changed( removed, added );
}
@Nonnull
@Override
public Map<String, IPeripheral> peripheralsAdded()
{
return added;
}
@Nonnull
@Override
public Map<String, IPeripheral> peripheralsRemoved()
{
return removed;
}
public boolean isEmpty()
{
return added.isEmpty() && removed.isEmpty();
}
void broadcast( Iterable<WiredNode> nodes )
{
if( !isEmpty() )
{
for( WiredNode node : nodes ) node.element.networkChanged( this );
}
}
void broadcast( WiredNode node )
{
if( !isEmpty() )
{
node.element.networkChanged( this );
}
}
}

View File

@@ -0,0 +1,151 @@
package dan200.computercraft.shared.wired;
import com.google.common.base.Preconditions;
import dan200.computercraft.api.network.IPacketReceiver;
import dan200.computercraft.api.network.Packet;
import dan200.computercraft.api.network.wired.IWiredElement;
import dan200.computercraft.api.network.wired.IWiredNetwork;
import dan200.computercraft.api.network.wired.IWiredNode;
import dan200.computercraft.api.network.wired.IWiredSender;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.Lock;
public final class WiredNode implements IWiredNode
{
private Set<IPacketReceiver> receivers;
final IWiredElement element;
Map<String, IPeripheral> peripherals = Collections.emptyMap();
final HashSet<WiredNode> neighbours = new HashSet<>();
volatile WiredNetwork network;
public WiredNode( IWiredElement element )
{
this.element = element;
this.network = new WiredNetwork( this );
}
@Override
public synchronized void addReceiver( @Nonnull IPacketReceiver receiver )
{
if( receivers == null ) receivers = new HashSet<>();
receivers.add( receiver );
}
@Override
public synchronized void removeReceiver( @Nonnull IPacketReceiver receiver )
{
if( receivers != null ) receivers.remove( receiver );
}
synchronized void tryTransmit( Packet packet, double packetDistance, boolean packetInterdimensional, double range, boolean interdimensional )
{
if( receivers == null ) return;
for( IPacketReceiver receiver : receivers )
{
if( !packetInterdimensional )
{
double receiveRange = Math.max( range, receiver.getRange() ); // Ensure range is symmetrical
if( interdimensional || receiver.isInterdimensional() || packetDistance < receiveRange )
{
receiver.receiveSameDimension( packet, packetDistance + element.getPosition().distanceTo( receiver.getPosition() ) );
}
}
else
{
if( interdimensional || receiver.isInterdimensional() )
{
receiver.receiveDifferentDimension( packet );
}
}
}
}
@Override
public boolean isWireless()
{
return false;
}
@Override
public void transmitSameDimension( @Nonnull Packet packet, double range )
{
Preconditions.checkNotNull( packet, "packet cannot be null" );
if( !(packet.getSender() instanceof IWiredSender) || ((IWiredSender) packet.getSender()).getNode() != this )
{
throw new IllegalArgumentException( "Sender is not in the network" );
}
acquireReadLock();
try
{
network.transmitPacket( this, packet, range, false );
}
finally
{
network.lock.readLock().unlock();
}
}
@Override
public void transmitInterdimensional( @Nonnull Packet packet )
{
Preconditions.checkNotNull( packet, "packet cannot be null" );
if( !(packet.getSender() instanceof IWiredSender) || ((IWiredSender) packet.getSender()).getNode() != this )
{
throw new IllegalArgumentException( "Sender is not in the network" );
}
acquireReadLock();
try
{
network.transmitPacket( this, packet, 0, true );
}
finally
{
network.lock.readLock().unlock();
}
}
@Nonnull
@Override
public IWiredElement getElement()
{
return element;
}
@Nonnull
@Override
public IWiredNetwork getNetwork()
{
return network;
}
@Override
public String toString()
{
return "WiredNode{@" + element.getPosition() + " (" + element.getClass().getSimpleName() + ")}";
}
private void acquireReadLock()
{
WiredNetwork currentNetwork = network;
while( true )
{
Lock lock = currentNetwork.lock.readLock();
lock.lock();
if( currentNetwork == network ) return;
lock.unlock();
}
}
}

View File

@@ -1,7 +1,11 @@
{
"parent": "minecraft:recipes/root",
"rewards": {
"recipes": [ "computercraft:wired_modem" ]
"recipes": [
"computercraft:wired_modem",
"computercraft:wired_modem_full_to",
"computercraft:wired_modem_full_from"
]
},
"criteria": {
"has_normal": {
@@ -22,6 +26,12 @@
"items": [ { "item": "computercraft:cable", "data": 0 } ]
}
},
"has_modem_full": {
"trigger": "minecraft:inventory_changed",
"conditions": {
"items": [ { "item": "computercraft:wired_modem_full", "data": 0 } ]
}
},
"has_the_recipe": {
"trigger": "minecraft:recipe_unlocked",
"conditions": { "recipe": "computercraft:wired_modem" }
@@ -32,6 +42,7 @@
"has_normal",
"has_advanced",
"has_cable",
"has_modem_full",
"has_the_recipe"
]
]

View File

@@ -0,0 +1,9 @@
{
"variants": {
"modem=false,peripheral=false": { "model": "computercraft:wired_modem_full_off" },
"modem=false,peripheral=true": { "model": "computercraft:wired_modem_full_off_peripheral" },
"modem=true,peripheral=false": { "model": "computercraft:wired_modem_full_on" },
"modem=true,peripheral=true": { "model": "computercraft:wired_modem_full_on_peripheral" }
}
}

View File

@@ -62,5 +62,6 @@ gui.computercraft:config.turtle_fuel_limit=Turtle fuel limit
gui.computercraft:config.advanced_turtle_fuel_limit=Advanced Turtle fuel limit
gui.computercraft:config.turtles_obey_block_protection=Turtles obey block protection
gui.computercraft:config.turtles_can_push=Turtles can push entities
gui.computercraft:config.turtle_disabled_actions=Disabled turtle actions
gui.computercraft:config.maximum_files_open=Maximum files open per computer
gui.computercraft:config.max_notes_per_tick=Maximum notes that a computer can play at once

View File

@@ -0,0 +1,4 @@
--[[
Alright then, don't ignore me. This file is to ensure the existence of the "modules/command" folder.
You can use this folder to add modules who can be loaded with require() to your Resourcepack.
]]

Some files were not shown because too many files have changed in this diff Show More