Search in sources :

Example 61 with EntityPlayerSP

use of net.minecraft.client.entity.EntityPlayerSP in project malmo by Microsoft.

the class DiscreteMovementCommandsImplementation method onExecute.

@Override
protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
    boolean handled = false;
    EntityPlayerSP player = Minecraft.getMinecraft().player;
    if (player != null) {
        int z = 0;
        int x = 0;
        int y = 0;
        DiscreteMovementCommand command = verbToCommand(verb);
        if (command == null)
            // Did not recognise this command.
            return false;
        switch(command) {
            case MOVENORTH:
            case JUMPNORTH:
                z = -1;
                break;
            case MOVESOUTH:
            case JUMPSOUTH:
                z = 1;
                break;
            case MOVEEAST:
            case JUMPEAST:
                x = 1;
                break;
            case MOVEWEST:
            case JUMPWEST:
                x = -1;
                break;
            case MOVE:
            case JUMPMOVE:
            case STRAFE:
            case JUMPSTRAFE:
                if (parameter != null && parameter.length() != 0) {
                    float velocity = Float.valueOf(parameter);
                    int offset = (velocity > 0) ? 1 : ((velocity < 0) ? -1 : 0);
                    int direction = getDirectionFromYaw(player.rotationYaw);
                    // For strafing, add one to direction:
                    if (command == DiscreteMovementCommand.STRAFE || command == DiscreteMovementCommand.JUMPSTRAFE)
                        direction = (direction + 1) % 4;
                    switch(direction) {
                        case // North
                        0:
                            z = offset;
                            break;
                        case // East
                        1:
                            x = -offset;
                            break;
                        case // South
                        2:
                            z = -offset;
                            break;
                        case // West
                        3:
                            x = offset;
                            break;
                    }
                    break;
                }
            case TURN:
                if (parameter != null && parameter.length() != 0) {
                    float yawDelta = Float.valueOf(parameter);
                    int direction = getDirectionFromYaw(player.rotationYaw);
                    direction += (yawDelta > 0) ? 1 : ((yawDelta < 0) ? -1 : 0);
                    direction = (direction + 4) % 4;
                    player.rotationYaw = direction * 90;
                    player.onUpdate();
                    // Send a message that the ContinuousMovementCommands can pick up on:
                    Event event = new CommandForWheeledRobotNavigationImplementation.ResetPitchAndYawEvent(true, player.rotationYaw, false, 0);
                    MinecraftForge.EVENT_BUS.post(event);
                    handled = true;
                }
                break;
            case LOOK:
                if (parameter != null && parameter.length() != 0) {
                    float pitchDelta = Float.valueOf(parameter);
                    player.rotationPitch += (pitchDelta < 0) ? -45 : ((pitchDelta > 0) ? 45 : 0);
                    player.onUpdate();
                    // Send a message that the ContinuousMovementCommands can pick up on:
                    Event event = new CommandForWheeledRobotNavigationImplementation.ResetPitchAndYawEvent(false, 0, true, player.rotationPitch);
                    MinecraftForge.EVENT_BUS.post(event);
                    handled = true;
                }
                break;
            case ATTACK:
                {
                    RayTraceResult mop = Minecraft.getMinecraft().objectMouseOver;
                    if (mop.typeOfHit == RayTraceResult.Type.BLOCK) {
                        BlockPos hitPos = mop.getBlockPos();
                        EnumFacing face = mop.sideHit;
                        IBlockState iblockstate = player.world.getBlockState(hitPos);
                        Block block = iblockstate.getBlock();
                        if (iblockstate.getMaterial() != Material.AIR) {
                            MalmoMod.network.sendToServer(new AttackActionMessage(hitPos, face, mop.hitVec));
                            // Trigger a reward for collecting the block
                            java.util.List<ItemStack> items = block.getDrops(player.world, hitPos, iblockstate, 0);
                            for (ItemStack item : items) {
                                RewardForCollectingItemImplementation.GainItemEvent event = new RewardForCollectingItemImplementation.GainItemEvent(item);
                                MinecraftForge.EVENT_BUS.post(event);
                            }
                        }
                    }
                    handled = true;
                    break;
                }
            case USE:
            case JUMPUSE:
                {
                    RayTraceResult mop = getObjectMouseOver(command);
                    if (mop.typeOfHit == RayTraceResult.Type.BLOCK) {
                        if (player.inventory.getCurrentItem() != null) {
                            ItemStack itemStack = player.inventory.getCurrentItem();
                            Block b = Block.getBlockFromItem(itemStack.getItem());
                            if (b != null) {
                                BlockPos pos = mop.getBlockPos().add(mop.sideHit.getDirectionVec());
                                // Can we place this block here?
                                AxisAlignedBB axisalignedbb = b.getDefaultState().getCollisionBoundingBox(player.world, pos);
                                Entity exceptedEntity = (command == DiscreteMovementCommand.USE) ? null : player;
                                // (Not ideal, but needed by jump-use to allow the player to place a block where their feet would be.)
                                if (axisalignedbb == null || player.world.checkNoEntityCollision(axisalignedbb.offset(pos), exceptedEntity)) {
                                    boolean standOnBlockPlaced = (command == DiscreteMovementCommand.JUMPUSE && mop.getBlockPos().equals(new BlockPos(player.posX, player.posY - 1, player.posZ)));
                                    MalmoMod.network.sendToServer(new UseActionMessage(mop.getBlockPos(), itemStack, mop.sideHit, standOnBlockPlaced, mop.hitVec));
                                }
                            }
                        }
                    }
                    handled = true;
                    break;
                }
            case JUMP:
                // Handled below.
                break;
        }
        // Handle jumping cases:
        if (command == DiscreteMovementCommand.JUMP || command == DiscreteMovementCommand.JUMPNORTH || command == DiscreteMovementCommand.JUMPEAST || command == DiscreteMovementCommand.JUMPSOUTH || command == DiscreteMovementCommand.JUMPWEST || command == DiscreteMovementCommand.JUMPMOVE || command == DiscreteMovementCommand.JUMPUSE || command == DiscreteMovementCommand.JUMPSTRAFE)
            y = 1;
        if (this.params.isAutoJump() && y == 0 && (z != 0 || x != 0)) {
            // Do we need to jump?
            if (!player.world.getCollisionBoxes(player, player.getEntityBoundingBox().offset(x, 0, z)).isEmpty())
                y = 1;
        }
        if (z != 0 || x != 0 || y != 0) {
            // Attempt to move the entity:
            double oldX = player.posX;
            double oldZ = player.posZ;
            player.move(MoverType.SELF, (double) x, (double) y, (double) z);
            player.onUpdate();
            if (this.params.isAutoFall()) {
                // Did we step off a block? If so, attempt to fast-forward our fall.
                // Give up after this many attempts
                int bailCountdown = 256;
                // (This is needed because, for example, if the player is caught in a web, the downward movement will have no effect.)
                while (!player.onGround && !player.capabilities.isFlying && bailCountdown > 0) {
                    // Fast-forward downwards.
                    player.move(MoverType.SELF, 0.0, Math.floor(player.posY - 0.0000001) - player.posY, 0.0);
                    player.onUpdate();
                    bailCountdown--;
                }
            }
            // Now check where we ended up:
            double newX = player.posX;
            double newZ = player.posZ;
            // Are we still in the centre of a square, or did we get shunted?
            double offsetX = newX - Math.floor(newX);
            double offsetZ = newZ - Math.floor(newZ);
            if (Math.abs(offsetX - 0.5) + Math.abs(offsetZ - 0.5) > 0.01) {
                // We failed to move to the centre of the target square.
                // This might be because the target square was occupied, and we
                // were shunted back into our source square,
                // or it might be that the target square is occupied by something smaller
                // than one block (eg a fence post), and we're in the target square but
                // shunted off-centre.
                // Either way, we can't stay here, so move back to our original position.
                // Before we do that, fire off a message - this will give the TouchingBlockType handlers
                // a chance to react to the current position:
                DiscretePartialMoveEvent event = new DiscretePartialMoveEvent(player.posX, player.posY, player.posZ);
                MinecraftForge.EVENT_BUS.post(event);
                // Now adjust the player:
                player.move(MoverType.SELF, oldX - newX, 0.0, oldZ - newZ);
                player.onUpdate();
            }
            // Now set the last tick pos values, to turn off inter-tick positional interpolation:
            player.lastTickPosX = player.posX;
            player.lastTickPosY = player.posY;
            player.lastTickPosZ = player.posZ;
            try {
                MalmoMod.getPropertiesForCurrentThread().put(MOVE_ATTEMPTED_KEY, true);
            } catch (Exception e) {
                // TODO - proper error reporting.
                System.out.println("Failed to access properties for the client thread after discrete movement - reward may be incorrect.");
            }
            handled = true;
        }
    }
    return handled;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Entity(net.minecraft.entity.Entity) EnumFacing(net.minecraft.util.EnumFacing) DiscreteMovementCommand(com.microsoft.Malmo.Schemas.DiscreteMovementCommand) BlockPos(net.minecraft.util.math.BlockPos) IBlockState(net.minecraft.block.state.IBlockState) RayTraceResult(net.minecraft.util.math.RayTraceResult) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) Event(net.minecraftforge.fml.common.eventhandler.Event) BlockEvent(net.minecraftforge.event.world.BlockEvent) Block(net.minecraft.block.Block) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) ItemStack(net.minecraft.item.ItemStack)

Example 62 with EntityPlayerSP

use of net.minecraft.client.entity.EntityPlayerSP in project malmo by Microsoft.

the class ObservationFromNearbyEntitiesImplementation method writeObservationsToJSON.

@Override
public void writeObservationsToJSON(JsonObject json, MissionInit missionInit) {
    this.tickCount++;
    EntityPlayerSP player = Minecraft.getMinecraft().player;
    // Get all the currently loaded entities:
    List<?> entities = Minecraft.getMinecraft().world.getLoadedEntityList();
    // Get the list of RangeDefinitions that need firing:
    List<RangeDefinition> rangesToFire = new ArrayList<RangeDefinition>();
    int index = 0;
    for (RangeDefinition rd : this.oneparams.getRange()) {
        if (this.tickCount - this.lastFiringTimes[index] >= rd.getUpdateFrequency()) {
            rangesToFire.add(rd);
            this.lastFiringTimes[index] = this.tickCount;
        }
        index++;
    }
    // Create a list of empty lists to populate:
    List<List<Entity>> entitiesInRange = new ArrayList<List<Entity>>();
    for (int i = 0; i < rangesToFire.size(); i++) entitiesInRange.add(new ArrayList<Entity>());
    // Populate all our lists according to which entities are in range:
    for (Object obj : entities) {
        if (obj instanceof Entity) {
            Entity e = (Entity) obj;
            index = 0;
            for (RangeDefinition rd : rangesToFire) {
                if (Math.abs(e.posX - player.posX) < rd.getXrange().doubleValue() && Math.abs(e.posY - player.posY) < rd.getYrange().doubleValue() && Math.abs(e.posZ - player.posZ) < rd.getZrange().doubleValue()) {
                    // Belongs in this list:
                    entitiesInRange.get(index).add(e);
                }
                index++;
            }
        }
    }
    // Now build up a JSON array for each populated list:
    index = 0;
    for (List<Entity> entsInRangeList : entitiesInRange) {
        if (!entitiesInRange.isEmpty()) {
            JsonArray arr = new JsonArray();
            for (Entity e : entsInRangeList) {
                JsonObject jsent = new JsonObject();
                jsent.addProperty("yaw", e.rotationYaw);
                jsent.addProperty("x", e.posX);
                jsent.addProperty("y", e.posY);
                jsent.addProperty("z", e.posZ);
                jsent.addProperty("pitch", e.rotationPitch);
                jsent.addProperty("id", e.getCachedUniqueIdString());
                jsent.addProperty("motionX", e.motionX);
                jsent.addProperty("motionY", e.motionY);
                jsent.addProperty("motionZ", e.motionZ);
                String name = MinecraftTypeHelper.getUnlocalisedEntityName(e);
                if (e instanceof EntityItem) {
                    ItemStack is = ((EntityItem) e).getEntityItem();
                    DrawItem di = MinecraftTypeHelper.getDrawItemFromItemStack(is);
                    if (di != null) {
                        name = di.getType();
                        if (di.getColour() != null)
                            jsent.addProperty("colour", di.getColour().value());
                        if (di.getVariant() != null)
                            jsent.addProperty("variation", di.getVariant().getValue());
                    }
                    jsent.addProperty("quantity", is.getCount());
                } else if (e instanceof EntityLivingBase) {
                    EntityLivingBase el = (EntityLivingBase) e;
                    jsent.addProperty("life", el.getHealth());
                }
                jsent.addProperty("name", name);
                arr.add(jsent);
            }
            json.add(this.oneparams.getRange().get(index).getName(), arr);
            index++;
        }
    }
}
Also used : Entity(net.minecraft.entity.Entity) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) EntityLivingBase(net.minecraft.entity.EntityLivingBase) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(com.google.gson.JsonObject) DrawItem(com.microsoft.Malmo.Schemas.DrawItem) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) ItemStack(net.minecraft.item.ItemStack) RangeDefinition(com.microsoft.Malmo.Schemas.RangeDefinition) EntityItem(net.minecraft.entity.item.EntityItem)

Example 63 with EntityPlayerSP

use of net.minecraft.client.entity.EntityPlayerSP in project malmo by Microsoft.

the class PlaceCommandsImplementation method onExecute.

@Override
protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
    EntityPlayerSP player = Minecraft.getMinecraft().player;
    if (player == null)
        return false;
    if (!verb.equalsIgnoreCase("place"))
        return false;
    Item item = Item.getByNameOrId(parameter);
    Block block = Block.getBlockFromItem(item);
    if (item == null || item.getRegistryName() == null || block.getRegistryName() == null)
        return false;
    InventoryPlayer inv = player.inventory;
    boolean blockInInventory = false;
    ItemStack stackInInventory = null;
    int stackIndex = -1;
    for (int i = 0; !blockInInventory && i < inv.getSizeInventory(); i++) {
        Item stack = inv.getStackInSlot(i).getItem();
        if (stack.getRegistryName() != null && stack.getRegistryName().equals(item.getRegistryName())) {
            stackInInventory = inv.getStackInSlot(i);
            stackIndex = i;
            blockInInventory = true;
        }
    }
    // We don't have that item in our inventories
    if (!blockInInventory)
        return false;
    RayTraceResult mop = Minecraft.getMinecraft().objectMouseOver;
    if (mop.typeOfHit == RayTraceResult.Type.BLOCK) {
        BlockPos pos = mop.getBlockPos().add(mop.sideHit.getDirectionVec());
        // Can we place this block here?
        AxisAlignedBB axisalignedbb = block.getDefaultState().getCollisionBoundingBox(player.world, pos);
        if (axisalignedbb == null || player.world.checkNoEntityCollision(axisalignedbb.offset(pos), null)) {
            MalmoMod.network.sendToServer(new DiscreteMovementCommandsImplementation.UseActionMessage(mop.getBlockPos(), new ItemStack(block), mop.sideHit, false, mop.hitVec));
            if (stackInInventory.getCount() == 1)
                inv.setInventorySlotContents(stackIndex, new ItemStack(Block.getBlockById(0)));
            else
                stackInInventory.setCount(stackInInventory.getCount() - 1);
        }
    }
    return true;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) Item(net.minecraft.item.Item) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) RayTraceResult(net.minecraft.util.math.RayTraceResult) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) ItemStack(net.minecraft.item.ItemStack)

Example 64 with EntityPlayerSP

use of net.minecraft.client.entity.EntityPlayerSP in project malmo by Microsoft.

the class MissionQuitCommandsImplementation method onExecute.

@Override
protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
    EntityPlayerSP player = Minecraft.getMinecraft().player;
    if (player == null) {
        return false;
    }
    if (!verb.equalsIgnoreCase(MissionQuitCommand.QUIT.value())) {
        return false;
    }
    player.sendChatMessage("Quitting mission");
    this.iWantToQuit = true;
    return true;
}
Also used : EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP)

Example 65 with EntityPlayerSP

use of net.minecraft.client.entity.EntityPlayerSP in project malmo by Microsoft.

the class ObservationFromDistanceImplementation method writeObservationsToJSON.

@Override
public void writeObservationsToJSON(JsonObject json, MissionInit missionInit) {
    for (NamedPoint marker : odparams.getMarker()) {
        EntityPlayerSP player = Minecraft.getMinecraft().player;
        json.addProperty("distanceFrom" + makeSafe(marker.getName()), PositionHelper.calcDistanceFromPlayerToPosition(player, marker));
    }
}
Also used : NamedPoint(com.microsoft.Malmo.Schemas.NamedPoint) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP)

Aggregations

EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)158 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)31 Minecraft (net.minecraft.client.Minecraft)29 ItemStack (net.minecraft.item.ItemStack)29 BlockPos (net.minecraft.util.math.BlockPos)27 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)23 Entity (net.minecraft.entity.Entity)21 EntityPlayer (net.minecraft.entity.player.EntityPlayer)13 TileEntity (net.minecraft.tileentity.TileEntity)13 WorldClient (net.minecraft.client.multiplayer.WorldClient)11 IBlockState (net.minecraft.block.state.IBlockState)10 Tessellator (net.minecraft.client.renderer.Tessellator)10 GCPlayerStatsClient (micdoodle8.mods.galacticraft.core.entities.player.GCPlayerStatsClient)8 ArrayList (java.util.ArrayList)6 Block (net.minecraft.block.Block)6 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)6 Vec3d (net.minecraft.util.math.Vec3d)6 World (net.minecraft.world.World)6 UUID (java.util.UUID)5 BufferBuilder (net.minecraft.client.renderer.BufferBuilder)5