de.craftolution.craftoplugin.modules.protection.loader.SubjectCache.java Source code

Java tutorial

Introduction

Here is the source code for de.craftolution.craftoplugin.modules.protection.loader.SubjectCache.java

Source

/*
 * 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.craftoplugin.modules.protection.loader;

import com.google.common.collect.Lists;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.util.Iterator;
import java.util.List;

public class SubjectCache {

    final List<Subject> subjects;

    SubjectCache(int length) {
        this.subjects = Lists.newArrayListWithCapacity(length);
    }

    void update() {
        // Step 1: Update existing subjects & throw out offline subjects
        final Iterator<Subject> subjectIterator = this.subjects.iterator();
        while (subjectIterator.hasNext()) {
            Subject subject = subjectIterator.next();
            Player player = Bukkit.getPlayer(subject.identifier);

            if (!shouldBeConsidered(player)) {
                // Delete subject
                subjectIterator.remove();
            } else {
                // Update subject
                subject.currX = player.getLocation().getBlockX();
                subject.currZ = player.getLocation().getBlockZ();
                subject.currChunkX = player.getLocation().getChunk().getX();
                subject.currChunkZ = player.getLocation().getChunk().getZ();
            }
        }

        // Step 2: Add new players to subject array
        playerLoop: for (Player player : Bukkit.getOnlinePlayers()) {
            // Check if player should even be considered in ProtectionLoader
            if (!shouldBeConsidered(player)) {
                continue playerLoop;
            }

            // Check if player is already in the list
            for (Subject subject : subjects) {
                if (subject.identifier.equals(player.getUniqueId())) {
                    // Player is already in list, skip
                    continue playerLoop;
                }
            }

            // Add player to list
            Subject newSubject = new Subject(player.getUniqueId(), player.getLocation().getBlockX(),
                    player.getLocation().getBlockZ());

            subjects.add(newSubject);
        }
    }

    boolean shouldBeConsidered(Player player) {
        return player != null && player.isOnline()
                && player.getWorld().getEnvironment().equals(World.Environment.NORMAL);
    }

}