Skip to content

Commit

Permalink
simplify. (#20)
Browse files Browse the repository at this point in the history
* override.

* simplify.

* format
  • Loading branch information
portlek authored Nov 22, 2024
1 parent 975f9e2 commit 261c5d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ ElementItemBuilder renderWith(
ElementItemBuilder updateOnClick();

@NotNull
@Override
ElementItemBuilder interactionDelay(@Nullable Duration interactionDelay);

@NotNull
@Override
ElementItemBuilder onInteractionDelay(
@NotNull Consumer<ContextElementClick> onInteractionDelay
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ElementBuilderImpl implements ElementBuilderRich {
public class ElementBuilderImpl<Self extends ElementBuilderImpl<Self>>
implements ElementBuilderRich {

Element root;
boolean cancelOnClick;
Expand Down Expand Up @@ -42,9 +43,9 @@ public ElementBuilderImpl() {}

@NotNull
@Override
public ElementBuilder root(@NotNull final Element root) {
public Self root(@NotNull final Element root) {
this.root = root;
return this;
return this.self();
}

@NotNull
Expand All @@ -55,62 +56,62 @@ public Element build(@NotNull final ContextBase context) {

@NotNull
@Override
public ElementBuilder cancelOnClick() {
public Self cancelOnClick() {
return this.cancelOnClick(true);
}

@NotNull
@Override
public ElementBuilder closeOnClick() {
public Self closeOnClick() {
return this.closeOnClick(true);
}

@NotNull
@Override
public ElementBuilder updateOnClick() {
public Self updateOnClick() {
return this.updateOnClick(true);
}

@NotNull
@Override
public ElementBuilder cancelOnClick(final boolean cancelOnClick) {
public Self cancelOnClick(final boolean cancelOnClick) {
this.cancelOnClick = cancelOnClick;
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder closeOnClick(final boolean cancelOnClick) {
public Self closeOnClick(final boolean cancelOnClick) {
this.closeOnClick = cancelOnClick;
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder updateOnClick(final boolean updateOnClick) {
public Self updateOnClick(final boolean updateOnClick) {
this.updateOnClick = updateOnClick;
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder interactionDelay(@Nullable final Duration interactionDelay) {
public Self interactionDelay(@Nullable final Duration interactionDelay) {
this.interactionDelay = interactionDelay;
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder onInteractionDelay(
public Self onInteractionDelay(
@NotNull final Consumer<ContextElementClick> onInteractionDelay
) {
this.onInteractionDelay = onInteractionDelay;
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder updateOnStateChange(
public Self updateOnStateChange(
@NotNull final State<?> state,
@NotNull final State<?> @NotNull... otherStates
) {
Expand All @@ -119,12 +120,12 @@ public ElementBuilder updateOnStateChange(
}
this.updateOnStateChange.add(state);
Collections.addAll(this.updateOnStateChange, otherStates);
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder updateOnStateAccess(
public Self updateOnStateAccess(
@NotNull final State<?> state,
@NotNull final State<?> @NotNull... otherStates
) {
Expand All @@ -133,31 +134,37 @@ public ElementBuilder updateOnStateAccess(
}
this.updateOnStateAccess.add(state);
Collections.addAll(this.updateOnStateAccess, otherStates);
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder displayIf(@NotNull final Predicate<ContextElementRender> condition) {
public Self displayIf(@NotNull final Predicate<ContextElementRender> condition) {
this.displayIf = condition;
return this;
return this.self();
}

@NotNull
@Override
public ElementBuilder displayIf(@NotNull final BooleanSupplier condition) {
public Self displayIf(@NotNull final BooleanSupplier condition) {
return this.displayIf(__ -> condition.getAsBoolean());
}

@NotNull
@Override
public ElementBuilder hideIf(@NotNull final Predicate<ContextElementRender> condition) {
public Self hideIf(@NotNull final Predicate<ContextElementRender> condition) {
return this.displayIf(ctx -> condition.negate().test(ctx));
}

@NotNull
@Override
public ElementBuilder hideIf(@NotNull final BooleanSupplier condition) {
public Self hideIf(@NotNull final BooleanSupplier condition) {
return this.hideIf(__ -> condition.getAsBoolean());
}

@NotNull
@SuppressWarnings("unchecked")
private Self self() {
return (Self) this;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package net.infumia.frame.element;

import java.time.Duration;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.context.element.ContextElementClick;
import net.infumia.frame.context.element.ContextElementItemClick;
import net.infumia.frame.context.element.ContextElementItemRender;
import net.infumia.frame.context.element.ContextElementItemUpdate;
import net.infumia.frame.context.element.ContextElementRender;
import net.infumia.frame.state.State;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class ElementItemBuilderImpl
extends ElementBuilderImpl
extends ElementBuilderImpl<ElementItemBuilderImpl>
implements ElementItemBuilderRich {

ItemStack item;
Expand Down Expand Up @@ -95,122 +89,9 @@ public ElementItemBuilder onUpdate(
return this;
}

@NotNull
@Override
public ElementItemBuilder root(@NotNull final Element root) {
super.root(root);
return this;
}

@NotNull
@Override
public ElementItem build(@NotNull final ContextBase parent) {
return new ElementItemImpl(this, parent);
}

@NotNull
@Override
public ElementItemBuilder cancelOnClick() {
super.cancelOnClick();
return this;
}

@NotNull
@Override
public ElementItemBuilder closeOnClick() {
super.closeOnClick();
return this;
}

@NotNull
@Override
public ElementItemBuilder updateOnClick() {
super.updateOnClick();
return this;
}

@NotNull
@Override
public ElementItemBuilder onInteractionDelay(
final @NotNull Consumer<ContextElementClick> onInteractionDelay
) {
super.onInteractionDelay(onInteractionDelay);
return this;
}

@NotNull
@Override
public ElementItemBuilder interactionDelay(final @Nullable Duration interactionDelay) {
super.interactionDelay(interactionDelay);
return this;
}

@NotNull
@Override
public ElementItemBuilder cancelOnClick(final boolean cancelOnClick) {
super.cancelOnClick(cancelOnClick);
return this;
}

@NotNull
@Override
public ElementItemBuilder closeOnClick(final boolean cancelOnClick) {
super.closeOnClick(cancelOnClick);
return this;
}

@NotNull
@Override
public ElementItemBuilder updateOnClick(final boolean updateOnClick) {
super.updateOnClick(updateOnClick);
return this;
}

@NotNull
@Override
public ElementItemBuilder updateOnStateChange(
@NotNull final State<?> state,
@NotNull final State<?> @NotNull... otherStates
) {
super.updateOnStateChange(state, otherStates);
return this;
}

@NotNull
@Override
public ElementItemBuilder updateOnStateAccess(
@NotNull final State<?> state,
@NotNull final State<?> @NotNull... otherStates
) {
super.updateOnStateAccess(state, otherStates);
return this;
}

@NotNull
@Override
public ElementItemBuilder displayIf(@NotNull final Predicate<ContextElementRender> condition) {
super.displayIf(condition);
return this;
}

@NotNull
@Override
public ElementItemBuilder displayIf(@NotNull final BooleanSupplier condition) {
super.displayIf(condition);
return this;
}

@NotNull
@Override
public ElementItemBuilder hideIf(@NotNull final Predicate<ContextElementRender> condition) {
super.hideIf(condition);
return this;
}

@NotNull
@Override
public ElementItemBuilder hideIf(@NotNull final BooleanSupplier condition) {
super.hideIf(condition);
return this;
}
}
Loading

0 comments on commit 261c5d8

Please sign in to comment.