Extract some duplicate code from internal implementations

This commit is contained in:
Jikoo
2022-05-13 09:11:12 -04:00
parent ac00261afb
commit b6e8e2ba42
8 changed files with 37 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 lishid. All rights reserved.
* Copyright (C) 2011-2022 lishid. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -43,11 +43,13 @@ public interface ISpecialInventory {
void setPlayerOffline();
/**
* Gets whether or not this ISpecialInventory is in use.
* Gets whether this ISpecialInventory is in use.
*
* @return true if the ISpecialInventory is in use
*/
boolean isInUse();
default boolean isInUse() {
return !getBukkitInventory().getViewers().isEmpty();
}
/**
* Gets the Player associated with this ISpecialInventory.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 lishid. All rights reserved.
* Copyright (C) 2011-2022 lishid. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -16,6 +16,35 @@
package com.lishid.openinv.internal;
import java.util.List;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.Inventory;
/**
* Interface defining behavior specific to a player inventory.
*/
public interface ISpecialPlayerInventory extends ISpecialInventory {
/*
* Player inventory usage varies from all other inventories - as the inventory is technically open at all times,
* if the player is online or has been online while the inventory is open, they are in the viewer list.
*/
@Override
default boolean isInUse() {
Inventory inventory = getBukkitInventory();
List<HumanEntity> viewers = inventory.getViewers();
if (viewers.size() != 1) {
return !viewers.isEmpty();
}
HumanEntity viewer = viewers.get(0);
if (!viewer.getUniqueId().equals(getPlayer().getUniqueId())) {
return true;
}
return viewer.getOpenInventory().getTopInventory().equals(inventory);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 lishid. All rights reserved.
* Copyright (C) 2011-2022 lishid. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -60,11 +60,6 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe
return inventory;
}
@Override
public boolean isInUse() {
return !this.getViewers().isEmpty();
}
@Override
public void setPlayerOffline() {
this.playerOnline = false;

View File

@@ -103,12 +103,6 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI
this.playerOnline = false;
}
@Override
public boolean isInUse() {
List<HumanEntity> viewers = this.getViewers();
return viewers.size() > 1 || !viewers.isEmpty() && !viewers.get(0).getUniqueId().equals(this.player.getUUID());
}
@Override
public @NotNull HumanEntity getPlayer() {
return this.player.getBukkitEntity();

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 lishid. All rights reserved.
* Copyright (C) 2011-2022 lishid. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -60,11 +60,6 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe
return inventory;
}
@Override
public boolean isInUse() {
return !this.getViewers().isEmpty();
}
@Override
public void setPlayerOffline() {
this.playerOnline = false;

View File

@@ -103,12 +103,6 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI
this.playerOnline = false;
}
@Override
public boolean isInUse() {
List<HumanEntity> viewers = this.getViewers();
return viewers.size() > 1 || !viewers.isEmpty() && !viewers.get(0).getUniqueId().equals(this.player.getUUID());
}
@Override
public @NotNull HumanEntity getPlayer() {
return this.player.getBukkitEntity();

View File

@@ -60,11 +60,6 @@ public class SpecialEnderChest extends PlayerEnderChestContainer implements ISpe
return inventory;
}
@Override
public boolean isInUse() {
return !this.getViewers().isEmpty();
}
@Override
public void setPlayerOffline() {
this.playerOnline = false;

View File

@@ -103,12 +103,6 @@ public class SpecialPlayerInventory extends Inventory implements ISpecialPlayerI
this.playerOnline = false;
}
@Override
public boolean isInUse() {
List<HumanEntity> viewers = this.getViewers();
return viewers.size() > 1 || !viewers.isEmpty() && !viewers.get(0).getUniqueId().equals(this.player.getUUID());
}
@Override
public @NotNull HumanEntity getPlayer() {
return this.player.getBukkitEntity();