<commit_before>package com.thomas15v.crossevents.network.packet.packets;

import com.google.common.base.Optional;
import com.thomas15v.crossevents.CrossEventsPlugin;
import com.thomas15v.crossevents.network.packet.PacketHandler;
import org.spongepowered.api.event.Event;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.UUID;

/**
 *
 * Packet Layout
 *
 * Target server
 * HOP number
 * event class name
 * eventdata
 * eventid
 *
 */
public class EventPacket extends Packet {
    private Optional<Event> event;
    private String eventData;
    private UUID eventId;
    private int hop = 0;
    private Optional<UUID> target;

    public EventPacket(){}

    public EventPacket(UUID sender, Event event){
        super(sender);
        this.event = Optional.of(event);
        eventId = UUID.randomUUID();
        target = Optional.absent();
    }

    public EventPacket(UUID sender, Event event, UUID target){
        this(sender, event);
        this.target = Optional.fromNullable(target);
    }

    public Optional<Event> getEvent() {
        return event;
    }

    @Override
    public void read(BufferedReader in) throws IOException {
        String target = in.readLine();
        if (target.equals("ALL"))
            this.target = Optional.absent();
        else
            this.target = Optional.of(UUID.fromString(target));
        hop = Integer.parseInt(in.readLine());
        event = parseEvent(in.readLine(), in.readLine());
        eventId = UUID.fromString(in.readLine());
        super.read(in);
    }

    private Optional<Event> parseEvent(String eventClazz, String eventData){
        this.eventData = eventData;
        try {
            return Optional.of(gson.fromJson(eventData,(Class<? extends Event>) Class.forName(eventClazz)));
        } catch (ClassNotFoundException e) {
            CrossEventsPlugin.getInstance().getLogger().debug("Could not find event class " + eventClazz);
            return Optional.absent();
        }
    }

    @Override
    public void write(BufferedWriter out) throws IOException {
        if (target.isPresent())
            writeln(out, target.get().toString());
        else
            writeln(out, "ALL");
        writeln(out, String.valueOf(hop));
        writeln(out, event.getClass().getName());
        if (event.isPresent())
            writeln(out, gson.toJson(event));
        else
            writeln(out, eventData);
        writeln(out, eventId.toString());
        super.write(out);
    }

    @Override<commit_msg>Fixed another bug with eventPacket when the class doesn't exist<commit_after>package com.thomas15v.crossevents.network.packet.packets;

import com.google.common.base.Optional;
import com.thomas15v.crossevents.CrossEventsPlugin;
import com.thomas15v.crossevents.network.packet.PacketHandler;
import org.spongepowered.api.event.Event;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.UUID;

/**
 *
 * Packet Layout
 *
 * Target server
 * HOP number
 * event class name
 * eventdata
 * eventid
 *
 */
public class EventPacket extends Packet {
    private Optional<Event> event;
    private String eventData;
    private String eventClass;
    private UUID eventId;
    private int hop = 0;
    private Optional<UUID> target;

    public EventPacket(){}

    public EventPacket(UUID sender, Event event){
        super(sender);
        this.event = Optional.of(event);
        eventId = UUID.randomUUID();
        target = Optional.absent();
    }

    public EventPacket(UUID sender, Event event, UUID target){
        this(sender, event);
        this.target = Optional.fromNullable(target);
    }

    public Optional<Event> getEvent() {
        return event;
    }

    @Override
    public void read(BufferedReader in) throws IOException {
        String target = in.readLine();
        if (target.equals("ALL"))
            this.target = Optional.absent();
        else
            this.target = Optional.of(UUID.fromString(target));
        hop = Integer.parseInt(in.readLine());
        event = parseEvent(in.readLine(), in.readLine());
        eventId = UUID.fromString(in.readLine());
        super.read(in);
    }

    private Optional<Event> parseEvent(String eventClass, String eventData){
        this.eventData = eventData;
        this.eventClass = eventClass;
        try {
            return Optional.of(gson.fromJson(eventData,(Class<? extends Event>) Class.forName(eventClass)));
        } catch (ClassNotFoundException e) {
            CrossEventsPlugin.getInstance().getLogger().debug("Could not find event class " + eventClass);
            return Optional.absent();
        }
    }

    @Override
    public void write(BufferedWriter out) throws IOException {
        if (target.isPresent())
            writeln(out, target.get().toString());
        else
            writeln(out, "ALL");
        writeln(out, String.valueOf(hop));
        if (event.isPresent()) {
            writeln(out, event.get().getClass().getName());
            writeln(out, gson.toJson(event.get()));
        }
        else {
            writeln(out, eventClass);
            writeln(out, eventData);
        }
        writeln(out, eventId.toString());
        super.write(out);
    }

    @Override
{'fraction_non_alphanumeric': 0.11655055545095086, 'fraction_numerical': 0.0026360384108454153, 'mean_word_length': 2.681219681219681, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5047067', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1491, 'n_words': 333}