1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-11 09:50:29 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/Peripherals.java
SquidDev 173ea72001 Turn inspections up to 11
OK, so let's get this out of the way, there's some actual changes mixed
in here too. I'm really sorry:
 - Turtles can now not be renamed with unnamed item tags (previously it
   would clear the name, this seemed a little unideal).
 - commands.getBlock(s)Data will also include NBT.

Now, onto the horror story which is these inspection changes:
 - Make a lot of methods static
 - Typo fixes
 - Make utility classes final + private constructor
 - Lots of reformatting (ifs -> ternary, invert control flow, etc...)
 - ???
 - Profit!

I'm so going to regret this - can pretty much guarantee this is going to
break something.
2019-03-29 21:26:21 +00:00

57 lines
1.8 KiB
Java

/*
* 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;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Objects;
public final class Peripherals
{
private static final Collection<IPeripheralProvider> providers = ComputerCraft.peripheralProviders;
private Peripherals() {}
public static void register( @Nonnull IPeripheralProvider provider )
{
Objects.requireNonNull( provider, "provider cannot be null" );
if( !providers.contains( provider ) ) providers.add( provider );
}
public static IPeripheral getPeripheral( World world, BlockPos pos, EnumFacing side )
{
return world.isValid( pos ) && !world.isRemote ? getPeripheralAt( world, pos, side ) : null;
}
private static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side )
{
// Try the handlers in order:
for( IPeripheralProvider peripheralProvider : providers )
{
try
{
IPeripheral peripheral = peripheralProvider.getPeripheral( world, pos, side );
if( peripheral != null ) return peripheral;
}
catch( Exception e )
{
ComputerCraft.log.error( "Peripheral provider " + peripheralProvider + " errored.", e );
}
}
return null;
}
}