Java tutorial
/* * This file is part of CraftoPlugin, licensed under the MIT License (MIT). * * Copyright (c) 2018 CraftolutionDE <https://craftolution.de> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Website: http://craftolution.de/ * Contact: support@craftolution.de */ package de.craftolution.craftoplugin4.modules.web.common; import com.google.common.collect.Lists; import de.craftolution.craftoplugin4.modules.web.packets.PacketType; import java.util.List; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; public class TCPPacket { private static final AtomicInteger PACKET_ID_GENERATOR = new AtomicInteger(); private static final String DELIMITER = "|"; public final int id; public int responseId; public final PacketType type; public final List<Object> data; private int nextIndex = 0; public TCPPacket(PacketType type) { this.id = PACKET_ID_GENERATOR.getAndIncrement(); this.responseId = -1; this.type = type; this.data = Lists.newArrayList(); } public TCPPacket(String string) { String[] rawParts = string.split('\\' + DELIMITER); this.id = Integer.parseInt(rawParts[0]); this.responseId = Integer.parseInt(rawParts[1]); this.type = PacketType.getById(rawParts[2]).get(); this.data = Lists.newArrayListWithCapacity(rawParts.length - 3); for (int i = 3; i < rawParts.length; i++) { this.data.add(rawParts[i]); } } // --- String ----------------------------------------------------------------------------------------------------- public TCPPacket add(String value) { this.data.add(value); return this; } public String getString(int index) { return String.valueOf(data.get(index)); } public String nextString() { return this.getString(nextIndex++); } // --- Int -------------------------------------------------------------------------------------------------------- public TCPPacket add(int value) { this.data.add(value); return this; } public int getInt(int index) { return Integer.parseInt(String.valueOf(data.get(index))); } public int nextInt() { return this.getInt(nextIndex++); } // --- Boolean ---------------------------------------------------------------------------------------------------- public TCPPacket add(boolean value) { this.data.add(value); return this; } public boolean getBool(int index) { return Boolean.parseBoolean(String.valueOf(data.get(index))); } public boolean nextBool() { return this.getBool(nextIndex++); } // --- UUID ------------------------------------------------------------------------------------------------------- public TCPPacket add(UUID uuid) { this.data.add(uuid); return this; } public UUID getUniqueId(int index) throws IllegalArgumentException { return UUID.fromString(String.valueOf(data.get(index))); } public UUID nextUniqueID() { return this.getUniqueId(nextIndex++); } // --- Float ------------------------------------------------------------------------------------------------------ public TCPPacket add(float value) { this.data.add(value); return this; } public float getFloat(int index) { return Float.parseFloat(String.valueOf(data.get(index))); } public float nextFloat() { return this.getFloat(nextIndex++); } // --- Double ------------------------------------------------------------------------------------------------------ public TCPPacket add(double value) { this.data.add(value); return this; } public double getDouble(int index) { return Double.parseDouble(String.valueOf(data.get(index))); } public double nextDouble() { return this.getDouble(nextIndex++); } // --- Misc ------------------------------------------------------------------------------------------------------- public String toString() { StringBuilder b = new StringBuilder().append(this.id).append(DELIMITER).append(this.responseId) .append(DELIMITER).append(this.type.getId()).append(DELIMITER); for (Object part : data) { b.append(part).append(DELIMITER); } return b.toString(); } }