2018-12-10 23:46:50 +00:00
/ *
* This file is part of ComputerCraft - http : //www.computercraft.info
2020-01-01 00:09:18 +00:00
* Copyright Daniel Ratcliffe , 2011 - 2020 . Do not distribute without permission .
2018-12-10 23:46:50 +00:00
* Send enquiries to dratcliffe @gmail.com
* /
package dan200.computercraft.shared ;
import dan200.computercraft.ComputerCraft ;
import dan200.computercraft.api.turtle.ITurtleUpgrade ;
import dan200.computercraft.shared.computer.core.ComputerFamily ;
import dan200.computercraft.shared.util.InventoryUtil ;
import net.minecraft.item.ItemStack ;
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 13:27:27 +01:00
import net.minecraftforge.fml.ModLoadingContext ;
2018-12-10 23:46:50 +00:00
import javax.annotation.Nonnull ;
2019-03-16 01:49:02 +00:00
import javax.annotation.Nullable ;
import java.util.* ;
2019-12-18 15:29:24 +00:00
import java.util.stream.Stream ;
2018-12-10 23:46:50 +00:00
public final class TurtleUpgrades
{
2019-12-24 19:16:06 +00:00
private static class Wrapper
2019-12-18 15:29:24 +00:00
{
final ITurtleUpgrade upgrade ;
final String id ;
final String modId ;
boolean enabled ;
2019-12-24 19:31:14 +00:00
Wrapper ( ITurtleUpgrade upgrade )
2019-12-18 15:29:24 +00:00
{
this . upgrade = upgrade ;
this . id = upgrade . getUpgradeID ( ) . toString ( ) ;
2019-12-24 19:16:06 +00:00
this . modId = ModLoadingContext . get ( ) . getActiveNamespace ( ) ;
2019-12-18 15:29:24 +00:00
this . enabled = true ;
}
}
private static ITurtleUpgrade [ ] vanilla ;
2018-12-10 23:46:50 +00:00
private static final Map < String , ITurtleUpgrade > upgrades = new HashMap < > ( ) ;
2019-12-18 15:29:24 +00:00
private static final IdentityHashMap < ITurtleUpgrade , Wrapper > wrappers = new IdentityHashMap < > ( ) ;
2019-12-24 19:16:06 +00:00
private static boolean needsRebuild ;
2018-12-10 23:46:50 +00:00
2019-03-29 21:21:39 +00:00
private TurtleUpgrades ( ) { }
2018-12-10 23:46:50 +00:00
public static void register ( @Nonnull ITurtleUpgrade upgrade )
{
2019-03-29 21:21:39 +00:00
Objects . requireNonNull ( upgrade , " upgrade cannot be null " ) ;
2019-12-24 19:16:06 +00:00
rebuild ( ) ;
2018-12-10 23:46:50 +00:00
2019-12-18 15:29:24 +00:00
Wrapper wrapper = new Wrapper ( upgrade ) ;
String id = wrapper . id ;
2018-12-10 23:46:50 +00:00
ITurtleUpgrade existing = upgrades . get ( id ) ;
if ( existing ! = null )
{
2019-12-24 19:16:06 +00:00
throw new IllegalStateException ( " Error registering ' " + upgrade . getUnlocalisedAdjective ( ) + " Turtle'. Upgrade ID ' " + id + " ' is already registered by ' " + existing . getUnlocalisedAdjective ( ) + " Turtle' " ) ;
2018-12-10 23:46:50 +00:00
}
upgrades . put ( id , upgrade ) ;
2019-12-18 15:29:24 +00:00
wrappers . put ( upgrade , wrapper ) ;
2018-12-10 23:46:50 +00:00
}
2019-12-18 15:29:24 +00:00
@Nullable
2018-12-10 23:46:50 +00:00
public static ITurtleUpgrade get ( String id )
{
2019-12-24 19:16:06 +00:00
rebuild ( ) ;
2018-12-10 23:46:50 +00:00
return upgrades . get ( id ) ;
}
2019-12-18 15:29:24 +00:00
@Nullable
public static String getOwner ( @Nonnull ITurtleUpgrade upgrade )
{
Wrapper wrapper = wrappers . get ( upgrade ) ;
return wrapper ! = null ? wrapper . modId : null ;
}
2018-12-10 23:46:50 +00:00
public static ITurtleUpgrade get ( @Nonnull ItemStack stack )
{
if ( stack . isEmpty ( ) ) return null ;
2019-12-24 19:16:06 +00:00
for ( Wrapper wrapper : wrappers . values ( ) )
2018-12-10 23:46:50 +00:00
{
2019-12-24 19:16:06 +00:00
if ( ! wrapper . enabled ) continue ;
ItemStack craftingStack = wrapper . upgrade . getCraftingItem ( ) ;
Be less strict in comparing upgrade crafting items
We currently generate the crafting item once when the upgrade is first
created, and cache it for the duration of the game. As the item never
changes throughout the game, and constructing a stack is a little
expensive (we need to fire an event, etc...), the caching is worth
having.
However, some mods may register capabilities after we've constructed our
ItemStack. This means the capability will be present on other items but
not ours, meaning they are not considered equivalent, and thus the item
cannot be equipped.
In order to avoid this, we use compare items using their share-tag, like
Forge's IngredientNBT. This means the items must still be "mostly" the
same (same enchantements, etc...), but allow differing capabilities.
See NillerMedDild/Enigmatica2Expert#655 for the original bug report -
in this case, Astral Sourcery was registering the capability in init,
but we construct upgrades just before then.
2019-01-19 21:57:21 +00:00
if ( ! craftingStack . isEmpty ( ) & & InventoryUtil . areItemsSimilar ( stack , craftingStack ) )
2018-12-10 23:46:50 +00:00
{
2019-12-24 19:16:06 +00:00
return wrapper . upgrade ;
2018-12-10 23:46:50 +00:00
}
}
return null ;
}
2019-12-18 15:29:24 +00:00
public static Stream < ITurtleUpgrade > getVanillaUpgrades ( )
2018-12-10 23:46:50 +00:00
{
2019-12-18 15:29:24 +00:00
if ( vanilla = = null )
{
vanilla = new ITurtleUpgrade [ ] {
2019-12-23 22:34:28 +00:00
// ComputerCraft upgrades
ComputerCraft . TurtleUpgrades . wirelessModemNormal ,
ComputerCraft . TurtleUpgrades . wirelessModemAdvanced ,
ComputerCraft . TurtleUpgrades . speaker ,
// Vanilla Minecraft upgrades
2019-12-18 15:29:24 +00:00
ComputerCraft . TurtleUpgrades . diamondPickaxe ,
ComputerCraft . TurtleUpgrades . diamondAxe ,
ComputerCraft . TurtleUpgrades . diamondSword ,
ComputerCraft . TurtleUpgrades . diamondShovel ,
ComputerCraft . TurtleUpgrades . diamondHoe ,
ComputerCraft . TurtleUpgrades . craftingTable ,
} ;
}
2018-12-10 23:46:50 +00:00
2019-12-18 15:29:24 +00:00
return Arrays . stream ( vanilla ) . filter ( x - > x ! = null & & wrappers . get ( x ) . enabled ) ;
2019-03-16 01:49:02 +00:00
}
2019-12-24 19:16:06 +00:00
public static Stream < ITurtleUpgrade > getUpgrades ( )
2019-03-16 01:49:02 +00:00
{
2019-12-24 19:16:06 +00:00
return wrappers . values ( ) . stream ( ) . filter ( x - > x . enabled ) . map ( x - > x . upgrade ) ;
2019-03-16 01:49:02 +00:00
}
2018-12-10 23:46:50 +00:00
public static boolean suitableForFamily ( ComputerFamily family , ITurtleUpgrade upgrade )
{
return true ;
}
2019-12-18 15:29:24 +00:00
2019-12-24 19:16:06 +00:00
/ * *
* 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 ;
}
2019-12-18 15:29:24 +00:00
public static void disable ( ITurtleUpgrade upgrade )
{
Wrapper wrapper = wrappers . get ( upgrade ) ;
if ( ! wrapper . enabled ) return ;
wrapper . enabled = false ;
upgrades . remove ( wrapper . id ) ;
2019-12-24 19:16:06 +00:00
}
public static void remove ( ITurtleUpgrade upgrade )
{
wrappers . remove ( upgrade ) ;
needsRebuild = true ;
2019-12-18 15:29:24 +00:00
}
2018-12-10 23:46:50 +00:00
}