net.lyonlancer5.mcmp.kawo.modes.BookProcessor.java Source code

Java tutorial

Introduction

Here is the source code for net.lyonlancer5.mcmp.kawo.modes.BookProcessor.java

Source

/***************************************************************************\
* Copyright 2017 [Lyonlancer5]                                              *
*                                                                           *
* 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.lyonlancer5.mcmp.kawo.modes;

import java.util.List;

import org.apache.commons.lang3.tuple.Pair;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagList;

/**
 * Book processor for mode changing
 * 
 * @author Lyonlancer5
 */
public class BookProcessor {

    public static List<Pair<String, String>> readBook(ItemStack book) {
        if (book.getTagCompound() == null) {
            return ImmutableList.of();
        }

        NBTTagList pages = book.getTagCompound().getTagList("pages", 8);

        if (pages == null || pages.tagCount() == 0) {
            return ImmutableList.of();
        }

        final List<Pair<String, String>> lines = Lists.newArrayList();
        for (int i = 0; i < pages.tagCount(); i++) {
            String[] crlf = pages.getStringTagAt(i).split("[\\r\\n]");
            for (String ln : crlf) {
                ln = ln.trim();
                if (!ln.isEmpty()) {
                    if (ln.contains("=")) {
                        String[] var0 = ln.split("=", 2);
                        lines.add(Pair.of(var0[0].trim(), var0[1].trim()));
                    }
                }
            }
        }
        return ImmutableList.copyOf(lines);
    }

}