Skip to content

Commit

Permalink
Implement new InventoryView logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannyboy11 committed Jun 17, 2024
1 parent c827b16 commit 16cbe60
Show file tree
Hide file tree
Showing 36 changed files with 808 additions and 276 deletions.
2 changes: 1 addition & 1 deletion InvSee++_Common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.20.6-R0.1-SNAPSHOT</version>
<version>1.21-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.janboerman.invsee.spigot.api.template.EnderChestSlot;
import com.janboerman.invsee.spigot.api.template.Mirror;
import com.janboerman.invsee.spigot.api.template.PlayerInventorySlot;
import com.janboerman.invsee.spigot.internal.view.SpectatorInventoryView;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
package com.janboerman.invsee.spigot.api;

import com.janboerman.invsee.spigot.api.template.EnderChestSlot;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;

/**
* Represents an open window for an {@link EnderSpectatorInventory}.
*/
public abstract class EnderSpectatorInventoryView extends SpectatorInventoryView<EnderChestSlot> {

protected EnderSpectatorInventoryView(CreationOptions<EnderChestSlot> creationOptions) {
super(creationOptions);
}

/** {@inheritDoc} */
@Override
public abstract EnderSpectatorInventory getTopInventory();

/** Sets the item in the desired slot of the InventoryView. If the slot is in the top inventory, then the inventory slot will be computed using
* {@code getMirror().getSlot(slot).defaultIndex()}.
* @see com.janboerman.invsee.spigot.api.template.Mirror */
@Override
public void setItem(int slot, ItemStack item) {
if (slot == -999 || slot >= getTopInventory().getSize()) {
super.setItem(slot, item);
} else {
EnderChestSlot ecSlot = getMirror().getSlot(slot);
if (ecSlot != null) super.setItem(ecSlot.defaultIndex(), item);
}
}

/** Gets the item in the desired slot of the InventoryView. If the slot is in the top inventory, then the inventory slot will be computed using
* {@code getMirror().getSlot(slot).defaultIndex()}.
* @see com.janboerman.invsee.spigot.api.template.Mirror */
@Override
public ItemStack getItem(int slot) {
if (slot == -999 || slot >= getTopInventory().getSize()) {
return super.getItem(slot);
} else {
EnderChestSlot ecSlot = getMirror().getSlot(slot);
return ecSlot == null ? null : super.getItem(ecSlot.defaultIndex());
}
}

/** {@inheritDoc} */
@Override
public InventoryType getType() {
return InventoryType.CHEST;
}

}
package com.janboerman.invsee.spigot.api;

import com.janboerman.invsee.spigot.api.template.EnderChestSlot;

import org.bukkit.event.inventory.InventoryType;

/**
* Represents an open window for an {@link EnderSpectatorInventory}.
*/
public interface EnderSpectatorInventoryView extends SpectatorInventoryView<EnderChestSlot> {

/** {@inheritDoc} */
@Override
public EnderSpectatorInventory getTopInventory();

/**
* Get the inventory type.
* @return the inventory type
*/
public default InventoryType getType() {
return InventoryType.CHEST;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import com.janboerman.invsee.spigot.internal.OpenSpectatorsCache;
import com.janboerman.invsee.spigot.internal.inventory.ShallowCopy;
import com.janboerman.invsee.spigot.internal.inventory.Personal;
import com.janboerman.invsee.spigot.internal.view.EnderSpectatorInventoryView;
import com.janboerman.invsee.spigot.internal.view.MainSpectatorInventoryView;
import com.janboerman.invsee.spigot.internal.view.SpectatorInventoryView;
import com.janboerman.invsee.utils.*;
import org.bukkit.*;
import org.bukkit.entity.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,21 @@
package com.janboerman.invsee.spigot.api;

import com.janboerman.invsee.spigot.api.template.PlayerInventorySlot;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;

/**
* Represents an open window for a {@link MainSpectatorInventory}.
*/
public abstract class MainSpectatorInventoryView extends SpectatorInventoryView<PlayerInventorySlot> {

protected MainSpectatorInventoryView(CreationOptions<PlayerInventorySlot> creationOptions) {
super(creationOptions);
}

/** {@inheritDoc} */
@Override
public abstract MainSpectatorInventory getTopInventory();

/** Sets the item in the desired slot of the InventoryView. If the slot is in the top inventory, then the inventory slot will be computed using
* {@code getMirror().getSlot(slot).defaultIndex()}.
* @see com.janboerman.invsee.spigot.api.template.Mirror */
@Override
public void setItem(int slot, ItemStack item) {
if (0 <= slot && slot < getTopInventory().getSize()) {
PlayerInventorySlot piSlot = getMirror().getSlot(slot);
if (piSlot != null) super.setItem(piSlot.defaultIndex(), item);
} else {
super.setItem(slot, item);
}
}

/** Gets the item in the desired slot of the InventoryView. If the slot is in the top inventory, then the inventory slot will be computed using
* {@code getMirror().getSlot(slot).defaultIndex()}.
* @see com.janboerman.invsee.spigot.api.template.Mirror */
@Override
public ItemStack getItem(int slot) {
if (0 <= slot && slot < getTopInventory().getSize()) {
PlayerInventorySlot piSlot = getMirror().getSlot(slot);
return piSlot == null ? null : super.getItem(piSlot.defaultIndex());
} else {
return super.getItem(slot);
}
}

/** {@inheritDoc} */
@Override
public InventoryType getType() {
return InventoryType.CHEST;
}

}
package com.janboerman.invsee.spigot.api;

import com.janboerman.invsee.spigot.api.template.PlayerInventorySlot;

import org.bukkit.event.inventory.InventoryType;

public interface MainSpectatorInventoryView extends SpectatorInventoryView<PlayerInventorySlot> {

/** {@inheritDoc} */
@Override
public MainSpectatorInventory getTopInventory();

/**
* Get the inventory type.
* @return the inventory type
*/
public default InventoryType getType() {
return InventoryType.CHEST;
}

}
Original file line number Diff line number Diff line change
@@ -1,64 +1,53 @@
package com.janboerman.invsee.spigot.api;

import com.janboerman.invsee.spigot.api.logging.Difference;
import com.janboerman.invsee.spigot.api.target.Target;
import com.janboerman.invsee.spigot.api.template.Mirror;
import org.bukkit.inventory.InventoryView;

import javax.annotation.Nullable;
import java.util.Objects;

/**
* Represents an open window for a {@link SpectatorInventory}.
* @param <Slot> the inventory's slot type.
*/
public abstract class SpectatorInventoryView<Slot> extends InventoryView {

private final CreationOptions<Slot> creationOptions;
protected Target target;

protected SpectatorInventoryView(CreationOptions<Slot> creationOptions) {
this.creationOptions = Objects.requireNonNull(creationOptions, "creation options cannot be null");
}

/** {@inheritDoc} */
@Override
public abstract SpectatorInventory<Slot> getTopInventory();

/**
* Get the difference tracked by this window.
* @return the difference
*/
public abstract @Nullable Difference getTrackedDifference();

/**
* Get the options this window was created with.
* @return a copy of the creation options
*/
public CreationOptions<Slot> getCreationOptions() {
return creationOptions.clone();
}

//can't override getTitle because that does not work on 1.12 (the method is final :/)
//can't override title() either because of Adventure Text (because I can't have return type String)
//ooh the burden of supporting multiple versions!

/**
* Get the mirror the {@link SpectatorInventory} is viewed through.
* @return the mirror
* @see <a href="https://github.com/Jannyboy11/InvSee-plus-plus/wiki/Customising-spectator-inventories#mirror">Mirror explanation in the InvSee++ wiki</a>
*/
public Mirror<Slot> getMirror() {
return creationOptions.getMirror();
}

/**
* Get the target of the {@link SpectatorInventory}.
* @return the target
*/
public Target getTarget() {
SpectatorInventory<Slot> top = getTopInventory();
return target == null ? target = Target.byGameProfile(top.getSpectatedPlayerId(), top.getSpectatedPlayerName()) : target;
}

}
package com.janboerman.invsee.spigot.api;

import javax.annotation.Nullable;

import com.janboerman.invsee.spigot.api.logging.Difference;
import com.janboerman.invsee.spigot.api.target.Target;
import com.janboerman.invsee.spigot.api.template.Mirror;

/**
* Represents an open window for a {@link SpectatorInventory}.
* @param <Slot> the inventory's slot type.
*/
// cannot extend InventoryView, because this class was not an interface before Bukkit 1.21.
public interface SpectatorInventoryView<Slot> {

/**
* Get the top inventory of this view.
* @return the top inventory
*/
public SpectatorInventory<Slot> getTopInventory();

/**
* Get the difference tracked by this window.
* @return the difference
*/
public @Nullable Difference getTrackedDifference();

/**
* Get the options this window was created with.
* @return a copy of the creation options
*/
public CreationOptions<Slot> getCreationOptions();

/**
* Get the title of this InventoryView.
* @return the title
*/
public String getTitle();

/**
* Get the mirror the {@link SpectatorInventory} is viewed through.
* @return the mirror
* @see <a href="https://github.com/Jannyboy11/InvSee-plus-plus/wiki/Customising-spectator-inventories#mirror">Mirror explanation in the InvSee++ wiki</a>
*/
public Mirror<Slot> getMirror();

/**
* Get the target of the {@link SpectatorInventory}.
* @return the target
*/
public Target getTarget();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.janboerman.invsee.spigot.api.target.Target;
import com.janboerman.invsee.spigot.internal.ConstantTitle;
import com.janboerman.invsee.spigot.internal.view.SpectatorInventoryView;

import java.util.function.Function;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import java.util.Map;
import java.util.Objects;

import com.janboerman.invsee.spigot.internal.view.SpectatorInventoryView;

/**
* The Difference of a {@link com.janboerman.invsee.spigot.api.SpectatorInventoryView} is the set of items that were added and removed the {@link com.janboerman.invsee.spigot.api.SpectatorInventory}.
* The Difference of a {@link SpectatorInventoryView} is the set of items that were added and removed the {@link com.janboerman.invsee.spigot.api.SpectatorInventory}.
*/
public class Difference {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.janboerman.invsee.spigot.api.logging;

import com.janboerman.invsee.spigot.internal.view.SpectatorInventoryView;

/**
* Granularity for logging.
*/
public enum LogGranularity {

/** Log never. */
LOG_NEVER,
/** Log when the {@link com.janboerman.invsee.spigot.api.SpectatorInventoryView} closes. */
/** Log when the {@link SpectatorInventoryView} closes. */
LOG_ON_CLOSE,
/** Log changes after every change to the {@link com.janboerman.invsee.spigot.api.SpectatorInventory}. */
LOG_EVERY_CHANGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.util.Objects;
import java.util.Set;

import com.janboerman.invsee.spigot.internal.view.SpectatorInventoryView;

/**
* Options for logging interactions with {@link com.janboerman.invsee.spigot.api.SpectatorInventoryView}s.
* Options for logging interactions with {@link SpectatorInventoryView}s.
*
* @see com.janboerman.invsee.spigot.api.CreationOptions
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.janboerman.invsee.spigot.api.placeholder;

import com.janboerman.invsee.spigot.api.MainSpectatorInventoryView;
import com.janboerman.invsee.spigot.api.template.PlayerInventorySlot;
import org.bukkit.inventory.ItemStack;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.janboerman.invsee.spigot.api.response;

import com.janboerman.invsee.spigot.api.SpectatorInventoryView;
import com.janboerman.invsee.spigot.internal.view.SpectatorInventoryView;

import java.util.NoSuchElementException;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.janboerman.invsee.spigot.api.CreationOptions;
import com.janboerman.invsee.spigot.api.EnderSpectatorInventory;
import com.janboerman.invsee.spigot.api.EnderSpectatorInventoryView;
import com.janboerman.invsee.spigot.internal.view.EnderSpectatorInventoryView;
import com.janboerman.invsee.spigot.api.MainSpectatorInventory;
import com.janboerman.invsee.spigot.api.MainSpectatorInventoryView;
import com.janboerman.invsee.spigot.internal.view.MainSpectatorInventoryView;
import com.janboerman.invsee.spigot.api.Title;
import com.janboerman.invsee.spigot.api.logging.LogGranularity;
import com.janboerman.invsee.spigot.api.logging.LogOptions;
Expand All @@ -21,7 +21,6 @@
import org.bukkit.plugin.Plugin;

import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.janboerman.invsee.spigot.internal.placeholder;

import com.janboerman.invsee.spigot.api.MainSpectatorInventoryView;
import com.janboerman.invsee.spigot.api.placeholder.PlaceholderGroup;
import com.janboerman.invsee.spigot.api.placeholder.PlaceholderPalette;
import com.janboerman.invsee.spigot.api.template.PlayerInventorySlot;
Expand Down
Loading

0 comments on commit 16cbe60

Please sign in to comment.