mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-18 19:29:31 +00:00
Convert Pixie Spawn Chance into an attribute
This commit is contained in:
1 parent
622fd8a3f6
commit
918372c404
12 files changed
+119
-81
No files matched your search
@@ -11,6 +11,7 @@
|
||||
package vazkii.botania.api;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.ai.attributes.IAttribute;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import net.minecraft.item.DyeColor;
|
||||
import net.minecraft.item.IArmorMaterial;
|
||||
@@ -37,6 +38,7 @@ import vazkii.botania.api.recipe.RecipeManaInfusion;
|
||||
import vazkii.botania.api.recipe.RecipePetals;
|
||||
import vazkii.botania.api.recipe.RecipePureDaisy;
|
||||
import vazkii.botania.api.recipe.RecipeRuneAltar;
|
||||
import vazkii.botania.common.core.handler.PixieHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Collections;
|
||||
@@ -356,4 +358,9 @@ public final class BotaniaAPI {
|
||||
return brewMap.get(key);
|
||||
return fallbackBrew;
|
||||
}
|
||||
|
||||
// todo 1.15 this is temporary, move when moving API to interface
|
||||
public static IAttribute getPixieSpawnChanceAttribute() {
|
||||
return PixieHandler.PIXIE_SPAWN_CHANCE;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
/**
|
||||
* This class was created by <Vazkii>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under the
|
||||
* Botania License: http://botaniamod.net/license.php
|
||||
*
|
||||
* File Created @ [Aug 6, 2014, 6:06:27 PM (GMT)]
|
||||
*/
|
||||
package vazkii.botania.api.item;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* Any item that implements this allows for pixies to be spawned when the player takes damage when...<br>
|
||||
* - Case the item is armor, having it equipped.<br>
|
||||
* - Case the item is a bauble, having it worn.<br>
|
||||
* - On any other case, having the item being the current held item.
|
||||
*/
|
||||
public interface IPixieSpawner {
|
||||
|
||||
/**
|
||||
* The chance this item adds for pixies to be spawned. From 0.0 to 1.0. All values
|
||||
* are put together when calculating.
|
||||
*/
|
||||
public float getPixieChance(ItemStack stack);
|
||||
|
||||
}
|
||||
@@ -2,29 +2,46 @@ package vazkii.botania.common.core.handler;
|
||||
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.SpawnReason;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.entity.ai.attributes.IAttribute;
|
||||
import net.minecraft.entity.ai.attributes.RangedAttribute;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.potion.Effect;
|
||||
import net.minecraft.potion.EffectInstance;
|
||||
import net.minecraft.potion.Effects;
|
||||
import net.minecraft.util.Util;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingHurtEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
import vazkii.botania.api.BotaniaAPI;
|
||||
import vazkii.botania.api.item.IPixieSpawner;
|
||||
import vazkii.botania.common.core.helper.PlayerHelper;
|
||||
import vazkii.botania.common.entity.EntityPixie;
|
||||
import vazkii.botania.common.item.ModItems;
|
||||
import vazkii.botania.common.item.equipment.armor.elementium.ItemElementiumHelm;
|
||||
import vazkii.botania.common.lib.LibMisc;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Mod.EventBusSubscriber(modid = LibMisc.MOD_ID)
|
||||
public final class PixieHandler {
|
||||
|
||||
private PixieHandler() {}
|
||||
|
||||
public static final IAttribute PIXIE_SPAWN_CHANCE = new RangedAttribute(null, "botania.pixieSpawnChance", 0, 0, 1);
|
||||
private static final Map<EquipmentSlotType, UUID> DEFAULT_MODIFIER_UUIDS = Util.make(new EnumMap<>(EquipmentSlotType.class), m -> {
|
||||
m.put(EquipmentSlotType.HEAD, UUID.fromString("3c1f559c-9ec4-412d-ada0-dbf3e714088e"));
|
||||
m.put(EquipmentSlotType.CHEST, UUID.fromString("9631121c-16f0-4ed4-ba0a-0e7a063cb71c"));
|
||||
m.put(EquipmentSlotType.LEGS, UUID.fromString("a87117a1-ac15-4b17-9fd5-e98d5fe31ff1"));
|
||||
m.put(EquipmentSlotType.FEET, UUID.fromString("ff67d38a-c5be-4a00-90ed-76bb12c45523"));
|
||||
m.put(EquipmentSlotType.MAINHAND, UUID.fromString("995829fa-94c0-41bd-b046-0468c509a488"));
|
||||
m.put(EquipmentSlotType.OFFHAND, UUID.fromString("34f62de8-f652-4fe7-899f-a8fc938c4940"));
|
||||
});
|
||||
|
||||
private static final Effect[] potions = {
|
||||
Effects.BLINDNESS,
|
||||
Effects.WITHER,
|
||||
@@ -32,20 +49,23 @@ public final class PixieHandler {
|
||||
Effects.WEAKNESS
|
||||
};
|
||||
|
||||
public static AttributeModifier makeModifier(EquipmentSlotType slot, String name, double amount) {
|
||||
return new AttributeModifier(DEFAULT_MODIFIER_UUIDS.get(slot), name, amount, AttributeModifier.Operation.ADDITION);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void playerJoin(EntityJoinWorldEvent evt) {
|
||||
if (evt.getEntity() instanceof PlayerEntity) {
|
||||
((PlayerEntity) evt.getEntity()).getAttributes().registerAttribute(PIXIE_SPAWN_CHANCE);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onDamageTaken(LivingHurtEvent event) {
|
||||
if(!event.getEntityLiving().world.isRemote && event.getEntityLiving() instanceof PlayerEntity && event.getSource().getTrueSource() instanceof LivingEntity) {
|
||||
PlayerEntity player = (PlayerEntity) event.getEntityLiving();
|
||||
ItemStack stack = PlayerHelper.getFirstHeldItemClass(player, IPixieSpawner.class);
|
||||
|
||||
float chance = getChance(stack);
|
||||
for(ItemStack element : player.inventory.armorInventory)
|
||||
chance += getChance(element);
|
||||
|
||||
IItemHandlerModifiable handler = BotaniaAPI.internalHandler.getAccessoriesInventory(player);
|
||||
for(int i = 0; i < handler.getSlots(); i++)
|
||||
chance += getChance(handler.getStackInSlot(i));
|
||||
|
||||
double chance = player.getAttribute(PIXIE_SPAWN_CHANCE).getValue();
|
||||
ItemStack stack = PlayerHelper.getFirstHeldItem(player, s -> true);
|
||||
|
||||
if(Math.random() < chance) {
|
||||
EntityPixie pixie = new EntityPixie(player.world);
|
||||
@@ -66,11 +86,4 @@ public final class PixieHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static float getChance(ItemStack stack) {
|
||||
if(stack.isEmpty() || !(stack.getItem() instanceof IPixieSpawner))
|
||||
return 0F;
|
||||
else return ((IPixieSpawner) stack.getItem()).getPixieChance(stack);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,26 +39,22 @@ public final class PlayerHelper {
|
||||
|| !player.getHeldItemOffhand().isEmpty() && template.isAssignableFrom(player.getHeldItemOffhand().getItem().getClass());
|
||||
}
|
||||
|
||||
// Checks main hand, then off hand for this item. Null otherwise.
|
||||
public static ItemStack getFirstHeldItem(PlayerEntity player, Item item) {
|
||||
return getFirstHeldItem(player, s -> s.getItem() == item);
|
||||
}
|
||||
|
||||
public static ItemStack getFirstHeldItem(PlayerEntity player, Predicate<ItemStack> pred) {
|
||||
ItemStack main = player.getHeldItemMainhand();
|
||||
ItemStack offhand = player.getHeldItemOffhand();
|
||||
if(!main.isEmpty() && item == main.getItem()) {
|
||||
if(!main.isEmpty() && pred.test(main)) {
|
||||
return main;
|
||||
} else if(!offhand.isEmpty() && item == offhand.getItem()) {
|
||||
} else if(!offhand.isEmpty() && pred.test(offhand)) {
|
||||
return offhand;
|
||||
} else return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
// Checks main hand, then off hand for this item class. Null otherwise.
|
||||
public static ItemStack getFirstHeldItemClass(PlayerEntity player, Class<?> template) {
|
||||
ItemStack main = player.getHeldItemMainhand();
|
||||
ItemStack offhand = player.getHeldItemOffhand();
|
||||
if(!main.isEmpty() && template.isAssignableFrom(main.getItem().getClass())) {
|
||||
return main;
|
||||
} else if(!offhand.isEmpty() && template.isAssignableFrom(offhand.getItem().getClass())) {
|
||||
return offhand;
|
||||
} else return ItemStack.EMPTY;
|
||||
return getFirstHeldItem(player, s -> template.isAssignableFrom(s.getItem().getClass()));
|
||||
}
|
||||
|
||||
public static ItemStack getAmmo(PlayerEntity player, Predicate<ItemStack> ammoFunc) {
|
||||
|
||||
+1
-2
@@ -11,7 +11,6 @@ import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import vazkii.botania.api.BotaniaAPI;
|
||||
import vazkii.botania.api.item.IPixieSpawner;
|
||||
import vazkii.botania.client.lib.LibResources;
|
||||
import vazkii.botania.client.model.armor.ModelArmorElementium;
|
||||
import vazkii.botania.common.core.handler.ConfigHandler;
|
||||
@@ -20,7 +19,7 @@ import vazkii.botania.common.item.equipment.armor.manasteel.ItemManasteelArmor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ItemElementiumArmor extends ItemManasteelArmor implements IPixieSpawner {
|
||||
public abstract class ItemElementiumArmor extends ItemManasteelArmor {
|
||||
|
||||
public ItemElementiumArmor(EquipmentSlotType type, Properties props) {
|
||||
super(type, BotaniaAPI.ELEMENTIUM_ARMOR_MAT, props);
|
||||
|
||||
+12
-2
@@ -1,7 +1,12 @@
|
||||
package vazkii.botania.common.item.equipment.armor.elementium;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.common.core.handler.PixieHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ItemElementiumBoots extends ItemElementiumArmor {
|
||||
|
||||
@@ -9,9 +14,14 @@ public class ItemElementiumBoots extends ItemElementiumArmor {
|
||||
super(EquipmentSlotType.FEET, props);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public float getPixieChance(ItemStack stack) {
|
||||
return 0.09F;
|
||||
public Multimap<String, AttributeModifier> getAttributeModifiers(@Nonnull EquipmentSlotType slot) {
|
||||
Multimap<String, AttributeModifier> ret = super.getAttributeModifiers(slot);
|
||||
if (slot == getEquipmentSlot()) {
|
||||
ret.put(PixieHandler.PIXIE_SPAWN_CHANCE.getName(), PixieHandler.makeModifier(slot, "Armor modifier", 0.09));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
+12
-2
@@ -1,7 +1,12 @@
|
||||
package vazkii.botania.common.item.equipment.armor.elementium;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.common.core.handler.PixieHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ItemElementiumChest extends ItemElementiumArmor {
|
||||
|
||||
@@ -9,9 +14,14 @@ public class ItemElementiumChest extends ItemElementiumArmor {
|
||||
super(EquipmentSlotType.CHEST, props);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public float getPixieChance(ItemStack stack) {
|
||||
return 0.17F;
|
||||
public Multimap<String, AttributeModifier> getAttributeModifiers(@Nonnull EquipmentSlotType slot) {
|
||||
Multimap<String, AttributeModifier> ret = super.getAttributeModifiers(slot);
|
||||
if (slot == getEquipmentSlot()) {
|
||||
ret.put(PixieHandler.PIXIE_SPAWN_CHANCE.getName(), PixieHandler.makeModifier(slot, "Armor modifier", 0.17));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
+11
-2
@@ -1,10 +1,14 @@
|
||||
package vazkii.botania.common.item.equipment.armor.elementium;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.api.mana.IManaDiscountArmor;
|
||||
import vazkii.botania.common.core.handler.PixieHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class ItemElementiumHelm extends ItemElementiumArmor implements IManaDiscountArmor {
|
||||
@@ -12,9 +16,14 @@ public class ItemElementiumHelm extends ItemElementiumArmor implements IManaDisc
|
||||
super(EquipmentSlotType.HEAD, props);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public float getPixieChance(ItemStack stack) {
|
||||
return 0.11F;
|
||||
public Multimap<String, AttributeModifier> getAttributeModifiers(@Nonnull EquipmentSlotType slot) {
|
||||
Multimap<String, AttributeModifier> ret = super.getAttributeModifiers(slot);
|
||||
if (slot == getEquipmentSlot()) {
|
||||
ret.put(PixieHandler.PIXIE_SPAWN_CHANCE.getName(), PixieHandler.makeModifier(slot, "Armor modifier", 0.11));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+12
-2
@@ -1,7 +1,12 @@
|
||||
package vazkii.botania.common.item.equipment.armor.elementium;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.common.core.handler.PixieHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ItemElementiumLegs extends ItemElementiumArmor {
|
||||
|
||||
@@ -9,9 +14,14 @@ public class ItemElementiumLegs extends ItemElementiumArmor {
|
||||
super(EquipmentSlotType.LEGS, props);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public float getPixieChance(ItemStack stack) {
|
||||
return 0.15F;
|
||||
public Multimap<String, AttributeModifier> getAttributeModifiers(@Nonnull EquipmentSlotType slot) {
|
||||
Multimap<String, AttributeModifier> ret = super.getAttributeModifiers(slot);
|
||||
if (slot == getEquipmentSlot()) {
|
||||
ret.put(PixieHandler.PIXIE_SPAWN_CHANCE.getName(), PixieHandler.makeModifier(slot, "Armor modifier", 0.15));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,18 +10,20 @@
|
||||
*/
|
||||
package vazkii.botania.common.item.equipment.bauble;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import vazkii.botania.api.item.IPixieSpawner;
|
||||
|
||||
public class ItemPixieRing extends ItemBauble implements IPixieSpawner {
|
||||
import vazkii.botania.common.core.handler.PixieHandler;
|
||||
|
||||
public class ItemPixieRing extends ItemBauble {
|
||||
public ItemPixieRing(Properties props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getPixieChance(ItemStack stack) {
|
||||
return 0.25F;
|
||||
public Multimap<String, AttributeModifier> getEquippedAttributeModifiers(ItemStack stack) {
|
||||
Multimap<String, AttributeModifier> ret = super.getEquippedAttributeModifiers(stack);
|
||||
ret.put(PixieHandler.PIXIE_SPAWN_CHANCE.getName(), new AttributeModifier(getBaubleUUID(stack), "Ring modifier", 0.25, AttributeModifier.Operation.ADDITION));
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
+14
-5
@@ -1,19 +1,28 @@
|
||||
package vazkii.botania.common.item.equipment.tool.elementium;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import com.google.common.collect.Multimap;
|
||||
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
||||
import net.minecraft.inventory.EquipmentSlotType;
|
||||
import vazkii.botania.api.BotaniaAPI;
|
||||
import vazkii.botania.api.item.IPixieSpawner;
|
||||
import vazkii.botania.common.core.handler.PixieHandler;
|
||||
import vazkii.botania.common.item.equipment.tool.manasteel.ItemManasteelSword;
|
||||
|
||||
public class ItemElementiumSword extends ItemManasteelSword implements IPixieSpawner {
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class ItemElementiumSword extends ItemManasteelSword {
|
||||
|
||||
public ItemElementiumSword(Properties props) {
|
||||
super(BotaniaAPI.ELEMENTIUM_ITEM_TIER, props);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public float getPixieChance(ItemStack stack) {
|
||||
return 0.05F;
|
||||
public Multimap<String, AttributeModifier> getAttributeModifiers(@Nonnull EquipmentSlotType slot) {
|
||||
Multimap<String, AttributeModifier> ret = super.getAttributeModifiers(slot);
|
||||
if (slot == EquipmentSlotType.MAINHAND) {
|
||||
ret.put(PixieHandler.PIXIE_SPAWN_CHANCE.getName(), PixieHandler.makeModifier(slot, "Sword modifier", 0.05));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user