content: /**
   * This file is part of Aion-Lightning <aion-lightning.org>.
   *
   *  Aion-Lightning is free software: you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation, either version 3 of the License, or
   *  (at your option) any later version.
   *
   *  Aion-Lightning is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details. *
   *  You should have received a copy of the GNU General Public License
   *  along with Aion-Lightning.
   *  If not, see <http://www.gnu.org/licenses/>.
   */
  package com.aionemu.gameserver.network.aion.clientpackets;
  
  import com.aionemu.gameserver.model.gameobjects.player.Player;
  import com.aionemu.gameserver.model.gameobjects.player.PlayerScripts;
  import com.aionemu.gameserver.model.house.House;
  import com.aionemu.gameserver.network.aion.AionClientPacket;
  import com.aionemu.gameserver.network.aion.AionConnection.State;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_HOUSE_SCRIPTS;
  import com.aionemu.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
  import com.aionemu.gameserver.utils.PacketSendUtility;
  
  /**
   * @author Rolandas
   */
  public class CM_HOUSE_SCRIPT extends AionClientPacket {
  
  	int address;
  	int scriptIndex;
  	int totalSize;
  	int compressedSize;
  	int uncompressedSize;
  	byte[] stream;
  
  	public CM_HOUSE_SCRIPT(int opcode, State state, State... restStates) {
  		super(opcode, state, restStates);
  	}
  
  	@Override
  	protected void readImpl() {
  		address = readD();
  		scriptIndex = readC();
  		totalSize = readH();
  		if (totalSize > 0) {
  			compressedSize = readD();
  			if (compressedSize < 8150) {
  				uncompressedSize = readD();
  				stream = readB(compressedSize);
  			}
  		}
  	}
  
  	@Override
  	protected void runImpl() {
  		Player player = getConnection().getActivePlayer();
  
  		if (compressedSize > 8149) {
  			PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_HOUSING_SCRIPT_OVERFLOW);
  		}
  
  		House house = player.getActiveHouse();
  		if (house == null) {
  			return;
  		}
  
  		PlayerScripts scripts = house.getPlayerScripts();
  
  		if (totalSize <= 0) {
  			// Deposit perhaps should send 0, while delete -1
  			// But the client sends the same packets now
  			scripts.addScript(scriptIndex, new byte[0], 0);
  		}
  		else {
  			scripts.addScript(scriptIndex, stream, uncompressedSize);
  		}
  
  		PacketSendUtility.sendPacket(player, new SM_HOUSE_SCRIPTS(address, scripts, scriptIndex, scriptIndex));
  	}
  }
  