1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-26 08:56:54 +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,48 @@
/*
* 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.pocket.peripherals;
import dan200.computercraft.api.pocket.IPocketUpgrade;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull;
public abstract class AbstractPocketUpgrade implements IPocketUpgrade
{
private final ResourceLocation id;
private final String adjective;
private final ItemStack stack;
protected AbstractPocketUpgrade( ResourceLocation id, String adjective, ItemStack stack )
{
this.id = id;
this.adjective = adjective;
this.stack = stack;
}
@Nonnull
@Override
public final ResourceLocation getUpgradeID()
{
return id;
}
@Nonnull
@Override
public final String getUnlocalisedAdjective()
{
return adjective;
}
@Nonnull
@Override
public final ItemStack getCraftingItem()
{
return stack;
}
}

View File

@ -2,82 +2,63 @@ package dan200.computercraft.shared.pocket.peripherals;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.pocket.IPocketAccess; import dan200.computercraft.api.pocket.IPocketAccess;
import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import dan200.computercraft.shared.peripheral.modem.ModemState; import dan200.computercraft.shared.peripheral.modem.ModemState;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class PocketModem implements IPocketUpgrade public class PocketModem extends AbstractPocketUpgrade
{ {
private final boolean m_advanced; private final boolean advanced;
public PocketModem( boolean m_advanced ) public PocketModem( boolean advanced )
{ {
this.m_advanced = m_advanced; super(
} advanced
? new ResourceLocation( "computercraft", "advanved_modem" )
@Nonnull : new ResourceLocation( "computercraft", "wireless_modem" ),
@Override advanced
public ResourceLocation getUpgradeID() ? "upgrade.computercraft:advanced_modem.adjective"
{ : "upgrade.computercraft:wireless_modem.adjective",
return m_advanced PeripheralItemFactory.create(
? new ResourceLocation( "computercraft", "advanved_modem" ) advanced ? PeripheralType.AdvancedModem : PeripheralType.WirelessModem,
: new ResourceLocation( "computercraft", "wireless_modem" ); null, 1
} )
@Nonnull
@Override
public String getUnlocalisedAdjective()
{
return m_advanced
? "upgrade.computercraft:advanced_modem.adjective"
: "upgrade.computercraft:wireless_modem.adjective";
}
@Nonnull
@Override
public ItemStack getCraftingItem()
{
return PeripheralItemFactory.create(
m_advanced ? PeripheralType.AdvancedModem : PeripheralType.WirelessModem,
null, 1
); );
this.advanced = advanced;
} }
@Nullable @Nullable
@Override @Override
public IPeripheral createPeripheral( @Nonnull IPocketAccess access ) public IPeripheral createPeripheral( @Nonnull IPocketAccess access )
{ {
return new PocketModemPeripheral( m_advanced ); return new PocketModemPeripheral( advanced );
} }
@Override @Override
public void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral ) public void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral )
{ {
if( peripheral instanceof PocketModemPeripheral ) if( !(peripheral instanceof PocketModemPeripheral) ) return;
Entity entity = access.getEntity();
PocketModemPeripheral modem = (PocketModemPeripheral) peripheral;
if( entity instanceof EntityLivingBase )
{ {
Entity entity = access.getEntity(); EntityLivingBase player = (EntityLivingBase) entity;
modem.setLocation( entity.getEntityWorld(), player.posX, player.posY + player.getEyeHeight(), player.posZ );
PocketModemPeripheral modem = (PocketModemPeripheral) peripheral;
if( entity instanceof EntityLivingBase )
{
EntityLivingBase player = (EntityLivingBase) entity;
modem.setLocation( entity.getEntityWorld(), player.posX, player.posY + player.getEyeHeight(), player.posZ );
}
else if( entity != null )
{
modem.setLocation( entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ );
}
ModemState state = modem.getModemState();
if( state.pollChanged() ) access.setLight( state.isOpen() ? 0xBA0000 : -1 );
} }
else if( entity != null )
{
modem.setLocation( entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ );
}
ModemState state = modem.getModemState();
if( state.pollChanged() ) access.setLight( state.isOpen() ? 0xBA0000 : -1 );
} }
} }

View File

@ -15,22 +15,22 @@ import javax.annotation.Nonnull;
public class PocketModemPeripheral extends WirelessModemPeripheral public class PocketModemPeripheral extends WirelessModemPeripheral
{ {
private World m_world; private World world;
private Vec3d m_position; private Vec3d position;
public PocketModemPeripheral( boolean advanced ) public PocketModemPeripheral( boolean advanced )
{ {
super( new ModemState(), advanced ); super( new ModemState(), advanced );
m_world = null; world = null;
m_position = new Vec3d( 0.0, 0.0, 0.0 ); position = new Vec3d( 0.0, 0.0, 0.0 );
} }
public void setLocation( World world, double x, double y, double z ) public void setLocation( World world, double x, double y, double z )
{ {
m_position = new Vec3d( x, y, z ); position = new Vec3d( x, y, z );
if( m_world != world ) if( this.world != world )
{ {
m_world = world; this.world = world;
switchNetwork(); switchNetwork();
} }
} }
@ -39,18 +39,14 @@ public class PocketModemPeripheral extends WirelessModemPeripheral
@Override @Override
public World getWorld() public World getWorld()
{ {
return m_world; return world;
} }
@Nonnull @Nonnull
@Override @Override
public Vec3d getPosition() public Vec3d getPosition()
{ {
if( m_world != null ) return world != null ? position : null;
{
return m_position;
}
return null;
} }
@Override @Override

View File

@ -8,42 +8,24 @@ package dan200.computercraft.shared.pocket.peripherals;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.pocket.IPocketAccess; import dan200.computercraft.api.pocket.IPocketAccess;
import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class PocketSpeaker implements IPocketUpgrade public class PocketSpeaker extends AbstractPocketUpgrade
{ {
public PocketSpeaker() public PocketSpeaker()
{ {
} super(
new ResourceLocation( "computercraft", "speaker" ),
@Nonnull "upgrade.computercraft:speaker.adjective",
@Override PeripheralItemFactory.create( PeripheralType.Speaker, null, 1 )
public ResourceLocation getUpgradeID() );
{
return new ResourceLocation( "computercraft", "speaker" );
}
@Nonnull
@Override
public String getUnlocalisedAdjective()
{
return "upgrade.computercraft:speaker.adjective";
}
@Nonnull
@Override
public ItemStack getCraftingItem()
{
return PeripheralItemFactory.create( PeripheralType.Speaker, null, 1 );
} }
@Nullable @Nullable
@ -56,24 +38,22 @@ public class PocketSpeaker implements IPocketUpgrade
@Override @Override
public void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral ) public void update( @Nonnull IPocketAccess access, @Nullable IPeripheral peripheral )
{ {
if( peripheral instanceof PocketSpeakerPeripheral ) if( !(peripheral instanceof PocketSpeakerPeripheral) ) return;
PocketSpeakerPeripheral speaker = (PocketSpeakerPeripheral) peripheral;
Entity entity = access.getEntity();
if( entity instanceof EntityLivingBase )
{ {
Entity entity = access.getEntity(); EntityLivingBase player = (EntityLivingBase) entity;
speaker.setLocation( entity.getEntityWorld(), player.posX, player.posY + player.getEyeHeight(), player.posZ );
PocketSpeakerPeripheral speaker = (PocketSpeakerPeripheral) peripheral;
if( entity instanceof EntityLivingBase )
{
EntityLivingBase player = (EntityLivingBase) entity;
speaker.setLocation( entity.getEntityWorld(), player.posX, player.posY + player.getEyeHeight(), player.posZ );
}
else if( entity != null )
{
speaker.setLocation( entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ );
}
speaker.update();
access.setLight( speaker.madeSound( 20 ) ? 0x3320fc : -1 );
} }
else if( entity != null )
{
speaker.setLocation( entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ );
}
speaker.update();
access.setLight( speaker.madeSound( 20 ) ? 0x3320fc : -1 );
} }
} }

View File

@ -13,46 +13,37 @@ import net.minecraft.world.World;
public class PocketSpeakerPeripheral extends SpeakerPeripheral public class PocketSpeakerPeripheral extends SpeakerPeripheral
{ {
private World m_world; private World world;
private BlockPos m_position; private BlockPos position;
PocketSpeakerPeripheral() PocketSpeakerPeripheral()
{ {
super(); super();
m_world = null; world = null;
m_position = new BlockPos( 0.0, 0.0, 0.0 ); position = BlockPos.ORIGIN;
} }
void setLocation( World world, double x, double y, double z ) void setLocation( World world, double x, double y, double z )
{ {
m_position = new BlockPos( x, y, z ); position = new BlockPos( x, y, z );
this.world = world;
if( m_world != world )
{
m_world = world;
}
} }
@Override @Override
public World getWorld() public World getWorld()
{ {
return m_world; return world;
} }
@Override @Override
public BlockPos getPos() public BlockPos getPos()
{ {
if( m_world != null ) return world != null ? position : null;
{
return m_position;
}
return null;
} }
@Override @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
{ {
// Sufficient because of use case: checking peripherals on individual pocket computers -- there will not be +1
return other instanceof PocketSpeakerPeripheral; return other instanceof PocketSpeakerPeripheral;
} }
} }

View File

@ -83,12 +83,6 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
new ResourceLocation( ComputerCraft.MOD_ID, "turtle_player" ), TurtlePlayer.class, "turtle_player", new ResourceLocation( ComputerCraft.MOD_ID, "turtle_player" ), TurtlePlayer.class, "turtle_player",
0, ComputerCraft.instance, Integer.MAX_VALUE, Integer.MAX_VALUE, false 0, ComputerCraft.instance, Integer.MAX_VALUE, Integer.MAX_VALUE, false
); );
registerUpgrades();
// Recipe types
// RecipeSorter.register( "computercraft:turtle", TurtleRecipe.class, RecipeSorter.Category.SHAPED, "after:minecraft:shapeless" );
// RecipeSorter.register( "computercraft:turtle_upgrade", TurtleUpgradeRecipe.class, RecipeSorter.Category.SHAPED, "after:minecraft:shapeless" );
} }
@Override @Override
@ -318,6 +312,8 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
registry.register( new ItemTurtleLegacy( ComputerCraft.Blocks.turtle ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "turtle" ) ) ); registry.register( new ItemTurtleLegacy( ComputerCraft.Blocks.turtle ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "turtle" ) ) );
registry.register( new ItemTurtleNormal( ComputerCraft.Blocks.turtleExpanded ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_expanded" ) ) ); registry.register( new ItemTurtleNormal( ComputerCraft.Blocks.turtleExpanded ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_expanded" ) ) );
registry.register( new ItemTurtleAdvanced( ComputerCraft.Blocks.turtleAdvanced ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_advanced" ) ) ); registry.register( new ItemTurtleAdvanced( ComputerCraft.Blocks.turtleAdvanced ).setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "turtle_advanced" ) ) );
registerUpgrades();
} }
@SubscribeEvent @SubscribeEvent

View File

@ -333,6 +333,8 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
// Pocket computer // Pocket computer
ComputerCraft.Items.pocketComputer = new ItemPocketComputer(); ComputerCraft.Items.pocketComputer = new ItemPocketComputer();
registry.register( ComputerCraft.Items.pocketComputer.setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "pocket_computer" ) ) ); registry.register( ComputerCraft.Items.pocketComputer.setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "pocket_computer" ) ) );
registerUpgrades();
} }
@SubscribeEvent @SubscribeEvent
@ -380,15 +382,6 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
// Printout // Printout
registry.register( new PrintoutRecipe().setRegistryName( new ResourceLocation( "computercraft:printout" ) ) ); registry.register( new PrintoutRecipe().setRegistryName( new ResourceLocation( "computercraft:printout" ) ) );
// Register pocket upgrades
ComputerCraft.PocketUpgrades.wirelessModem = new PocketModem( false );
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.wirelessModem );
ComputerCraft.PocketUpgrades.advancedModem = new PocketModem( true );
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.advancedModem );
ComputerCraft.PocketUpgrades.pocketSpeaker = new PocketSpeaker();
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.pocketSpeaker );
// Wireless Pocket Computer // Wireless Pocket Computer
registry.register( new PocketComputerUpgradeRecipe().setRegistryName( new ResourceLocation( "computercraft:pocket_computer_upgrade" ) ) ); registry.register( new PocketComputerUpgradeRecipe().setRegistryName( new ResourceLocation( "computercraft:pocket_computer_upgrade" ) ) );
@ -415,6 +408,18 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
} }
} }
private void registerUpgrades()
{
// Register pocket upgrades
ComputerCraft.PocketUpgrades.wirelessModem = new PocketModem( false );
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.wirelessModem );
ComputerCraft.PocketUpgrades.advancedModem = new PocketModem( true );
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.advancedModem );
ComputerCraft.PocketUpgrades.pocketSpeaker = new PocketSpeaker();
ComputerCraftAPI.registerPocketUpgrade( ComputerCraft.PocketUpgrades.pocketSpeaker );
}
@SubscribeEvent @SubscribeEvent
public void remapItems( RegistryEvent.MissingMappings<Item> mappings ) public void remapItems( RegistryEvent.MissingMappings<Item> mappings )
{ {

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; import static dan200.computercraft.core.apis.ArgumentHelper.optInt;
public class CraftingTablePeripheral public class CraftingTablePeripheral implements IPeripheral
implements IPeripheral
{ {
private final ITurtleAccess m_turtle; private final ITurtleAccess turtle;
public CraftingTablePeripheral( ITurtleAccess turtle ) public CraftingTablePeripheral( ITurtleAccess turtle )
{ {
m_turtle = turtle; this.turtle = turtle;
} }
// IPeripheral implementation
@Nonnull @Nonnull
@Override @Override
public String getType() public String getType()
@ -48,10 +45,7 @@ public class CraftingTablePeripheral
private int parseCount( Object[] arguments ) throws LuaException private int parseCount( Object[] arguments ) throws LuaException
{ {
int count = optInt( arguments, 0, 64 ); int count = optInt( arguments, 0, 64 );
if( count < 0 || count > 64 ) if( count < 0 || count > 64 ) throw new LuaException( "Crafting count " + count + " out of range" );
{
throw new LuaException( "Crafting count " + count + " out of range" );
}
return count; return count;
} }
@ -64,7 +58,7 @@ public class CraftingTablePeripheral
{ {
// craft // craft
final int limit = parseCount( arguments ); final int limit = parseCount( arguments );
return m_turtle.executeCommand( context, new TurtleCraftCommand( limit ) ); return turtle.executeCommand( context, new TurtleCraftCommand( limit ) );
} }
default: default:
{ {
@ -76,6 +70,6 @@ public class CraftingTablePeripheral
@Override @Override
public boolean equals( IPeripheral other ) 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.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleUpgradeType; import dan200.computercraft.api.turtle.TurtleUpgradeType;
import net.minecraft.client.Minecraft; 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.ModelManager;
import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@ -25,12 +23,8 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.vecmath.Matrix4f; 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 ) @SideOnly( Side.CLIENT )
private ModelResourceLocation m_leftModel; private ModelResourceLocation m_leftModel;
@ -39,43 +33,10 @@ public class TurtleCraftingTable implements ITurtleUpgrade
public TurtleCraftingTable( int legacyId ) public TurtleCraftingTable( int legacyId )
{ {
m_id = new ResourceLocation( "minecraft", "crafting_table" ); super(
m_legacyID = legacyId; new ResourceLocation( "minecraft", "crafting_table" ), legacyId, TurtleUpgradeType.Peripheral,
m_item = new ItemStack( Blocks.CRAFTING_TABLE, 1, 0 ); "upgrade.minecraft:crafting_table.adjective", Blocks.CRAFTING_TABLE
} );
@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;
} }
@Override @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.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelManager; import net.minecraft.client.renderer.block.model.ModelManager;
import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
@ -30,30 +29,30 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.vecmath.Matrix4f; import javax.vecmath.Matrix4f;
public class TurtleModem implements ITurtleUpgrade public class TurtleModem extends AbstractTurtleUpgrade
{ {
private static class Peripheral extends WirelessModemPeripheral 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 ); super( new ModemState(), advanced );
m_turtle = turtle; this.turtle = turtle;
} }
@Nonnull @Nonnull
@Override @Override
public World getWorld() public World getWorld()
{ {
return m_turtle.getWorld(); return turtle.getWorld();
} }
@Nonnull @Nonnull
@Override @Override
public Vec3d getPosition() public Vec3d getPosition()
{ {
BlockPos turtlePos = m_turtle.getPosition(); BlockPos turtlePos = turtle.getPosition();
return new Vec3d( return new Vec3d(
turtlePos.getX(), turtlePos.getX(),
turtlePos.getY(), turtlePos.getY(),
@ -64,18 +63,11 @@ public class TurtleModem implements ITurtleUpgrade
@Override @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
{ {
if( other instanceof Peripheral ) return this == other || (other instanceof Peripheral && ((Peripheral) other).turtle == turtle);
{
Peripheral otherModem = (Peripheral) other;
return otherModem.m_turtle == m_turtle;
}
return false;
} }
} }
private boolean m_advanced; private boolean advanced;
private ResourceLocation m_id;
private int m_legacyID;
@SideOnly( Side.CLIENT ) @SideOnly( Side.CLIENT )
private ModelResourceLocation m_leftOffModel; private ModelResourceLocation m_leftOffModel;
@ -91,63 +83,19 @@ public class TurtleModem implements ITurtleUpgrade
public TurtleModem( boolean advanced, ResourceLocation id, int legacyId ) public TurtleModem( boolean advanced, ResourceLocation id, int legacyId )
{ {
m_advanced = advanced; super(
m_id = id; id, legacyId, TurtleUpgradeType.Peripheral,
m_legacyID = legacyId; 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 this.advanced = advanced;
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 );
}
} }
@Override @Override
public IPeripheral createPeripheral( @Nonnull ITurtleAccess turtle, @Nonnull TurtleSide side ) public IPeripheral createPeripheral( @Nonnull ITurtleAccess turtle, @Nonnull TurtleSide side )
{ {
return new Peripheral( turtle, m_advanced ); return new Peripheral( turtle, advanced );
} }
@Nonnull @Nonnull
@ -162,7 +110,7 @@ public class TurtleModem implements ITurtleUpgrade
{ {
if( m_leftOffModel == null ) if( m_leftOffModel == null )
{ {
if( m_advanced ) if( advanced )
{ {
m_leftOffModel = new ModelResourceLocation( "computercraft:advanced_turtle_modem_off_left", "inventory" ); m_leftOffModel = new ModelResourceLocation( "computercraft:advanced_turtle_modem_off_left", "inventory" );
m_rightOffModel = new ModelResourceLocation( "computercraft:advanced_turtle_modem_off_right", "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.peripheral.IPeripheral;
import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.TurtleUpgradeType; import dan200.computercraft.api.turtle.TurtleUpgradeType;
import dan200.computercraft.shared.peripheral.PeripheralType; 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.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelManager; import net.minecraft.client.renderer.block.model.ModelManager;
import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -30,17 +28,16 @@ import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.vecmath.Matrix4f; import javax.vecmath.Matrix4f;
public class TurtleSpeaker implements ITurtleUpgrade public class TurtleSpeaker extends AbstractTurtleUpgrade
{ {
private static class Peripheral extends SpeakerPeripheral private static class Peripheral extends SpeakerPeripheral
{ {
// Members ITurtleAccess turtle;
ITurtleAccess m_turtle;
public Peripheral( ITurtleAccess turtle ) Peripheral( ITurtleAccess turtle )
{ {
super(); super();
m_turtle = turtle; this.turtle = turtle;
} }
@Override @Override
@ -52,13 +49,13 @@ public class TurtleSpeaker implements ITurtleUpgrade
@Override @Override
public World getWorld() public World getWorld()
{ {
return m_turtle.getWorld(); return turtle.getWorld();
} }
@Override @Override
public BlockPos getPos() public BlockPos getPos()
{ {
return m_turtle.getPosition(); return turtle.getPosition();
} }
@Override @Override
@ -67,17 +64,13 @@ public class TurtleSpeaker implements ITurtleUpgrade
if( other instanceof Peripheral ) if( other instanceof Peripheral )
{ {
Peripheral otherPeripheral = (Peripheral) other; Peripheral otherPeripheral = (Peripheral) other;
return otherPeripheral.m_turtle == m_turtle; return otherPeripheral.turtle == turtle;
} }
return false; return false;
} }
} }
// Members
private ResourceLocation m_id;
private int m_legacyID;
@SideOnly( Side.CLIENT ) @SideOnly( Side.CLIENT )
private ModelResourceLocation m_leftModel; private ModelResourceLocation m_leftModel;
@ -86,42 +79,10 @@ public class TurtleSpeaker implements ITurtleUpgrade
public TurtleSpeaker( ResourceLocation id, int legacyId ) public TurtleSpeaker( ResourceLocation id, int legacyId )
{ {
m_id = id; super( id, legacyId, TurtleUpgradeType.Peripheral,
m_legacyID = legacyId; "upgrade.computercraft:speaker.adjective",
} PeripheralItemFactory.create( PeripheralType.Speaker, null, 1 )
);
@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 );
} }
@Override @Override

View File

@ -44,55 +44,16 @@ import javax.vecmath.Matrix4f;
import java.util.List; import java.util.List;
import java.util.function.Function; 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; protected ItemStack m_item;
public TurtleTool( ResourceLocation id, int legacyID, String adjective, Item item ) public TurtleTool( ResourceLocation id, int legacyID, String adjective, Item item )
{ {
m_id = id; super( id, legacyID, TurtleUpgradeType.Tool, adjective, item );
m_legacyId = legacyID;
m_adjective = adjective;
m_item = new ItemStack( item, 1, 0 ); 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 @Nonnull
@Override @Override
@SideOnly( Side.CLIENT ) @SideOnly( Side.CLIENT )
@ -119,17 +80,11 @@ public class TurtleTool implements ITurtleUpgrade
switch( verb ) switch( verb )
{ {
case Attack: case Attack:
{
return attack( turtle, direction, side ); return attack( turtle, direction, side );
}
case Dig: case Dig:
{
return dig( turtle, direction, side ); return dig( turtle, direction, side );
}
default: default:
{
return TurtleCommandResult.failure( "Unsupported action" ); return TurtleCommandResult.failure( "Unsupported action" );
}
} }
} }
@ -229,77 +184,76 @@ public class TurtleTool implements ITurtleUpgrade
// Get ready to dig // Get ready to dig
World world = turtle.getWorld(); World world = turtle.getWorld();
BlockPos turtlePosition = turtle.getPosition(); BlockPos turtlePosition = turtle.getPosition();
BlockPos blockPosition = WorldUtil.moveCoords( turtlePosition, direction ); BlockPos blockPosition = turtlePosition.offset( direction );
if( WorldUtil.isBlockInWorld( world, blockPosition ) && if( world.isAirBlock( blockPosition ) || WorldUtil.isLiquidBlock( world, blockPosition ) )
!world.isAirBlock( blockPosition ) &&
!WorldUtil.isLiquidBlock( world, blockPosition ) )
{ {
IBlockState state = world.getBlockState( blockPosition ); return TurtleCommandResult.failure( "Nothing to dig here" );
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" ); 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 ) private Function<ItemStack, ItemStack> turtleDropConsumer( ITurtleAccess turtle )