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);
}
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;
}
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);
}
}
}
}
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;
}
}
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;
}
Aggregations