use of net.minecraft.inventory.Inventory in project Polymorph by TheIllusiveC4.
the class AbstractRecipeData method getRecipe.
@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Inventory> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, World pWorld, List<T> pRecipes) {
this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().get(id)).ifPresent(selected -> {
try {
if (selected.getType() == pType && (((T) selected).matches(pInventory, pWorld) || isEmpty(pInventory))) {
this.setSelectedRecipe(selected);
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
}
this.loadedRecipe = null;
});
if (this.isEmpty(pInventory)) {
this.setFailing(false);
this.sendRecipesListToListeners(true);
return Optional.empty();
}
AtomicReference<T> ref = new AtomicReference<>(null);
this.getLastRecipe().ifPresent(recipe -> {
try {
if (recipe.getType() == pType && ((T) recipe).matches(pInventory, pWorld)) {
this.getSelectedRecipe().ifPresent(selected -> {
try {
if (selected.getType() == pType && ((T) selected).matches(pInventory, pWorld)) {
ref.set((T) selected);
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
}
});
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", recipe.getId(), pInventory);
}
});
T result = ref.get();
if (result != null) {
this.setFailing(false);
this.sendRecipesListToListeners(false);
return Optional.of(result);
}
SortedSet<RecipePair> newDataset = new TreeSet<>();
List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getAllMatches(pType, pInventory, pWorld) : pRecipes;
if (recipes.isEmpty()) {
this.setFailing(true);
this.sendRecipesListToListeners(true);
return Optional.empty();
}
for (T entry : recipes) {
Identifier id = entry.getId();
if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
ref.set(entry);
}
newDataset.add(new RecipePairImpl(id, entry.craft(pInventory)));
}
this.setRecipesList(newDataset);
result = ref.get();
result = result != null ? result : recipes.get(0);
this.lastRecipe = result;
this.setSelectedRecipe(result);
this.setFailing(false);
this.sendRecipesListToListeners(false);
return Optional.of(result);
}
use of net.minecraft.inventory.Inventory in project Polymorph by TheIllusiveC4.
the class AbstractStackRecipeData method getRecipe.
@SuppressWarnings("unchecked")
@Override
public <T extends Recipe<C>, C extends Inventory> Optional<T> getRecipe(RecipeType<T> pType, C pInventory, World pWorld, List<T> pRecipes) {
this.getLoadedRecipe().flatMap(id -> pWorld.getRecipeManager().get(id)).ifPresent(selected -> {
try {
if (selected.getType() == pType && (((T) selected).matches(pInventory, pWorld) || isEmpty(pInventory))) {
this.setSelectedRecipe(selected);
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
}
this.loadedRecipe = null;
});
if (this.isEmpty(pInventory)) {
this.setFailing(false);
this.sendRecipesListToListeners(true);
return Optional.empty();
}
AtomicReference<T> ref = new AtomicReference<>(null);
this.getLastRecipe().ifPresent(recipe -> {
try {
if (recipe.getType() == pType && ((T) recipe).matches(pInventory, pWorld)) {
this.getSelectedRecipe().ifPresent(selected -> {
try {
if (selected.getType() == pType && ((T) selected).matches(pInventory, pWorld)) {
ref.set((T) selected);
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", selected.getId(), pInventory);
}
});
}
} catch (ClassCastException e) {
PolymorphMod.LOGGER.error("Recipe {} does not match inventory {}", recipe.getId(), pInventory);
}
});
T result = ref.get();
if (result != null) {
this.setFailing(false);
this.sendRecipesListToListeners(false);
return Optional.of(result);
}
SortedSet<RecipePair> newDataset = new TreeSet<>();
List<T> recipes = pRecipes.isEmpty() ? pWorld.getRecipeManager().getAllMatches(pType, pInventory, pWorld) : pRecipes;
if (recipes.isEmpty()) {
this.setFailing(true);
this.sendRecipesListToListeners(true);
return Optional.empty();
}
for (T entry : recipes) {
Identifier id = entry.getId();
if (ref.get() == null && this.getSelectedRecipe().map(recipe -> recipe.getId().equals(id)).orElse(false)) {
ref.set(entry);
}
newDataset.add(new RecipePairImpl(id, entry.craft(pInventory)));
}
this.setRecipesList(newDataset);
result = ref.get();
result = result != null ? result : recipes.get(0);
this.lastRecipe = result;
this.setSelectedRecipe(result);
this.setFailing(false);
this.sendRecipesListToListeners(false);
return Optional.of(result);
}
use of net.minecraft.inventory.Inventory in project Mekanism by mekanism.
the class EntityFlame method smeltBlock.
private void smeltBlock(PlayerEntity shooter, BlockState hitState, BlockPos blockPos, Direction hitSide) {
if (hitState.isAir(level, blockPos)) {
return;
}
ItemStack stack = new ItemStack(hitState.getBlock());
if (stack.isEmpty()) {
return;
}
Optional<FurnaceRecipe> recipe;
try {
recipe = level.getRecipeManager().getRecipeFor(IRecipeType.SMELTING, new Inventory(stack), level);
} catch (Exception e) {
return;
}
if (recipe.isPresent()) {
if (!level.isClientSide) {
if (MinecraftForge.EVENT_BUS.post(new BlockEvent.BreakEvent(level, blockPos, hitState, shooter))) {
// We can't break the block exit
return;
}
ItemStack result = recipe.get().getResultItem();
if (!(result.getItem() instanceof BlockItem) || !tryPlace(shooter, blockPos, hitSide, Block.byItem(result.getItem()).defaultBlockState())) {
level.removeBlock(blockPos, false);
ItemEntity item = new ItemEntity(level, blockPos.getX() + 0.5, blockPos.getY() + 0.5, blockPos.getZ() + 0.5, result.copy());
item.setDeltaMovement(0, 0, 0);
level.addFreshEntity(item);
}
level.levelEvent(WorldEvents.BREAK_BLOCK_EFFECTS, blockPos, Block.getId(hitState));
spawnParticlesAt((ServerWorld) level, blockPos);
}
}
}
use of net.minecraft.inventory.Inventory in project bewitchment by MoriyaShiine.
the class MakeEntitiesWetRitualFunction method start.
@Override
public void start(ServerWorld world, BlockPos glyphPos, BlockPos effectivePos, Inventory inventory, boolean catFamiliar) {
int radius = catFamiliar ? 9 : 3;
world.getEntitiesByClass(Entity.class, new Box(effectivePos).expand(radius), Entity::isAlive).forEach(entity -> BWComponents.ADDITIONAL_WATER_DATA_COMPONENT.get(entity).setWetTimer(6000 * (catFamiliar ? 3 : 1)));
BWUtil.getBlockPoses(effectivePos, radius, currentPos -> world.getBlockState(currentPos).getBlock() instanceof AbstractFireBlock).forEach(foundPos -> world.setBlockState(foundPos, Blocks.AIR.getDefaultState()));
super.start(world, glyphPos, effectivePos, inventory, catFamiliar);
}
use of net.minecraft.inventory.Inventory in project SophisticatedBackpacks by P3pp3rF1y.
the class CraftingUpgradeTweakProvider method rotateGrid.
@Override
public void rotateGrid(PlayerEntity entityPlayer, BackpackContainer container, int id, boolean counterClockwise) {
IInventory craftMatrix = getCraftMatrix(entityPlayer, container, id);
if (craftMatrix != null) {
int start = getCraftingGridStart(entityPlayer, container, id);
int size = getCraftingGridSize(entityPlayer, container, id);
IInventory matrixClone = new Inventory(size);
int i;
int slotIndex;
for (i = 0; i < size; ++i) {
slotIndex = container.getSlot(start + i).getSlotIndex();
matrixClone.setItem(i, craftMatrix.getItem(slotIndex));
}
for (i = 0; i < size; ++i) {
if (!ROTATION_HANDLER.ignoreSlotId(i)) {
slotIndex = container.getSlot(start + ROTATION_HANDLER.rotateSlotId(i, counterClockwise)).getSlotIndex();
craftMatrix.setItem(slotIndex, matrixClone.getItem(i));
}
}
container.sendSlotUpdates();
}
}
Aggregations