Unicopia/src/main/java/com/minelittlepony/unicopia/item/ItemApple.java

140 lines
3 KiB
Java
Raw Normal View History

2018-09-12 01:29:49 +02:00
package com.minelittlepony.unicopia.item;
import java.util.Random;
2019-01-13 21:05:40 +01:00
import com.minelittlepony.unicopia.UItems;
2018-09-12 01:29:49 +02:00
import net.minecraft.block.BlockPlanks;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
public class ItemApple extends ItemFood {
private int[] typeRarities = new int[0];
2019-01-13 21:05:40 +01:00
2018-09-12 01:29:49 +02:00
private String[] subTypes = new String[0];
2019-01-13 21:05:40 +01:00
2018-09-12 01:29:49 +02:00
private String[] variants = subTypes;
2019-01-13 21:05:40 +01:00
public ItemStack getRandomApple(Random rand, Object variant) {
2018-09-12 01:29:49 +02:00
int[] rarity = typeRarities;
2019-01-13 21:05:40 +01:00
2018-09-12 01:29:49 +02:00
int result = 0;
2019-01-13 21:05:40 +01:00
2018-09-12 01:29:49 +02:00
for (int i = 0; i < rarity.length && i < subTypes.length; i++) {
if (rand.nextInt(rarity[i]) == 0) {
result++;
}
}
2019-01-13 21:05:40 +01:00
2018-09-12 01:29:49 +02:00
if (variant == BlockPlanks.EnumType.JUNGLE) {
2019-01-13 21:05:40 +01:00
result = oneOr(result, 0, 1);
2018-09-12 01:29:49 +02:00
}
2019-01-13 21:05:40 +01:00
2018-09-12 01:29:49 +02:00
if (variant == BlockPlanks.EnumType.BIRCH) {
2019-01-13 21:05:40 +01:00
result = oneOr(result, 0, 2);
}
if (variant == BlockPlanks.EnumType.SPRUCE) {
if (result == 0) {
return new ItemStack(UItems.rotten_apple, 1);
}
2018-09-12 01:29:49 +02:00
}
2019-01-13 21:05:40 +01:00
2018-09-12 01:29:49 +02:00
if (variant == BlockPlanks.EnumType.DARK_OAK) {
2019-01-13 21:05:40 +01:00
if (result == 1) {
return new ItemStack(UItems.zap_apple, 1);
}
2018-09-12 01:29:49 +02:00
}
2019-01-13 21:05:40 +01:00
if (variant == BlockPlanks.EnumType.ACACIA) {
result = oneOr(result, 0, 4);
}
2019-01-13 21:05:40 +01:00
return new ItemStack(this, 1, result);
2018-09-12 01:29:49 +02:00
}
2019-01-13 21:05:40 +01:00
int oneOr(int initial, int a, int b) {
if (initial == a) {
return b;
}
2018-09-12 01:29:49 +02:00
2019-01-13 21:05:40 +01:00
if (initial == b) {
return a;
}
2018-09-12 01:29:49 +02:00
2019-01-13 21:05:40 +01:00
return initial;
2018-09-12 01:29:49 +02:00
}
2019-01-13 21:05:40 +01:00
public ItemApple(String domain, String name) {
super(4, 3, false);
setTranslationKey(name);
setRegistryName(domain, name);
2018-09-12 01:29:49 +02:00
}
public ItemApple setSubTypes(String... types) {
setHasSubtypes(types.length > 0);
2019-01-13 21:05:40 +01:00
setMaxDamage(0);
2018-09-12 01:29:49 +02:00
subTypes = types;
2019-01-13 21:05:40 +01:00
variants = new String[subTypes.length];
2018-09-12 01:29:49 +02:00
setTranslationKey(variants[0] = types[0]);
2018-09-12 01:29:49 +02:00
for (int i = 1; i < variants.length; i++) {
2019-01-13 21:05:40 +01:00
variants[i] = variants[0] + "_" + subTypes[i % subTypes.length];
2018-09-12 01:29:49 +02:00
}
return this;
}
public ItemApple setTypeRarities(int ... rarity) {
typeRarities = rarity;
return this;
}
public String[] getVariants() {
return variants;
}
@Override
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> items) {
if (isInCreativeTab(tab)) {
2019-01-13 21:05:40 +01:00
items.add(new ItemStack(this, 1, 0));
for (int i = 1; i < subTypes.length; i++) {
items.add(new ItemStack(this, 1, i));
}
}
2018-09-12 01:29:49 +02:00
}
@Override
public int getDamage(ItemStack stack) {
return super.getDamage(stack);// % subTypes.length;
}
@Override
public int getMetadata(ItemStack stack) {
if (getHasSubtypes()) {
return super.getMetadata(stack) % subTypes.length;
}
return super.getMetadata(stack);
}
2018-09-12 01:29:49 +02:00
@Override
public String getTranslationKey(ItemStack stack) {
if (getHasSubtypes()) {
2019-01-13 21:05:40 +01:00
int meta = Math.max(0, stack.getMetadata() % subTypes.length);
if (meta > 0) {
return super.getTranslationKey(stack) + "." + subTypes[meta];
}
2019-01-13 21:05:40 +01:00
}
2018-09-12 01:29:49 +02:00
2019-01-13 21:05:40 +01:00
return super.getTranslationKey(stack);
2018-09-12 01:29:49 +02:00
}
}