Items Class Restriction

Szyu's Items Class Restriction​

Introduction

Easily specify items, weapons and armors, which can only be used/equipped by certain classes

How to Use

An item's note have to contain one of these:

<classes: x> # This will allow specified classes to use this item<!classes: x> # This will forbit specified classes to use this item

Seperate multiple classes with ','!

Allowed Database Items, which can be restricted by this script:

- Items

- Weapons

- Armors

 

If There is none of those tags in the items note, every class is permitted to use or equip this item!

 

Links

Pastebin

Credits

Credits to Szyu if you plan to use this script.
im doing what you said,not doing anything
tested and works fine.

<actors: x> and <classes: x> both work as described.

So from reading your other post about using the character class names, let me say that this script also uses the class numbers and actor numbers.

<actors: 1>, <classes: 1>
<actors: 2>, <classes: 3>
<actors: 4>, <classes: 5>
<actors: 7>, <classes: 8>
etc...
Would love to use this script, but can't.

error message.jpg


I have tried using the !actors and !classes version to no avail.
The purpose is to make items only usable by a particular actor in battle. The items work fine without the script loaded, but can be used by anyone in the party.
Only other scripts I am using are Yanfly's Core Engine / extra parameters, Yanfly's Battle Engine, and Yanfly's Status Menu.
Any suggestions?
Try this version instead
Spoiler: SZYU - Item Restriction
Ruby:
#==============================================================================
# Szyu's Item's Class Restriction
# Version 1.2
# By Szyu
#
# About:
# Easily specify items, weapons and armors, which can only be used/equipped
# by certain classes
#
# Instructions:
# - Place below "▼ Materials" but above "▼ Main Process".
#
# How to Use:
# - An item's note have to contain one of these:
# <classes: x> # This will allow specified classes to use this item
# <!classes: x> # This will forbit specified classes to use this item
#
# Seperate multiple classes with ','!
# Allowed Database Items, which can be restricted by this script:
# - Items
# - Weapons
# - Armors
#
# If There is none of those tags in the items note, every class is permitted to
# use or equip this item
#
#
# Requires:
# - RPG Maker VX Ace
#
# Terms of Use:
# - Free for commercal and non-commercial use. Please list me
#   in the credits to support my work.
#
#
# Changelog:
# - Same syntax can now be used to restrict for actors:
#   <actors: x>
#   <!actors: x>
# - Added Use Restriction for battles too. Restricted classes and actors can no
#   longer use restricted items in battle
#
#
#
# Pastebin:
# http://adf.ly/rYIZm
#
#
#==============================================================
#   * Game_BattlerBase
#==============================================================
class Game_BattlerBase
  alias sz_iucr_equippable? equippable?
 
  def equippable?(item)
    return false unless item.is_a?(RPG::EquipItem)
    return false if self.is_a?(Game_Actor) &&
      (item.forbid_classes.include?(self.class_id) || item.forbid_actors.include?(self.id))
    return sz_iucr_equippable?(item)
  end
end
#==============================================================
#   * Game_Battler
#==============================================================
class Game_Battler < Game_BattlerBase
  alias sz_iucr_item_test item_test
 
  def item_test(user, item)
#~     return false if item.is_a?(RPG::Item) &&
#~      (item.forbid_classes.include?(self.class_id) || item.forbid_actors.include?(self.id))
#~     return sz_iucr_item_test(user, item)
  return sz_iucr_item_test(user, item) if self.is_a?(Game_Enemy)
  if SceneManager.scene_is?(Scene_Battle)
    return false if item.is_a?(RPG::Item) &&
     (item.forbid_classes.include?(user.class_id) || item.forbid_actors.include?(user.id))
   else
     return false if item.is_a?(RPG::Item) &&
     (item.forbid_classes.include?(self.class_id) || item.forbid_actors.include?(self.id))
   end
   return sz_iucr_item_test(user, item)
    end
end
 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
#==============================================================
#   * Initialize BaseItems
#==============================================================
module DataManager
  class << self
    alias load_db_iucr_sz load_database
  end
 
  def self.load_database
    load_db_iucr_sz
    load_iucr_items
  end
 
  def self.load_iucr_items
    groups = [$data_items, $data_weapons, $data_armors]
    for group in groups
      for obj in group
        next if obj.nil?
        obj.load_iucr_notetags_sz
      end
    end
  end
end

#==============================================================================
# ** Window_BattleActor
#------------------------------------------------------------------------------
#  This window is for selecting an actor's action target on the battle screen.
#==============================================================================
class Window_BattleActor < Window_BattleStatus
  #--------------------------------------------------------------------------
  # * Get Activation State of Selection Item
  #--------------------------------------------------------------------------
  def current_item_enabled?
    return false if !BattleManager.actor.input.item.is_a?(RPG::UsableItem) ||
      BattleManager.actor.input.item.forbid_classes.include?(BattleManager.actor.input.subject.class_id) ||
      BattleManager.actor.input.item.forbid_actors.include?(BattleManager.actor.input.subject.id)
    return true
  end
end
 
#==============================================================================
# ** Window_BattleActor
#------------------------------------------------------------------------------
#  This window is for selecting an actor's action target on the battle screen.
#==============================================================================
class Window_BattleEnemy < Window_Selectable
  #--------------------------------------------------------------------------
  # * Get Activation State of Selection Item
  #--------------------------------------------------------------------------
  def current_item_enabled?
    return false if !BattleManager.actor.input.item.is_a?(RPG::UsableItem) ||
      BattleManager.actor.input.item.forbid_classes.include?(BattleManager.actor.input.subject.class_id) ||
      BattleManager.actor.input.item.forbid_actors.include?(BattleManager.actor.input.subject.id)
    return true
  end
end
#==============================================================
#   * Content of Recycling Items
#==============================================================
class RPG::BaseItem
  attr_accessor :forbid_classes
  attr_accessor :forbid_actors
 
  def load_iucr_notetags_sz
    @forbid_classes = []
    @forbid_actors = []
    self.note.split(/[\r\n]+/).each do |line|
      # Forbid Classes
      if line =~ /<classes:([\d+,?\s*]+)>/i
        $data_classes.each do |cl|
          @forbid_classes.push(cl.id) if cl
        end
        $1.scan(/\s*,?\d+,?\s*/i).each do |cl|
          @forbid_classes.delete(cl.to_i)
        end
      elsif line =~ /<!classes:([\d+,?\s*]+)>/i
        $1.scan(/\s*,?\d+,?\s*/i).each do |cl|
          @forbid_classes.push(cl.to_i)
        end
        # Forbid Actors
      elsif line =~ /<actors:([\d+,?\s*]+)>/i
        $data_actors.each do |ac|
          @forbid_actors.push(ac.id) if ac
        end
        $1.scan(/\s*,?\d+,?\s*/i).each do |ac|
          @forbid_actors.delete(ac.to_i)
        end
      elsif line =~ /<!actors:([\d+,?\s*]+)>/i
        $1.scan(/\s*,?\d+,?\s*/i).each do |ac|
          @forbid_actors.push(ac.to_i)
        end
      end
    end
  end  
  def forbid_classes
    load_iucr_notetags_sz unless @forbid_classes
    return @forbid_classes
  end
  def forbid_actors
    load_iucr_notetags_sz unless @forbid_actors
    return @forbid_actors
  end
end
What did you change? =)
The Game no longer crashes, but the classes and actors marked with !actors / !classes can still use the items that they should not be able to.

Am I not using the tags properly? Attached are the Actor and Class tab showing the # to be forbid, and then items using the !actors and !classes tags. Hope this helps.
classes and actors marked with !actors / !classes can still use the items
Ok the script was designed for using items by the actors. Not by the enemies.
I added in a battle_enemy component and works on my end.
Code updated above.
Wow thank you so much!!
This works really well -- except for in battle, you can still select a restricted actor to use the item on, when the item's notebox specifies that you should not be able to. The item seems to have no effect though, but even so, I would like for you to be prevented from selecting that actor, similar to Sixth's Target One Ally (Not the User) Scope script.
In the item menu outside of battle, it works like this; selecting the restricted actor will simply cause the buzzer sound to play and the actor cannot use the item. Is an edit to make this happen in battle possible? Or does it actually do this already and I have some sort of script compatibility issue?
prevented from selecting that actor,
Ruby:
class Scene_Battle < Scene_Base
  def on_actor_ok
    user = $game_party.members[@actor_window.index]
    if @item.is_a?(RPG::Item) && (@item.forbid_classes.include?(user.class_id) || @item.forbid_actors.include?(user.id))
      Sound.play_buzzer
      @actor_window.activate
      return
    end
    BattleManager.actor.input.target_index = @actor_window.index
    @actor_window.hide
    @skill_window.hide
    @item_window.hide
    next_command
  end
end
Ruby:
class Scene_Battle < Scene_Base
  def on_actor_ok
    user = $game_party.members[@actor_window.index]
    if @item.is_a?(RPG::Item) && (@item.forbid_classes.include?(user.class_id) || @item.forbid_actors.include?(user.id))
      Sound.play_buzzer
      @actor_window.activate
      return
    end
    BattleManager.actor.input.target_index = @actor_window.index
    @actor_window.hide
    @skill_window.hide
    @item_window.hide
    next_command
  end
end
Awesome, thank you! One more thing though, just so it's consistent with other scripts, could the actor's name be greyed out when using an item on them is forbidden?
the actor's name be greyed out
This might work for you. Depends on what scripts you are using.
I found a way, but it's probably not the best. I moved data around several times to get it to the place it needed to be.
Spoiler: test
Ruby:
module BattleManager
  def self.item_disabled(item)
    @actor_item = item
  end
  def self.actor_item_restrict
    return @actor_item
  end
end
class Window_BattleStatus < Window_Selectable
  def draw_actor_name(actor, x, y, width = 112)
    change_color(hp_color(actor))
    if @greyed == true
      change_color(Color.new(128, 128, 128, 255))
    end
    draw_text(x, y, width, line_height, actor.name)
  end
  def draw_basic_area(rect, actor)
    @item = BattleManager.actor_item_restrict
    if @item.is_a?(RPG::Item) && 
      (@item.forbid_classes.include?(actor.class_id) || @item.forbid_actors.include?(actor.id))
      @greyed = true
    end
    draw_actor_name(actor, rect.x + 0, rect.y, 100)
    draw_actor_icons(actor, rect.x + 104, rect.y, rect.width - 104)
    @greyed = false
  end
end
class Scene_Battle < Scene_Base
  def select_actor_selection
    BattleManager.item_disabled(@item)
    @actor_window.refresh
    @actor_window.show.activate
  end
end

Original thread: https://forums.rpgmakerweb.com/threads/items-class-restriction.31546/