Java源码示例:net.minecraft.network.play.server.SPacketChangeGameState

示例1
@SubscribeEvent
public void onPacketIncoming(PacketEvent.Incoming.Pre event) {
  if (event.getPacket() instanceof SPacketChangeGameState) {
    int state = ((SPacketChangeGameState) event.getPacket()).getGameState();
    float strength = ((SPacketChangeGameState) event.getPacket()).getValue();
    boolean isRainState = false;
    switch (state) {
      case 1: // end rain
        isRainState = false;
        setState(false, 0.f, 0.f);
        break;
      case 2: // start rain
        isRainState = true;
        setState(true, 1.f, 1.f);
        break;
      case 7: // fade value: sky brightness
        isRainState = true; // needs to be cancelled to avoid flicker
        break;
    }
    if (isRainState) {
      disableRain();
      event.setCanceled(true);
    }
  }
}
 
示例2
@Override
public void updateWeather()
{
	if (!hasNoSky())
	{
		if (!world.isRemote && world.provider.getDimension() == 0)
		{
			world.getWorldInfo().setRainTime(48000);
			world.getWorldInfo().setRaining(false);
			int i = world.getWorldInfo().getCleanWeatherTime();

			if (i > 0)
			{
				--i;
				world.getWorldInfo().setCleanWeatherTime(i);
				world.getWorldInfo().setThunderTime(world.getWorldInfo().isThundering() ? 1 : 2);
				world.getWorldInfo().setRainTime(world.getWorldInfo().isRaining() ? 1 : 2);
			}
			int j = world.getWorldInfo().getThunderTime();

			if (j <= 0)
			{
				if (world.getWorldInfo().isThundering())
				{
					world.getWorldInfo().setThunderTime(world.rand.nextInt(12000) + 3600);
				}
				else
				{
					world.getWorldInfo().setThunderTime(world.rand.nextInt(168000) + 12000);
				}
			}
			else
			{
				--j;
				world.getWorldInfo().setThunderTime(j);

				if (j <= 0)
				{
					world.getWorldInfo().setThundering(!world.getWorldInfo().isThundering());
				}
			}

			world.prevThunderingStrength = world.thunderingStrength;

			/*if (worldObj.getWorldInfo().isThundering())
			{
				worldObj.thunderingStrength = (float)((double)worldObj.thunderingStrength + 0.01D);
			}
			else
			{
				worldObj.thunderingStrength = (float)((double)worldObj.thunderingStrength - 0.01D);
			}

			worldObj.thunderingStrength = MathHelper.clamp_float(worldObj.thunderingStrength, 0.0F, 1.0F);*/


			world.prevRainingStrength = world.rainingStrength;

			Iterator iterator = world.playerEntities.iterator();

			//TODO: Add thunder support

			while (iterator.hasNext())
			{
				EntityPlayerMP player = (EntityPlayerMP) iterator.next();
				if(!player.isDead && player.dimension == 0)
				{
					float old = player.getEntityData().getFloat("oldPrecipitation");
					float precip = (float)WeatherManager.getInstance().getPrecipitation((int)player.posX, (int)player.posZ);
					if(precip != old)
					{
						player.connection.sendPacket(new SPacketChangeGameState(7, precip));
					}
				}
			}

		}
	}
}