Search in sources :

Example 1 with IBlockState

use of net.minecraft.block.state.IBlockState in project malmo by Microsoft.

the class ObservationFromRayImplementation method buildMouseOverData.

/** Build the json object for the object under the cursor, whether it is a block or a creature.<br>
     * If there is any data to be returned, the json will be added in a subnode called "LineOfSight".
     * @param json a JSON object into which the info for the object under the mouse will be added.
     */
public static void buildMouseOverData(JsonObject json) {
    // We could use Minecraft.getMinecraft().objectMouseOver but it's limited to the block reach distance, and
    // doesn't register floating tile items.
    // Ideally use Minecraft.timer.renderPartialTicks - but we don't need sub-tick resolution.
    float partialTicks = 0;
    Entity viewer = Minecraft.getMinecraft().thePlayer;
    // Hard-coded for now - in future will be parameterised via the XML.
    float depth = 50;
    Vec3 eyePos = viewer.getPositionEyes(partialTicks);
    Vec3 lookVec = viewer.getLook(partialTicks);
    Vec3 searchVec = eyePos.addVector(lookVec.xCoord * depth, lookVec.yCoord * depth, lookVec.zCoord * depth);
    MovingObjectPosition mop = Minecraft.getMinecraft().theWorld.rayTraceBlocks(eyePos, searchVec, false, false, false);
    MovingObjectPosition mopEnt = findEntity(eyePos, lookVec, depth, mop, true);
    if (mopEnt != null)
        mop = mopEnt;
    if (mop == null) {
        // Nothing under the mouse.
        return;
    }
    // Calculate ranges for player interaction:
    double hitDist = mop.hitVec.distanceTo(eyePos);
    double blockReach = Minecraft.getMinecraft().playerController.getBlockReachDistance();
    double entityReach = Minecraft.getMinecraft().playerController.extendedReach() ? 6.0 : 3.0;
    JsonObject jsonMop = new JsonObject();
    if (mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
        // We're looking at a block - send block data:
        jsonMop.addProperty("hitType", "block");
        jsonMop.addProperty("x", mop.hitVec.xCoord);
        jsonMop.addProperty("y", mop.hitVec.yCoord);
        jsonMop.addProperty("z", mop.hitVec.zCoord);
        IBlockState state = Minecraft.getMinecraft().theWorld.getBlockState(mop.getBlockPos());
        List<IProperty> extraProperties = new ArrayList<IProperty>();
        DrawBlock db = MinecraftTypeHelper.getDrawBlockFromBlockState(state, extraProperties);
        jsonMop.addProperty("type", db.getType().value());
        if (db.getColour() != null)
            jsonMop.addProperty("colour", db.getColour().value());
        if (db.getVariant() != null)
            jsonMop.addProperty("variant", db.getVariant().getValue());
        if (db.getFace() != null)
            jsonMop.addProperty("facing", db.getFace().value());
        if (extraProperties.size() > 0) {
            // Add the extra properties that aren't covered by colour/variant/facing.
            for (IProperty prop : extraProperties) {
                String key = "prop_" + prop.getName();
                if (prop.getValueClass() == Boolean.class)
                    jsonMop.addProperty(key, Boolean.valueOf(state.getValue(prop).toString()));
                else if (prop.getValueClass() == Integer.class)
                    jsonMop.addProperty(key, Integer.valueOf(state.getValue(prop).toString()));
                else
                    jsonMop.addProperty(key, state.getValue(prop).toString());
            }
        }
        jsonMop.addProperty("inRange", hitDist <= blockReach);
        jsonMop.addProperty("distance", hitDist);
    } else if (mop.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) {
        // Looking at an entity:
        Entity entity = mop.entityHit;
        if (entity != null) {
            jsonMop.addProperty("x", entity.posX);
            jsonMop.addProperty("y", entity.posY);
            jsonMop.addProperty("z", entity.posZ);
            String name = entity.getName();
            String hitType = "entity";
            if (entity instanceof EntityItem) {
                ItemStack is = ((EntityItem) entity).getEntityItem();
                DrawItem di = MinecraftTypeHelper.getDrawItemFromItemStack(is);
                if (di.getColour() != null)
                    jsonMop.addProperty("colour", di.getColour().value());
                if (di.getVariant() != null)
                    jsonMop.addProperty("variant", di.getVariant().getValue());
                jsonMop.addProperty("stackSize", is.stackSize);
                name = di.getType();
                hitType = "item";
            }
            jsonMop.addProperty("type", name);
            jsonMop.addProperty("hitType", hitType);
        }
        jsonMop.addProperty("inRange", hitDist <= entityReach);
        jsonMop.addProperty("distance", hitDist);
    }
    json.add("LineOfSight", jsonMop);
}
Also used : Entity(net.minecraft.entity.Entity) IBlockState(net.minecraft.block.state.IBlockState) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) MovingObjectPosition(net.minecraft.util.MovingObjectPosition) IProperty(net.minecraft.block.properties.IProperty) Vec3(net.minecraft.util.Vec3) DrawItem(com.microsoft.Malmo.Schemas.DrawItem) DrawBlock(com.microsoft.Malmo.Schemas.DrawBlock) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 2 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class ModelLoader method onPostBakeEvent.

/**
     * Internal, do not use.
     */
public void onPostBakeEvent(IRegistry<ModelResourceLocation, IBakedModel> modelRegistry) {
    IBakedModel missingModel = modelRegistry.getObject(MODEL_MISSING);
    Map<String, Integer> modelErrors = Maps.newHashMap();
    Set<ResourceLocation> printedBlockStateErrors = Sets.newHashSet();
    Multimap<ModelResourceLocation, IBlockState> reverseBlockMap = null;
    Multimap<ModelResourceLocation, String> reverseItemMap = null;
    if (enableVerboseMissingInfo) {
        reverseBlockMap = HashMultimap.create();
        for (Map.Entry<IBlockState, ModelResourceLocation> entry : blockModelShapes.getBlockStateMapper().putAllStateModelLocations().entrySet()) {
            reverseBlockMap.put(entry.getValue(), entry.getKey());
        }
        reverseItemMap = HashMultimap.create();
        for (Item item : GameData.getItemRegistry().typeSafeIterable()) {
            for (String s : getVariantNames(item)) {
                ModelResourceLocation memory = getInventoryVariant(s);
                reverseItemMap.put(memory, item.getRegistryName().toString());
            }
        }
    }
    for (Map.Entry<ResourceLocation, Exception> entry : loadingExceptions.entrySet()) {
        // ignoring pure ResourceLocation arguments, all things we care about pass ModelResourceLocation
        if (entry.getKey() instanceof ModelResourceLocation) {
            ModelResourceLocation location = (ModelResourceLocation) entry.getKey();
            IBakedModel model = modelRegistry.getObject(location);
            if (model == null || model == missingModel) {
                String domain = entry.getKey().getResourceDomain();
                Integer errorCountBox = modelErrors.get(domain);
                int errorCount = errorCountBox == null ? 0 : errorCountBox;
                errorCount++;
                if (errorCount < verboseMissingInfoCount) {
                    String errorMsg = "Exception loading model for variant " + entry.getKey();
                    if (enableVerboseMissingInfo) {
                        Collection<IBlockState> blocks = reverseBlockMap.get(location);
                        if (!blocks.isEmpty()) {
                            if (blocks.size() == 1) {
                                errorMsg += " for blockstate \"" + blocks.iterator().next() + "\"";
                            } else {
                                errorMsg += " for blockstates [\"" + Joiner.on("\", \"").join(blocks) + "\"]";
                            }
                        }
                        Collection<String> items = reverseItemMap.get(location);
                        if (!items.isEmpty()) {
                            if (!blocks.isEmpty())
                                errorMsg += " and";
                            if (items.size() == 1) {
                                errorMsg += " for item \"" + items.iterator().next() + "\"";
                            } else {
                                errorMsg += " for items [\"" + Joiner.on("\", \"").join(items) + "\"]";
                            }
                        }
                    }
                    if (entry.getValue() instanceof ItemLoadingException) {
                        ItemLoadingException ex = (ItemLoadingException) entry.getValue();
                        FMLLog.getLogger().error(errorMsg + ", normal location exception: ", ex.normalException);
                        FMLLog.getLogger().error(errorMsg + ", blockstate location exception: ", ex.blockstateException);
                    } else {
                        FMLLog.getLogger().error(errorMsg, entry.getValue());
                    }
                    ResourceLocation blockstateLocation = new ResourceLocation(location.getResourceDomain(), location.getResourcePath());
                    if (loadingExceptions.containsKey(blockstateLocation) && !printedBlockStateErrors.contains(blockstateLocation)) {
                        FMLLog.getLogger().error("Exception loading blockstate for the variant " + location + ": ", loadingExceptions.get(blockstateLocation));
                        printedBlockStateErrors.add(blockstateLocation);
                    }
                }
                modelErrors.put(domain, errorCount);
            }
            if (model == null) {
                modelRegistry.putObject(location, missingModel);
            }
        }
    }
    for (ModelResourceLocation missing : missingVariants) {
        IBakedModel model = modelRegistry.getObject(missing);
        if (model == null || model == missingModel) {
            String domain = missing.getResourceDomain();
            Integer errorCountBox = modelErrors.get(domain);
            int errorCount = errorCountBox == null ? 0 : errorCountBox;
            errorCount++;
            if (errorCount < verboseMissingInfoCount) {
                FMLLog.severe("Model definition for location %s not found", missing);
            }
            modelErrors.put(domain, errorCount);
        }
        if (model == null) {
            modelRegistry.putObject(missing, missingModel);
        }
    }
    for (Map.Entry<String, Integer> e : modelErrors.entrySet()) {
        if (e.getValue() >= verboseMissingInfoCount) {
            FMLLog.severe("Suppressed additional %s model loading errors for domain %s", e.getValue() - verboseMissingInfoCount, e.getKey());
        }
    }
    isLoading = false;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) MissingVariantException(net.minecraft.client.renderer.block.model.ModelBlockDefinition.MissingVariantException) Item(net.minecraft.item.Item) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ResourceLocation(net.minecraft.util.ResourceLocation) IBakedModel(net.minecraft.client.renderer.block.model.IBakedModel) Map(java.util.Map) TextureMap(net.minecraft.client.renderer.texture.TextureMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class AnimationTESR method renderTileEntityFast.

public void renderTileEntityFast(@Nonnull T te, double x, double y, double z, float partialTick, int breakStage, @Nonnull VertexBuffer renderer) {
    if (!te.hasCapability(CapabilityAnimation.ANIMATION_CAPABILITY, null)) {
        return;
    }
    if (blockRenderer == null)
        blockRenderer = Minecraft.getMinecraft().getBlockRendererDispatcher();
    BlockPos pos = te.getPos();
    IBlockAccess world = MinecraftForgeClient.getRegionRenderCache(te.getWorld(), pos);
    IBlockState state = world.getBlockState(pos);
    if (state.getPropertyKeys().contains(Properties.StaticProperty)) {
        state = state.withProperty(Properties.StaticProperty, false);
    }
    if (state instanceof IExtendedBlockState) {
        IExtendedBlockState exState = (IExtendedBlockState) state;
        if (exState.getUnlistedNames().contains(Properties.AnimationProperty)) {
            float time = Animation.getWorldTime(getWorld(), partialTick);
            IAnimationStateMachine capability = te.getCapability(CapabilityAnimation.ANIMATION_CAPABILITY, null);
            if (capability != null) {
                Pair<IModelState, Iterable<Event>> pair = capability.apply(time);
                handleEvents(te, time, pair.getRight());
                // TODO: caching?
                IBakedModel model = blockRenderer.getBlockModelShapes().getModelForState(exState.getClean());
                exState = exState.withProperty(Properties.AnimationProperty, pair.getLeft());
                renderer.setTranslation(x - pos.getX(), y - pos.getY(), z - pos.getZ());
                blockRenderer.getBlockModelRenderer().renderModel(world, model, exState, pos, renderer, false);
            }
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IExtendedBlockState(net.minecraftforge.common.property.IExtendedBlockState) IBlockAccess(net.minecraft.world.IBlockAccess) IModelState(net.minecraftforge.common.model.IModelState) BlockPos(net.minecraft.util.math.BlockPos) IBakedModel(net.minecraft.client.renderer.block.model.IBakedModel) IAnimationStateMachine(net.minecraftforge.common.model.animation.IAnimationStateMachine)

Example 4 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryPlaceFluid.

/**
     * Tries to place a fluid in the world in block form and drains the container.
     * Makes a fluid emptying sound when successful.
     * Honors the amount of fluid contained by the used container.
     * Checks if water-like fluids should vaporize like in the nether.
     *
     * Modeled after {@link net.minecraft.item.ItemBucket#tryPlaceContainedLiquid(EntityPlayer, World, BlockPos)}
     *
     * @param player    Player who places the fluid. May be null for blocks like dispensers.
     * @param world     World to place the fluid in
     * @param pos       The position in the world to place the fluid block
     * @param container The fluid container holding the fluidStack to place
     * @param resource  The fluidStack to place
     * @return the container's ItemStack with the remaining amount of fluid if the placement was successful, null otherwise
     */
@Nonnull
public static FluidActionResult tryPlaceFluid(@Nullable EntityPlayer player, World world, BlockPos pos, @Nonnull ItemStack container, FluidStack resource) {
    if (world == null || resource == null || pos == null) {
        return FluidActionResult.FAILURE;
    }
    Fluid fluid = resource.getFluid();
    if (fluid == null || !fluid.canBePlacedInWorld()) {
        return FluidActionResult.FAILURE;
    }
    // check that we can place the fluid at the destination
    IBlockState destBlockState = world.getBlockState(pos);
    Material destMaterial = destBlockState.getMaterial();
    boolean isDestNonSolid = !destMaterial.isSolid();
    boolean isDestReplaceable = destBlockState.getBlock().isReplaceable(world, pos);
    if (!world.isAirBlock(pos) && !isDestNonSolid && !isDestReplaceable) {
        // Non-air, solid, unreplacable block. We can't put fluid here.
        return FluidActionResult.FAILURE;
    }
    if (world.provider.doesWaterVaporize() && fluid.doesVaporize(resource)) {
        fluid.vaporize(player, world, pos, resource);
        return tryEmptyContainer(container, new VoidFluidHandler(), Integer.MAX_VALUE, player, true);
    } else {
        if (!world.isRemote && (isDestNonSolid || isDestReplaceable) && !destMaterial.isLiquid()) {
            world.destroyBlock(pos, true);
        }
        // Defer the placement to the fluid block
        // Instead of actually "filling", the fluid handler method replaces the block
        Block block = fluid.getBlock();
        IFluidHandler handler;
        if (block instanceof IFluidBlock) {
            handler = new FluidBlockWrapper((IFluidBlock) block, world, pos);
        } else if (block instanceof BlockLiquid) {
            handler = new BlockLiquidWrapper((BlockLiquid) block, world, pos);
        } else {
            handler = new BlockWrapper(block, world, pos);
        }
        FluidActionResult result = tryEmptyContainer(container, handler, Integer.MAX_VALUE, player, true);
        if (result.isSuccess()) {
            SoundEvent soundevent = fluid.getEmptySound(resource);
            world.playSound(player, pos, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
        }
        return result;
    }
}
Also used : VoidFluidHandler(net.minecraftforge.fluids.capability.templates.VoidFluidHandler) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) IBlockState(net.minecraft.block.state.IBlockState) Material(net.minecraft.block.material.Material) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) SoundEvent(net.minecraft.util.SoundEvent) BlockLiquid(net.minecraft.block.BlockLiquid) BlockLiquidWrapper(net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper) Block(net.minecraft.block.Block) Nonnull(javax.annotation.Nonnull)

Example 5 with IBlockState

use of net.minecraft.block.state.IBlockState in project MinecraftForge by MinecraftForge.

the class FluidUtil method getFluidHandler.

/**
     * Helper method to get an IFluidHandler for at a block position.
     *
     * Returns null if there is no valid fluid handler.
     */
@Nullable
public static IFluidHandler getFluidHandler(World world, BlockPos blockPos, @Nullable EnumFacing side) {
    IBlockState state = world.getBlockState(blockPos);
    Block block = state.getBlock();
    if (block.hasTileEntity(state)) {
        TileEntity tileEntity = world.getTileEntity(blockPos);
        if (tileEntity != null && tileEntity.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side)) {
            return tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
        }
    } else if (block instanceof IFluidBlock) {
        return new FluidBlockWrapper((IFluidBlock) block, world, blockPos);
    } else if (block instanceof BlockLiquid) {
        return new BlockLiquidWrapper((BlockLiquid) block, world, blockPos);
    }
    return null;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) IBlockState(net.minecraft.block.state.IBlockState) BlockLiquid(net.minecraft.block.BlockLiquid) BlockLiquidWrapper(net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper) Block(net.minecraft.block.Block) Nullable(javax.annotation.Nullable)

Aggregations

IBlockState (net.minecraft.block.state.IBlockState)2965 BlockPos (net.minecraft.util.math.BlockPos)1194 Block (net.minecraft.block.Block)790 ItemStack (net.minecraft.item.ItemStack)515 EnumFacing (net.minecraft.util.EnumFacing)415 TileEntity (net.minecraft.tileentity.TileEntity)336 World (net.minecraft.world.World)254 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)171 EntityPlayer (net.minecraft.entity.player.EntityPlayer)136 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)126 ArrayList (java.util.ArrayList)124 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)119 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)95 IBakedModel (net.minecraft.client.renderer.block.model.IBakedModel)93 Entity (net.minecraft.entity.Entity)93 ItemBlock (net.minecraft.item.ItemBlock)88 Random (java.util.Random)82 Vec3d (net.minecraft.util.math.Vec3d)81 BlockPos (net.minecraft.util.BlockPos)77 RayTraceResult (net.minecraft.util.math.RayTraceResult)76