ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/util/AwfulUtil.java

108 lines
3.8 KiB
Java
Raw Normal View History

/*
* This file ("AwfulUtil.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.util;
2019-05-02 09:10:29 +02:00
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists;
2019-05-02 09:10:29 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootTable;
2017-08-06 10:29:28 +02:00
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
//This is stuff copied from somewhere in vanilla and changed so that it works properly
//It's unpolished and vanilla-y, so don't look at it! O_O
2019-05-02 09:10:29 +02:00
public final class AwfulUtil {
2019-05-02 09:10:29 +02:00
public static void fillInventory(LootTable table, IItemHandlerModifiable inventory, Random rand, LootContext context) {
List<ItemStack> list = table.generateLootForPools(rand, context);
List<Integer> list1 = getEmptySlotsRandomized(inventory, rand);
shuffleItems(list, list1.size(), rand);
2019-05-02 09:10:29 +02:00
for (ItemStack itemstack : list) {
if (itemstack.isEmpty()) {
inventory.setStackInSlot(list1.remove(list1.size() - 1), ItemStack.EMPTY);
} else {
inventory.setStackInSlot(list1.remove(list1.size() - 1), itemstack);
}
}
}
2019-05-02 09:10:29 +02:00
private static void shuffleItems(List<ItemStack> stacks, int someInt, Random rand) {
List<ItemStack> list = Lists.newArrayList();
Iterator<ItemStack> iterator = stacks.iterator();
2019-05-02 09:10:29 +02:00
while (iterator.hasNext()) {
ItemStack itemstack = iterator.next();
2019-05-02 09:10:29 +02:00
if (itemstack.isEmpty()) {
iterator.remove();
2019-05-02 09:10:29 +02:00
} else if (itemstack.getCount() > 1) {
list.add(itemstack);
iterator.remove();
}
}
2019-05-02 09:10:29 +02:00
someInt = someInt - stacks.size();
2019-05-02 09:10:29 +02:00
while (someInt > 0 && list.size() > 0) {
ItemStack itemstack2 = list.remove(MathHelper.getInt(rand, 0, list.size() - 1));
int i = MathHelper.getInt(rand, 1, itemstack2.getCount() / 2);
ItemStack itemstack1 = itemstack2.splitStack(i);
2019-05-02 09:10:29 +02:00
if (itemstack2.getCount() > 1 && rand.nextBoolean()) {
list.add(itemstack2);
2019-05-02 09:10:29 +02:00
} else {
stacks.add(itemstack2);
}
2019-05-02 09:10:29 +02:00
if (itemstack1.getCount() > 1 && rand.nextBoolean()) {
list.add(itemstack1);
2019-05-02 09:10:29 +02:00
} else {
stacks.add(itemstack1);
}
}
stacks.addAll(list);
Collections.shuffle(stacks, rand);
}
2019-05-02 09:10:29 +02:00
private static List<Integer> getEmptySlotsRandomized(IItemHandlerModifiable inventory, Random rand) {
List<Integer> list = Lists.newArrayList();
2019-05-02 09:10:29 +02:00
for (int i = 0; i < inventory.getSlots(); ++i) {
if (inventory.getStackInSlot(i).isEmpty()) {
list.add(i);
}
}
Collections.shuffle(list, rand);
return list;
}
2019-02-27 19:53:05 +01:00
2017-08-06 10:29:28 +02:00
public static void callTheFuckinPolice(Object... stuff) {
2019-02-27 19:53:05 +01:00
int i = 0;
String error = "Actually Additions: Something is very wrong. This method was provided with ";
2019-05-02 09:10:29 +02:00
for (Object k : stuff) {
2019-02-27 19:53:05 +01:00
error += "\n" + i++ + ": " + (k == null ? "null" : k.getClass().getSimpleName() + " <- CLASS | INSTANCE -> " + k.toString() + ", ");
}
error += "\n" + "The current side is: " + FMLCommonHandler.instance().getEffectiveSide();
error += "\n" + "Report this to https://github.com/Ellpeck/ActuallyAdditions/issues";
throw new IllegalStateException(error);
2017-08-06 10:29:28 +02:00
}
}