<commit_before>/*
 * Trident - A Multithreaded Server Alternative
 * Copyright 2014 The TridentSDK Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.tridentsdk.server.data;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import net.tridentsdk.base.Substance;
import net.tridentsdk.inventory.Item;
import net.tridentsdk.meta.nbt.*;

public class Slot implements Writable, NBTSerializable {
    @NBTField(name = "id", type = TagType.SHORT)
    protected volatile short id;
    protected volatile Substance mat;

    @NBTField(name = "Slot", type = TagType.BYTE)
    protected volatile byte slot;
    @NBTField(name = "Count", type = TagType.BYTE)
    protected volatile byte quantity;
    @NBTField(name = "Damage", type = TagType.SHORT)
    protected volatile short damageValue;
    @NBTField(name = "tag", type = TagType.COMPOUND)
    protected volatile CompoundTag compoundTag;

    public Slot(ByteBuf buf) {
        try {
            ByteBuf potentialBuffer = buf.copy();
            short possibleId = potentialBuffer.readUnsignedByte();
            this.id = buf.readShort();

            if(this.id > 255 || this.id < 0) {
                this.id = possibleId;
                buf = potentialBuffer;
            }

            this.mat = Substance.fromId(this.id);

            if(this.id == -1) {
                return;
            }

            this.quantity = buf.readByte();
            this.damageValue = buf.readShort();
            byte b;

            if((b = buf.readByte()) != 0) {
                try {
                    NBTDecoder builder = new NBTDecoder(new ByteBufInputStream(buf));

                    this.compoundTag = builder.decode(b);
                } catch(NBTException ignored) {
                    // do something
                }
            }
        } catch(Exception ignored) { // TODO Find out why this throws exceptions
        }
    }

    public Slot(Item is) {
        if (is == null) {
            this.id = -1;
            return;
        }
        this.id = (short) is.id();
        this.mat = is.type();
<commit_msg>Fix Slot bugs<commit_after>/*
 * Trident - A Multithreaded Server Alternative
 * Copyright 2014 The TridentSDK Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.tridentsdk.server.data;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import net.tridentsdk.base.Substance;
import net.tridentsdk.inventory.Item;
import net.tridentsdk.meta.nbt.*;

public class Slot implements Writable, NBTSerializable {
    @NBTField(name = "id", type = TagType.SHORT)
    protected volatile short id;
    protected volatile Substance mat;

    @NBTField(name = "Slot", type = TagType.BYTE)
    protected volatile byte slot;
    @NBTField(name = "Count", type = TagType.BYTE)
    protected volatile byte quantity;
    @NBTField(name = "Damage", type = TagType.SHORT)
    protected volatile short damageValue;
    @NBTField(name = "tag", type = TagType.COMPOUND)
    protected volatile CompoundTag compoundTag;

    public Slot(ByteBuf buf) {
        try {
            //short possibleId = potentialBuffer.readUnsignedByte();
            this.id = buf.readShort();

            this.mat = Substance.fromId(this.id);

            if(this.id == -1) {
                return;
            }

            this.quantity = (byte) buf.readUnsignedByte();
            this.damageValue = buf.readShort();
            byte b;

            if((b = buf.readByte()) != 0) {
                try {
                    NBTDecoder builder = new NBTDecoder(new ByteBufInputStream(buf));

                    this.compoundTag = builder.decode(b);
                } catch(NBTException ignored) {
                    // do something
                }
            }
        } catch(Exception ignored) { // TODO Find out why this throws exceptions
        }
    }

    public Slot(Item is) {
        if (is == null) {
            this.id = -1;
            return;
        }
        this.id = (short) is.id();
        this.mat = is.type();

{'fraction_non_alphanumeric': 0.09810874704491726, 'fraction_numerical': 0.00512214342001576, 'mean_word_length': 2.209228824273072, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5038513', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1374, 'n_words': 432}