Java Code Examples for net.minecraftforge.fml.common.network.simpleimpl.MessageContext#getServerHandler()

The following examples show how to use net.minecraftforge.fml.common.network.simpleimpl.MessageContext#getServerHandler() . 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: ObservationFromServer.java    From malmo with MIT License 6 votes vote down vote up
/** IMPORTANT: Call this from the onMessage method in the subclass. */
public IMessage processMessage(ObservationRequestMessage message, MessageContext ctx)
{
    IThreadListener mainThread = (WorldServer) ctx.getServerHandler().playerEntity.world;
    final EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    final ObservationRequestMessage mess = message;
    mainThread.addScheduledTask(new Runnable()
    {
        @Override
        public void run()
        {
            JsonObject json = new JsonObject();
            buildJson(json, player, mess);
            // Send this message back again now we've filled in the json stats.
            Map<String, String> returnData = new HashMap<String, String>();
            returnData.put("json", json.toString());
            mess.addReturnData(returnData);
            MalmoMod.network.sendTo(new MalmoMod.MalmoMessage(MalmoMessageType.SERVER_OBSERVATIONSREADY, mess.id, returnData), player);
        }
    });
    return null; // no response in this case
}
 
Example 2
Source File: InventoryCommandsImplementation.java    From malmo with MIT License 6 votes vote down vote up
@Override
public InventoryChangeMessage onMessage(final InventoryMessage message, MessageContext ctx)
{
    final EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    IThreadListener mainThread = (WorldServer)ctx.getServerHandler().playerEntity.world;
    mainThread.addScheduledTask(new Runnable()
    {
        @Override
        public void run()
        {
            ItemStack[] changes = null;
            if (message.combine)
                changes = combineSlots(player, message.invA, message.slotA, message.invB, message.slotB, message.containerPos);
            else
                changes = swapSlots(player, message.invA, message.slotA, message.invB, message.slotB, message.containerPos);
            if (changes != null)
                MalmoMod.network.sendTo(new InventoryChangeMessage(changes[0], changes[1]), player);
        }
    });
    return null;
}
 
Example 3
Source File: VSGuiButtonHandler.java    From Valkyrien-Skies with Apache License 2.0 6 votes vote down vote up
@Override
public IMessage onMessage(VSGuiButtonMessage message, MessageContext ctx) {
    IThreadListener mainThread = ctx.getServerHandler().server;
    mainThread.addScheduledTask(() -> {
        World playerWorld = ctx.getServerHandler().player.world;
        TileEntity tileEntity = playerWorld.getTileEntity(message.getTileEntityPos());
        if (tileEntity == null) {
            // Nothing there, ignore this message
            return;
        }
        int buttonId = message.getButtonId();
        // Tell the tile entity that this player tried pressing the given button.
        if (tileEntity instanceof IVSTileGui) {
            ((IVSTileGui) tileEntity).onButtonPress(buttonId, ctx.getServerHandler().player);
        }
    });
    return null;
}
 
Example 4
Source File: SimpleCraftCommandsImplementation.java    From malmo with MIT License 6 votes vote down vote up
@Override
public IMessage onMessage(CraftMessage message, MessageContext ctx)
{
    EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    // Try crafting recipes first:
    List<IRecipe> matching_recipes;
    String[] split = message.parameters.split(" ");
    matching_recipes = CraftingHelper.getRecipesForRequestedOutput(message.parameters, split.length > 1);

    for (IRecipe recipe : matching_recipes)
    {
        if (CraftingHelper.attemptCrafting(player, recipe))
            return null;
    }
    // Now try furnace recipes:
    ItemStack input = CraftingHelper.getSmeltingRecipeForRequestedOutput(message.parameters);
    if (input != null)
    {
        if (CraftingHelper.attemptSmelting(player, input))
            return null;
    }
    return null;
}
 
Example 5
Source File: MessageQuestUpdate.java    From ToroQuest with GNU General Public License v3.0 6 votes vote down vote up
@Override
public IMessage onMessage(final MessageQuestUpdate message, MessageContext ctx) {
	if (ctx.side != Side.SERVER) {
		return null;
	}

	final EntityPlayerMP player = ctx.getServerHandler().player;

	if (player == null) {
		return null;
	}

	final WorldServer worldServer = player.getServerWorld();

	worldServer.addScheduledTask(new Runnable() {
		@Override
		public void run() {
			new Worker(message.action).work(message, player);
		}
	});

	return null;
}
 
Example 6
Source File: PacketRemovePearlFromBelt.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void handle(@NotNull MessageContext ctx) {
	EntityPlayerMP player = ctx.getServerHandler().player;

	ItemStack stack = player.getHeldItemMainhand();

	if (stack.getItem() instanceof IPearlStorageHolder) {
		IPearlStorageHolder holder = (IPearlStorageHolder) stack.getItem();
		int originalPearlCount = holder.getPearlCount(stack);

		ItemStack pearl = holder.removePearl(stack, index, true);
		player.addItemStackToInventory(pearl);

		int newPearlCount = holder.getPearlCount(stack);

		PacketHandler.NETWORK.sendTo(new PacketUpdatePearlGUI(originalPearlCount, newPearlCount, index, null, stack.getTagCompound()), player);
	}
}
 
Example 7
Source File: PilotControlsMessageHandler.java    From Valkyrien-Skies with Apache License 2.0 6 votes vote down vote up
@Override
    public IMessage onMessage(final PilotControlsMessage message, final MessageContext ctx) {
        IThreadListener mainThread = ctx.getServerHandler().server;
        mainThread.addScheduledTask(new Runnable() {
            @Override
            public void run() {
                World worldObj = ctx.getServerHandler().player.world;
                if (ValkyrienSkiesMod.VS_PHYSICS_MANAGER.getManagerForWorld(worldObj) != null) {
//                    UUID shipId = message.shipFor;
                    BlockPos posFor = message.controlBlockPos;
                    TileEntity tile = worldObj.getTileEntity(posFor);

                    if (tile instanceof ITileEntityPilotable) {
                        ((ITileEntityPilotable) tile)
                            .onPilotControlsMessage(message, ctx.getServerHandler().player);
                    }
                }
            }
        });

        return null;
    }
 
Example 8
Source File: UpdateHudColorPacket.java    From Cyberware with MIT License 5 votes vote down vote up
@Override
public IMessage onMessage(UpdateHudColorPacket message, MessageContext ctx)
{
	EntityPlayerMP player = ctx.getServerHandler().playerEntity;
	DimensionManager.getWorld(player.worldObj.provider.getDimension()).addScheduledTask(new DoSync(message.color, player));

	return null;
}
 
Example 9
Source File: SubspacedEntityRecordHandler.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
@Override
public IMessage onMessage(final SubspacedEntityRecordMessage message,
    final MessageContext ctx) {
    IThreadListener threadScheduler = null;
    World world = null;
    if (ctx.side.isClient()) {
        // We are receiving this on the client
        threadScheduler = getClientThreadListener();
        world = getClientWorld();
    } else {
        // Otherwise we are receiving this on the server
        threadScheduler = ctx.getServerHandler().server;
        world = ctx.getServerHandler().player.world;
    }
    final World worldFinal = world;
    threadScheduler.addScheduledTask(() -> {
        Entity physicsEntity = worldFinal.getEntityByID(message.physicsObjectWrapperID);
        Entity subspacedEntity = worldFinal.getEntityByID(message.entitySubspacedID);
        if (physicsEntity != null && subspacedEntity != null) {
            PhysicsWrapperEntity wrapperEntity = (PhysicsWrapperEntity) physicsEntity;
            ISubspacedEntityRecord record = message.createRecordForThisMessage(
                (ISubspacedEntity) subspacedEntity, wrapperEntity.getPhysicsObject()
                    .getSubspace());
            IDraggable draggable = (IDraggable) subspacedEntity;
            draggable.setForcedRelativeSubspace(wrapperEntity);
            wrapperEntity.getPhysicsObject().getSubspace()
                .forceSubspaceRecord(record.getParentEntity(), record);
            // Now just synchronize the player to the data sent by the client.
        } else {
            System.err.println("An incorrect SubspacedEntityRecordMessage has been thrown out");
        }
    });
    return null;
}
 
Example 10
Source File: PacketUpdateMiscCapToServer.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
public PacketBase reply(@NotNull MessageContext ctx) {
	EntityPlayer player = ctx.getServerHandler().player;

	IMiscCapability cap = MiscCapabilityProvider.getCap(player);

	if (cap != null) {
		cap.deserializeNBT(tags);
	}

	return new PacketUpdateMiscCapToClient(tags);
}
 
Example 11
Source File: SyncHotkeyPacket.java    From Cyberware with MIT License 5 votes vote down vote up
@Override
public IMessage onMessage(SyncHotkeyPacket message, MessageContext ctx)
{
	EntityPlayerMP player = ctx.getServerHandler().playerEntity;
	DimensionManager.getWorld(player.worldObj.provider.getDimension()).addScheduledTask(new DoSync(message.selectedPart, message.key, player));

	return null;
}
 
Example 12
Source File: PacketSuccPearlsToStorageHolder.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void handle(@NotNull MessageContext ctx) {
	EntityPlayerMP player = ctx.getServerHandler().player;

	ItemStack stack = player.getHeldItemMainhand();

	if (stack.getItem() instanceof IPearlStorageHolder) {
		IPearlStorageHolder holder = (IPearlStorageHolder) stack.getItem();
		int originalPearlCount = holder.getPearlCount(stack);

		holder.succPearls(player);

		PacketHandler.NETWORK.sendTo(new PacketUpdatePearlGUI(originalPearlCount, holder.getPearlCount(stack), null, stack.getTagCompound()), player);
	}
}
 
Example 13
Source File: MessageProcessorAction.java    From Minecoprocessors with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IMessage onMessage(final MessageProcessorAction message, MessageContext ctx) {
  if (message.action == null) {
    return null;
  }
  final EntityPlayerMP payer = ctx.getServerHandler().player;
  payer.getServerWorld().addScheduledTask(new Worker(payer, message));
  return null;
}
 
Example 14
Source File: MessageBookCodeData.java    From Minecoprocessors with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onMessageSynchronized(final MessageBookCodeData message, final MessageContext context) {
  final EntityPlayer player = context.getServerHandler().player;
  if (player != null) {
    final ItemStack stack = player.getHeldItem(EnumHand.MAIN_HAND);
    if (ItemBookCode.isBookCode(stack)) {
      final ItemBookCode.Data data = ItemBookCode.Data.loadFromNBT(message.getNbt());
      ItemBookCode.Data.saveToStack(stack, data);
    }
  }
}
 
Example 15
Source File: TriggerActiveAbilityPacket.java    From Cyberware with MIT License 5 votes vote down vote up
@Override
public IMessage onMessage(TriggerActiveAbilityPacket message, MessageContext ctx)
{
	EntityPlayerMP player = ctx.getServerHandler().playerEntity;
	DimensionManager.getWorld(player.worldObj.provider.getDimension()).addScheduledTask(new DoSync(message.stack, player));

	return null;
}
 
Example 16
Source File: NearbyCraftCommandsImplementation.java    From malmo with MIT License 4 votes vote down vote up
@Override
public IMessage onMessage(CraftNearbyMessage message, MessageContext ctx) {
    EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    Vec3d headPos = new Vec3d(player.posX, player.posY + 1.6, player.posZ);

    // Location checking
    boolean closeTable = false;
    for (BlockPos furnace : craftingTables) {
        Vec3d blockVec = new Vec3d(furnace.getX() + 0.5, furnace.getY() + 0.5, furnace.getZ() + 0.5);

        if (headPos.squareDistanceTo(blockVec) <= 25.0) {
            // Within a reasonable FOV?
            // Lots of trig, let's go
            double fov = Minecraft.getMinecraft().gameSettings.fovSetting;
            double height = Minecraft.getMinecraft().displayHeight;
            double width = Minecraft.getMinecraft().displayWidth;
            Vec3d lookVec = player.getLookVec();
            Vec3d toBlock = blockVec.subtract(headPos);

            // Projection of block onto player look vector - if greater than 0, then in front of us
            double scalarProjection = lookVec.dotProduct(toBlock) / lookVec.lengthVector();
            if (scalarProjection > 0) {
                Vec3d yUnit = new Vec3d(0, 1.0, 0);
                Vec3d lookCross = lookVec.crossProduct(yUnit);
                Vec3d blockProjectedOntoCross = lookCross.scale(lookCross.dotProduct(toBlock) / lookCross.lengthVector());
                Vec3d blockProjectedOntoPlayerPlane = toBlock.subtract(blockProjectedOntoCross);
                double xyDot = lookVec.dotProduct(blockProjectedOntoPlayerPlane);
                double pitchTheta = Math.acos(xyDot / (lookVec.lengthVector() * blockProjectedOntoPlayerPlane.lengthVector()));

                Vec3d playerY = lookCross.crossProduct(lookVec);
                Vec3d blockProjectedOntoPlayerY = playerY.scale(playerY.dotProduct(toBlock) / playerY.lengthVector());
                Vec3d blockProjectedOntoYawPlane = toBlock.subtract(blockProjectedOntoPlayerY);
                double xzDot = lookVec.dotProduct(blockProjectedOntoYawPlane);
                double yawTheta = Math.acos(xzDot / (lookVec.lengthVector() * blockProjectedOntoYawPlane.lengthVector()));

                if (Math.abs(Math.toDegrees(yawTheta)) <= Math.min(1, width / height) * (fov / 2.0) && Math.abs(Math.toDegrees(pitchTheta)) <= Math.min(1, height / width) * (fov / 2.0))
                    closeTable = true;
            }
        }
    }

    if (closeTable) {
        // We are close enough, try crafting recipes
        List<IRecipe> matching_recipes;
        String[] split = message.parameters.split(" ");
        if (split.length > 1)
            matching_recipes = CraftingHelper.getRecipesForRequestedOutput(message.parameters, true);
        else
            matching_recipes = CraftingHelper.getRecipesForRequestedOutput(message.parameters, false);

        for (IRecipe recipe : matching_recipes)
            if (CraftingHelper.attemptCrafting(player, recipe))
                return null;
    }

    return null;
}
 
Example 17
Source File: PacketBounce.java    From Wizardry with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void handle(@NotNull MessageContext ctx) {
	ctx.getServerHandler().player.fallDistance = 0;
}
 
Example 18
Source File: CommonProxy.java    From EmergingTechnology with MIT License 4 votes vote down vote up
public World getWorld(MessageContext ctx) {
return ctx.getServerHandler().player.world;
  }
 
Example 19
Source File: NearbySmeltCommandsImplementation.java    From malmo with MIT License 4 votes vote down vote up
@Override
public IMessage onMessage(SmeltNearbyMessage message, MessageContext ctx) {
    EntityPlayerMP player = ctx.getServerHandler().playerEntity;
    Vec3d headPos = new Vec3d(player.posX, player.posY + 1.6, player.posZ);

    // Location checking
    boolean closeFurnace = false;
    for (BlockPos furnace : furnaces) {
        Vec3d blockVec = new Vec3d(furnace.getX() + 0.5, furnace.getY() + 0.5, furnace.getZ() + 0.5);

        if (headPos.squareDistanceTo(blockVec) <= 25.0) {
            // Within a reasonable FOV?
            // Lots of trig, let's go
            double fov = Minecraft.getMinecraft().gameSettings.fovSetting;
            double height = Minecraft.getMinecraft().displayHeight;
            double width = Minecraft.getMinecraft().displayWidth;
            Vec3d lookVec = player.getLookVec();
            Vec3d toBlock = blockVec.subtract(headPos);

            // Projection of block onto player look vector - if greater than 0, then in front of us
            double scalarProjection = lookVec.dotProduct(toBlock) / lookVec.lengthVector();
            if (scalarProjection > 0) {
                Vec3d yUnit = new Vec3d(0, 1.0, 0);
                Vec3d lookCross = lookVec.crossProduct(yUnit);
                Vec3d blockProjectedOntoCross = lookCross.scale(lookCross.dotProduct(toBlock) / lookCross.lengthVector());
                Vec3d blockProjectedOntoPlayerPlane = toBlock.subtract(blockProjectedOntoCross);
                double xyDot = lookVec.dotProduct(blockProjectedOntoPlayerPlane);
                double pitchTheta = Math.acos(xyDot / (lookVec.lengthVector() * blockProjectedOntoPlayerPlane.lengthVector()));

                Vec3d playerY = lookCross.crossProduct(lookVec);
                Vec3d blockProjectedOntoPlayerY = playerY.scale(playerY.dotProduct(toBlock) / playerY.lengthVector());
                Vec3d blockProjectedOntoYawPlane = toBlock.subtract(blockProjectedOntoPlayerY);
                double xzDot = lookVec.dotProduct(blockProjectedOntoYawPlane);
                double yawTheta = Math.acos(xzDot / (lookVec.lengthVector() * blockProjectedOntoYawPlane.lengthVector()));

                if (Math.abs(Math.toDegrees(yawTheta)) <= Math.min(1, width / height) * (fov / 2.0) && Math.abs(Math.toDegrees(pitchTheta)) <= Math.min(1, height / width) * (fov / 2.0))
                    closeFurnace = true;
            }
        }
    }

    if (closeFurnace) {
        ItemStack input = CraftingHelper.getSmeltingRecipeForRequestedOutput(message.parameters);
        if (input != null)
            if (CraftingHelper.attemptSmelting(player, input))
                return null;
    }

    return null;
}
 
Example 20
Source File: OptimiserUpdatePacket.java    From EmergingTechnology with MIT License 4 votes vote down vote up
private void handle(OptimiserUpdatePacket message, MessageContext ctx) {
    EntityPlayerMP player = ctx.getServerHandler().player;

    if (player == null)
        return;

    if (message.packetField == 0)
        return;

    OptimiserTileEntity tileEntity = getTileEntity(player.world, message.pos);

    if (tileEntity == null)
        return;

    tileEntity.setField(EnumTileField.getById(message.packetField), message.value);
}