<commit_before>package org.apollo.game.event.handler.impl;

import org.apollo.game.event.handler.EventHandler;
import org.apollo.game.event.handler.EventHandlerContext;
import org.apollo.game.event.impl.ItemOptionEvent;
import org.apollo.game.model.Item;
import org.apollo.game.model.def.EquipmentDefinition;
import org.apollo.game.model.entity.EquipmentConstants;
import org.apollo.game.model.entity.Player;
import org.apollo.game.model.entity.Skill;
import org.apollo.game.model.inv.Inventory;
import org.apollo.game.model.inv.SynchronizationInventoryListener;
import org.apollo.util.LanguageUtil;

/**
 * An {@link EventHandler} that equips items.
 * 
 * @author Major
 * @author Graham
 */
public final class EquipEventHandler extends EventHandler<ItemOptionEvent> {

	@Override
	public void handle(EventHandlerContext ctx, Player player, ItemOptionEvent event) {
		if (event.getOption() != 2 || event.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID) {
			return;
		}

		int inventorySlot = event.getSlot();
		Item equipping = player.getInventory().get(inventorySlot);
		int equippingId = equipping.getId();
		EquipmentDefinition definition = EquipmentDefinition.lookup(equippingId);

		if (definition == null) {
			return;
		}

		for (int id = 0; id < 5; id++) {
			int requirement = definition.getLevel(id);

			if (player.getSkillSet().getSkill(id).getMaximumLevel() < requirement) {
				String skillName = Skill.getName(id);
				String article = LanguageUtil.getIndefiniteArticle(skillName);

				player.sendMessage("You need " + article + " " + skillName + " level of " + requirement
						+ " to equip this item.");
				ctx.breakHandlerChain();
				return;
			}
		}

		Inventory inventory = player.getInventory();
		Inventory equipment = player.getEquipment();

		int equipmentSlot = definition.getSlot();
		Item currentlyEquipped = equipment.get(equipmentSlot);

		if (equipping.getDefinition().isStackable()
				&& (currentlyEquipped == null || currentlyEquipped.getId() == equippingId)) {
			equipment.set(definition.getSlot(), equipping);
			inventory.reset(inventorySlot);
			ctx.breakHandlerChain();
			return;
		}

		Item weapon = equipment.get(EquipmentConstants.WEAPON);
		Item shield = equipment.get(EquipmentConstants.SHIELD);

		if (definition.isTwoHanded()) {
			int slotsRequired = weapon != null ? shield != null ? 1 : 0 : 0;
			if (inventory.freeSlots() < slotsRequired) {
				ctx.breakHandlerChain();
				return;
			}

			equipment.reset(EquipmentConstants.WEAPON);
			equipment.reset(EquipmentConstants.SHIELD);
			equipment.set(EquipmentConstants.WEAPON, inventory.reset(inventorySlot));

			if (shield != null) {
				inventory.add(shield);
			}
			if (weapon != null) {
				inventory.add(weapon);
			}
		} else if (definition.getSlot() == EquipmentConstants.SHIELD && weapon != null
				&& EquipmentDefinition.lookup(weapon.getId()).isTwoHanded()) {
			equipment.set(EquipmentConstants.SHIELD, inventory.reset(inventorySlot));
			inventory.add(equipment.reset(EquipmentConstants.WEAPON));
		} else {
			Item previous = equipment.reset(equipmentSlot);
			inventory.remove(equipping); // no need for fancy stuff here as we know the item isn't stackable.
			equipment.set(equipmentSlot, equipping);
			if (previous != null) {
				inventory.add(previous);
			}
			ctx.breakHandlerChain();
		}
	}

}<commit_msg>Stop EquipEventHandler breaking the chain when equipping was successful.<commit_after>package org.apollo.game.event.handler.impl;

import org.apollo.game.event.handler.EventHandler;
import org.apollo.game.event.handler.EventHandlerContext;
import org.apollo.game.event.impl.ItemOptionEvent;
import org.apollo.game.model.Item;
import org.apollo.game.model.def.EquipmentDefinition;
import org.apollo.game.model.entity.EquipmentConstants;
import org.apollo.game.model.entity.Player;
import org.apollo.game.model.entity.Skill;
import org.apollo.game.model.inv.Inventory;
import org.apollo.game.model.inv.SynchronizationInventoryListener;
import org.apollo.util.LanguageUtil;

/**
 * An {@link EventHandler} that equips items.
 * 
 * @author Major
 * @author Graham
 */
public final class EquipEventHandler extends EventHandler<ItemOptionEvent> {

	@Override
	public void handle(EventHandlerContext ctx, Player player, ItemOptionEvent event) {
		if (event.getOption() != 2 || event.getInterfaceId() != SynchronizationInventoryListener.INVENTORY_ID) {
			return;
		}

		int inventorySlot = event.getSlot();
		Item equipping = player.getInventory().get(inventorySlot);
		int equippingId = equipping.getId();
		EquipmentDefinition definition = EquipmentDefinition.lookup(equippingId);

		if (definition == null) {
			// We don't break the chain here or any item option events won't work!
			return;
		}

		for (int id = 0; id < 5; id++) {
			int requirement = definition.getLevel(id);

			if (player.getSkillSet().getSkill(id).getMaximumLevel() < requirement) {
				String skillName = Skill.getName(id);
				String article = LanguageUtil.getIndefiniteArticle(skillName);

				player.sendMessage("You need " + article + " " + skillName + " level of " + requirement
						+ " to equip this item.");
				ctx.breakHandlerChain();
				return;
			}
		}

		Inventory inventory = player.getInventory();
		Inventory equipment = player.getEquipment();

		int equipmentSlot = definition.getSlot();
		Item currentlyEquipped = equipment.get(equipmentSlot);

		if (equipping.getDefinition().isStackable()
				&& (currentlyEquipped == null || currentlyEquipped.getId() == equippingId)) {
			equipment.set(definition.getSlot(), equipping);
			inventory.reset(inventorySlot);
			return;
		}

		Item weapon = equipment.get(EquipmentConstants.WEAPON);
		Item shield = equipment.get(EquipmentConstants.SHIELD);

		if (definition.isTwoHanded()) {
			int slotsRequired = weapon != null ? shield != null ? 1 : 0 : 0;
			if (inventory.freeSlots() < slotsRequired) {
				ctx.breakHandlerChain();
				return;
			}

			equipment.reset(EquipmentConstants.WEAPON);
			equipment.reset(EquipmentConstants.SHIELD);
			equipment.set(EquipmentConstants.WEAPON, inventory.reset(inventorySlot));

			if (shield != null) {
				inventory.add(shield);
			}
			if (weapon != null) {
				inventory.add(weapon);
			}
		} else if (definition.getSlot() == EquipmentConstants.SHIELD && weapon != null
				&& EquipmentDefinition.lookup(weapon.getId()).isTwoHanded()) {
			equipment.set(EquipmentConstants.SHIELD, inventory.reset(inventorySlot));
			inventory.add(equipment.reset(EquipmentConstants.WEAPON));
		} else {
			Item previous = equipment.reset(equipmentSlot);
			inventory.remove(equipping); // no need for fancy stuff here as we know the item isn't stackable.
			equipment.set(equipmentSlot, equipping);
			if (previous != null) {
				inventory.add(previous);
			}
		}
	}

}
{'fraction_non_alphanumeric': 0.12527634487840825, 'fraction_numerical': 0.0017686072218128224, 'mean_word_length': 4.494736842105263, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6887857', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2062, 'n_words': 409}