Skip to content

Commit

Permalink
Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNatan committed Dec 9, 2023
1 parent 5dfa13b commit c728fbf
Show file tree
Hide file tree
Showing 27 changed files with 302 additions and 213 deletions.
3 changes: 1 addition & 2 deletions inventory-framework-anvil-input/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
plugins {
alias(libs.plugins.shadowjar)
alias(libs.plugins.kotlin)
}

apply from: '../library.gradle'
apply from: '../publish.gradle'

dependencies {
compileOnly libs.spigot
compileOnlyApi projects.inventoryFrameworkPlatformBukkit
compileOnly projects.inventoryFrameworkPlatformBukkit
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void stateValueSet(
context.getConfig().getTitle(),
scopedInitialInput.isEmpty() ? globalInitialInput : scopedInitialInput);
final ViewContainer container =
new BukkitViewContainer(inventory, context.isShared(), ViewType.ANVIL, true);
new BukkitViewContainer(inventory, ViewType.ANVIL, false, true);

context.setContainer(container);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

public interface ViewContainer {

String getTitle();

/**
* The type of this container.
*
Expand Down Expand Up @@ -39,6 +37,8 @@ public interface ViewContainer {

void removeItem(int slot);

void renderItem(int slot, Object platformItem);

/**
* The number of slots in this container.
*
Expand Down Expand Up @@ -78,4 +78,8 @@ public interface ViewContainer {
boolean isEntityContainer();

boolean isProxied();

ViewContainer unproxied();

boolean isExternal();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class ViewType {
public static final ViewType SHULKER_BOX = new ViewType("shulker-box", 27, 3, 9);
public static final ViewType SMOKER = new ViewType("smoker", 3, 2, 2, false, new int[] {2});
public static final ViewType VILLAGER_TRADING = new ViewType("villager-trading", 3, 1, 3, false, new int[] {2});
public static final ViewType PLAYER = new ViewType("player", 54, 3, 9, false);
public static final ViewType PLAYER = new ViewType("player", 36, 3, 9, false);

private static final int[] EMPTY_RESULT_SLOTS = new int[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,18 @@ public final void hide() {

@Override
public final @NotNull ComponentHandle getHandle() {
if (handle != null) getPipeline().removeInterceptor(handle);

return handle == null ? NOOP_HANDLE : handle;
}

@Override
public final void setHandle(ComponentHandle handle) {
if (this.handle != null) getPipeline().removeInterceptor(this.handle);
if (handle != null) {
for (final PipelinePhase phase :
new PipelinePhase[] {Component.RENDER, Component.UPDATE, Component.CLEAR, Component.CLICK})
getPipeline().intercept(phase, handle);
new PipelinePhase[] {Component.RENDER, Component.UPDATE, Component.CLEAR, Component.CLICK}) {

getPipeline().intercept(phase, handle);
}
}

this.handle = handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public final List<Component> getComponentsAt(int position) {

@Override
public final void addComponent(@NotNull Component component) {
synchronized (getInternalComponents()) {
synchronized (getInternalComponents()) {
getInternalComponents().add(0, component);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void intercept(PipelineContext<VirtualView> pipeline, VirtualView subject
* @param context The context.
*/
private void registerComponents(IFRenderContext context) {
context.getNotRenderedComponents().stream()
context.getNotRenderedComponents().stream()
.map(builder -> builder.buildComponent(context))
.peek(this::assignReference)
.forEach(context::addComponent);
Expand Down
2 changes: 0 additions & 2 deletions inventory-framework-platform-bukkit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.apache.tools.ant.filters.ReplaceTokens

plugins {
alias(libs.plugins.shadowjar)
alias(libs.plugins.bukkit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,40 @@
public final class BukkitViewContainer implements ViewContainer {

private final Inventory inventory;
private final boolean shared;
private final ViewType type;
private final boolean proxied;
private final boolean proxied, external;

public BukkitViewContainer(@NotNull Inventory inventory, boolean shared, ViewType type, boolean proxied) {
public BukkitViewContainer(@NotNull Inventory inventory, ViewType type) {
this(inventory, type, false, false);
}

public BukkitViewContainer(@NotNull Inventory inventory, ViewType type, boolean proxied, boolean external) {
this.inventory = inventory;
this.shared = shared;
this.type = type;
this.proxied = proxied;
this.external = external;
}

public Inventory getInventory() {
return inventory;
}

public boolean isShared() {
return shared;
}

@Override
public boolean isProxied() {
return proxied;
}

@Override
public String getTitle() {
final boolean diffTitle = inventory.getViewers().stream()
.map(HumanEntity::getOpenInventory)
.map(InventoryView::getTitle)
.distinct()
.findAny()
.isPresent();
@Override
public ViewContainer unproxied() {
return this;
}

if (diffTitle && shared) throw new IllegalStateException("Cannot get unique title of shared inventory");
@Override
public boolean isExternal() {
return external;
}

return inventory.getViewers().get(0).getOpenInventory().getTitle();
}

@Override
@Override
public @NotNull ViewType getType() {
return type;
}
Expand All @@ -78,7 +73,15 @@ public void removeItem(int slot) {
inventory.setItem(slot, null);
}

private void requireSupportedItem(Object item) {
@Override
public void renderItem(int slot, Object platformItem) {
System.out.println("slot = " + slot);
final int fixedSlot = isEntityContainer() && isProxied() ? getLastSlot() - slot : slot;
System.out.println("fixedSlot = " + fixedSlot);
inventory.setItem(fixedSlot, (ItemStack) platformItem);
}

private void requireSupportedItem(Object item) {
if (item == null || item instanceof ItemStack) return;

throw new IllegalStateException(
Expand All @@ -102,11 +105,16 @@ public int getSlotsCount() {

@Override
public int getFirstSlot() {
return 0;
return getType() == ViewType.PLAYER ? 45 : 0;
}

@Override
public int getLastSlot() {
System.out.println("isEntityContainer() = " + isEntityContainer());
System.out.println("inventory.getSize() = " + inventory.getSize());
if (isEntityContainer())
return inventory.getSize() - 1;

final int[] resultSlots = getType().getResultSlots();
int lastSlot = getSlotsCount();
if (resultSlots != null) {
Expand Down Expand Up @@ -152,18 +160,17 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BukkitViewContainer that = (BukkitViewContainer) o;
return shared == that.shared
&& Objects.equals(inventory, that.inventory)
return Objects.equals(inventory, that.inventory)
&& Objects.equals(getType(), that.getType());
}

@Override
public int hashCode() {
return Objects.hash(inventory, shared, getType());
return Objects.hash(inventory, getType());
}

@Override
public String toString() {
return "BukkitViewContainer{" + "inventory=" + inventory + ", shared=" + shared + ", type=" + type + '}';
return "BukkitViewContainer{" + "inventory=" + inventory + ", type=" + type + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public void close() {
@Override
public @NotNull ViewContainer getSelfContainer() {
if (selfContainer == null)
selfContainer = new BukkitViewContainer(
getPlayer().getInventory(), getActiveContext().isShared(), ViewType.PLAYER, false);
selfContainer = new BukkitViewContainer(getPlayer().getInventory(), ViewType.PLAYER);

return selfContainer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.devnatan.inventoryframework.component;

import me.devnatan.inventoryframework.VirtualView;
import org.bukkit.inventory.ItemStack;

public class BukkitItemComponentBuilder<SELF> extends BukkitComponentBuilder<SELF> implements ItemComponentBuilder {
Expand All @@ -10,7 +11,27 @@ public class BukkitItemComponentBuilder<SELF> extends BukkitComponentBuilder<SEL

public BukkitItemComponentBuilder() {}

protected final int getPosition() {
@Override
public Component buildComponent(VirtualView root) {
final Component component = new BukkitItemComponentImpl(
getPosition(),
getItem(),
getKey(),
root,
getReference(),
getWatchingStates(),
getDisplayCondition(),
getRenderHandler(),
getUpdateHandler(),
getClickHandler(),
isCancelOnClick(),
isCloseOnClick(),
isUpdateOnClick());
component.setHandle(new BukkitItemComponentImplHandle());
return component;
}

protected final int getPosition() {
return position;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class BukkitItemComponentImpl extends PlatformComponent implements
updateOnClick);
this.position = position;
this.stack = itemStack;
setHandle(new Handle());
setHandle(new BukkitItemComponentImplHandle());
}

@Override
Expand Down Expand Up @@ -106,106 +106,3 @@ public String toString() {
return "BukkitItemComponentImpl{" + "position=" + position + ", itemStack=" + stack + "} " + super.toString();
}
}

class Handle extends BukkitComponentHandle<BukkitItemComponentBuilder<Void>> {

@Override
public void rendered(@NotNull ComponentRenderContext context) {
final BukkitItemComponentImpl component = (BukkitItemComponentImpl) context.getComponent();

if (component.getRenderHandler() != null) {
final int initialSlot = component.getPosition();
component.getRenderHandler().accept(context);

// Externally managed components have its own displacement measures
// FIXME Missing implementation
// TODO Component-based context do not need displacement measures?
if (!component.isManagedExternally()) {
final int updatedSlot = ((BukkitItemComponentImpl) context.getComponent()).getPosition();
component.setPosition(updatedSlot);

if (updatedSlot == -1 && initialSlot == -1) {
// TODO needs more user-friendly "do something"-like message
throw new InventoryFrameworkException("Missing position (unset slot) for item component");
}

// TODO Misplaced - move this to overall item component misplacement check
if (initialSlot != -1 && initialSlot != updatedSlot) {
context.getContainer().removeItem(initialSlot);
component.hide();
}
}

// context.getContainer().renderItem(getPosition(), context.getResult());
component.setVisible(true);
return;
}

if (component.getItemStack() == null) {
if (context.getContainer().getType().isResultSlot(component.getPosition())) {
component.show();
return;
}
throw new IllegalStateException("At least one fallback item or render handler must be provided");
}

context.getContainer().renderItem(component.getPosition(), component.getItemStack());
component.show();
}

@SuppressWarnings("unchecked")
@Override
public void updated(@NotNull IFComponentUpdateContext context) {
if (context.isCancelled()) return;

@SuppressWarnings("rawtypes")
final PlatformComponent component = (PlatformComponent) context.getComponent();

// Static item with no `displayIf` must not even reach the update handler
if (!context.isForceUpdate() && component.getDisplayCondition() == null && component.getRenderHandler() == null)
return;

if (component.isVisible() && component.getUpdateHandler() != null) {
component.getUpdateHandler().accept(context);
if (context.isCancelled()) return;
}

((IFRenderContext) context.getTopLevelContext()).renderComponent(component);
}

@Override
public void cleared(@NotNull IFComponentContext context) {
final Component component = context.getComponent();
component.getContainer().removeItem(((ItemComponent) component).getPosition());
}

@Override
public void clicked(@NotNull IFSlotClickContext context) {
@SuppressWarnings("rawtypes")
final PlatformComponent component = (PlatformComponent) context.getComponent();
if (component.isUpdateOnClick()) context.update();
}

@Override
public BukkitItemComponentBuilder<Void> builder() {
return new BukkitItemComponentBuilder<Void>() {
@Override
public Component buildComponent(VirtualView root) {
return new BukkitItemComponentImpl(
getPosition(),
getItem(),
getKey(),
root,
getReference(),
getWatchingStates(),
getDisplayCondition(),
getRenderHandler(),
getUpdateHandler(),
getClickHandler(),
isCancelOnClick(),
isCloseOnClick(),
isUpdateOnClick());
}
};
}
}
Loading

0 comments on commit c728fbf

Please sign in to comment.