Update CT integration

Sadly we have to disable -Werror, as the annotation class files are not
available on maven, so this produces a warning.
This commit is contained in:
SquidDev 2019-12-24 19:16:06 +00:00
parent 037cbabb32
commit 4320a4f851
12 changed files with 240 additions and 127 deletions

View File

@ -139,7 +139,7 @@ accessTransformer file('src/main/resources/META-INF/accesstransformer.cfg')
[compileJava, compileTestJava].forEach {
it.configure {
options.compilerArgs << "-Xlint" << "-Xlint:-processing" << "-Werror"
options.compilerArgs << "-Xlint" << "-Xlint:-processing"
}
}

View File

@ -11,7 +11,6 @@
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.util.InventoryUtil;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModLoadingContext;
import javax.annotation.Nonnull;
@ -21,21 +20,18 @@
public final class TurtleUpgrades
{
public static class Wrapper
private static class Wrapper
{
final ITurtleUpgrade upgrade;
final int legacyId;
final String id;
final String modId;
boolean enabled;
public Wrapper( ITurtleUpgrade upgrade )
{
ModContainer mc = ModLoadingContext.get().getActiveContainer();
this.upgrade = upgrade;
this.id = upgrade.getUpgradeID().toString();
this.modId = mc != null && mc.getModId() != null ? mc.getModId() : null;
this.modId = ModLoadingContext.get().getActiveNamespace();
this.enabled = true;
}
}
@ -44,19 +40,21 @@ public Wrapper( ITurtleUpgrade upgrade )
private static final Map<String, ITurtleUpgrade> upgrades = new HashMap<>();
private static final IdentityHashMap<ITurtleUpgrade, Wrapper> wrappers = new IdentityHashMap<>();
private static boolean needsRebuild;
private TurtleUpgrades() {}
public static void register( @Nonnull ITurtleUpgrade upgrade )
{
Objects.requireNonNull( upgrade, "upgrade cannot be null" );
rebuild();
Wrapper wrapper = new Wrapper( upgrade );
String id = wrapper.id;
ITurtleUpgrade existing = upgrades.get( id );
if( existing != null )
{
throw new IllegalStateException( "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turle'. UpgradeID '" + id + "' is already registered by '" + existing.getUnlocalisedAdjective() + " Turtle'" );
throw new IllegalStateException( "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turtle'. Upgrade ID '" + id + "' is already registered by '" + existing.getUnlocalisedAdjective() + " Turtle'" );
}
upgrades.put( id, upgrade );
@ -66,15 +64,10 @@ public static void register( @Nonnull ITurtleUpgrade upgrade )
@Nullable
public static ITurtleUpgrade get( String id )
{
rebuild();
return upgrades.get( id );
}
@Nullable
public static ITurtleUpgrade get( int id )
{
return legacyUpgrades.get( id );
}
@Nullable
public static String getOwner( @Nonnull ITurtleUpgrade upgrade )
{
@ -86,12 +79,14 @@ public static ITurtleUpgrade get( @Nonnull ItemStack stack )
{
if( stack.isEmpty() ) return null;
for( ITurtleUpgrade upgrade : upgrades.values() )
for( Wrapper wrapper : wrappers.values() )
{
ItemStack craftingStack = upgrade.getCraftingItem();
if( !wrapper.enabled ) continue;
ItemStack craftingStack = wrapper.upgrade.getCraftingItem();
if( !craftingStack.isEmpty() && InventoryUtil.areItemsSimilar( stack, craftingStack ) )
{
return upgrade;
return wrapper.upgrade;
}
}
@ -121,9 +116,9 @@ public static Stream<ITurtleUpgrade> getVanillaUpgrades()
return Arrays.stream( vanilla ).filter( x -> x != null && wrappers.get( x ).enabled );
}
public static Iterable<ITurtleUpgrade> getUpgrades()
public static Stream<ITurtleUpgrade> getUpgrades()
{
return Collections.unmodifiableCollection( upgrades.values() );
return wrappers.values().stream().filter( x -> x.enabled ).map( x -> x.upgrade );
}
public static boolean suitableForFamily( ComputerFamily family, ITurtleUpgrade upgrade )
@ -131,6 +126,41 @@ public static boolean suitableForFamily( ComputerFamily family, ITurtleUpgrade u
return true;
}
/**
* Rebuild the cache of turtle upgrades. This is done before querying the cache or registering new upgrades.
*/
private static void rebuild()
{
if( !needsRebuild ) return;
upgrades.clear();
for( Wrapper wrapper : wrappers.values() )
{
if( !wrapper.enabled ) continue;
ITurtleUpgrade existing = upgrades.get( wrapper.id );
if( existing != null )
{
ComputerCraft.log.error( "Error registering '" + wrapper.upgrade.getUnlocalisedAdjective() + " Turtle'." +
" Upgrade ID '" + wrapper.id + "' is already registered by '" + existing.getUnlocalisedAdjective() + " Turtle'" );
continue;
}
upgrades.put( wrapper.id, wrapper.upgrade );
}
needsRebuild = false;
}
public static void enable( ITurtleUpgrade upgrade )
{
Wrapper wrapper = wrappers.get( upgrade );
if( wrapper.enabled ) return;
wrapper.enabled = true;
needsRebuild = true;
}
public static void disable( ITurtleUpgrade upgrade )
{
Wrapper wrapper = wrappers.get( upgrade );
@ -138,6 +168,11 @@ public static void disable( ITurtleUpgrade upgrade )
wrapper.enabled = false;
upgrades.remove( wrapper.id );
if( wrapper.legacyId >= 0 ) legacyUpgrades.remove( wrapper.legacyId );
}
public static void remove( ITurtleUpgrade upgrade )
{
wrappers.remove( upgrade );
needsRebuild = true;
}
}

View File

@ -0,0 +1,40 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.integration.crafttweaker;
import com.blamejared.crafttweaker.api.logger.ILogger;
/**
* Logger which tracks if it has any messages.
*/
public final class TrackingLogger
{
private final ILogger logger;
private boolean ok = true;
public TrackingLogger( ILogger logger )
{
this.logger = logger;
}
public boolean isOk()
{
return ok;
}
public void warning( String message )
{
ok = false;
logger.warning( message );
}
public void error( String message )
{
ok = false;
logger.error( message );
}
}

View File

@ -6,65 +6,67 @@
package dan200.computercraft.shared.integration.crafttweaker;
import crafttweaker.CraftTweakerAPI;
import crafttweaker.annotations.ModOnly;
import crafttweaker.annotations.ZenDoc;
import crafttweaker.annotations.ZenRegister;
import crafttweaker.api.item.IItemStack;
import crafttweaker.api.minecraft.CraftTweakerMC;
import dan200.computercraft.ComputerCraft;
import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.item.IItemStack;
import dan200.computercraft.shared.integration.crafttweaker.actions.AddTurtleTool;
import dan200.computercraft.shared.integration.crafttweaker.actions.RemoveTurtleUpgradeByItem;
import dan200.computercraft.shared.integration.crafttweaker.actions.RemoveTurtleUpgradeByName;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
import org.openzen.zencode.java.ZenCodeType;
@ZenRegister
@ZenClass( "dan200.computercraft.turtle" )
@ModOnly( ComputerCraft.MOD_ID )
@ZenCodeType.Name( "dan200.computercraft.turtle" )
public class TurtleTweaker
{
@ZenMethod
@ZenDoc( "Remove a turtle upgrade with the given id" )
/**
* Remove a turtle upgrade with the given id
*
* @param upgrade The ID of the to remove
*/
@ZenCodeType.Method
public static void removeUpgrade( String upgrade )
{
CraftTweakerAPI.apply( new RemoveTurtleUpgradeByName( upgrade ) );
}
@ZenMethod
@ZenDoc( "Remove a turtle upgrade crafted with the given item stack" )
/**
* Remove a turtle upgrade crafted with the given item stack"
*
* @param stack The stack with which the upgrade is crafted.
*/
@ZenCodeType.Method
public static void removeUpgrade( IItemStack stack )
{
CraftTweakerAPI.apply( new RemoveTurtleUpgradeByItem( CraftTweakerMC.getItemStack( stack ) ) );
CraftTweakerAPI.apply( new RemoveTurtleUpgradeByItem( stack.getInternal() ) );
}
@ZenMethod
@ZenDoc( "Add a new turtle tool with the given id, which crafts and acts using the given stack." )
/**
* Add a new turtle tool with the given id, which crafts and acts using the given stack.
*
* @param id The new upgrade's ID
* @param stack The stack used for crafting the upgrade and used by the turtle as a tool.
*/
@ZenCodeType.Method
public static void addTool( String id, IItemStack stack )
{
addTool( id, stack, stack, "tool" );
}
@ZenMethod
@ZenDoc( "Add a new turtle tool with the given id, which is crafted with one item, and uses another." )
@ZenCodeType.Method
public static void addTool( String id, IItemStack craftingStack, IItemStack toolStack )
{
addTool( id, craftingStack, toolStack, "tool" );
}
@ZenMethod
@ZenDoc( "Add a new turtle tool with the given id, which crafts and acts using the given stack. You may also" +
"specify a 'kind' of tool, which limits what blocks the turtle can break (for instance, an 'axe' may only break wood)." )
@ZenCodeType.Method
public static void addTool( String id, IItemStack stack, String kind )
{
addTool( id, stack, stack, kind );
}
@ZenMethod
@ZenDoc( "Add a new turtle tool with the given id, which is crafted with one item, and uses another. You may also" +
"specify a 'kind' of tool, which limits what blocks the turtle can break (for instance, an 'axe' may only break wood)." )
@ZenCodeType.Method
public static void addTool( String id, IItemStack craftingStack, IItemStack toolStack, String kind )
{
CraftTweakerAPI.apply( new AddTurtleTool( id, CraftTweakerMC.getItemStack( craftingStack ), CraftTweakerMC.getItemStack( toolStack ), kind ) );
CraftTweakerAPI.apply( new AddTurtleTool( id, craftingStack.getInternal(), toolStack.getInternal(), kind ) );
}
}

View File

@ -6,20 +6,24 @@
package dan200.computercraft.shared.integration.crafttweaker.actions;
import com.blamejared.crafttweaker.api.actions.IUndoableAction;
import com.blamejared.crafttweaker.api.logger.ILogger;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.shared.TurtleUpgrades;
import dan200.computercraft.shared.integration.crafttweaker.TrackingLogger;
import dan200.computercraft.shared.turtle.upgrades.*;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.LogicalSide;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* Register a new turtle tool.
*/
public class AddTurtleTool implements IAction
public class AddTurtleTool implements IUndoableAction
{
private interface Factory
{
@ -42,6 +46,8 @@ private interface Factory
private final ItemStack toolItem;
private final String kind;
private ITurtleUpgrade upgrade;
public AddTurtleTool( String id, ItemStack craftItem, ItemStack toolItem, String kind )
{
this.id = id;
@ -53,16 +59,22 @@ public AddTurtleTool( String id, ItemStack craftItem, ItemStack toolItem, String
@Override
public void apply()
{
Factory factory = kinds.get( kind );
if( factory == null )
ITurtleUpgrade upgrade = this.upgrade;
if( upgrade == null )
{
ComputerCraft.log.error( "Unknown turtle upgrade kind '{}' (this should have been rejected by verify!)", kind );
return;
Factory factory = kinds.get( kind );
if( factory == null )
{
ComputerCraft.log.error( "Unknown turtle upgrade kind '{}' (this should have been rejected by verify!)", kind );
return;
}
upgrade = this.upgrade = factory.create( new ResourceLocation( id ), craftItem, toolItem );
}
try
{
TurtleUpgrades.register( factory.create( new ResourceLocation( id ), craftItem, toolItem ) );
TurtleUpgrades.register( upgrade );
}
catch( RuntimeException e )
{
@ -76,21 +88,40 @@ public String describe()
return String.format( "Add new turtle %s '%s' (crafted with '%s', uses a '%s')", kind, id, craftItem, toolItem );
}
public Optional<String> getValidationProblem()
@Override
public void undo()
{
if( craftItem.isEmpty() ) return Optional.of( "Crafting item stack is empty." );
if( craftItem.hasTagCompound() && !craftItem.getTagCompound().isEmpty() )
{
return Optional.of( "Crafting item has NBT." );
}
if( toolItem.isEmpty() ) return Optional.of( "Tool item stack is empty." );
if( !kinds.containsKey( kind ) ) return Optional.of( String.format( "Unknown kind '%s'.", kind ) );
if( upgrade != null ) TurtleUpgrades.remove( upgrade );
}
@Override
public String describeUndo()
{
return String.format( "Removing turtle upgrade %s.", id );
}
public boolean validate( ILogger logger )
{
TrackingLogger trackLog = new TrackingLogger( logger );
if( craftItem.isEmpty() ) trackLog.warning( "Crafting item stack is empty." );
if( craftItem.hasTag() && !craftItem.getTag().isEmpty() ) trackLog.warning( "Crafting item has NBT." );
if( toolItem.isEmpty() ) trackLog.error( "Tool item stack is empty." );
if( !kinds.containsKey( kind ) ) trackLog.error( String.format( "Unknown kind '%s'.", kind ) );
if( TurtleUpgrades.get( id ) != null )
{
return Optional.of( String.format( "An upgrade with the same name ('%s') has already been registered.", id ) );
trackLog.error( String.format( "An upgrade with the same name ('%s') has already been registered.", id ) );
}
return Optional.empty();
return trackLog.isOk();
}
@Override
public boolean shouldApplyOn( LogicalSide side )
{
return true;
}
}

View File

@ -1,32 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.integration.crafttweaker.actions;
import java.util.Optional;
/**
* An extension of {@link IAction} with a single validation function, rather than two.
*/
public interface IAction extends crafttweaker.IAction
{
default Optional<String> getValidationProblem()
{
return Optional.empty();
}
@Override
default boolean validate()
{
return !getValidationProblem().isPresent();
}
@Override
default String describeInvalid()
{
return getValidationProblem().orElse( "No problems found." );
}
}

View File

@ -6,18 +6,20 @@
package dan200.computercraft.shared.integration.crafttweaker.actions;
import com.blamejared.crafttweaker.api.actions.IUndoableAction;
import com.blamejared.crafttweaker.api.logger.ILogger;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.shared.TurtleUpgrades;
import net.minecraft.item.ItemStack;
import java.util.Optional;
import net.minecraftforge.fml.LogicalSide;
/**
* Removes a turtle upgrade crafted with the given stack.
*/
public class RemoveTurtleUpgradeByItem implements IAction
public class RemoveTurtleUpgradeByItem implements IUndoableAction
{
private final ItemStack stack;
private ITurtleUpgrade upgrade;
public RemoveTurtleUpgradeByItem( ItemStack stack )
{
@ -27,7 +29,7 @@ public RemoveTurtleUpgradeByItem( ItemStack stack )
@Override
public void apply()
{
ITurtleUpgrade upgrade = TurtleUpgrades.get( stack );
ITurtleUpgrade upgrade = this.upgrade = TurtleUpgrades.get( stack );
if( upgrade != null ) TurtleUpgrades.disable( upgrade );
}
@ -38,13 +40,32 @@ public String describe()
}
@Override
public Optional<String> getValidationProblem()
public void undo()
{
if( this.upgrade != null ) TurtleUpgrades.enable( upgrade );
}
@Override
public String describeUndo()
{
return String.format( "Adding back turtle upgrades crafted with '%s'", stack );
}
@Override
public boolean validate( ILogger logger )
{
if( TurtleUpgrades.get( stack ) == null )
{
return Optional.of( String.format( "Unknown turtle upgrade crafted with '%s'.", stack ) );
logger.error( String.format( "Unknown turtle upgrade crafted with '%s'.", stack ) );
return false;
}
return Optional.empty();
return true;
}
@Override
public boolean shouldApplyOn( LogicalSide side )
{
return true;
}
}

View File

@ -6,17 +6,19 @@
package dan200.computercraft.shared.integration.crafttweaker.actions;
import com.blamejared.crafttweaker.api.actions.IUndoableAction;
import com.blamejared.crafttweaker.api.logger.ILogger;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.shared.TurtleUpgrades;
import java.util.Optional;
import net.minecraftforge.fml.LogicalSide;
/**
* Removes a turtle upgrade with the given id.
*/
public class RemoveTurtleUpgradeByName implements IAction
public class RemoveTurtleUpgradeByName implements IUndoableAction
{
private final String id;
private ITurtleUpgrade upgrade;
public RemoveTurtleUpgradeByName( String id )
{
@ -26,7 +28,7 @@ public RemoveTurtleUpgradeByName( String id )
@Override
public void apply()
{
ITurtleUpgrade upgrade = TurtleUpgrades.get( id );
ITurtleUpgrade upgrade = this.upgrade = TurtleUpgrades.get( id );
if( upgrade != null ) TurtleUpgrades.disable( upgrade );
}
@ -37,13 +39,32 @@ public String describe()
}
@Override
public Optional<String> getValidationProblem()
public void undo()
{
if( this.upgrade != null ) TurtleUpgrades.enable( upgrade );
}
@Override
public String describeUndo()
{
return String.format( "Adding back turtle upgrade '%s'", id );
}
@Override
public boolean validate( ILogger logger )
{
if( TurtleUpgrades.get( id ) == null )
{
return Optional.of( String.format( "Unknown turtle upgrade '%s'.", id ) );
logger.error( String.format( "Unknown turtle upgrade '%s'.", id ) );
return false;
}
return Optional.empty();
return true;
}
@Override
public boolean shouldApplyOn( LogicalSide side )
{
return true;
}
}

View File

@ -76,12 +76,10 @@ public void onRuntimeAvailable( IJeiRuntime runtime )
List<ItemStack> upgradeItems = new ArrayList<>();
for( ComputerFamily family : MAIN_FAMILIES )
{
for( ITurtleUpgrade upgrade : TurtleUpgrades.getUpgrades() )
{
if( !TurtleUpgrades.suitableForFamily( family, upgrade ) ) continue;
upgradeItems.add( TurtleItemFactory.create( -1, null, -1, family, null, upgrade, 0, null ) );
}
TurtleUpgrades.getUpgrades()
.filter( x -> TurtleUpgrades.suitableForFamily( family, x ) )
.map( x -> TurtleItemFactory.create( -1, null, -1, family, null, x, 0, null ) )
.forEach( upgradeItems::add );
for( IPocketUpgrade upgrade : PocketUpgrades.getUpgrades() )
{

View File

@ -53,15 +53,14 @@ private void setupCache()
if( initialised ) return;
initialised = true;
for( ITurtleUpgrade upgrade : TurtleUpgrades.getUpgrades() )
{
TurtleUpgrades.getUpgrades().forEach( upgrade -> {
ItemStack stack = upgrade.getCraftingItem();
if( stack.isEmpty() ) continue;
if( stack.isEmpty() ) return;
UpgradeInfo info = new UpgradeInfo( stack, upgrade );
upgradeItemLookup.computeIfAbsent( stack.getItem(), k -> new ArrayList<>( 1 ) ).add( info );
turtleUpgrades.add( info );
}
} );
for( IPocketUpgrade upgrade : PocketUpgrades.getUpgrades() )
{

View File

@ -66,12 +66,10 @@ public void fillItemGroup( @Nonnull ItemGroup group, @Nonnull NonNullList<ItemSt
ComputerFamily family = getFamily();
list.add( create( -1, null, -1, null, null, 0, null ) );
for( ITurtleUpgrade upgrade : TurtleUpgrades.getVanillaUpgrades() )
{
if( !TurtleUpgrades.suitableForFamily( family, upgrade ) ) continue;
list.add( create( -1, null, -1, null, upgrade, 0, null ) );
}
TurtleUpgrades.getVanillaUpgrades()
.filter( x -> TurtleUpgrades.suitableForFamily( family, x ) )
.map( x -> create( -1, null, -1, null, x, 0, null ) )
.forEach( list::add );
}
@Nonnull

View File

@ -65,7 +65,7 @@ public TurtleTool( ResourceLocation id, Item item )
public TurtleTool( ResourceLocation id, ItemStack craftItem, ItemStack toolItem )
{
super( id, -1, TurtleUpgradeType.Tool, craftItem );
super( id, TurtleUpgradeType.Tool, craftItem );
this.item = toolItem;
}