use of net.minecraft.util.NonNullList in project ImmersiveEngineering by BluSunrize.
the class TileEntityAssembler method update.
@Override
public void update() {
super.update();
if (isDummy() || isRSDisabled() || world.isRemote || world.getTotalWorldTime() % 16 != ((getPos().getX() ^ getPos().getZ()) & 15))
return;
boolean update = false;
NonNullList<ItemStack>[] outputBuffer = new NonNullList[patterns.length];
for (int p = 0; p < patterns.length; p++) {
CrafterPatternInventory pattern = patterns[p];
if (isComputerControlled && !computerOn[p])
continue;
if (!pattern.inv.get(9).isEmpty() && canOutput(pattern.inv.get(9), p)) {
ItemStack output = pattern.inv.get(9).copy();
// List of all available inputs in the inventory
ArrayList<ItemStack> availableStacks = new ArrayList<>();
for (NonNullList<ItemStack> bufferedStacks : outputBuffer) if (bufferedStacks != null)
for (ItemStack stack : bufferedStacks) if (!stack.isEmpty())
availableStacks.add(stack);
for (ItemStack stack : this.inventory) if (!stack.isEmpty())
availableStacks.add(stack);
int consumed = IEConfig.Machines.assembler_consumption;
AssemblerHandler.IRecipeAdapter adapter = AssemblerHandler.findAdapter(pattern.recipe);
AssemblerHandler.RecipeQuery[] queries = adapter.getQueriedInputs(pattern.recipe, pattern.inv);
if (queries == null)
continue;
if (this.energyStorage.extractEnergy(consumed, true) == consumed && this.consumeIngredients(queries, availableStacks, false, null)) {
this.energyStorage.extractEnergy(consumed, false);
// List of all outputs for the current recipe. This includes discarded containers
NonNullList<ItemStack> outputList = NonNullList.create();
outputList.add(output);
NonNullList<ItemStack> gridItems = NonNullList.withSize(9, ItemStack.EMPTY);
this.consumeIngredients(queries, availableStacks, true, gridItems);
NonNullList<ItemStack> remainingItems = pattern.recipe.getRemainingItems(Utils.InventoryCraftingFalse.createFilledCraftingInventory(3, 3, gridItems));
for (ItemStack rem : remainingItems) if (!rem.isEmpty())
outputList.add(rem);
outputBuffer[p] = outputList;
update = true;
}
}
}
BlockPos outputPos = getPos().offset(facing, 2);
TileEntity inventoryTile = Utils.getExistingTileEntity(world, outputPos);
for (int buffer = 0; buffer < outputBuffer.length; buffer++) if (outputBuffer[buffer] != null && outputBuffer[buffer].size() > 0)
for (int iOutput = 0; iOutput < outputBuffer[buffer].size(); iOutput++) {
ItemStack output = outputBuffer[buffer].get(iOutput);
if (!output.isEmpty() && output.getCount() > 0) {
if (!isRecipeIngredient(output, buffer) && inventoryTile != null) {
output = Utils.insertStackIntoInventory(inventoryTile, output, facing.getOpposite());
if (output.isEmpty() || output.getCount() <= 0)
continue;
}
int free = -1;
if (// Main recipe output
iOutput == 0) {
if (this.inventory.get(18 + buffer).isEmpty() && free < 0)
free = 18 + buffer;
else if (!this.inventory.get(18 + buffer).isEmpty() && OreDictionary.itemMatches(output, this.inventory.get(18 + buffer), true) && this.inventory.get(18 + buffer).getCount() + output.getCount() <= this.inventory.get(18 + buffer).getMaxStackSize()) {
this.inventory.get(18 + buffer).grow(output.getCount());
free = -1;
continue;
}
} else
for (int i = 0; i < this.inventory.size(); i++) {
if (this.inventory.get(i).isEmpty() && free < 0)
free = i;
else if (!this.inventory.get(i).isEmpty() && OreDictionary.itemMatches(output, this.inventory.get(i), true) && this.inventory.get(i).getCount() + output.getCount() <= this.inventory.get(i).getMaxStackSize()) {
this.inventory.get(i).grow(output.getCount());
free = -1;
break;
}
}
if (free >= 0)
this.inventory.set(free, output.copy());
}
}
for (int i = 0; i < 3; i++) if (!isRecipeIngredient(this.inventory.get(18 + i), i) && inventoryTile != null)
this.inventory.set(18 + i, Utils.insertStackIntoInventory(inventoryTile, this.inventory.get(18 + i), facing.getOpposite()));
if (update) {
this.markDirty();
this.markContainingBlockForUpdate(null);
}
}
use of net.minecraft.util.NonNullList in project ImmersiveEngineering by BluSunrize.
the class ClientEventHandler method onRenderTooltip.
@SubscribeEvent()
public void onRenderTooltip(RenderTooltipEvent.PostText event) {
ItemStack stack = event.getStack();
if (stack.getItem() instanceof IBulletContainer) {
NonNullList<ItemStack> bullets = ((IBulletContainer) stack.getItem()).getBullets(stack, true);
if (bullets != null) {
int bulletAmount = ((IBulletContainer) stack.getItem()).getBulletCount(stack);
int line = event.getLines().size() - Utils.findSequenceInList(event.getLines(), BULLET_TOOLTIP, (s, s2) -> s.equals(s2.substring(2)));
int currentX = event.getX();
int currentY = line > 0 ? event.getY() + (event.getHeight() + 1 - line * 10) : event.getY() - 42;
GlStateManager.pushMatrix();
GlStateManager.enableBlend();
GlStateManager.enableRescaleNormal();
GlStateManager.translate(currentX, currentY, 700);
GlStateManager.scale(.5f, .5f, 1);
GuiRevolver.drawExternalGUI(bullets, bulletAmount);
GlStateManager.disableRescaleNormal();
GlStateManager.popMatrix();
}
}
}
use of net.minecraft.util.NonNullList in project BluePower by Qmunity.
the class BPRecyclingReloadListener method onResourceManagerReload.
public static void onResourceManagerReload(RecipeManager recipeManager) {
AlloyFurnaceRegistry.getInstance().blacklist.clear();
List<String> blacklistStr = Arrays.asList(BPConfig.CONFIG.alloyFurnaceBlacklist.get().split(","));
for (String configString : blacklistStr) {
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(configString));
if (item != null) {
AlloyFurnaceRegistry.getInstance().blacklist.add(item);
}
}
AlloyFurnaceRegistry.getInstance().recyclingRecipes.clear();
for (ItemStack outputItem : AlloyFurnaceRegistry.getInstance().recyclingItems) {
// Build the blacklist based on config
Set<Item> blacklist = new HashSet<>(AlloyFurnaceRegistry.getInstance().blacklist);
for (IRecipe<?> recipe : recipeManager.getAllRecipesFor(IRecipeType.CRAFTING)) {
// Take into account other mods with Dynamic Recipes
NonNullList<Ingredient> ingredients = null;
try {
ingredients = recipe.getIngredients();
} catch (IllegalStateException ignored) {
}
// If Recipe Contains a Recyclable Item check the recipe
if (ingredients != null && !recipe.isSpecial() && ingredients.stream().anyMatch(ingredient -> ingredient.test(outputItem))) {
int recyclingAmount = 0;
ItemStack currentlyRecycledInto = ItemStack.EMPTY;
for (ItemStack recyclingItem : AlloyFurnaceRegistry.getInstance().recyclingItems) {
try {
if (recipe instanceof ICraftingRecipe) {
if (!recipe.getIngredients().isEmpty()) {
for (Ingredient input : recipe.getIngredients()) {
if (!input.isEmpty()) {
// Serialize and Deserialize the Object so the base tag isn't affected.
Ingredient ingredient = Ingredient.fromJson(input.toJson());
if (ingredient.test(recyclingItem)) {
ItemStack moltenDownItem = AlloyFurnaceRegistry.getInstance().getRecyclingStack(recyclingItem);
if (currentlyRecycledInto.isEmpty() || ItemStackUtils.isItemFuzzyEqual(currentlyRecycledInto, moltenDownItem)) {
currentlyRecycledInto = moltenDownItem;
recyclingAmount += moltenDownItem.getCount();
}
}
}
}
}
}
} catch (Throwable e) {
BluePower.log.error("Error when generating an Alloy Furnace recipe for item " + recyclingItem.getDisplayName().getString() + ", recipe output: " + recipe.getResultItem().getDisplayName().getString());
e.printStackTrace();
}
}
if (recyclingAmount > 0 && recipe.getResultItem().getCount() > 0) {
// Try to avoid Duping
if (!blacklist.contains(recipe.getResultItem().getItem()) && recipe.getResultItem().getCount() > recyclingAmount) {
blacklist.add(recipe.getResultItem().getItem());
}
// Skip item if it is on the blacklist
if (blacklist.contains(recipe.getResultItem().getItem())) {
continue;
}
// Divide by the Recipe Output
ItemStack output = new ItemStack(currentlyRecycledInto.getItem(), Math.min(64, recyclingAmount / recipe.getResultItem().getCount()));
AlloyFurnaceRegistry.getInstance().recyclingRecipes.put(recipe.getResultItem().getItem(), output);
}
}
}
}
}
use of net.minecraft.util.NonNullList in project malmo by Microsoft.
the class CraftingHelper method attemptCrafting.
/**
* Attempt to craft the given recipe.<br>
* This pays no attention to tedious things like using the right crafting table / brewing stand etc, or getting the right shape.<br>
* It simply takes the raw ingredients out of the player's inventory, and inserts the output of the recipe, if possible.
*
* @param player the SERVER SIDE player that will do the crafting.
* @param recipe the IRecipe we wish to craft.
* @return true if the recipe had an output, and the player had the required ingredients to create it; false otherwise.
*/
public static boolean attemptCrafting(EntityPlayerMP player, IRecipe recipe) {
if (player == null || recipe == null)
return false;
ItemStack is = recipe.getRecipeOutput();
List<ItemStack> ingredients = getIngredients(recipe);
if (playerHasIngredients(player, ingredients)) {
// We have the ingredients we need, so directly manipulate the inventory.
// First, remove the ingredients:
removeIngredientsFromPlayer(player, ingredients);
// Now add the output of the recipe:
ItemStack resultForInventory = is.copy();
ItemStack resultForReward = is.copy();
player.inventory.addItemStackToInventory(resultForInventory);
RewardForCollectingItemImplementation.GainItemEvent event = new RewardForCollectingItemImplementation.GainItemEvent(resultForReward);
event.setCause(1);
MinecraftForge.EVENT_BUS.post(event);
// Now trigger a craft event
List<IRecipe> recipes = getRecipesForRequestedOutput(resultForReward, true);
for (IRecipe iRecipe : recipes) {
if (iRecipe instanceof ShapedRecipes) {
ShapedRecipes shapedRecipe = (ShapedRecipes) iRecipe;
InventoryCrafting craftMatrix;
if (shapedRecipe.recipeItems.length <= 4)
craftMatrix = new InventoryCrafting(player.inventoryContainer, 2, 2);
else
craftMatrix = new InventoryCrafting(player.inventoryContainer, 3, 3);
for (int i = 0; i < shapedRecipe.recipeItems.length; i++) craftMatrix.setInventorySlotContents(i, shapedRecipe.recipeItems[i]);
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, resultForReward, craftMatrix));
break;
} else if (iRecipe instanceof ShapelessRecipes) {
ShapelessRecipes shapelessRecipe = (ShapelessRecipes) iRecipe;
InventoryCrafting craftMatrix;
if (shapelessRecipe.recipeItems.size() <= 4) {
craftMatrix = new InventoryCrafting(player.inventoryContainer, 2, 2);
for (int i = 0; i < shapelessRecipe.recipeItems.size(); i++) craftMatrix.setInventorySlotContents(i, shapelessRecipe.recipeItems.get(i));
} else {
craftMatrix = new InventoryCrafting(player.inventoryContainer, 3, 3);
for (int i = 0; i < shapelessRecipe.recipeItems.size(); i++) craftMatrix.setInventorySlotContents(i, shapelessRecipe.recipeItems.get(i));
}
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, resultForReward, craftMatrix));
break;
} else if (iRecipe instanceof ShapedOreRecipe) {
ShapedOreRecipe oreRecipe = (ShapedOreRecipe) iRecipe;
Object[] input = oreRecipe.getInput();
InventoryCrafting craftMatrix = new InventoryCrafting(player.inventoryContainer, 3, 3);
for (int i = 0; i < input.length; i++) {
if (input[i] instanceof ItemStack)
craftMatrix.setInventorySlotContents(i, (ItemStack) input[i]);
else if (input[i] instanceof NonNullList)
if (((NonNullList) input[i]).size() != 0)
craftMatrix.setInventorySlotContents(i, (ItemStack) ((NonNullList) input[i]).get(0));
}
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, resultForReward, craftMatrix));
}
}
return true;
}
return false;
}
use of net.minecraft.util.NonNullList in project Almura by AlmuraDev.
the class MixinBlockTallGrass method getDrops.
/**
* @author Zidane - Chris Sanders
* @reason Add in content seeds to drop list for Tall Grass
*/
@Overwrite(remap = false)
public void getDrops(final NonNullList<ItemStack> drops, final IBlockAccess access, final BlockPos pos, final IBlockState state, final int fortune) {
World world;
if (access instanceof ChunkCache) {
world = ((ChunkCache) access).world;
} else if (access instanceof World) {
world = (World) access;
} else {
return;
}
final Random random = world.rand;
// Roll 1 is Vanilla's 1/8 chance to drop a seed
final int roll1 = random.nextInt(8);
if (roll1 == 0) {
// Forge Start - Lookup seed each time and then do random check. Almura handles its own chance code
final ItemStack modSeed = net.minecraftforge.common.ForgeHooks.getGrassSeed(random, fortune);
if (!modSeed.isEmpty()) {
drops.add(modSeed);
// Don't double up with Vanilla/mod drops
return;
}
final Biome biome = world.getBiome(pos);
// Roll 2 is shuffling Almura seeds and picking the first one after shuffling
registry.getAllOf(ItemType.class).stream().filter(itemType -> itemType instanceof SeedItem && ((SeedItem) itemType).getGrass() != null).collect(Collectors.collectingAndThen(Collectors.toList(), collected -> {
Collections.shuffle(collected);
return collected.stream();
})).findFirst().ifPresent((itemType) -> {
final SeedItem seed = (SeedItem) itemType;
final IntRange amountRange = seed.getGrass().getOrLoadAmountRequiredRangeForBiome(biome);
if (amountRange != null) {
final int stackSize = amountRange.random(random);
final DoubleRange chanceRange = seed.getGrass().getOrLoadChanceRangeForBiome(biome);
if (chanceRange != null) {
final double chance = chanceRange.random(random);
// Roll 3 is allowing the seed configuration to determine the chance for the drop
if (random.nextDouble() <= (chance / 100)) {
drops.add((ItemStack) (Object) org.spongepowered.api.item.inventory.ItemStack.of(itemType, stackSize));
}
} else {
drops.add((ItemStack) (Object) org.spongepowered.api.item.inventory.ItemStack.of(itemType, stackSize));
}
}
});
}
// Almura End
}
Aggregations