1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +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 Drop( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull
/**
* 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.
* @return The item stack which will be dropped. This should <b>not</b> be modified.
*/
@Nonnull
public ItemStack getStack()
{
return stack.copy();
return stack;
}
}
}

View File

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

View File

@ -25,11 +25,11 @@
public class TurtleInspectCommand implements ITurtleCommand
{
private final InteractDirection m_direction;
private final InteractDirection direction;
public TurtleInspectCommand( InteractDirection direction )
{
m_direction = direction;
this.direction = direction;
}
@Nonnull
@ -37,7 +37,7 @@ public TurtleInspectCommand( InteractDirection direction )
public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
{
// 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
World world = turtle.getWorld();
@ -59,18 +59,15 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
table.put( "metadata", metadata );
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();
Object value = entry.getValue();
if( value instanceof String || value instanceof Number || value instanceof Boolean )
{
stateTable.put( propertyName, value );
}
else
{
stateTable.put( propertyName, value.toString() );
}
IProperty property = entry.getKey();
Comparable value = entry.getValue();
@SuppressWarnings( "unchecked" )
Object valueName = value instanceof String || value instanceof Number || value instanceof Boolean
? value : property.getName( value );
stateTable.put( property.getName(), valueName );
}
table.put( "state", stateTable );