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

118 lines
4.9 KiB
Java
Raw Normal View History

2018-09-12 01:29:49 +02:00
package com.minelittlepony.unicopia.item;
import java.util.List;
2019-02-07 10:46:59 +01:00
import java.util.function.Supplier;
2018-09-12 01:29:49 +02:00
import javax.annotation.Nullable;
2020-01-16 12:35:46 +01:00
import com.minelittlepony.unicopia.UItems;
2020-01-17 14:27:26 +01:00
import com.minelittlepony.unicopia.item.consumables.IEdible;
import com.minelittlepony.unicopia.item.consumables.Toxicity;
2019-02-07 10:46:59 +01:00
import com.minelittlepony.util.collection.Pool;
import com.minelittlepony.util.collection.Weighted;
2018-09-12 01:29:49 +02:00
2020-01-16 16:46:24 +01:00
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.FoodComponents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
2018-09-12 01:29:49 +02:00
import net.minecraft.item.ItemStack;
2020-01-16 16:46:24 +01:00
import net.minecraft.particle.ParticleTypes;
import net.minecraft.tag.BlockTags;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
2018-09-12 01:29:49 +02:00
2020-01-16 16:46:24 +01:00
public class AppleItem extends Item implements IEdible {
2019-02-07 10:46:59 +01:00
2020-01-16 16:46:24 +01:00
private static final Pool<Object, Weighted<Supplier<ItemStack>>> typeVariantMap = Pool.of(PlanksBlock.Type.OAK,
PlanksBlock.Type.OAK, new Weighted<Supplier<ItemStack>>()
2019-02-07 10:46:59 +01:00
.put(1, () -> new ItemStack(UItems.rotten_apple))
2019-02-07 11:14:41 +01:00
.put(2, () -> new ItemStack(UItems.green_apple))
.put(3, () -> new ItemStack(UItems.red_apple)),
2020-01-16 16:46:24 +01:00
PlanksBlock.Type.SPRUCE, new Weighted<Supplier<ItemStack>>()
2019-02-07 11:14:41 +01:00
.put(1, () -> new ItemStack(UItems.sour_apple))
.put(2, () -> new ItemStack(UItems.green_apple))
.put(3, () -> new ItemStack(UItems.sweet_apple))
2019-02-07 10:46:59 +01:00
.put(4, () -> new ItemStack(UItems.rotten_apple)),
2020-01-16 16:46:24 +01:00
PlanksBlock.Type.BIRCH, new Weighted<Supplier<ItemStack>>()
2019-02-07 10:46:59 +01:00
.put(1, () -> new ItemStack(UItems.rotten_apple))
2019-02-07 11:14:41 +01:00
.put(2, () -> new ItemStack(UItems.sweet_apple))
.put(5, () -> new ItemStack(UItems.green_apple)),
2020-01-16 16:46:24 +01:00
PlanksBlock.Type.JUNGLE, new Weighted<Supplier<ItemStack>>()
2019-02-07 11:14:41 +01:00
.put(5, () -> new ItemStack(UItems.green_apple))
.put(2, () -> new ItemStack(UItems.sweet_apple))
.put(1, () -> new ItemStack(UItems.sour_apple)),
2020-01-16 16:46:24 +01:00
PlanksBlock.Type.ACACIA, new Weighted<Supplier<ItemStack>>()
2019-02-07 10:46:59 +01:00
.put(1, () -> new ItemStack(UItems.rotten_apple))
2019-02-07 11:14:41 +01:00
.put(2, () -> new ItemStack(UItems.sweet_apple))
.put(5, () -> new ItemStack(UItems.green_apple)),
2020-01-16 16:46:24 +01:00
PlanksBlock.Type.DARK_OAK, new Weighted<Supplier<ItemStack>>()
2019-02-07 10:46:59 +01:00
.put(1, () -> new ItemStack(UItems.rotten_apple))
2019-02-07 11:14:41 +01:00
.put(2, () -> new ItemStack(UItems.sweet_apple))
2019-02-07 10:46:59 +01:00
.put(5, () -> new ItemStack(UItems.zap_apple)
)
);
public static ItemStack getRandomItemStack(Object variant) {
return typeVariantMap.getOptional(variant)
.flatMap(Weighted::get)
.map(Supplier::get)
.orElse(ItemStack.EMPTY);
2019-02-06 21:24:22 +01:00
}
2018-09-12 01:29:49 +02:00
2020-01-16 16:46:24 +01:00
public AppleItem(FoodComponent components) {
super(new Item.Settings()
.group(ItemGroup.FOOD)
.food(components));
2019-02-06 21:24:22 +01:00
}
2018-09-12 01:29:49 +02:00
@Override
2020-01-16 16:46:24 +01:00
public boolean onEntityItemUpdate(ItemEntity item) {
2020-01-16 16:46:24 +01:00
if (!item.removed && item.age > item.pickupDelay) {
2020-01-16 12:35:46 +01:00
if (!item.world.isClient) {
2020-01-16 16:46:24 +01:00
item.remove();
2020-01-16 16:46:24 +01:00
ItemEntity neu = EntityType.ITEM.create(item.world);
neu.copyPositionAndRotation(item);
neu.setStack(new ItemStack(UItems.rotten_apple));
item.world.spawnEntity(neu);
2020-01-16 16:46:24 +01:00
ItemEntity copy = EntityType.ITEM.create(item.world);
copy.copyPositionAndRotation(item);
copy.setStack(item.getStack());
copy.getStack().decrement(1);
item.world.spawnEntity(copy);
} else {
2020-01-16 16:46:24 +01:00
float bob = MathHelper.sin(((float)item.getAge() + 1) / 10F + item.hoverHeight) * 0.1F + 0.1F;
for (int i = 0; i < 3; i++) {
2020-01-16 16:46:24 +01:00
item.world.addParticle(ParticleTypes.AMBIENT_ENTITY_EFFECT, item.x, item.y + bob, item.z,
item.world.random.nextGaussian() - 0.5F,
item.world.random.nextGaussian() - 0.5F,
item.world.random.nextGaussian() - 0.5F);
}
}
}
return false;
}
2019-02-07 11:14:41 +01:00
@Override
2020-01-16 16:46:24 +01:00
public void appendTooltip(ItemStack stack, @Nullable World worldIn, List<Text> tooltip, TooltipContext context) {
tooltip.add(getToxicityLevel(stack).getTooltip());
}
@Override
public Toxicity getToxicityLevel(ItemStack stack) {
return Toxicity.SAFE;
}
2018-09-12 01:29:49 +02:00
}