name: Magikey
card_text: Target 1 Normal Monster, 1 "Magikey" monster, or 1 "Magikey Maftea" in your GY; shuffle it into the Deck, then if you activated this card in response to your opponent's card or effect activation, Normal Monsters and "Magikey" monsters you control (except Tokens) are unaffected by that opponent's card effect.
external_ids: local CARD_ID_0 = 99426088 --Magikey Maftea
  local SETCARD_ID_0 = 0x165 --Magikey
  
lua: local CARD_ID_0 = 99426088 --Magikey Maftea
  local SETCARD_ID_0 = 0x165 --Magikey
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return (c:IsType(TYPE_NORMAL) or c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) or c:IsCode(CARD_ID_0)) and c:IsAbleToDeck()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_GRAVE) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 then
  		local ct = Duel.GetCurrentChain()
  		if ct < 2 then
  			return
  		end
  		local te, tep = Duel.GetChainInfo(ct - 1, CHAININFO_TRIGGERING_EFFECT, CHAININFO_TRIGGERING_PLAYER)
  		if tep == 1 - tp and e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  			Duel.BreakEffect()
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetCode(EFFECT_IMMUNE_EFFECT)
  			e1:SetTargetRange(LOCATION_MZONE, 0)
  			e1:SetTarget(s.etarget)
  			e1:SetValue(s.efilter)
  			e1:SetLabelObject(te)
  			e1:SetReset(RESET_EVENT + RESET_CHAIN)
  			Duel.RegisterEffect(e1, tp)
  		end
  	end
  end
  function s.efilter(e, re)
  	return re == e:GetLabelObject()
  end
  function s.etarget(e, c)
  	return not c:IsType(TYPE_TOKEN) and (c:IsType(TYPE_NORMAL) or c:IsSetCard(SETCARD_ID_0))
  end
  
  
source_text: Magikey
  Target 1 Normal Monster, 1 "Magikey" monster, or 1 "Magikey Maftea" in your GY; shuffle it into the Deck, then if you activated this card in response to your opponent's card or effect activation, Normal Monsters and "Magikey" monsters you control (except Tokens) are unaffected by that opponent's card effect.
target_text: -- Magikey
  local CARD_ID_0 = 99426088 --Magikey Maftea
  local SETCARD_ID_0 = 0x165 --Magikey
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return (c:IsType(TYPE_NORMAL) or c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) or c:IsCode(CARD_ID_0)) and c:IsAbleToDeck()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_GRAVE) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 1, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 then
  		local ct = Duel.GetCurrentChain()
  		if ct < 2 then
  			return
  		end
  		local te, tep = Duel.GetChainInfo(ct - 1, CHAININFO_TRIGGERING_EFFECT, CHAININFO_TRIGGERING_PLAYER)
  		if tep == 1 - tp and e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  			Duel.BreakEffect()
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetCode(EFFECT_IMMUNE_EFFECT)
  			e1:SetTargetRange(LOCATION_MZONE, 0)
  			e1:SetTarget(s.etarget)
  			e1:SetValue(s.efilter)
  			e1:SetLabelObject(te)
  			e1:SetReset(RESET_EVENT + RESET_CHAIN)
  			Duel.RegisterEffect(e1, tp)
  		end
  	end
  end
  function s.efilter(e, re)
  	return re == e:GetLabelObject()
  end
  function s.etarget(e, c)
  	return not c:IsType(TYPE_TOKEN) and (c:IsType(TYPE_NORMAL) or c:IsSetCard(SETCARD_ID_0))
  end
  
  