dan200.computercraft.api.peripheral.IPeripheral Java Examples

The following examples show how to use dan200.computercraft.api.peripheral.IPeripheral. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 6 votes vote down vote up
@Override
protected IPeripheralFactory<TileEntity> create(Class<? extends TileEntity> targetCls) {
	try {
		if (IPeripheral.class.isAssignableFrom(targetCls)) return NULL_FACTORY;
		if (ICustomPeripheralProvider.class.isAssignableFrom(targetCls)) return PROVIDER_ADAPTER;
		if (TileEntityBlacklist.INSTANCE.isBlacklisted(targetCls)) return NULL_FACTORY;

		final IndexedMethodMap methods = getMethodsForClass(targetCls);
		if (methods.isEmpty()) return NULL_FACTORY;

		final Set<Class<?>> proxyClasses = getProxyClasses(targetCls);
		return proxyClasses.isEmpty()? createDirectFactory(methods) : createProxyFactory(methods, targetCls, proxyClasses);
	} catch (Exception e) {
		Log.warn(e, "Failed to create factory for %s", targetCls);
		return SafePeripheralFactory.BROKEN_FACTORY;
	}
}
 
Example #2
Source File: TileEntityPeripheralProxy.java    From OpenPeripheral-Addons with MIT License 6 votes vote down vote up
@Override
public IPeripheral createPeripheral(int side) {
	final ForgeDirection rotation = getOrientation().up();
	if (rotation.getOpposite().ordinal() != side) return null;

	final int targetX = xCoord + rotation.offsetX;
	final int targetY = yCoord + rotation.offsetY;
	final int targetZ = zCoord + rotation.offsetZ;

	IPeripheral peripheral = access.getPeripheralAt.call(null, worldObj, targetX, targetY, targetZ, 0);
	if (peripheral != null) {
		attachedBlock = worldObj.getBlock(targetX, targetY, targetZ);
		return new WrappedPeripheral(peripheral);
	} else {
		attachedBlock = null;
		return null;
	}
}
 
Example #3
Source File: SafePeripheralFactory.java    From OpenPeripheral with MIT License 6 votes vote down vote up
@Override
public IPeripheral getPeripheral(TileEntity tile, int side) {
	if (tile == null) return null;

	try {
		return createPeripheral(tile, side);
	} catch (InvalidClassException e) {
		Throwable cause = e.getCause();
		if (cause != null) {
			Log.severe(cause, "Can't create peripheral for TE %s @ (%d,%d,%d) in world %s due to error in class",
					tile.getClass(), tile.xCoord, tile.yCoord, tile.zCoord, tile.getWorldObj().provider.dimensionId);
		} else {
			Log.severe("Can't create peripheral for TE %s @ (%d,%d,%d) in world %s due to error in class %s",
					tile.getClass(), tile.xCoord, tile.yCoord, tile.zCoord, tile.getWorldObj().provider.dimensionId, tile.getClass());
		}

	} catch (Throwable t) {
		Log.severe(t, "Can't create peripheral for TE %s @ (%d,%d,%d) in world %s",
				tile.getClass(), tile.xCoord, tile.yCoord, tile.zCoord, tile.getWorldObj().provider.dimensionId);

	}
	return PLACEHOLDER;
}
 
Example #4
Source File: TileEntityBase.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public boolean equals(IPeripheral other){
    if(other == null) {
        return false;
    }
    if(this == other) {
        return true;
    }
    if(other instanceof TileEntity) {
        TileEntity tother = (TileEntity)other;
        return tother.getWorldObj().equals(worldObj) && tother.xCoord == xCoord && tother.yCoord == yCoord && tother.zCoord == zCoord;
    }

    return false;
}
 
Example #5
Source File: TileEntityDroneInterface.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public boolean equals(IPeripheral other){
    if(other == null) {
        return false;
    }
    if(this == other) {
        return true;
    }
    if(other instanceof TileEntity) {
        TileEntity tother = (TileEntity)other;
        return tother.getWorldObj().equals(worldObj) && tother.xCoord == xCoord && tother.yCoord == yCoord && tother.zCoord == zCoord;
    }

    return false;
}
 
Example #6
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 5 votes vote down vote up
public static IPeripheral createAdaptedPeripheral(Object target) {
	final Class<?> targetClass = target.getClass();
	final IndexedMethodMap methods = getMethodsForClass(targetClass);
	if (methods.isEmpty()) return null;

	final Set<Class<?>> proxied = getProxyClasses(targetClass);
	if (proxied.isEmpty()) return new AdapterPeripheral(methods, target);

	final Set<Class<?>> allImplemented = appendCommonInterfaces(proxied);
	final InvocationHandler handler = new ProxyAdapterPeripheral(methods, target);
	final Class<?>[] interfaces = allImplemented.toArray(new Class<?>[allImplemented.size()]);
	return (IPeripheral)Proxy.newProxyInstance(targetClass.getClassLoader(), interfaces, handler);
}
 
Example #7
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 5 votes vote down vote up
private static IPeripheralFactory<TileEntity> createProxyFactory(final IndexedMethodMap methods, Class<?> cls, Set<Class<?>> proxyClasses) {
	final Set<Class<?>> interfaces = appendCommonInterfaces(proxyClasses);
	final Constructor<? extends IPeripheral> ctor = getProxyConstructor(cls, interfaces);
	ctor.setAccessible(true);

	return new SafePeripheralFactory() {
		@Override
		public IPeripheral createPeripheral(TileEntity tile, int side) throws Exception {
			final InvocationHandler handler = new ProxyAdapterPeripheral(methods, tile);
			return ctor.newInstance(handler);
		}
	};
}
 
Example #8
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 5 votes vote down vote up
private static IPeripheralFactory<TileEntity> createDirectFactory(final IndexedMethodMap methods) {
	return new SafePeripheralFactory() {
		@Override
		protected IPeripheral createPeripheral(TileEntity target, int side) {
			return new AdapterPeripheral(methods, target);
		}
	};
}
 
Example #9
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 5 votes vote down vote up
public static IPeripheral createAdaptedPeripheralWrapped(Object target) {
	Preconditions.checkNotNull(target, "Null target");
	try {
		return createAdaptedPeripheral(target);
	} catch (Throwable t) {
		throw new GenerationFailedException(String.format("%s (%s)", target, target.getClass()), t);
	}
}
 
Example #10
Source File: BlockReactorPart.java    From BigReactors with MIT License 5 votes vote down vote up
@Optional.Method(modid ="ComputerCraft")
@Override
public IPeripheral getPeripheral(World world, int x, int y, int z, int side) {
	TileEntity te = world.getTileEntity(x, y, z);
	
	if(te instanceof TileEntityReactorComputerPort)
		return (IPeripheral)te;
	
	return null;
}
 
Example #11
Source File: BlockPneumaticCraft.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Produce an peripheral implementation from a block location.
 * @see dan200.computercraft.api.ComputerCraftAPI#registerPeripheralProvider(IPeripheralProvider)
 * @return a peripheral, or null if there is not a peripheral here you'd like to handle.
 */
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public IPeripheral getPeripheral(World world, int x, int y, int z, int side){
    TileEntity te = world.getTileEntity(x, y, z);
    return te instanceof IPeripheral ? (IPeripheral)te : null;
}
 
Example #12
Source File: BlockTrackEntryPeripheral.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void addInformation(World world, int x, int y, int z, TileEntity te, List<String> infoList){
    infoList.add("blockTracker.info.peripheral.title");
    infoList.add("blockTracker.info.peripheral.availableMethods");
    IPeripheral peripheral = ((IPeripheralProvider)world.getBlock(x, y, z)).getPeripheral(world, x, y, z, 0);
    if(peripheral != null) {
        for(String method : peripheral.getMethodNames()) {
            infoList.add("-" + method);
        }
    }
}
 
Example #13
Source File: BlockTrackEntryPeripheral.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean shouldTrackWithThisEntry(IBlockAccess world, int x, int y, int z, Block block, TileEntity te){
    if(block instanceof IPeripheralProvider) {
        IPeripheral peripheral = ((IPeripheralProvider)block).getPeripheral(te.getWorldObj(), x, y, z, 0);
        return peripheral != null && peripheral.getMethodNames().length != 0;
    } else {
        return false;
    }
}
 
Example #14
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@Override
public IPeripheral getPeripheral(World world, int x, int y, int z, int side) {
	final TileEntity te = world.getTileEntity(x, y, z);
	if (te == null) return null;

	final IPeripheralFactory<TileEntity> factory = getFactoryForClass(te.getClass());
	final IPeripheral peripheral = factory.getPeripheral(te, side);
	return peripheral;
}
 
Example #15
Source File: ProxyAdapterPeripheral.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	try {
		final Class<?> declaringClass = method.getDeclaringClass();
		if (declaringClass == IPeripheral.class || declaringClass == Object.class) return method.invoke(this, args);
		return method.invoke(target, args);
	} catch (InvocationTargetException e) {
		Throwable wrapper = e.getCause();
		throw wrapper != null? wrapper : e;
	}
}
 
Example #16
Source File: BlockTurbinePart.java    From BigReactors with MIT License 5 votes vote down vote up
@Optional.Method(modid = "ComputerCraft")
@Override
public IPeripheral getPeripheral(World world, int x, int y, int z, int side) {
	TileEntity te = world.getTileEntity(x, y, z);
	
	if(te instanceof TileEntityTurbineComputerPort)
		return (IPeripheral)te;
	
	return null;
}
 
Example #17
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 5 votes vote down vote up
private static Constructor<? extends IPeripheral> getProxyConstructor(Class<?> cls, final Set<Class<?>> interfaces) {
	final Class<?>[] tmp = interfaces.toArray(new Class<?>[interfaces.size()]);
	try {
		@SuppressWarnings("unchecked")
		Class<? extends IPeripheral> proxyCls = (Class<? extends IPeripheral>)Proxy.getProxyClass(cls.getClassLoader(), tmp);
		return proxyCls.getConstructor(InvocationHandler.class);
	} catch (Throwable t) {
		throw new RuntimeException(String.format("Failed to create proxy class for %s", cls), t);
	}
}
 
Example #18
Source File: PeripheralProviderFramez.java    From Framez with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IPeripheral getPeripheral(World world, int x, int y, int z, int side) {

    TileEntity te = world.getTileEntity(x, y, z);
    if (te != null && te instanceof TileMotor)
        return new PeripheralMotor((TileMotor) te);

    return null;
}
 
Example #19
Source File: SafePeripheralFactory.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public IPeripheral getPeripheral(TileEntity obj, int side) {
	return PLACEHOLDER;
}
 
Example #20
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 4 votes vote down vote up
private static Set<Class<?>> appendCommonInterfaces(Set<Class<?>> proxyClasses) {
	final Set<Class<?>> interfaces = Sets.newHashSet(proxyClasses);
	interfaces.add(IPeripheral.class);
	interfaces.add(IOpenPeripheral.class);
	return interfaces;
}
 
Example #21
Source File: SafePeripheralFactory.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public boolean equals(IPeripheral other) {
	return other == this;
}
 
Example #22
Source File: TurtleUpgradeSensor.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public IPeripheral createPeripheral(ITurtleAccess turtle, TurtleSide side) {
	return ApiAccess.getApi(IComputerCraftObjectsFactory.class).createPeripheral(new TurtleSensorEnvironment(turtle));
}
 
Example #23
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
protected IPeripheral createPeripheral(TileEntity tile, int side) {
	return ((ICustomPeripheralProvider)tile).createPeripheral(side);
}
 
Example #24
Source File: AdapterFactoryWrapperCC.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public IPeripheral createPeripheral(Object target) {
	return PeripheralProvider.createAdaptedPeripheralWrapped(target);
}
 
Example #25
Source File: PeripheralProvider.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public IPeripheral getPeripheral(TileEntity obj, int side) {
	return null;
}
 
Example #26
Source File: TileEntityTurbineComputerPort.java    From BigReactors with MIT License 4 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public boolean equals(IPeripheral other) {
	return hashCode() == other.hashCode();
}
 
Example #27
Source File: TileEntityReactorComputerPort.java    From BigReactors with MIT License 4 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public boolean equals(IPeripheral other) {
	return hashCode() == other.hashCode();
}
 
Example #28
Source File: TurtleUpgradeNarcissistic.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public IPeripheral createPeripheral(ITurtleAccess turtle, TurtleSide side) {
	return ApiAccess.getApi(IComputerCraftObjectsFactory.class).createPeripheral(new TurtleInventoryDelegate(turtle));
}
 
Example #29
Source File: WrappedPeripheral.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public boolean equals(IPeripheral other) {
	return other.equals(peripheral);
}
 
Example #30
Source File: WrappedPeripheral.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
public WrappedPeripheral(IPeripheral peripheral) {
	this.peripheral = peripheral;
}