001package net.tnemc.item.component.helper; 002/* 003 * The New Item Library 004 * Copyright (C) 2022 - 2025 Daniel "creatorfromhell" Vidmar 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020 021import net.tnemc.item.JSONHelper; 022import net.tnemc.item.component.helper.effect.EffectInstance; 023import org.json.simple.JSONObject; 024 025import java.util.ArrayList; 026 027/** 028 * FoodRule 029 * 030 * @author creatorfromhell 031 * @since 0.0.1.0 032 */ 033public class FoodRule { 034 035 protected EffectInstance potionEffect; 036 protected float chance; 037 038 public FoodRule() { 039 040 } 041 042 public FoodRule(final EffectInstance potionEffect, final float chance) { 043 044 this.potionEffect = potionEffect; 045 this.chance = chance; 046 } 047 048 public JSONObject toJSON() { 049 050 final JSONObject rule = new JSONObject(); 051 rule.put("effect", potionEffect.toJSON()); 052 rule.put("chance", chance); 053 054 return rule; 055 } 056 057 public static FoodRule readJSON(final JSONHelper json) { 058 059 final FoodRule rule = new FoodRule(); 060 061 if(json.has("effect")) { 062 final EffectInstance effect = new EffectInstance(); 063 effect.readJSON(json.getHelper("effect")); 064 rule.setPotionEffect(effect); 065 } 066 067 if(json.has("chance")) { 068 rule.setChance(json.getFloat("chance")); 069 } 070 071 return rule; 072 } 073 074 public EffectInstance getPotionEffect() { 075 076 return potionEffect; 077 } 078 079 public void setPotionEffect(final EffectInstance potionEffect) { 080 081 this.potionEffect = potionEffect; 082 } 083 084 public float getChance() { 085 086 return chance; 087 } 088 089 public void setChance(final float chance) { 090 091 this.chance = chance; 092 } 093 094 @Override 095 public FoodRule clone() throws CloneNotSupportedException { 096 097 final EffectInstance clonedEffect = this.potionEffect != null? this.potionEffect.clone() : null; 098 return new FoodRule(clonedEffect, this.chance); 099 } 100}