1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-10 02:13:04 +00:00

Move some shared upgrade code into a base class

Most upgrades provides a couple of constant getters (upgrade ID,
adjective, crafting item). We move the getters into a parent class and
pass the values in via the constructor instead.

Also do a tiny bit of cleanup to the upgrades. Mostly just reducing
nesting, renaming fields, etc...
This commit is contained in:
SquidDev
2018-12-26 09:01:03 +00:00
parent 5b48a0fa5f
commit dd6bab5413
13 changed files with 317 additions and 424 deletions

View File

@@ -0,0 +1,78 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.turtle.upgrades;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleUpgradeType;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull;
public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade
{
private final ResourceLocation id;
private final int legacyId;
private final TurtleUpgradeType type;
private final String adjective;
private final ItemStack stack;
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, ItemStack stack )
{
this.id = id;
this.legacyId = legacyId;
this.type = type;
this.adjective = adjective;
this.stack = stack;
}
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Item item )
{
this( id, legacyId, type, adjective, new ItemStack( item ) );
}
public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Block block )
{
this( id, legacyId, type, adjective, new ItemStack( block ) );
}
@Nonnull
@Override
public final ResourceLocation getUpgradeID()
{
return id;
}
@Override
public final int getLegacyUpgradeID()
{
return legacyId;
}
@Nonnull
@Override
public final String getUnlocalisedAdjective()
{
return adjective;
}
@Nonnull
@Override
public final TurtleUpgradeType getType()
{
return type;
}
@Nonnull
@Override
public final ItemStack getCraftingItem()
{
return stack;
}
}

View File

@@ -17,18 +17,15 @@ import javax.annotation.Nonnull;
import static dan200.computercraft.core.apis.ArgumentHelper.optInt;
public class CraftingTablePeripheral
implements IPeripheral
public class CraftingTablePeripheral implements IPeripheral
{
private final ITurtleAccess m_turtle;
private final ITurtleAccess turtle;
public CraftingTablePeripheral( ITurtleAccess turtle )
{
m_turtle = turtle;
this.turtle = turtle;
}
// IPeripheral implementation
@Nonnull
@Override
public String getType()
@@ -48,10 +45,7 @@ public class CraftingTablePeripheral
private int parseCount( Object[] arguments ) throws LuaException
{
int count = optInt( arguments, 0, 64 );
if( count < 0 || count > 64 )
{
throw new LuaException( "Crafting count " + count + " out of range" );
}
if( count < 0 || count > 64 ) throw new LuaException( "Crafting count " + count + " out of range" );
return count;
}
@@ -64,7 +58,7 @@ public class CraftingTablePeripheral
{
// craft
final int limit = parseCount( arguments );
return m_turtle.executeCommand( context, new TurtleCraftCommand( limit ) );
return turtle.executeCommand( context, new TurtleCraftCommand( limit ) );
}
default:
{
@@ -76,6 +70,6 @@ public class CraftingTablePeripheral
@Override
public boolean equals( IPeripheral other )
{
return (other != null && other.getClass() == this.getClass());
return this == other || other instanceof CraftingTablePeripheral;
}
}

View File

@@ -8,7 +8,6 @@ package dan200.computercraft.shared.turtle.upgrades;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleUpgradeType;
import net.minecraft.client.Minecraft;
@@ -16,7 +15,6 @@ import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelManager;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -25,12 +23,8 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.vecmath.Matrix4f;
public class TurtleCraftingTable implements ITurtleUpgrade
public class TurtleCraftingTable extends AbstractTurtleUpgrade
{
private ResourceLocation m_id;
private int m_legacyID;
private ItemStack m_item;
@SideOnly( Side.CLIENT )
private ModelResourceLocation m_leftModel;
@@ -39,43 +33,10 @@ public class TurtleCraftingTable implements ITurtleUpgrade
public TurtleCraftingTable( int legacyId )
{
m_id = new ResourceLocation( "minecraft", "crafting_table" );
m_legacyID = legacyId;
m_item = new ItemStack( Blocks.CRAFTING_TABLE, 1, 0 );
}
@Nonnull
@Override
public ResourceLocation getUpgradeID()
{
return m_id;
}
@Override
public int getLegacyUpgradeID()
{
return m_legacyID;
}
@Nonnull
@Override
public String getUnlocalisedAdjective()
{
return "upgrade.minecraft:crafting_table.adjective";
}
@Nonnull
@Override
public TurtleUpgradeType getType()
{
return TurtleUpgradeType.Peripheral;
}
@Nonnull
@Override
public ItemStack getCraftingItem()
{
return m_item;
super(
new ResourceLocation( "minecraft", "crafting_table" ), legacyId, TurtleUpgradeType.Peripheral,
"upgrade.minecraft:crafting_table.adjective", Blocks.CRAFTING_TABLE
);
}
@Override

View File

@@ -16,7 +16,6 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelManager;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
@@ -30,30 +29,30 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.vecmath.Matrix4f;
public class TurtleModem implements ITurtleUpgrade
public class TurtleModem extends AbstractTurtleUpgrade
{
private static class Peripheral extends WirelessModemPeripheral
{
private final ITurtleAccess m_turtle;
private final ITurtleAccess turtle;
public Peripheral( ITurtleAccess turtle, boolean advanced )
Peripheral( ITurtleAccess turtle, boolean advanced )
{
super( new ModemState(), advanced );
m_turtle = turtle;
this.turtle = turtle;
}
@Nonnull
@Override
public World getWorld()
{
return m_turtle.getWorld();
return turtle.getWorld();
}
@Nonnull
@Override
public Vec3d getPosition()
{
BlockPos turtlePos = m_turtle.getPosition();
BlockPos turtlePos = turtle.getPosition();
return new Vec3d(
turtlePos.getX(),
turtlePos.getY(),
@@ -64,18 +63,11 @@ public class TurtleModem implements ITurtleUpgrade
@Override
public boolean equals( IPeripheral other )
{
if( other instanceof Peripheral )
{
Peripheral otherModem = (Peripheral) other;
return otherModem.m_turtle == m_turtle;
}
return false;
return this == other || (other instanceof Peripheral && ((Peripheral) other).turtle == turtle);
}
}
private boolean m_advanced;
private ResourceLocation m_id;
private int m_legacyID;
private boolean advanced;
@SideOnly( Side.CLIENT )
private ModelResourceLocation m_leftOffModel;
@@ -91,63 +83,19 @@ public class TurtleModem implements ITurtleUpgrade
public TurtleModem( boolean advanced, ResourceLocation id, int legacyId )
{
m_advanced = advanced;
m_id = id;
m_legacyID = legacyId;
}
super(
id, legacyId, TurtleUpgradeType.Peripheral,
advanced ? "upgrade.computercraft:advanced_modem.adjective" : "upgrade.computercraft:wireless_modem.adjective",
advanced ? PeripheralItemFactory.create( PeripheralType.AdvancedModem, null, 1 ) : PeripheralItemFactory.create( PeripheralType.WirelessModem, null, 1 )
@Nonnull
@Override
public ResourceLocation getUpgradeID()
{
return m_id;
}
@Override
public int getLegacyUpgradeID()
{
return m_legacyID;
}
@Nonnull
@Override
public String getUnlocalisedAdjective()
{
if( m_advanced )
{
return "upgrade.computercraft:advanced_modem.adjective";
}
else
{
return "upgrade.computercraft:wireless_modem.adjective";
}
}
@Nonnull
@Override
public TurtleUpgradeType getType()
{
return TurtleUpgradeType.Peripheral;
}
@Nonnull
@Override
public ItemStack getCraftingItem()
{
if( m_advanced )
{
return PeripheralItemFactory.create( PeripheralType.AdvancedModem, null, 1 );
}
else
{
return PeripheralItemFactory.create( PeripheralType.WirelessModem, null, 1 );
}
);
this.advanced = advanced;
}
@Override
public IPeripheral createPeripheral( @Nonnull ITurtleAccess turtle, @Nonnull TurtleSide side )
{
return new Peripheral( turtle, m_advanced );
return new Peripheral( turtle, advanced );
}
@Nonnull
@@ -162,7 +110,7 @@ public class TurtleModem implements ITurtleUpgrade
{
if( m_leftOffModel == null )
{
if( m_advanced )
if( advanced )
{
m_leftOffModel = new ModelResourceLocation( "computercraft:advanced_turtle_modem_off_left", "inventory" );
m_rightOffModel = new ModelResourceLocation( "computercraft:advanced_turtle_modem_off_right", "inventory" );

View File

@@ -9,7 +9,6 @@ package dan200.computercraft.shared.turtle.upgrades;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleUpgradeType;
import dan200.computercraft.shared.peripheral.PeripheralType;
@@ -19,7 +18,6 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelManager;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@@ -30,17 +28,16 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.vecmath.Matrix4f;
public class TurtleSpeaker implements ITurtleUpgrade
public class TurtleSpeaker extends AbstractTurtleUpgrade
{
private static class Peripheral extends SpeakerPeripheral
{
// Members
ITurtleAccess m_turtle;
ITurtleAccess turtle;
public Peripheral( ITurtleAccess turtle )
Peripheral( ITurtleAccess turtle )
{
super();
m_turtle = turtle;
this.turtle = turtle;
}
@Override
@@ -52,13 +49,13 @@ public class TurtleSpeaker implements ITurtleUpgrade
@Override
public World getWorld()
{
return m_turtle.getWorld();
return turtle.getWorld();
}
@Override
public BlockPos getPos()
{
return m_turtle.getPosition();
return turtle.getPosition();
}
@Override
@@ -67,17 +64,13 @@ public class TurtleSpeaker implements ITurtleUpgrade
if( other instanceof Peripheral )
{
Peripheral otherPeripheral = (Peripheral) other;
return otherPeripheral.m_turtle == m_turtle;
return otherPeripheral.turtle == turtle;
}
return false;
}
}
// Members
private ResourceLocation m_id;
private int m_legacyID;
@SideOnly( Side.CLIENT )
private ModelResourceLocation m_leftModel;
@@ -86,42 +79,10 @@ public class TurtleSpeaker implements ITurtleUpgrade
public TurtleSpeaker( ResourceLocation id, int legacyId )
{
m_id = id;
m_legacyID = legacyId;
}
@Nonnull
@Override
public ResourceLocation getUpgradeID()
{
return m_id;
}
@Override
public int getLegacyUpgradeID()
{
return m_legacyID;
}
@Nonnull
@Override
public String getUnlocalisedAdjective()
{
return "upgrade.computercraft:speaker.adjective";
}
@Nonnull
@Override
public TurtleUpgradeType getType()
{
return TurtleUpgradeType.Peripheral;
}
@Nonnull
@Override
public ItemStack getCraftingItem()
{
return PeripheralItemFactory.create( PeripheralType.Speaker, null, 1 );
super( id, legacyId, TurtleUpgradeType.Peripheral,
"upgrade.computercraft:speaker.adjective",
PeripheralItemFactory.create( PeripheralType.Speaker, null, 1 )
);
}
@Override

View File

@@ -44,55 +44,16 @@ import javax.vecmath.Matrix4f;
import java.util.List;
import java.util.function.Function;
public class TurtleTool implements ITurtleUpgrade
public class TurtleTool extends AbstractTurtleUpgrade
{
private ResourceLocation m_id;
private int m_legacyId;
private String m_adjective;
protected ItemStack m_item;
public TurtleTool( ResourceLocation id, int legacyID, String adjective, Item item )
{
m_id = id;
m_legacyId = legacyID;
m_adjective = adjective;
super( id, legacyID, TurtleUpgradeType.Tool, adjective, item );
m_item = new ItemStack( item, 1, 0 );
}
@Nonnull
@Override
public ResourceLocation getUpgradeID()
{
return m_id;
}
@Override
public int getLegacyUpgradeID()
{
return m_legacyId;
}
@Nonnull
@Override
public String getUnlocalisedAdjective()
{
return m_adjective;
}
@Nonnull
@Override
public TurtleUpgradeType getType()
{
return TurtleUpgradeType.Tool;
}
@Nonnull
@Override
public ItemStack getCraftingItem()
{
return m_item.copy();
}
@Nonnull
@Override
@SideOnly( Side.CLIENT )
@@ -119,17 +80,11 @@ public class TurtleTool implements ITurtleUpgrade
switch( verb )
{
case Attack:
{
return attack( turtle, direction, side );
}
case Dig:
{
return dig( turtle, direction, side );
}
default:
{
return TurtleCommandResult.failure( "Unsupported action" );
}
}
}
@@ -229,77 +184,76 @@ public class TurtleTool implements ITurtleUpgrade
// Get ready to dig
World world = turtle.getWorld();
BlockPos turtlePosition = turtle.getPosition();
BlockPos blockPosition = WorldUtil.moveCoords( turtlePosition, direction );
BlockPos blockPosition = turtlePosition.offset( direction );
if( WorldUtil.isBlockInWorld( world, blockPosition ) &&
!world.isAirBlock( blockPosition ) &&
!WorldUtil.isLiquidBlock( world, blockPosition ) )
if( world.isAirBlock( blockPosition ) || WorldUtil.isLiquidBlock( world, blockPosition ) )
{
IBlockState state = world.getBlockState( blockPosition );
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, turtlePosition, direction );
turtlePlayer.loadInventory( m_item.copy() );
if( ComputerCraft.turtlesObeyBlockProtection )
{
// Check spawn protection
if( MinecraftForge.EVENT_BUS.post( new BlockEvent.BreakEvent( world, blockPosition, state, turtlePlayer ) ) )
{
return TurtleCommandResult.failure( "Cannot break protected block" );
}
if( !ComputerCraft.isBlockEditable( world, blockPosition, turtlePlayer ) )
{
return TurtleCommandResult.failure( "Cannot break protected block" );
}
}
// Check if we can break the block
if( !canBreakBlock( state, world, blockPosition, turtlePlayer ) )
{
return TurtleCommandResult.failure( "Unbreakable block detected" );
}
// Fire the dig event, checking whether it was cancelled.
TurtleBlockEvent.Dig digEvent = new TurtleBlockEvent.Dig( turtle, turtlePlayer, world, blockPosition, state, this, side );
if( MinecraftForge.EVENT_BUS.post( digEvent ) )
{
return TurtleCommandResult.failure( digEvent.getFailureMessage() );
}
// Consume the items the block drops
ComputerCraft.setDropConsumer( world, blockPosition, turtleDropConsumer( turtle ) );
TileEntity tile = world.getTileEntity( blockPosition );
// Much of this logic comes from PlayerInteractionManager#tryHarvestBlock, so it's a good idea
// to consult there before making any changes.
// Play the destruction sound
world.playEvent( 2001, blockPosition, Block.getStateId( state ) );
// Destroy the block
boolean canHarvest = state.getBlock().canHarvestBlock( world, blockPosition, turtlePlayer );
boolean canBreak = state.getBlock().removedByPlayer( state, world, blockPosition, turtlePlayer, canHarvest );
if( canBreak ) state.getBlock().onPlayerDestroy( world, blockPosition, state );
if( canHarvest )
{
state.getBlock().harvestBlock( world, turtlePlayer, blockPosition, state, tile, turtlePlayer.getHeldItemMainhand() );
}
stopConsuming( turtle );
// Remember the previous block
if( turtle instanceof TurtleBrain )
{
TurtleBrain brain = (TurtleBrain) turtle;
brain.saveBlockChange( blockPosition, state );
}
return TurtleCommandResult.success();
return TurtleCommandResult.failure( "Nothing to dig here" );
}
return TurtleCommandResult.failure( "Nothing to dig here" );
IBlockState state = world.getBlockState( blockPosition );
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, turtlePosition, direction );
turtlePlayer.loadInventory( m_item.copy() );
if( ComputerCraft.turtlesObeyBlockProtection )
{
// Check spawn protection
if( MinecraftForge.EVENT_BUS.post( new BlockEvent.BreakEvent( world, blockPosition, state, turtlePlayer ) ) )
{
return TurtleCommandResult.failure( "Cannot break protected block" );
}
if( !ComputerCraft.isBlockEditable( world, blockPosition, turtlePlayer ) )
{
return TurtleCommandResult.failure( "Cannot break protected block" );
}
}
// Check if we can break the block
if( !canBreakBlock( state, world, blockPosition, turtlePlayer ) )
{
return TurtleCommandResult.failure( "Unbreakable block detected" );
}
// Fire the dig event, checking whether it was cancelled.
TurtleBlockEvent.Dig digEvent = new TurtleBlockEvent.Dig( turtle, turtlePlayer, world, blockPosition, state, this, side );
if( MinecraftForge.EVENT_BUS.post( digEvent ) )
{
return TurtleCommandResult.failure( digEvent.getFailureMessage() );
}
// Consume the items the block drops
ComputerCraft.setDropConsumer( world, blockPosition, turtleDropConsumer( turtle ) );
TileEntity tile = world.getTileEntity( blockPosition );
// Much of this logic comes from PlayerInteractionManager#tryHarvestBlock, so it's a good idea
// to consult there before making any changes.
// Play the destruction sound
world.playEvent( 2001, blockPosition, Block.getStateId( state ) );
// Destroy the block
boolean canHarvest = state.getBlock().canHarvestBlock( world, blockPosition, turtlePlayer );
boolean canBreak = state.getBlock().removedByPlayer( state, world, blockPosition, turtlePlayer, canHarvest );
if( canBreak ) state.getBlock().onPlayerDestroy( world, blockPosition, state );
if( canHarvest )
{
state.getBlock().harvestBlock( world, turtlePlayer, blockPosition, state, tile, turtlePlayer.getHeldItemMainhand() );
}
stopConsuming( turtle );
// Remember the previous block
if( turtle instanceof TurtleBrain )
{
TurtleBrain brain = (TurtleBrain) turtle;
brain.saveBlockChange( blockPosition, state );
}
return TurtleCommandResult.success();
}
private Function<ItemStack, ItemStack> turtleDropConsumer( ITurtleAccess turtle )