@Override
 public void setTargetedLocation(@Nullable Vector3d vec) {
   this.targetedLocation = vec != null ? vec : VecHelper.toVector3d(this.worldObj.getSpawnPoint());
   if (!((Object) this instanceof EntityPlayerMP)) {
     this.worldObj.setSpawnPoint(VecHelper.toBlockPos(this.targetedLocation));
   }
 }
Example #2
0
 @Override
 public SpongeBlockSnapshot createSpongeBlockSnapshot(
     IBlockState state, IBlockState extended, BlockPos pos, int updateFlag) {
   this.builder.reset();
   Location<World> location = new Location<>((World) this, VecHelper.toVector(pos));
   this.builder
       .blockState((BlockState) state)
       .extendedState((BlockState) extended)
       .worldId(location.getExtent().getUniqueId())
       .position(location.getBlockPosition());
   Optional<UUID> creator = getCreator(pos.getX(), pos.getY(), pos.getZ());
   Optional<UUID> notifier = getNotifier(pos.getX(), pos.getY(), pos.getZ());
   if (creator.isPresent()) {
     this.builder.creator(creator.get());
   }
   if (notifier.isPresent()) {
     this.builder.notifier(notifier.get());
   }
   if (state.getBlock() instanceof ITileEntityProvider) {
     net.minecraft.tileentity.TileEntity te = getTileEntity(pos);
     if (te != null) {
       TileEntity tile = (TileEntity) te;
       for (DataManipulator<?, ?> manipulator : tile.getContainers()) {
         this.builder.add(manipulator);
       }
       NBTTagCompound nbt = new NBTTagCompound();
       te.writeToNBT(nbt);
       this.builder.unsafeNbt(nbt);
     }
   }
   return new SpongeBlockSnapshot(this.builder, updateFlag);
 }
 @Override
 public void populate(Chunk chunk, Random random) {
   Vector3i min = chunk.getBlockMin();
   World world = (World) chunk.getWorld();
   BlockPos position = new BlockPos(min.getX(), min.getY(), min.getZ());
   ShrubType stype = ShrubTypes.TALL_GRASS;
   List<ShrubType> result;
   // The vanilla populator places down grass in batches of 128, which is a
   // decent enough amount in order to get nice 'patches' of grass so we
   // divide the total count into batches of 128.
   int n = (int) Math.ceil(this.count.getFlooredAmount(random) / 128f);
   for (int i = 0; i < n; i++) {
     BlockPos pos = position.add(random.nextInt(16) + 8, 0, random.nextInt(16) + 8);
     pos = world.getTopSolidOrLiquidBlock(pos).add(0, 1, 0);
     if (this.override != null) {
       Location<Chunk> pos2 = new Location<>(chunk, VecHelper.toVector(pos));
       stype = this.override.apply(pos2);
     } else {
       result = this.types.get(random);
       if (result.isEmpty()) {
         continue;
       }
       stype = result.get(0);
     }
     BlockTallGrass.EnumType type = (BlockTallGrass.EnumType) (Object) stype;
     this.tallGrassState =
         Blocks.tallgrass.getDefaultState().withProperty(BlockTallGrass.TYPE, type);
     generate(world, random, pos);
   }
 }
  public static BlockEvent.NeighborNotifyEvent createBlockNeighborNotifyEvent(Event event) {
    if (!(event instanceof NotifyNeighborBlockEvent)) {
      throw new IllegalArgumentException(
          "Event " + event.getClass() + " is not a valid NotifyNeighborBlockEvent.");
    }

    NotifyNeighborBlockEvent spongeEvent = (NotifyNeighborBlockEvent) event;
    Optional<BlockSnapshot> blockSnapshot = spongeEvent.getCause().first(BlockSnapshot.class);
    if (!blockSnapshot.isPresent() || !blockSnapshot.get().getLocation().isPresent()) {
      return null;
    }

    EnumSet<EnumFacing> facings = EnumSet.noneOf(EnumFacing.class);
    for (Map.Entry<Direction, BlockState> mapEntry : spongeEvent.getNeighbors().entrySet()) {
      facings.add(DirectionFacingProvider.getInstance().get(mapEntry.getKey()).get());
    }

    IBlockState state = (IBlockState) blockSnapshot.get().getState();
    BlockPos pos = VecHelper.toBlockPos(blockSnapshot.get().getLocation().get());
    net.minecraft.world.World world =
        (net.minecraft.world.World) blockSnapshot.get().getLocation().get().getExtent();

    final NeighborNotifyEvent forgeEvent = new NeighborNotifyEvent(world, pos, state, facings);
    return forgeEvent;
  }
 @Override
 public Optional<EyeLocationData> from(DataHolder dataHolder) {
   if (supports(dataHolder)) {
     final Entity entity = (Entity) dataHolder;
     return Optional.<EyeLocationData>of(
         new SpongeEyeLocationData(
             VecHelper.toVector(entity.getPositionVector()), entity.getEyeHeight()));
   }
   return Optional.absent();
 }
 public static Cause generateCauseFor(DamageSource damageSource) {
   if (damageSource instanceof EntityDamageSourceIndirect) {
     net.minecraft.entity.Entity source = damageSource.getEntity();
     Optional<User> owner =
         ((IMixinEntity) source).getTrackedPlayer(NbtDataUtil.SPONGE_ENTITY_CREATOR);
     if (owner.isPresent()) {
       return Cause.of(
           NamedCause.of(DamageEntityEvent.SOURCE, damageSource),
           NamedCause.of(DamageEntityEvent.CREATOR, owner.get()));
     } else {
       return Cause.of(NamedCause.of(DamageEntityEvent.SOURCE, damageSource));
     }
   } else if (damageSource instanceof EntityDamageSource) {
     net.minecraft.entity.Entity source = damageSource.getEntity();
     Optional<User> owner =
         ((IMixinEntity) source).getTrackedPlayer(NbtDataUtil.SPONGE_ENTITY_CREATOR);
     Optional<User> notifier =
         ((IMixinEntity) source).getTrackedPlayer(NbtDataUtil.SPONGE_ENTITY_NOTIFIER);
     List<Object> causeObjects = new ArrayList<>();
     causeObjects.add(NamedCause.of(DamageEntityEvent.SOURCE, damageSource));
     if (notifier.isPresent()) {
       causeObjects.add(NamedCause.of(DamageEntityEvent.NOTIFIER, notifier.get()));
     }
     if (owner.isPresent()) {
       causeObjects.add(NamedCause.of(DamageEntityEvent.CREATOR, owner.get()));
     }
     return Cause.of(causeObjects.toArray());
   } else if (damageSource instanceof BlockDamageSource) {
     List<Object> causeObjects = new ArrayList<>();
     Location<org.spongepowered.api.world.World> location =
         ((BlockDamageSource) damageSource).getLocation();
     BlockPos blockPos = VecHelper.toBlockPos(location);
     Optional<User> owner =
         ((IMixinChunk)
                 ((net.minecraft.world.World) location.getExtent())
                     .getChunkFromBlockCoords(blockPos))
             .getBlockOwner(blockPos);
     Optional<User> notifier =
         ((IMixinChunk)
                 ((net.minecraft.world.World) location.getExtent())
                     .getChunkFromBlockCoords(blockPos))
             .getBlockNotifier(blockPos);
     causeObjects.add(NamedCause.of(DamageEntityEvent.SOURCE, damageSource));
     if (notifier.isPresent()) {
       causeObjects.add(NamedCause.of(DamageEntityEvent.NOTIFIER, notifier.get()));
     }
     if (owner.isPresent()) {
       causeObjects.add(NamedCause.of(DamageEntityEvent.CREATOR, owner.get()));
     }
     return Cause.of(causeObjects.toArray());
   } else {
     return Cause.of(NamedCause.of(DamageEntityEvent.SOURCE, damageSource));
   }
 }
 public SpongeProxyBlockAccess(
     IBlockAccess original, List<Transaction<BlockSnapshot>> snapshotTransaction) {
   this.original = original;
   this.transactions = snapshotTransaction;
   this.poses =
       this.transactions
           .stream()
           .map(transaction -> VecHelper.toBlockPos(transaction.getOriginal().getPosition()))
           .collect(GuavaCollectors.toImmutableList());
   this.index = 0;
 }
  @Overwrite
  public static PlayerInteractEvent onPlayerInteract(
      EntityPlayer player,
      Action action,
      net.minecraft.world.World world,
      BlockPos pos,
      EnumFacing face) {
    if (world.isRemote) {
      PlayerInteractEvent event = new PlayerInteractEvent(player, action, pos, face, world);
      MinecraftForge.EVENT_BUS.post(event);
      return event;
    }

    InteractBlockEvent event = null;
    if (action == Action.LEFT_CLICK_BLOCK) {
      event =
          SpongeEventFactory.createInteractBlockEventPrimary(
              SpongeImpl.getGame(),
              Cause.of(player),
              Optional.empty(),
              ((World) world).createSnapshot(VecHelper.toVector(pos)),
              face == null
                  ? Direction.NONE
                  : DirectionFacingProvider.getInstance().getKey(face).get());
    } else {
      event =
          SpongeEventFactory.createInteractBlockEventSecondary(
              SpongeImpl.getGame(),
              Cause.of(player),
              Optional.empty(),
              ((World) world).createSnapshot(VecHelper.toVector(pos)),
              face == null
                  ? Direction.NONE
                  : DirectionFacingProvider.getInstance().getKey(face).get());
    }

    SpongeImpl.postEvent(event);

    return (PlayerInteractEvent) SpongeForgeEventFactory.lastForgeEvent;
  }
    @SuppressWarnings({"unchecked"})
    @Override
    public void syncDataToForge(org.spongepowered.api.event.Event spongeEvent) {
      super.syncDataToForge(spongeEvent);

      ExplosionEvent.Detonate event = (ExplosionEvent.Detonate) spongeEvent;
      List<BlockPos> affectedBlocks = this.explosion.func_180343_e();
      affectedBlocks.clear();

      for (BlockTransaction blockTransaction : event.getTransactions()) {
        if (blockTransaction.isValid()) {
          affectedBlocks.add(
              VecHelper.toBlockPos(blockTransaction.getFinalReplacement().getPosition()));
        }
      }
    }
 @Inject(method = "<init>", at = @At("RETURN"))
 public void onConstructed(
     net.minecraft.world.World world,
     BlockPos pos,
     IBlockState state,
     EntityPlayer player,
     CallbackInfo ci) {
   this.blockOriginal = ((World) world).createSnapshot(pos.getX(), pos.getY(), pos.getZ());
   this.blockReplacement =
       BlockTypes.AIR
           .getDefaultState()
           .snapshotFor(new Location<World>((World) world, VecHelper.toVector(pos)));
   this.blockTransactions =
       new ImmutableList.Builder<BlockTransaction>()
           .add(new BlockTransaction(this.blockOriginal, this.blockReplacement))
           .build();
 }
 @Redirect(
     method = "onUpdate",
     at =
         @At(
             value = "INVOKE",
             target = "Lnet/minecraft/entity/player/EntityPlayer;isPlayerSleeping()Z"))
 public boolean onIsPlayerSleeping(EntityPlayer self) {
   if (self.isPlayerSleeping()) {
     if (!this.worldObj.isRemote) {
       SpongeImpl.postEvent(
           SpongeEventFactory.createSleepingEventTick(
               Cause.of(NamedCause.source(this)),
               this.getWorld().createSnapshot(VecHelper.toVector(this.playerLocation)),
               this));
     }
     return true;
   }
   return false;
 }
Example #12
0
 @Inject(
     method = "randomTick",
     at = @At(value = "HEAD"),
     locals = LocalCapture.CAPTURE_FAILEXCEPTION,
     cancellable = true)
 public void callRandomTickEvent(
     World world, BlockPos pos, IBlockState state, Random rand, CallbackInfo ci) {
   final BlockRandomTickEvent event =
       SpongeEventFactory.createBlockRandomTick(
           Sponge.getGame(),
           null,
           new Location<org.spongepowered.api.world.World>(
               (org.spongepowered.api.world.World) world,
               VecHelper.toVector(pos))); // TODO Fix null Cause
   Sponge.getGame().getEventManager().post(event);
   if (event.isCancelled()) {
     ci.cancel();
   }
 }
  private static PlayerInteractEvent createPlayerInteractEvent(Event event) {
    if (!(event instanceof InteractBlockEvent)) {
      throw new IllegalArgumentException("Event " + event + " is not a valid InteractBlockEvent.");
    }

    InteractBlockEvent spongeEvent = (InteractBlockEvent) event;
    Optional<Player> player = spongeEvent.getCause().first(Player.class);
    if (!player.isPresent()) {
      return null;
    }

    BlockPos pos =
        VecHelper.toBlockPos(spongeEvent.getTargetBlock().getLocation().get().getPosition());
    Optional<EnumFacing> face =
        DirectionFacingProvider.getInstance().get(spongeEvent.getTargetSide());
    Action action = null;
    if (spongeEvent instanceof InteractBlockEvent.Primary) {
      action = Action.LEFT_CLICK_BLOCK;
    } else if (spongeEvent instanceof InteractBlockEvent.Secondary) {
      if (spongeEvent.getTargetBlock().getState().getType() == BlockTypes.AIR) {
        action = Action.RIGHT_CLICK_AIR;
      } else {
        action = Action.RIGHT_CLICK_BLOCK;
      }
    } else { // attempt to determine action
      EntityPlayer entityplayer = (EntityPlayer) player.get();
      action = Action.RIGHT_CLICK_BLOCK;
      if (entityplayer.isUsingItem()) {
        action = Action.LEFT_CLICK_BLOCK;
      } else if (entityplayer.worldObj.isAirBlock(pos)) {
        action = Action.RIGHT_CLICK_AIR;
      }
    }

    PlayerInteractEvent forgeEvent =
        new PlayerInteractEvent(
            (EntityPlayer) player.get(),
            action,
            pos,
            face.isPresent() ? face.get() : null,
            (net.minecraft.world.World) player.get().getWorld());
    return forgeEvent;
  }
 @Inject(
     method = "randomTick",
     at = @At(value = "HEAD"),
     locals = LocalCapture.CAPTURE_FAILEXCEPTION,
     cancellable = true)
 public void callRandomTickEvent(
     net.minecraft.world.World world,
     BlockPos pos,
     IBlockState state,
     Random rand,
     CallbackInfo ci) {
   BlockSnapshot blockSnapshot = ((World) world).createSnapshot(VecHelper.toVector(pos));
   final TickBlockEvent event =
       SpongeEventFactory.createTickBlockEvent(Cause.of(NamedCause.source(world)), blockSnapshot);
   SpongeImpl.postEvent(event);
   if (event.isCancelled()) {
     ci.cancel();
   }
 }
 @SuppressWarnings("unchecked")
 public void createSpongeData() {
   List<BlockPos> affectedPositions = this.explosion.func_180343_e();
   ImmutableList.Builder<BlockTransaction> builder =
       new ImmutableList.Builder<BlockTransaction>();
   for (BlockPos pos : affectedPositions) {
     Location<World> location = new Location<World>((World) this.world, VecHelper.toVector(pos));
     BlockSnapshot originalSnapshot =
         ((IMixinBlockSnapshot)
                 net.minecraftforge.common.util.BlockSnapshot.getBlockSnapshot(this.world, pos))
             .createSpongeBlockSnapshot();
     final SpongeBlockSnapshotBuilder replacementBuilder =
         new SpongeBlockSnapshotBuilder()
             .blockState(BlockTypes.AIR.getDefaultState())
             .position(location.getBlockPosition())
             .worldId(location.getExtent().getUniqueId());
     BlockSnapshot replacementSnapshot = replacementBuilder.build();
     builder.add(new BlockTransaction(originalSnapshot, replacementSnapshot)).build();
   }
   this.blockTransactions = builder.build();
 }
 private boolean onStrikeBlock(
     net.minecraft.world.World world, BlockPos pos, IBlockState blockState) {
   if (!this.effect) {
     Vector3i pos3i = VecHelper.toVector(pos);
     Transaction<BlockSnapshot> transaction =
         new Transaction<BlockSnapshot>(
             new SpongeBlockSnapshotBuilder()
                 .blockState((BlockState) world.getBlockState(pos))
                 .world(((World) world).getProperties())
                 .position(pos3i)
                 .build(),
             new SpongeBlockSnapshotBuilder()
                 .blockState((BlockState) blockState)
                 .world(((World) world).getProperties())
                 .position(pos3i)
                 .build());
     if (!this.struckBlocks.contains(transaction)) {
       this.struckBlocks.add(transaction);
     }
   }
   return false;
 }
    @SuppressWarnings("unchecked")
    @Override
    public void syncDataToSponge(net.minecraftforge.fml.common.eventhandler.Event forgeEvent) {
      super.syncDataToSponge(forgeEvent);

      net.minecraftforge.event.world.ExplosionEvent event =
          (net.minecraftforge.event.world.ExplosionEvent) forgeEvent;
      // TODO - handle this better
      List<BlockPos> affectedBlocks = event.explosion.func_180343_e();
      for (BlockTransaction transaction : this.blockTransactions) {
        BlockPos pos = VecHelper.toBlockPos(transaction.getFinalReplacement().getPosition());
        boolean match = false;
        for (BlockPos forgePos : affectedBlocks) {
          if (forgePos.getX() == pos.getX()
              && forgePos.getY() == pos.getY()
              && forgePos.getZ() == pos.getZ()) {
            match = true;
          }
        }
        if (!match) {
          transaction.setIsValid(false);
        }
      }
    }
Example #18
0
 @Override
 public boolean containsBlock(int x, int y, int z) {
   return VecHelper.inBounds(x, y, z, BLOCK_MIN, BLOCK_MAX);
 }
Example #19
0
 @Override
 public boolean containsBiome(int x, int z) {
   return VecHelper.inBounds(x, z, BIOME_MIN, BIOME_MAX);
 }
  public static ChangeBlockEvent.Place callBlockPlaceEvent(Event event) {
    if (!(event instanceof ChangeBlockEvent.Place)) {
      throw new IllegalArgumentException("Event is not a valid ChangeBlockEventPlace");
    }

    ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) event;

    if (spongeEvent.getCause().first(Player.class).isPresent()) {
      EntityPlayer player = (EntityPlayer) spongeEvent.getCause().first(Player.class).get();
      net.minecraft.world.World world = (net.minecraft.world.World) spongeEvent.getTargetWorld();

      if (spongeEvent.getTransactions().size() == 1) {
        BlockPos pos =
            VecHelper.toBlockPos(spongeEvent.getTransactions().get(0).getOriginal().getPosition());
        IBlockState state =
            (IBlockState) spongeEvent.getTransactions().get(0).getOriginal().getState();
        net.minecraftforge.common.util.BlockSnapshot blockSnapshot =
            new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
        IBlockState placedAgainst = Blocks.air.getDefaultState();
        if (StaticMixinHelper.packetPlayer != null
            && StaticMixinHelper.processingPacket instanceof C08PacketPlayerBlockPlacement) {
          C08PacketPlayerBlockPlacement packet =
              (C08PacketPlayerBlockPlacement) StaticMixinHelper.processingPacket;
          EnumFacing facing = EnumFacing.getFront(packet.getPlacedBlockDirection());
          placedAgainst =
              blockSnapshot.world.getBlockState(blockSnapshot.pos.offset(facing.getOpposite()));
        }

        BlockEvent.PlaceEvent forgeEvent =
            new BlockEvent.PlaceEvent(blockSnapshot, placedAgainst, player);
        ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
        if (forgeEvent.isCanceled()) {
          spongeEvent.getTransactions().get(0).setValid(false);
        }
      } else { // multi
        Iterator<Transaction<BlockSnapshot>> iterator = spongeEvent.getTransactions().iterator();
        List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = new ArrayList<>();

        while (iterator.hasNext()) {
          Transaction<BlockSnapshot> transaction = iterator.next();
          Location<World> location = transaction.getOriginal().getLocation().get();
          IBlockState state = (IBlockState) transaction.getOriginal().getState();
          BlockPos pos =
              new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
          net.minecraftforge.common.util.BlockSnapshot blockSnapshot =
              new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
          blockSnapshots.add(blockSnapshot);
        }

        IBlockState placedAgainst = Blocks.air.getDefaultState();
        if (StaticMixinHelper.packetPlayer != null
            && StaticMixinHelper.processingPacket instanceof C08PacketPlayerBlockPlacement) {
          C08PacketPlayerBlockPlacement packet =
              (C08PacketPlayerBlockPlacement) StaticMixinHelper.processingPacket;
          EnumFacing facing = EnumFacing.getFront(packet.getPlacedBlockDirection());
          placedAgainst =
              blockSnapshots
                  .get(0)
                  .world
                  .getBlockState(blockSnapshots.get(0).pos.offset(facing.getOpposite()));
        }

        BlockEvent.MultiPlaceEvent forgeEvent =
            new BlockEvent.MultiPlaceEvent(blockSnapshots, placedAgainst, player);
        ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
        if (forgeEvent.isCanceled()) {
          while (iterator.hasNext()) {
            iterator.next().setValid(false);
          }
        }
      }
    }
    return spongeEvent;
  }
 @Inject(
     method = "<init>(Lnet/minecraft/world/World;Lcom/mojang/authlib/GameProfile;)V",
     at = @At("RETURN"))
 public void construct(World worldIn, GameProfile gameProfileIn, CallbackInfo ci) {
   this.targetedLocation = VecHelper.toVector3d(worldIn.getSpawnPoint());
 }