name: Justice
card_text: During your End Phase, if you have activated a "Spellbook" Spell Card previously this turn: You can banish this card from the field; add both 1 Level 5 or higher LIGHT or DARK Spellcaster-Type monster and 1 "Spellbook" Spell Card from your Deck to your hand. You can only use the effect of "Justice of Prophecy" once per turn.
external_ids: local SETCARD_ID_0 = 0x106e --Spellbook
  
lua: local SETCARD_ID_0 = 0x106e --Spellbook
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.thcon)
  	e1:SetCost(s.thcost)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	Duel.AddCustomActivityCounter(id, ACTIVITY_CHAIN, s.chainfilter)
  end
  function s.chainfilter(re, tp, cid)
  	return not (re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL) and re:GetHandler():IsSetCard(SETCARD_ID_0))
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetCustomActivityCount(id, tp, ACTIVITY_CHAIN) > 0 and e:GetHandler():IsAbleToRemoveAsCost()
  	end
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_COST)
  end
  function s.filter1(c)
  	return c:IsLevelAbove(5) and c:IsAttribute(ATTRIBUTE_LIGHT + ATTRIBUTE_DARK) and c:IsRace(RACE_SPELLCASTER) and c:IsAbleToHand()
  end
  function s.filter2(c)
  	return c:IsType(TYPE_SPELL) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter1, tp, LOCATION_DECK, 0, 1, nil) and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 2, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local g1 = Duel.GetMatchingGroup(s.filter1, tp, LOCATION_DECK, 0, nil)
  	local g2 = Duel.GetMatchingGroup(s.filter2, tp, LOCATION_DECK, 0, nil)
  	if g1:GetCount() > 0 and g2:GetCount() > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg1 = g1:Select(tp, 1, 1, nil)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg2 = g2:Select(tp, 1, 1, nil)
  		sg1:Merge(sg2)
  		Duel.SendtoHand(sg1, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg1)
  	end
  end
  
  
source_text: Justice
  During your End Phase, if you have activated a "Spellbook" Spell Card previously this turn: You can banish this card from the field; add both 1 Level 5 or higher LIGHT or DARK Spellcaster-Type monster and 1 "Spellbook" Spell Card from your Deck to your hand. You can only use the effect of "Justice of Prophecy" once per turn.
target_text: -- Justice
  local SETCARD_ID_0 = 0x106e --Spellbook
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.thcon)
  	e1:SetCost(s.thcost)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	Duel.AddCustomActivityCounter(id, ACTIVITY_CHAIN, s.chainfilter)
  end
  function s.chainfilter(re, tp, cid)
  	return not (re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL) and re:GetHandler():IsSetCard(SETCARD_ID_0))
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetCustomActivityCount(id, tp, ACTIVITY_CHAIN) > 0 and e:GetHandler():IsAbleToRemoveAsCost()
  	end
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_COST)
  end
  function s.filter1(c)
  	return c:IsLevelAbove(5) and c:IsAttribute(ATTRIBUTE_LIGHT + ATTRIBUTE_DARK) and c:IsRace(RACE_SPELLCASTER) and c:IsAbleToHand()
  end
  function s.filter2(c)
  	return c:IsType(TYPE_SPELL) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter1, tp, LOCATION_DECK, 0, 1, nil) and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 2, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local g1 = Duel.GetMatchingGroup(s.filter1, tp, LOCATION_DECK, 0, nil)
  	local g2 = Duel.GetMatchingGroup(s.filter2, tp, LOCATION_DECK, 0, nil)
  	if g1:GetCount() > 0 and g2:GetCount() > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg1 = g1:Select(tp, 1, 1, nil)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg2 = g2:Select(tp, 1, 1, nil)
  		sg1:Merge(sg2)
  		Duel.SendtoHand(sg1, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg1)
  	end
  end
  
  