use of net.minecraftforge.common.util.FakePlayer in project Wizardry by TeamWizardry.
the class BlockUtils method placeBlock.
/**
* Tries placing a block safely and fires an event for it.
*
* @return Whether the block was successfully placed
*/
public static boolean placeBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nullable EntityPlayerMP player) {
if (!world.isBlockLoaded(pos))
return false;
EntityPlayerMP playerMP;
if (player == null) {
playerMP = new FakePlayer((WorldServer) world, placer);
playerMP.setPosition(pos.getX(), pos.getY(), pos.getZ());
playerMP.setSneaking(true);
} else
playerMP = player;
if (!hasEditPermission(pos, playerMP))
return false;
BlockEvent.PlaceEvent event = new BlockEvent.PlaceEvent(BlockSnapshot.getBlockSnapshot(world, pos), Blocks.AIR.getDefaultState(), playerMP, playerMP.getActiveHand());
MinecraftForge.EVENT_BUS.post(event);
if (!event.isCanceled()) {
world.setBlockState(pos, state);
world.notifyBlockUpdate(pos, state, state, 3);
return true;
}
return false;
}
use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.
the class MiningAssistEventHooks method onHarvestDrops.
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onHarvestDrops(HarvestDropsEvent event) {
World world = event.getWorld();
if (world.isRemote) {
return;
}
EntityPlayer player = event.getHarvester();
if (player == null || player instanceof FakePlayer) {
return;
}
BlockPos pos = event.getPos();
MiningAssistUnit assist = MiningAssistUnit.get(player);
if (!assist.getCaptureDrops()) {
return;
}
NonNullList<ItemStack> items = NonNullList.create();
List<ItemStack> drops = event.getDrops();
float chance = event.getDropChance();
for (ItemStack stack : drops) {
if (CaveEventHooks.RANDOM.nextFloat() < chance) {
items.add(stack);
}
}
drops.clear();
assist.addDrops(pos, items);
}
use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.
the class CaveEventHooks method onHarvestDrops.
@SubscribeEvent
public void onHarvestDrops(HarvestDropsEvent event) {
if (!GeneralConfig.criticalMining || event.isSilkTouching()) {
return;
}
World world = event.getWorld();
if (world.isRemote) {
return;
}
EntityPlayer player = event.getHarvester();
if (player == null || player instanceof FakePlayer || !CavernAPI.dimension.isInCaveDimensions(player)) {
return;
}
IBlockState state = event.getState();
if (MinerStats.getPointAmount(state) <= 0) {
return;
}
if (state.getMaterial() != Material.ROCK) {
return;
}
MinerRank rank = MinerRank.get(MinerStats.get(player).getRank());
float f = rank.getBoost();
if (f <= 1.0F) {
return;
}
f = (f - 1.0F) * 0.3F;
ItemStack held = player.getHeldItemMainhand();
String tool = state.getBlock().getHarvestTool(state);
if (held.isEmpty() || tool == null) {
return;
}
int toolLevel = held.getItem().getHarvestLevel(held, tool, player, state);
if (toolLevel <= 0) {
return;
}
f *= 1.0F + toolLevel * 0.1F;
List<ItemStack> originalDrops = event.getDrops();
List<ItemStack> drops = Lists.newArrayList();
for (ItemStack stack : originalDrops) {
if (!stack.isEmpty() && !(stack.getItem() instanceof ItemBlock) && RANDOM.nextFloat() <= f) {
drops.add(stack.copy());
}
}
if (!drops.isEmpty()) {
CriticalMiningEvent criticalEvent = new CriticalMiningEvent(world, event.getPos(), state, player, event.getFortuneLevel(), originalDrops, drops);
if (MinecraftForge.EVENT_BUS.post(criticalEvent)) {
return;
}
player.sendStatusMessage(new TextComponentTranslation("cavern.message.mining.critical"), true);
originalDrops.addAll(criticalEvent.getBonusDrops());
}
}
use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.
the class CaveEventHooks method onLivingUpdate.
@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {
EntityLivingBase entity = event.getEntityLiving();
if (!(entity instanceof EntityPlayer) || entity instanceof FakePlayer) {
return;
}
EntityPlayer player = (EntityPlayer) entity;
MiningData.get(player).onUpdate();
if (!player.isInWater() || !CavernAPI.dimension.isInCaveDimensions(player)) {
return;
}
IMinerStats stats = MinerStats.get(player);
if (stats.getRank() < MinerRank.AQUA_MINER.getRank()) {
return;
}
if (!player.canBreatheUnderwater() && !player.isPotionActive(MobEffects.WATER_BREATHING) && player.ticksExisted % 20 == 0) {
player.setAir(300);
}
if (player.capabilities.isFlying || EnchantmentHelper.getDepthStriderModifier(player) > 0) {
return;
}
double prevY = player.posY;
float vec1 = 0.6F;
float vec2 = 0.01F;
float vec3 = 0.4F;
if (!player.onGround) {
vec3 *= 0.5F;
}
if (player.getArmorVisibility() >= 0.75F) {
vec3 *= 0.5F;
}
if (vec3 > 0.0F) {
vec1 += (0.54600006F - vec1) * vec3 / 3.0F;
vec2 += (player.getAIMoveSpeed() - vec2) * vec3 / 3.0F;
}
player.moveRelative(player.moveStrafing, player.moveVertical, player.moveForward, vec2);
player.move(MoverType.SELF, player.motionX, player.motionY, player.motionZ);
player.motionX *= vec1;
player.motionY *= 0.800000011920929D;
player.motionZ *= vec1;
if (player.collidedHorizontally && player.isOffsetPositionInLiquid(player.motionX, player.motionY + 0.6000000238418579D - player.posY + prevY, player.motionZ)) {
player.motionY = 0.30000001192092896D;
}
if (player.isSwingInProgress || player.isSneaking()) {
player.motionY *= 0.3D;
}
}
use of net.minecraftforge.common.util.FakePlayer in project Cavern2 by kegare.
the class CaveEventHooks method onBlockBreak.
@SubscribeEvent
public void onBlockBreak(BreakEvent event) {
EntityPlayer entityPlayer = event.getPlayer();
if (entityPlayer == null || entityPlayer instanceof FakePlayer || !(entityPlayer instanceof EntityPlayerMP)) {
return;
}
EntityPlayerMP player = (EntityPlayerMP) entityPlayer;
if (!CavernAPI.dimension.isInCaveDimensions(player)) {
return;
}
World world = event.getWorld();
IBlockState state = event.getState();
ItemStack stack = player.getHeldItemMainhand();
if (GeneralConfig.isMiningPointItem(stack)) {
int point = MinerStats.getPointAmount(state);
if (point != 0) {
IMinerStats stats = MinerStats.get(player);
IMiningData data = MiningData.get(player);
if (player.inventory.hasItemStack(ItemCave.EnumType.MINER_ORB.getItemStack())) {
if (RANDOM.nextDouble() < 0.15D) {
point += Math.max(point / 2, 1);
}
}
stats.addPoint(point);
stats.addMiningRecord(new BlockMeta(state));
data.notifyMining(state, point);
int combo = data.getMiningCombo();
if (combo > 0 && combo % 10 == 0) {
world.playSound(null, player.posX, player.posY + 0.25D, player.posZ, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 0.1F, 0.5F * ((RANDOM.nextFloat() - RANDOM.nextFloat()) * 0.7F + 1.8F));
player.addExperience(combo / 10);
}
if (combo >= 50) {
PlayerHelper.grantAdvancement(player, "good_mine");
}
CaveNetworkRegistry.sendTo(new MiningMessage(state, point), player);
}
}
if (CavernAPI.dimension.isInFrostMountains(player)) {
if (player.capabilities.isCreativeMode) {
return;
}
if (state.getBlock() != Blocks.PACKED_ICE || EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, stack) > 0) {
return;
}
BlockPos pos = event.getPos();
Biome biome = world.getBiome(pos);
if (!biome.isSnowyBiome()) {
return;
}
if (RANDOM.nextDouble() < 0.05D) {
Block.spawnAsEntity(world, pos, new ItemStack(Blocks.ICE));
} else if (RANDOM.nextDouble() < 0.005D) {
Block.spawnAsEntity(world, pos, RandomiteHelper.getDropItem());
} else if (stack.getItem() instanceof IIceEquipment && RANDOM.nextDouble() < 0.03D || RANDOM.nextDouble() < 0.01D) {
Block.spawnAsEntity(world, pos, new ItemStack(Blocks.PACKED_ICE));
}
}
}
Aggregations