Create custom potion effect type in 1.12.2

Discussion in 'Plugin Development' started by LaPiMoNsTeR, Aug 4, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    LaPiMoNsTeR

    Hello, i would like to know if its possible to create a custom potion effect type.
    I already tried to register a new PotionEffectTypeWrapper with the method in the class PotionEffectType, but when i add the potion effect to the potion that doesnt work, the potion effect is not displayed in potion description.
    Thanks
     
  2. Offline

    The_Spaceman

    I would try something like make a custom bottle with custom name, use maybe PlayerInteractEvent and check for your custom bottle and apply your effect.

    I hope this helps, its not much (I know) but its a start
     
  3. Offline

    KarimAKL

    I think PlayerItemConsumeEvent would be better.
    "This event will fire when a player is finishing consuming an item (food, potion, milk bucket).
    If the ItemStack is modified the server will use the effects of the new item and not remove the original one from the player's inventory.
    If the event is cancelled the effect will not be applied and the item will not be removed from the player's inventory."
     
  4. Offline

    The_Spaceman

    With the event, I didn't do any research on what to use...
    And you can cancel the event and remove the item from the player
    Code:
    @EventHandler
    public void event(PlayerItemConsumeEvent  e) {
        if (e.getItem().equals(/*your custom bottle*/)) {
            e.setCancelled(true);
            player.getInventory().getItemInMainHand().setType(Material.GLASS_BOTTLE);
            //your custom effect
        }
    }
    
    maybe this will work
     
  5. Offline

    LaPiMoNsTeR

    Thats a good idea but i want the potion show my effect in description like other potion effect
    Screenshot (open)

    [​IMG]

    I told you i tried to register custom potion effect, here is the code
    CustomPotionEffectType (open)
    Code:
    public class CustomPotionEffectType {
    
        public static final PotionEffectType RAGE = new Rage(28);
    
        private static List<PotionEffectType> effects = new ArrayList<>();
    
        public CustomPotionEffectType() {
            try {
                /**
                 * Access to the array byId who contains normal potion effects
                 */
                Field field = PotionEffectType.class.getDeclaredField("byId");
                field.setAccessible(true);
    
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    
    
                /**
                 * Check if the array byId isn't resized
                 */
                if(((PotionEffectType[]) field.get(null)).length == 28) {
    
    
                    /**
                     * If he isn't, add all custom effect (that we created), in our List
                     */
                    for(Field f : CustomPotionEffectType.class.getDeclaredFields()) {
                        if(f.getType() == PotionEffectType.class) {
                            PotionEffectType effectType = (PotionEffectType) f.get(null);
    
                            effects.add(effectType);
                        }
                    }
    
    
                    /**
                     * Calculate the new array size and create new array with the new array size
                     */
                    int arraySize = 28 + effects.size();
    
                    PotionEffectType[] customEffectTypes = new PotionEffectType[arraySize];
    
    
                    /**
                     * Add all normal effects to our new array
                     */
                    PotionEffectType[] effectTypes = (PotionEffectType[]) field.get(null);
                    for(int i=0;i<28;i++) {
                        customEffectTypes[i] = effectTypes[i];
                    }
    
    
                    /**
                     * redefine the variable byId with our new array
                     */
                    field.set(null, customEffectTypes);
    
    
                    /**
                     * Check if the variable byId is resized
                     */
                    System.out.println("byId size : " + ((PotionEffectType[]) field.get(null)).length);
    
    
                    /**
                     * Accessing to the boolean and set him to true
                     */
                    field = PotionEffectType.class.getDeclaredField("acceptingNew");
                    field.setAccessible(true);
    
                    field.set(null, true);
    
    
                    /**
                     * Register our custom effects
                     */
                    for(PotionEffectType effectType : effects) {
                        PotionEffectType.registerPotionEffectType(effectType);
    
                        //noinspection deprecation
                        System.out.println(effectType + "("+effectType.getId()+") registered");
                    }
                }
    
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    
    
    
        public static List<PotionEffectType> getActiveCustomEffects(LivingEntity entity) {
            List<PotionEffectType> l = new ArrayList<>();
    
            for(PotionEffect potionEffect : entity.getActivePotionEffects()) {
                if(effects.contains(potionEffect.getType())) {
                    l.add(potionEffect.getType());
                }
            }
    
            return l;
        }
    
    }

    the operation work but when i add the custom potion effect to potion nothing is happening


    EDIT : I tried do dirrectly add the potion effect to player but the server crash
     
    Last edited: Aug 8, 2018
  6. Offline

    LaPiMoNsTeR

  7. Offline

    LaPiMoNsTeR

    nobody ?
     
  8. Offline

    KarimAKL

    Why not set a lore on the item?
     
  9. Offline

    The_Spaceman

    Like waht RarimKL said, add lore
    Code:
    ItemStack customPotion = new ItemStack(Material.POTION);
    ItemMeta im = customPotion.getItemMeta();
    im.setLore(Arrays.asList("your", "effect", "description", "here"));
    customPotion.setItemMeta(im);
    //some example
    ShapedRecipe recipe = new ShapedRecipe(customPotion);
    recipe.shape("abc", "def", "ghi");
    recipe.setIngredient('a', Material.DIAMOND);
    recipe.setIngredient('b', "some other");
    recipe.setIngredient('c', "you get the idea...")
    getServer().addRecipe(recipe);
    
    you can save your itemStack to check with the item used in the event

    This is a recipe for in a crafting table. If you want to use it for brewingstand I don't know, but the internet does :D
     
Thread Status:
Not open for further replies.

Share This Page