Java Code Examples for net.minecraft.entity.EntityLivingBase#getCapability()

The following examples show how to use net.minecraft.entity.EntityLivingBase#getCapability() . 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: SquashableMod.java    From CommunityMod with GNU Lesser General Public License v2.1 6 votes vote down vote up
@SubscribeEvent
public static void onLivingUpdate(LivingEvent.LivingUpdateEvent event) {
    EntityLivingBase entity = event.getEntityLiving();
    if (entity.world.isRemote) {
        return;
    }

    if (entity.collidedHorizontally && isBeingPushedByPiston(entity)) {
        Squashable squashable = entity.getCapability(squashableCap(), null);
        if (squashable == null) {
            return;
        }

        double[] pistonDeltas = getPistonDeltas(entity);
        double pushedAngle = Math.atan2(pistonDeltas[2], pistonDeltas[0]);

        EnumFacing.Axis faceAxis = EnumFacing.fromAngle(entity.rotationYaw).getAxis();
        EnumFacing.Axis pushAxis = EnumFacing.fromAngle(pushedAngle).getAxis();

        EnumFacing.Axis squashAxis = faceAxis == pushAxis ? EnumFacing.Axis.Z : EnumFacing.Axis.X;

        squashable.squash(squashAxis);

        NETWORK.sendToAllTracking(new SquashEntityMessage(entity, squashAxis), entity);
    }
}
 
Example 2
Source File: SquashableMod.java    From CommunityMod with GNU Lesser General Public License v2.1 6 votes vote down vote up
@SubscribeEvent
@SideOnly(Side.CLIENT)
public static void onRenderLivingPre(RenderLivingEvent.Pre<?> event) {
    EntityLivingBase entity = event.getEntity();
    Squashable squashable = entity.getCapability(squashableCap(), null);
    if (squashable == null) {
        return;
    }

    EnumFacing.Axis squashedAxis = squashable.getSquashedAxis();
    if (squashedAxis != null) {
        RenderLivingBase<?> renderer = event.getRenderer();
        originalModel = renderer.getMainModel();
        ModelHooks.setMainModel(renderer, new FlattenedModel(originalModel, squashedAxis));
    }
}
 
Example 3
Source File: ProxyCommon.java    From WearableBackpacks with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onEntityInteract(PlayerInteractEvent.EntityInteract event) {

	// When players right-click equipped backpacks, interact with them.

	if (!WearableBackpacks.CONFIG.enableEquippedInteraction.get() ||
	    !(event.getTarget() instanceof EntityLivingBase)) return;
	EntityPlayer player = event.getEntityPlayer();
	EntityLivingBase target = (EntityLivingBase)event.getTarget();

	BackpackCapability backpack = (BackpackCapability)target.getCapability(IBackpack.CAPABILITY, null);
	if ((backpack == null) || !BackpackHelper.canInteractWithEquippedBackpack(player, target)) return;

	IBackpackType type = backpack.getType();
	if (type == null) {
		WearableBackpacks.LOG.error("Backpack type was null when accessing equipped backpack");
		return;
	}
	if (!player.world.isRemote && (backpack.getData() == null)) {
		IBackpackData data = type.createBackpackData(backpack.getStack());
		if (data != null) {
			// Only show this error message if the backpack type is supposed to have backpack data.
			// Some backpacks might not need any to function, for example an ender backpack.
			WearableBackpacks.LOG.error("Backpack data was null when accessing equipped backpack");
			backpack.setData(data);
		}
	}
	type.onEquippedInteract(player, target, backpack);

}
 
Example 4
Source File: ProxyCommon.java    From WearableBackpacks with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {

	// Update equipped backpacks and check
	// if they've been removed somehow.

	EntityLivingBase entity = event.getEntityLiving();
	BackpackCapability backpack = (BackpackCapability)entity
		.getCapability(IBackpack.CAPABILITY, null);
	if (backpack == null) return;

	if (backpack.spawnWith != null)
		onSpawnedWith(entity, backpack, backpack.spawnWith);
	boolean hasBackpack = !backpack.getStack().isEmpty();

	if (backpack.isChestArmor()) {
		if (entity instanceof EntityPlayer)
			SlotBackpackWrapper.replace((EntityPlayer)entity, backpack.getStack());

		if (!hasBackpack) {
			// Backpack has been removed somehow.
			backpack.getType().onFaultyRemoval(entity, backpack);
			backpack.setStack(ItemStack.EMPTY);
			return;
		}
	} else if (!hasBackpack) return;

	backpack.getType().onEquippedTick(entity, backpack);
	if (entity.world.isRemote)
		BackpackHelper.updateLidTicks(backpack, entity.posX, entity.posY + 1.0, entity.posZ);

}
 
Example 5
Source File: BackpackHelper.java    From WearableBackpacks with MIT License 5 votes vote down vote up
/** Returns if the entity can equip a backpack right now.
 *  Requires the entity to be able to wear backpacks, not currently have a backpack equipped, and
 *  if {@link equipAsChestArmor} is true and the entity is a player, an empty chest armor slot. */
public static boolean canEquipBackpack(EntityLivingBase entity) {
	return (entity.getCapability(IBackpack.CAPABILITY, null) != null) // Has backpack capability.
		&& (getBackpack(entity) == null)                              // Doesn't currently have backpack equipped.
		&& !(equipAsChestArmor && (entity instanceof EntityPlayer)    // Isn't wearing a chestplate while equipAsChestArmor is on.
			&& !entity.getItemStackFromSlot(EntityEquipmentSlot.CHEST).isEmpty());
	// FIXME: How does this work with non-player entities? Do / should they always wear backpacks as armor or what?
}
 
Example 6
Source File: BackpackHelper.java    From WearableBackpacks with MIT License 5 votes vote down vote up
/** Sets the entity's equipped backpack and data. */
public static void setEquippedBackpack(EntityLivingBase entity, ItemStack stack,
                                       IBackpackData backpackData) {
	IBackpackType backpackType = getBackpackType(stack);
	if (!stack.isEmpty() && (backpackType == null))
		throw new IllegalArgumentException("Backpack item isn't an IBackpackType.");
	
	IBackpack backpack = entity.getCapability(IBackpack.CAPABILITY, null);
	backpack.setStack(stack);
	backpack.setData(backpackData);
}
 
Example 7
Source File: ProxyCommon.java    From WearableBackpacks with MIT License 4 votes vote down vote up
@SubscribeEvent
public void onLivingDeath(LivingDeathEvent event) {
	// If an entity wearing a backpack dies, try
	// to place it as a block, or drop the items.

	EntityLivingBase entity = event.getEntityLiving();
	World world = entity.world;
	if (world.isRemote) return;

	BackpackCapability backpack = (BackpackCapability)entity
		.getCapability(IBackpack.CAPABILITY, null);
	if ((backpack == null) || backpack.getStack().isEmpty()) return;

	// If keep inventory (or other item keeping method) is on,
	// keep the backpack capability so we can copy it over to the new player
	// entity in onPlayerClone.
	if (MiscUtils.shouldKeepItem(entity, backpack.getStack())) return;

	// Attempt to place the backpack as a block instead of dropping the items.
	if (WearableBackpacks.CONFIG.dropAsBlockOnDeath.get()) {

		List<BlockCoord> coords = new ArrayList<BlockCoord>();
		for (int x = -2; x <= 2; x++)
			for (int z = -2; z <= 2; z++)
				coords.add(new BlockCoord(entity, x, z));

		// Try to place the backpack on the ground nearby,
		// or look for a ground above or below to place it.

		Collections.sort(coords, new Comparator<BlockCoord>() {
			@Override public int compare(BlockCoord o1, BlockCoord o2) {
				if (o1.distance < o2.distance) return -1;
				else if (o1.distance > o2.distance) return 1;
				else return 0;
			}
		});
		while (!coords.isEmpty()) {
			Iterator<BlockCoord> iter = coords.iterator();
			while (iter.hasNext()) {
				BlockCoord coord = iter.next();
				// Attempt to place and unequip the backpack at
				// this coordinate. If successful, we're done here.
				if (BackpackHelper.placeBackpack(world, coord, backpack.getStack(), entity, true)) {
					// TODO: I'm aware that this is not the cleanest solution.
					((TileEntityBackpack)world.getTileEntity(coord)).setPlacedOnDeath(backpack.mayDespawn);
					return;
				}
				boolean replacable = world.getBlockState(coord).getBlock().isReplaceable(world, coord);
				coord.add(0, (replacable ? -1 : 1), 0);
				coord.moved += (replacable ? 1 : 5);
				if ((coord.getY() <= 0) || (coord.getY() > world.getHeight()) ||
					(coord.moved > 24 - coord.distance * 4)) iter.remove();
			}
		}

	}

	// In the case of regular backpacks, this causes their contents to be dropped.
	backpack.getType().onDeath(entity, backpack);

	// Drop the backpack as an item and remove it from the entity.
	if (!backpack.getStack().isEmpty())
		WorldUtils.dropStackFromEntity(entity, backpack.getStack(), 4.0F);
	BackpackHelper.setEquippedBackpack(entity, ItemStack.EMPTY, null);
}