Skip to content

Commit

Permalink
Re-work Hopper features, incoming/outgoing filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuuta0217 committed Jun 7, 2024
1 parent ded0c65 commit 7fef9f0
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 118 deletions.
29 changes: 29 additions & 0 deletions src/main/java/net/unknown/launchwrapper/hopper/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,46 @@

package net.unknown.launchwrapper.hopper;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

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

public interface Filter {
static Filter fromTag(CompoundTag filterData, HolderLookup.Provider registryLookup) {
if (filterData.contains("id")) {
ResourceLocation id = ResourceLocation.tryParse(filterData.getString("id"));
Optional<Item> item = BuiltInRegistries.ITEM.getOptional(id);
if (item.isPresent()) {
DataComponentPatch componentPatch = filterData.contains("components") ? DataComponentPatch.CODEC.parse(registryLookup.createSerializationContext(NbtOps.INSTANCE), filterData.get("components")).getOrThrow() : null;
return new ItemFilter(item.get(), componentPatch);
}
}

if (filterData.contains("tag")) {
TagKey<Item> itemTag = TagKey.create(Registries.ITEM, new ResourceLocation(filterData.getString("tag")));
DataComponentPatch componentPatch = filterData.contains("nbt") ? DataComponentPatch.CODEC.parse(registryLookup.createSerializationContext(NbtOps.INSTANCE), filterData.get("components")).getOrThrow() : null;
return new TagFilter(itemTag, componentPatch);
}

return null;
}

@Nullable
DataComponentPatch getDataPatch();

boolean matches(@Nullable ItemStack stack, TransportType transportType);

TransportType getTransportType();

CompoundTag toTag(HolderLookup.Provider registryLookup);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,75 @@
import java.util.Set;

public interface IMixinHopperBlockEntity extends Hopper {
Set<Filter> getFilters();
@Deprecated
default Set<Filter> getFilters() {
return this.getIncomingFilters();
}

void setFilters(Set<Filter> filters);
Set<Filter> getIncomingFilters();

void addFilter(Filter filter);
@Deprecated
default void setFilters(Set<Filter> filters) {
this.setIncomingFilters(filters);
}

FilterType getFilterMode();
void setIncomingFilters(Set<Filter> incomingFilters);

void setFilterMode(FilterType filterMode);
@Deprecated
default void addFilter(Filter filter) {
this.addIncomingFilter(filter);
}

boolean isFilterEnabled();
void addIncomingFilter(Filter filter);

@Deprecated
default FilterType getFilterMode() {
return this.getIncomingFilterMode();
}

FilterType getIncomingFilterMode();

@Deprecated
default void setFilterMode(FilterType filterMode) {
this.setIncomingFilterMode(filterMode);
}

void setIncomingFilterMode(FilterType incomingFilterMode);

@Deprecated
default boolean isFilterEnabled() {
return this.isIncomingFilterEnabled();
}

boolean isIncomingFilterEnabled();

Set<Filter> getOutgoingFilters();

void setOutgoingFilters(Set<Filter> outgoingFilters);

void addOutgoingFilter(Filter filter);

FilterType getOutgoingFilterMode();

void setOutgoingFilterMode(FilterType outgoingFilterMode);

boolean isOutgoingFilterEnabled();

boolean isEnabledFindItem();

boolean isEnabledPullItem();

boolean isEnabledPushItem();

void setEnabledFindItem(boolean enabled);

void setEnabledPullItem(boolean enabled);

void setEnabledPushItem(boolean enabled);

AABB getItemFindAABB(double baseX, double baseY, double baseZ);

void setItemFindAABB(double aX, double aY, double aZ, double bX, double bY, double bZ);

boolean isDebugMode();
boolean isEventDisabled();
}
13 changes: 13 additions & 0 deletions src/main/java/net/unknown/launchwrapper/hopper/ItemFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@

package net.unknown.launchwrapper.hopper;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -81,4 +84,14 @@ public boolean matches(@Nullable ItemStack stack, @Nonnull TransportType transpo
public TransportType getTransportType() {
return this.transportType;
}

@Override
public CompoundTag toTag(HolderLookup.Provider registryLookup) {
CompoundTag tag = new CompoundTag();
tag.putString("id", BuiltInRegistries.ITEM.getKey(this.getItem()).toString());
if (this.getDataPatch() != null) {
tag.put("components", DataComponentPatch.CODEC.encodeStart(registryLookup.createSerializationContext(NbtOps.INSTANCE), this.getDataPatch()).getOrThrow());
}
return tag;
}
}
13 changes: 13 additions & 0 deletions src/main/java/net/unknown/launchwrapper/hopper/TagFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@

package net.unknown.launchwrapper.hopper;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -82,4 +85,14 @@ public boolean matches(@Nullable ItemStack stack, @Nonnull TransportType transpo
public TransportType getTransportType() {
return this.transportType;
}

@Override
public CompoundTag toTag(HolderLookup.Provider registryLookup) {
CompoundTag tag = new CompoundTag();
tag.putString("tag", this.getTag().location().toString());
if (this.getDataPatch() != null) {
tag.put("components", DataComponentPatch.CODEC.encodeStart(registryLookup.createSerializationContext(NbtOps.INSTANCE), this.getDataPatch()).getOrThrow());
}
return tag;
}
}
20 changes: 17 additions & 3 deletions src/main/java/net/unknown/launchwrapper/hopper/TransportType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@
package net.unknown.launchwrapper.hopper;

public enum TransportType {
PULL_FROM_DROPPED_ITEM,
PULL_FROM_CONTAINER,
PUSH_TO_CONTAINER
PULL_FROM_DROPPED_ITEM(false),
PULL_FROM_CONTAINER(false),
PUSH_TO_CONTAINER(true);

private final boolean outgoing;

TransportType(boolean outgoing) {
this.outgoing = outgoing;
}

public boolean isOutgoing() {
return this.outgoing;
}

public boolean isIncoming() {
return !this.outgoing;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@

import net.minecraft.ChatFormatting;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.Style;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -66,16 +62,22 @@ public MixinHopperBlock(Properties settings) {
public List<ItemStack> getDrops(BlockState state, LootParams.Builder builder) {
BlockEntity blockEntity = builder.getOptionalParameter(LootContextParams.BLOCK_ENTITY);
if (blockEntity instanceof IMixinHopperBlockEntity hopper) {
if (hopper.getFilterMode() != null) {
if (hopper.getFilters().size() > 0) {
if (hopper.getIncomingFilterMode() != null) {
if (!hopper.getIncomingFilters().isEmpty() || !hopper.getOutgoingFilters().isEmpty()) {
List<ItemStack> drops = super.getDrops(state, builder);
drops.forEach(stack -> {
if (stack.getItem() == state.getBlock().asItem()) {
stack.set(DataComponents.BLOCK_ENTITY_DATA, CustomData.of(((BlockEntity) hopper).saveWithoutMetadata(MinecraftServer.getDefaultRegistryAccess())));

List<Component> styledLore = new ArrayList<>() {{
add(Component.literal("フィルターモード: " + hopper.getFilterMode().getLocalizedName()).withStyle(Style.EMPTY.withItalic(false).withColor(ChatFormatting.AQUA)));
add(Component.literal("登録フィルター数: " + hopper.getFilters().size()).withStyle(Style.EMPTY.withItalic(false).withColor(ChatFormatting.AQUA)));
if (!hopper.getIncomingFilters().isEmpty()) {
add(Component.literal("搬入フィルターモード: " + hopper.getIncomingFilterMode().getLocalizedName()).withStyle(Style.EMPTY.withItalic(false).withColor(ChatFormatting.AQUA)));
add(Component.literal("搬入フィルター登録数: " + hopper.getIncomingFilters().size()).withStyle(Style.EMPTY.withItalic(false).withColor(ChatFormatting.AQUA)));
}
if (!hopper.getOutgoingFilters().isEmpty()) {
add(Component.literal("搬出フィルターモード: " + hopper.getOutgoingFilterMode().getLocalizedName()).withStyle(Style.EMPTY.withItalic(false).withColor(ChatFormatting.AQUA)));
add(Component.literal("搬出フィルター登録数: " + hopper.getOutgoingFilters().size()).withStyle(Style.EMPTY.withItalic(false).withColor(ChatFormatting.AQUA)));
}
}};

ItemLore lore = new ItemLore(ComponentUtil.stripStyles(styledLore), styledLore);
Expand Down
Loading

0 comments on commit 7fef9f0

Please sign in to comment.