下面列出了怎么用net.minecraftforge.common.util.FakePlayerFactory的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Spawns a new cart entity using the provided item.
* <p/>
* The backing item must implement <code>IMinecartItem</code> and/or extend
* <code>ItemMinecart</code>.
* <p/>
* Generally Forge requires all cart items to extend ItemMinecart.
*
* @param owner The player name that should used as the owner
* @param cart An ItemStack containing a cart item, will not be changed by
* the function
* @param world The World object
* @param x x-Coord
* @param y y-Coord
* @param z z-Coord
* @return the cart placed or null if failed
* @see IMinecartItem, ItemMinecart
*/
public static EntityMinecart placeCart(GameProfile owner, ItemStack cart, WorldServer world, int x, int y, int z) {
if (cart == null)
return null;
cart = cart.copy();
if (cart.getItem() instanceof IMinecartItem) {
IMinecartItem mi = (IMinecartItem) cart.getItem();
return mi.placeCart(owner, cart, world, x, y, z);
} else if (cart.getItem() instanceof ItemMinecart)
try {
boolean placed = cart.getItem().onItemUse(cart, FakePlayerFactory.get(world, railcraftProfile), world, x, y, z
, 0, 0, 0, 0);
if (placed) {
List<EntityMinecart> carts = getMinecartsAt(world, x, y, z, 0.3f);
if (carts.size() > 0) {
setCartOwner(carts.get(0), owner);
return carts.get(0);
}
}
} catch (Exception e) {
return null;
}
return null;
}
public static FakePlayer get(WorldServer world) {
FakePlayer ret = GREGTECH_PLAYER != null ? GREGTECH_PLAYER.get() : null;
if (ret == null) {
ret = FakePlayerFactory.get(world, GREGTECH);
GREGTECH_PLAYER = new WeakReference<>(ret);
}
return ret;
}
public static EntityPlayerMP makePlacer(@Nonnull World world, @Nonnull BlockPos pos, @Nullable Entity entity) {
if (entity instanceof EntityPlayerMP) {
return (EntityPlayerMP) entity;
}
EntityPlayerMP bro = FakePlayerFactory.get((WorldServer) world, PLACER);
bro.moveToBlockPosAndAngles(pos, 0, -90);
bro.setSneaking(true);
return bro;
}
public static boolean placeBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing facing, @Nonnull ItemStack stack) {
if (!world.isBlockLoaded(pos)) return false;
FakePlayer player = FakePlayerFactory.get((WorldServer) world, PLACER);
player.moveToBlockPosAndAngles(pos, 0, -90);
player.setHeldItem(EnumHand.MAIN_HAND, stack);
player.setSneaking(true);
if (!hasEditPermission(pos, player)) return false;
EnumActionResult result = player.interactionManager.processRightClickBlock(
player, world, stack, EnumHand.MAIN_HAND,
pos, facing, 0, 0, 0);
return result != EnumActionResult.FAIL;
}
public static EntityPlayerMP makeBreaker(@Nonnull World world, @Nonnull BlockPos pos, @Nullable Entity entity) {
if (entity instanceof EntityPlayerMP) {
return (EntityPlayerMP) entity;
}
EntityPlayerMP bro = FakePlayerFactory.get((WorldServer) world, BREAKER);
bro.moveToBlockPosAndAngles(pos, 0, -90);
return bro;
}
@Nonnull
private FakePlayer getPlayer()
{
if (this.fakePlayer == null)
{
int dim = this.getWorld().provider.getDimension();
this.fakePlayer = FakePlayerFactory.get((WorldServer) this.getWorld(),
new GameProfile(new UUID(dim, dim), Reference.MOD_ID + ":drawbridge"));
}
return this.fakePlayer;
}
private EntityPlayer getFakePlayer(){
return FakePlayerFactory.get((WorldServer)worldObj, new GameProfile(null, "Sentry Turret"));
}