1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-15 19:55:42 +00:00

Add an event for inspecting items too

We've had one for blocks for ever after all.
This commit is contained in:
SquidDev 2019-03-27 20:58:00 +00:00
parent 6ed03e1fcd
commit 1230cabcb0
4 changed files with 130 additions and 165 deletions

View File

@ -0,0 +1,74 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.api.turtle.event;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.turtle.ITurtleAccess;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
import java.util.Map;
import java.util.Objects;
/**
* Fired when a turtle gathers data on an item in its inventory.
*
* You may prevent items being inspected, or add additional information to the result. Be aware that this is fired on
* the computer thread, and so any operations on it must be thread safe.
*
* @see TurtleAction#INSPECT_ITEM
*/
public class TurtleInspectItemEvent extends TurtleActionEvent
{
private final ItemStack stack;
private final Map<String, Object> data;
public TurtleInspectItemEvent( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack, @Nonnull Map<String, Object> data )
{
super( turtle, TurtleAction.INSPECT_ITEM );
Objects.requireNonNull( stack, "stack cannot be null" );
Objects.requireNonNull( data, "data cannot be null" );
this.stack = stack;
this.data = data;
}
/**
* The item which is currently being inspected.
*
* @return The item stack which is being inspected. This should <b>not</b> be modified.
*/
@Nonnull
public ItemStack getStack()
{
return stack;
}
/**
* Get the "inspection data" from this item, which will be returned to the user.
*
* @return This items'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 convertible to Lua (see
* {@link dan200.computercraft.api.peripheral.IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}).
*/
public void addData( @Nonnull Map<String, ?> newData )
{
Objects.requireNonNull( newData, "newData cannot be null" );
data.putAll( newData );
}
}

View File

@ -76,13 +76,12 @@ public abstract class TurtleInventoryEvent extends TurtleBlockEvent
/** /**
* The item which will be inserted into the inventory/dropped on the ground. * 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. This should <b>not</b> be modified.
*
* @return The item stack which will be dropped.
*/ */
@Nonnull
public ItemStack getStack() public ItemStack getStack()
{ {
return stack.copy(); return stack;
} }
} }
} }

View File

@ -13,8 +13,8 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand; import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult; import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent; import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.api.turtle.event.TurtleInspectItemEvent;
import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.tracking.TrackingField; import dan200.computercraft.core.tracking.TrackingField;
import dan200.computercraft.shared.turtle.core.*; import dan200.computercraft.shared.turtle.core.*;
@ -152,42 +152,24 @@ public class TurtleAPI implements ILuaAPI
{ {
switch( method ) switch( method )
{ {
case 0: case 0: // forward
{
// forward
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Forward ) ); return tryCommand( context, new TurtleMoveCommand( MoveDirection.Forward ) );
} case 1: // back
case 1:
{
// back
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Back ) ); return tryCommand( context, new TurtleMoveCommand( MoveDirection.Back ) );
} case 2: // up
case 2:
{
// up
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Up ) ); return tryCommand( context, new TurtleMoveCommand( MoveDirection.Up ) );
} case 3: // down
case 3:
{
// down
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Down ) ); return tryCommand( context, new TurtleMoveCommand( MoveDirection.Down ) );
} case 4: // turnLeft
case 4:
{
// turnLeft
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Left ) ); return tryCommand( context, new TurtleTurnCommand( TurnDirection.Left ) );
} case 5: // turnRight
case 5:
{
// turnRight
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Right ) ); return tryCommand( context, new TurtleTurnCommand( TurnDirection.Right ) );
}
case 6: case 6:
{ {
// dig // dig
@ -209,24 +191,15 @@ public class TurtleAPI implements ILuaAPI
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, TurtleToolCommand.dig( InteractDirection.Down, side ) ); return tryCommand( context, TurtleToolCommand.dig( InteractDirection.Down, side ) );
} }
case 9: case 9: // place
{
// place
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Forward, args ) ); return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Forward, args ) );
} case 10: // placeUp
case 10:
{
// placeUp
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Up, args ) ); return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Up, args ) );
} case 11: // placeDown
case 11:
{
// placeDown
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Down, args ) ); return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Down, args ) );
}
case 12: case 12:
{ {
// drop // drop
@ -248,58 +221,27 @@ public class TurtleAPI implements ILuaAPI
// getItemCount // getItemCount
int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() ); int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() );
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot ); ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
if( !stack.isEmpty() )
{
return new Object[] { stack.getCount() }; return new Object[] { stack.getCount() };
} }
else
{
return new Object[] { 0 };
}
}
case 15: case 15:
{ {
// getItemSpace // getItemSpace
int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() ); int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() );
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot ); ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
if( !stack.isEmpty() ) return new Object[] { stack.isEmpty() ? 64 : Math.min( stack.getMaxStackSize(), 64 ) - stack.getCount() };
{
return new Object[] {
Math.min( stack.getMaxStackSize(), 64 ) - stack.getCount()
};
} }
return new Object[] { 64 }; case 16: // detect
}
case 16:
{
// detect
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Forward ) ); return tryCommand( context, new TurtleDetectCommand( InteractDirection.Forward ) );
} case 17: // detectUp
case 17:
{
// detectUp
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Up ) ); return tryCommand( context, new TurtleDetectCommand( InteractDirection.Up ) );
} case 18: // detectDown
case 18:
{
// detectDown
return tryCommand( context, new TurtleDetectCommand( InteractDirection.Down ) ); return tryCommand( context, new TurtleDetectCommand( InteractDirection.Down ) );
} case 19: // compare
case 19:
{
// compare
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Forward ) ); return tryCommand( context, new TurtleCompareCommand( InteractDirection.Forward ) );
} case 20: // compareUp
case 20:
{
// compareUp
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Up ) ); return tryCommand( context, new TurtleCompareCommand( InteractDirection.Up ) );
} case 21: // compareDown
case 21:
{
// compareDown
return tryCommand( context, new TurtleCompareCommand( InteractDirection.Down ) ); return tryCommand( context, new TurtleCompareCommand( InteractDirection.Down ) );
}
case 22: case 22:
{ {
// attack // attack
@ -356,18 +298,8 @@ public class TurtleAPI implements ILuaAPI
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Down, count ) ); return tryCommand( context, new TurtleSuckCommand( InteractDirection.Down, count ) );
} }
case 30: case 30: // getFuelLevel
{ return new Object[] { m_turtle.isFuelNeeded() ? m_turtle.getFuelLevel() : "unlimited" };
// getFuelLevel
if( m_turtle.isFuelNeeded() )
{
return new Object[] { m_turtle.getFuelLevel() };
}
else
{
return new Object[] { "unlimited" };
}
}
case 31: case 31:
{ {
// refuel // refuel
@ -387,84 +319,47 @@ public class TurtleAPI implements ILuaAPI
int count = parseCount( args, 1 ); int count = parseCount( args, 1 );
return tryCommand( context, new TurtleTransferToCommand( slot, count ) ); return tryCommand( context, new TurtleTransferToCommand( slot, count ) );
} }
case 34: case 34: // getSelectedSlot
{
// getSelectedSlot
return new Object[] { m_turtle.getSelectedSlot() + 1 }; return new Object[] { m_turtle.getSelectedSlot() + 1 };
} case 35: // getFuelLimit
case 35: return new Object[] { m_turtle.isFuelNeeded() ? m_turtle.getFuelLimit() : "unlimited" };
{ case 36: // equipLeft
// getFuelLimit
if( m_turtle.isFuelNeeded() )
{
return new Object[] { m_turtle.getFuelLimit() };
}
else
{
return new Object[] { "unlimited" };
}
}
case 36:
{
// equipLeft
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Left ) ); return tryCommand( context, new TurtleEquipCommand( TurtleSide.Left ) );
} case 37: // equipRight
case 37:
{
// equipRight
m_environment.addTrackingChange( TrackingField.TURTLE_OPS ); m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Right ) ); return tryCommand( context, new TurtleEquipCommand( TurtleSide.Right ) );
} case 38: // inspect
case 38:
{
// inspect
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Forward ) ); return tryCommand( context, new TurtleInspectCommand( InteractDirection.Forward ) );
} case 39: // inspectUp
case 39:
{
// inspectUp
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Up ) ); return tryCommand( context, new TurtleInspectCommand( InteractDirection.Up ) );
} case 40: // inspectDown
case 40:
{
// inspectDown
return tryCommand( context, new TurtleInspectCommand( InteractDirection.Down ) ); return tryCommand( context, new TurtleInspectCommand( InteractDirection.Down ) );
}
case 41: case 41:
{ {
// getItemDetail // getItemDetail
int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() ); int slot = parseOptionalSlotNumber( args, 0, m_turtle.getSelectedSlot() );
ItemStack stack = m_turtle.getInventory().getStackInSlot( slot ); ItemStack stack = m_turtle.getInventory().getStackInSlot( slot );
if( !stack.isEmpty() ) if( stack.isEmpty() ) return new Object[] { null };
{
Item item = stack.getItem(); Item item = stack.getItem();
String name = Item.REGISTRY.getNameForObject( item ).toString(); String name = Item.REGISTRY.getNameForObject( item ).toString();
int damage = stack.getItemDamage(); int damage = stack.getItemDamage();
int count = stack.getCount(); int count = stack.getCount();
Map<Object, Object> table = new HashMap<>(); Map<String, Object> table = new HashMap<>();
table.put( "name", name ); table.put( "name", name );
table.put( "damage", damage ); table.put( "damage", damage );
table.put( "count", count ); table.put( "count", count );
TurtleActionEvent event = new TurtleActionEvent( m_turtle, TurtleAction.INSPECT_ITEM ); TurtleActionEvent event = new TurtleInspectItemEvent( m_turtle, stack, table );
if( MinecraftForge.EVENT_BUS.post( event ) ) if( MinecraftForge.EVENT_BUS.post( event ) ) return new Object[] { false, event.getFailureMessage() };
{
return new Object[] { false, event.getFailureMessage() };
}
return new Object[] { table }; return new Object[] { table };
} }
else
{
return new Object[] { null };
}
}
default: default:
{
return null; return null;
} }
} }
} }
}

View File

@ -25,11 +25,11 @@ import java.util.Map;
public class TurtleInspectCommand implements ITurtleCommand public class TurtleInspectCommand implements ITurtleCommand
{ {
private final InteractDirection m_direction; private final InteractDirection direction;
public TurtleInspectCommand( InteractDirection direction ) public TurtleInspectCommand( InteractDirection direction )
{ {
m_direction = direction; this.direction = direction;
} }
@Nonnull @Nonnull
@ -37,7 +37,7 @@ public class TurtleInspectCommand implements ITurtleCommand
public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle ) public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
{ {
// Get world direction from direction // Get world direction from direction
EnumFacing direction = m_direction.toWorldDir( turtle ); EnumFacing direction = this.direction.toWorldDir( turtle );
// Check if thing in front is air or not // Check if thing in front is air or not
World world = turtle.getWorld(); World world = turtle.getWorld();
@ -59,18 +59,15 @@ public class TurtleInspectCommand implements ITurtleCommand
table.put( "metadata", metadata ); table.put( "metadata", metadata );
Map<Object, Object> stateTable = new HashMap<>(); Map<Object, Object> stateTable = new HashMap<>();
for( ImmutableMap.Entry<IProperty<?>, ?> entry : state.getActualState( world, newPosition ).getProperties().entrySet() ) for( ImmutableMap.Entry<IProperty<?>, ? extends Comparable<?>> entry : state.getActualState( world, newPosition ).getProperties().entrySet() )
{ {
String propertyName = entry.getKey().getName(); IProperty property = entry.getKey();
Object value = entry.getValue();
if( value instanceof String || value instanceof Number || value instanceof Boolean ) Comparable value = entry.getValue();
{ @SuppressWarnings( "unchecked" )
stateTable.put( propertyName, value ); Object valueName = value instanceof String || value instanceof Number || value instanceof Boolean
} ? value : property.getName( value );
else stateTable.put( property.getName(), valueName );
{
stateTable.put( propertyName, value.toString() );
}
} }
table.put( "state", stateTable ); table.put( "state", stateTable );