Skip to content

Commit

Permalink
Move apply() to common interface and rename as with()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rover656 committed Jul 15, 2024
1 parent c64748d commit 82716ac
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class RegiliteBlock<T extends Block> extends DeferredBlock<T> implements ITagagble<Block> {
public class RegiliteBlock<T extends Block> extends DeferredBlock<T> implements ITagagble<Block>, RegiliteHolder<RegiliteBlock<T>> {
private final Supplier<String> supplier = () -> get().getDescriptionId();
private final Regilite regilite;
private Set<TagKey<Block>> blockTags = Set.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class RegiliteBlockEntity<T extends BlockEntity> extends DeferredHolder<BlockEntityType<? extends BlockEntity>, BlockEntityType<T>> implements ITagagble<BlockEntityType<?>> {
public class RegiliteBlockEntity<T extends BlockEntity> extends DeferredHolder<BlockEntityType<? extends BlockEntity>, BlockEntityType<T>>
implements ITagagble<BlockEntityType<?>>, RegiliteHolder<RegiliteBlockEntity<T>> {
protected Set<TagKey<BlockEntityType<?>>> BlockEntityTags = new HashSet<>();
protected Supplier<Function<BlockEntityRendererProvider.Context, BlockEntityRenderer<? super T>>> renderer;

Expand Down Expand Up @@ -79,17 +80,11 @@ public Supplier<Function<BlockEntityRendererProvider.Context, BlockEntityRendere
return renderer;
}

public <TCap, TContext> RegiliteBlockEntity<T> addCapability(BlockCapability<TCap, TContext> capability, ICapabilityProvider<? super T, TContext, TCap> provider) {
public <TCap, TContext> RegiliteBlockEntity<T> withCapability(BlockCapability<TCap, TContext> capability, ICapabilityProvider<? super T, TContext, TCap> provider) {
attachedCapabilityList.add(new AttachedCapability<>(capability, provider));
return this;
}

// Allows wrapping common holder builder methods into other methods and applying them.
public RegiliteBlockEntity<T> apply(Consumer<RegiliteBlockEntity<T>> applicator) {
applicator.accept(this);
return this;
}

@ApiStatus.Internal
public void registerCapabilityProviders(RegisterCapabilitiesEvent event) {
for (AttachedCapability<T, ?, ?> capabilityProvider : attachedCapabilityList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class RegiliteEntity<T extends Entity> extends DeferredHolder<EntityType<? extends Entity>, EntityType<T>> implements ITagagble<EntityType<?>> {
public class RegiliteEntity<T extends Entity> extends DeferredHolder<EntityType<? extends Entity>, EntityType<T>> implements ITagagble<EntityType<?>>, RegiliteHolder<RegiliteEntity<T>> {
private final Set<TagKey<EntityType<?>>> entityTags = new HashSet<>();
private final Supplier<String> supplier = () -> get().getDescriptionId();
private final Regilite regilite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class RegiliteFluid<T extends FluidType> extends DeferredHolder<FluidType, T> implements ITagagble<Fluid> {
public class RegiliteFluid<T extends FluidType> extends DeferredHolder<FluidType, T> implements ITagagble<Fluid>, RegiliteHolder<RegiliteFluid<T>> {
private final Supplier<String> descriptionIdSupplier = () -> get().getDescriptionId();
private final Regilite regilite;
private Set<TagKey<Fluid>> fluidTags = Set.of();
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/enderio/regilite/holder/RegiliteHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.enderio.regilite.holder;

import java.util.function.Consumer;

public interface RegiliteHolder<T extends RegiliteHolder<T>> {

/**
* Mutate this object using the provided applicator.
* This can be used to collect common combinations of modifications together to reduce repetition.
* @param applicator An applicator that will modify the object.
* @return The object.
*/
default T with(Consumer<T> applicator) {
//noinspection unchecked
T self = (T)this;
applicator.accept(self);
return self;
}
}
11 changes: 2 additions & 9 deletions src/main/java/com/enderio/regilite/holder/RegiliteItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.neoforged.neoforge.capabilities.ICapabilityProvider;
import net.neoforged.neoforge.capabilities.ItemCapability;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredItem;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -28,7 +27,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

public class RegiliteItem<T extends Item> extends DeferredItem<T> implements ITagagble<Item> {
public class RegiliteItem<T extends Item> extends DeferredItem<T> implements ITagagble<Item>, RegiliteHolder<RegiliteItem<T>> {
private final Supplier<String> supplier = () -> get().getDescriptionId();
private final Regilite regilite;
protected Set<TagKey<Item>> ItemTags = new HashSet<>();
Expand Down Expand Up @@ -96,17 +95,11 @@ public RegiliteItem<T> setColorSupplier(Supplier<Supplier<ItemColor>> colorSuppl
return this;
}

public <TCap, TContext> RegiliteItem<T> addCapability(ItemCapability<TCap, TContext> capability, ICapabilityProvider<ItemStack, TContext, TCap> provider) {
public <TCap, TContext> RegiliteItem<T> withCapability(ItemCapability<TCap, TContext> capability, ICapabilityProvider<ItemStack, TContext, TCap> provider) {
attachedCapabilityList.add(new AttachedCapability<>(capability, provider));
return this;
}

// Allows wrapping common holder builder methods into other methods and applying them.
public RegiliteItem<T> apply(Consumer<RegiliteItem<T>> applicator) {
applicator.accept(this);
return this;
}

@ApiStatus.Internal
public void registerCapabilityProviders(RegisterCapabilitiesEvent event) {
for (AttachedCapability<T, ?, ?> capabilityProvider : attachedCapabilityList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.util.function.Supplier;

public class RegiliteMenu<T extends AbstractContainerMenu> extends DeferredHolder<MenuType<? extends AbstractContainerMenu>, MenuType<T>> {
public class RegiliteMenu<T extends AbstractContainerMenu> extends DeferredHolder<MenuType<? extends AbstractContainerMenu>, MenuType<T>> implements RegiliteHolder<RegiliteMenu<T>> {

private Supplier<IScreenConstructor<T, ? extends AbstractContainerScreen<T>>> screenConstructor;

Expand Down

0 comments on commit 82716ac

Please sign in to comment.