mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 10:20:28 +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:
parent
5b48a0fa5f
commit
dd6bab5413
@ -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;
|
||||
}
|
||||
}
|
@ -2,82 +2,63 @@ package dan200.computercraft.shared.pocket.peripherals;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.pocket.IPocketAccess;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
|
||||
import dan200.computercraft.shared.peripheral.modem.ModemState;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
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;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ResourceLocation getUpgradeID()
|
||||
{
|
||||
return m_advanced
|
||||
? new ResourceLocation( "computercraft", "advanved_modem" )
|
||||
: new ResourceLocation( "computercraft", "wireless_modem" );
|
||||
}
|
||||
|
||||
@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
|
||||
super(
|
||||
advanced
|
||||
? new ResourceLocation( "computercraft", "advanved_modem" )
|
||||
: new ResourceLocation( "computercraft", "wireless_modem" ),
|
||||
advanced
|
||||
? "upgrade.computercraft:advanced_modem.adjective"
|
||||
: "upgrade.computercraft:wireless_modem.adjective",
|
||||
PeripheralItemFactory.create(
|
||||
advanced ? PeripheralType.AdvancedModem : PeripheralType.WirelessModem,
|
||||
null, 1
|
||||
)
|
||||
);
|
||||
this.advanced = advanced;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IPeripheral createPeripheral( @Nonnull IPocketAccess access )
|
||||
{
|
||||
return new PocketModemPeripheral( m_advanced );
|
||||
return new PocketModemPeripheral( advanced );
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
|
||||
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 );
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
@ -15,22 +15,22 @@ import javax.annotation.Nonnull;
|
||||
|
||||
public class PocketModemPeripheral extends WirelessModemPeripheral
|
||||
{
|
||||
private World m_world;
|
||||
private Vec3d m_position;
|
||||
private World world;
|
||||
private Vec3d position;
|
||||
|
||||
public PocketModemPeripheral( boolean advanced )
|
||||
{
|
||||
super( new ModemState(), advanced );
|
||||
m_world = null;
|
||||
m_position = new Vec3d( 0.0, 0.0, 0.0 );
|
||||
world = null;
|
||||
position = new Vec3d( 0.0, 0.0, 0.0 );
|
||||
}
|
||||
|
||||
public void setLocation( World world, double x, double y, double z )
|
||||
{
|
||||
m_position = new Vec3d( x, y, z );
|
||||
if( m_world != world )
|
||||
position = new Vec3d( x, y, z );
|
||||
if( this.world != world )
|
||||
{
|
||||
m_world = world;
|
||||
this.world = world;
|
||||
switchNetwork();
|
||||
}
|
||||
}
|
||||
@ -39,18 +39,14 @@ public class PocketModemPeripheral extends WirelessModemPeripheral
|
||||
@Override
|
||||
public World getWorld()
|
||||
{
|
||||
return m_world;
|
||||
return world;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Vec3d getPosition()
|
||||
{
|
||||
if( m_world != null )
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
return null;
|
||||
return world != null ? position : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -8,42 +8,24 @@ package dan200.computercraft.shared.pocket.peripherals;
|
||||
|
||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||
import dan200.computercraft.api.pocket.IPocketAccess;
|
||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||
import dan200.computercraft.shared.peripheral.PeripheralType;
|
||||
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class PocketSpeaker implements IPocketUpgrade
|
||||
public class PocketSpeaker extends AbstractPocketUpgrade
|
||||
{
|
||||
public PocketSpeaker()
|
||||
{
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
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 );
|
||||
super(
|
||||
new ResourceLocation( "computercraft", "speaker" ),
|
||||
"upgrade.computercraft:speaker.adjective",
|
||||
PeripheralItemFactory.create( PeripheralType.Speaker, null, 1 )
|
||||
);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@ -56,24 +38,22 @@ public class PocketSpeaker implements IPocketUpgrade
|
||||
@Override
|
||||
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();
|
||||
|
||||
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 );
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
@ -13,46 +13,37 @@ import net.minecraft.world.World;
|
||||
|
||||
public class PocketSpeakerPeripheral extends SpeakerPeripheral
|
||||
{
|
||||
private World m_world;
|
||||
private BlockPos m_position;
|
||||
private World world;
|
||||
private BlockPos position;
|
||||
|
||||
PocketSpeakerPeripheral()
|
||||
{
|
||||
super();
|
||||
m_world = null;
|
||||
m_position = new BlockPos( 0.0, 0.0, 0.0 );
|
||||
world = null;
|
||||
position = BlockPos.ORIGIN;
|
||||
}
|
||||
|
||||
void setLocation( World world, double x, double y, double z )
|
||||
{
|
||||
m_position = new BlockPos( x, y, z );
|
||||
|
||||
if( m_world != world )
|
||||
{
|
||||
m_world = world;
|
||||
}
|
||||
position = new BlockPos( x, y, z );
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
@Override
|
||||
public World getWorld()
|
||||
{
|
||||
return m_world;
|
||||
return world;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockPos getPos()
|
||||
{
|
||||
if( m_world != null )
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
return null;
|
||||
return world != null ? position : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -83,12 +83,6 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
new ResourceLocation( ComputerCraft.MOD_ID, "turtle_player" ), TurtlePlayer.class, "turtle_player",
|
||||
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
|
||||
@ -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 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" ) ) );
|
||||
|
||||
registerUpgrades();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
|
@ -333,6 +333,8 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
// Pocket computer
|
||||
ComputerCraft.Items.pocketComputer = new ItemPocketComputer();
|
||||
registry.register( ComputerCraft.Items.pocketComputer.setRegistryName( new ResourceLocation( ComputerCraft.MOD_ID, "pocket_computer" ) ) );
|
||||
|
||||
registerUpgrades();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@ -380,15 +382,6 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
|
||||
// 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
|
||||
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
|
||||
public void remapItems( RegistryEvent.MissingMappings<Item> mappings )
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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" );
|
||||
|
@ -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
|
||||
|
@ -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 )
|
||||
|
Loading…
Reference in New Issue
Block a user