name: Magikey
card_text: You can Ritual Summon this card with "Magikey Maftea". Gains 300 ATK for each different Attribute in your GY. You can only use each of the following effects of "Magikey Mechmortar - Garesglasser" once per turn. When your opponent activates an effect of a monster with the same Attribute as a monster in your GY, if at least 2 Attributes of monsters were used for this card's Ritual Summon (Quick Effect): You can negate the activation, and if you do, destroy that card. If this Ritual Summoned card is sent to the GY: You can add 1 "Magikey" monster from your Deck to your hand.
external_ids: local SETCARD_ID_0 = 0x165 --Magikey
  
lua: local SETCARD_ID_0 = 0x165 --Magikey
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--different attr check
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_SINGLE)
  	e0:SetCode(EFFECT_MATERIAL_CHECK)
  	e0:SetValue(s.valcheck)
  	c:RegisterEffect(e0)
  	--atkup
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetValue(s.atkval)
  	c:RegisterEffect(e1)
  	--negate
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.activate)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e3:SetCondition(s.matcon)
  	e3:SetOperation(s.matop)
  	c:RegisterEffect(e3)
  	e0:SetLabelObject(e3)
  	--search
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	c:RegisterEffect(e4)
  end
  function s.atkval(e, c)
  	local g = Duel.GetMatchingGroup(Card.IsType, c:GetControler(), LOCATION_GRAVE, 0, nil, TYPE_MONSTER)
  	return g:GetClassCount(Card.GetAttribute) * 300
  end
  function s.matcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL) and e:GetLabel() == 1
  end
  function s.matop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, EFFECT_FLAG_CLIENT_HINT, 1, 0, aux.Stringid(id, 2))
  end
  function s.attfilter(c)
  	return c:GetAttribute() > 0
  end
  function s.valcheck(e, c)
  	local mg = c:GetMaterial()
  	local fg = mg:Filter(s.attfilter, nil)
  	if fg:GetClassCount(Card.GetAttribute) > 1 then
  		e:GetLabelObject():SetLabel(1)
  	else
  		e:GetLabelObject():SetLabel(0)
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return rp == 1 - tp
  		and re:IsActiveType(TYPE_MONSTER)
  		and Duel.IsChainNegatable(ev)
  		and e:GetHandler():GetFlagEffect(id) > 0
  		and Duel.IsExistingMatchingCard(Card.IsAttribute, tp, LOCATION_GRAVE, 0, 1, nil, re:GetHandler():GetAttribute())
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsPreviousLocation(LOCATION_MZONE) and c:IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local hg = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if hg:GetCount() > 0 then
  		Duel.SendtoHand(hg, tp, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, hg)
  	end
  end
  
  
source_text: Magikey
  You can Ritual Summon this card with "Magikey Maftea". Gains 300 ATK for each different Attribute in your GY. You can only use each of the following effects of "Magikey Mechmortar - Garesglasser" once per turn. When your opponent activates an effect of a monster with the same Attribute as a monster in your GY, if at least 2 Attributes of monsters were used for this card's Ritual Summon (Quick Effect): You can negate the activation, and if you do, destroy that card. If this Ritual Summoned card is sent to the GY: You can add 1 "Magikey" monster from your Deck to your hand.
target_text: -- Magikey
  local SETCARD_ID_0 = 0x165 --Magikey
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--different attr check
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_SINGLE)
  	e0:SetCode(EFFECT_MATERIAL_CHECK)
  	e0:SetValue(s.valcheck)
  	c:RegisterEffect(e0)
  	--atkup
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetValue(s.atkval)
  	c:RegisterEffect(e1)
  	--negate
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.activate)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e3:SetCondition(s.matcon)
  	e3:SetOperation(s.matop)
  	c:RegisterEffect(e3)
  	e0:SetLabelObject(e3)
  	--search
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	c:RegisterEffect(e4)
  end
  function s.atkval(e, c)
  	local g = Duel.GetMatchingGroup(Card.IsType, c:GetControler(), LOCATION_GRAVE, 0, nil, TYPE_MONSTER)
  	return g:GetClassCount(Card.GetAttribute) * 300
  end
  function s.matcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL) and e:GetLabel() == 1
  end
  function s.matop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, EFFECT_FLAG_CLIENT_HINT, 1, 0, aux.Stringid(id, 2))
  end
  function s.attfilter(c)
  	return c:GetAttribute() > 0
  end
  function s.valcheck(e, c)
  	local mg = c:GetMaterial()
  	local fg = mg:Filter(s.attfilter, nil)
  	if fg:GetClassCount(Card.GetAttribute) > 1 then
  		e:GetLabelObject():SetLabel(1)
  	else
  		e:GetLabelObject():SetLabel(0)
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return rp == 1 - tp
  		and re:IsActiveType(TYPE_MONSTER)
  		and Duel.IsChainNegatable(ev)
  		and e:GetHandler():GetFlagEffect(id) > 0
  		and Duel.IsExistingMatchingCard(Card.IsAttribute, tp, LOCATION_GRAVE, 0, 1, nil, re:GetHandler():GetAttribute())
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsPreviousLocation(LOCATION_MZONE) and c:IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local hg = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if hg:GetCount() > 0 then
  		Duel.SendtoHand(hg, tp, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, hg)
  	end
  end
  
  