Skip to content

Commit

Permalink
Brand new filters system
Browse files Browse the repository at this point in the history
Bro I learned Java reflection at past day and right now I am using it XDDDD
  • Loading branch information
ZiYueCommentary committed Aug 10, 2024
1 parent 910d661 commit be1714e
Show file tree
Hide file tree
Showing 21 changed files with 297 additions and 221 deletions.
11 changes: 0 additions & 11 deletions fabric/src/main/java/org/mtr/mapping/registry/RegistryHelper.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.fabricmc.api.ClientModInitializer;
import ziyue.tjmetro.mod.Registry;
import ziyue.tjmetro.mod.TianjinMetroClient;
import ziyue.tjmetro.mod.client.Filters;

/**
* @since 1.0.0-beta-1
Expand All @@ -13,7 +14,6 @@ public final class MainFabricClient implements ClientModInitializer
@Override
public void onInitializeClient() {
TianjinMetroClient.init();
Registry.FILTERS_REGISTRY_ITEM.forEach(pair -> pair.getFirst().addItems(pair.getSecond().get().data));
Registry.FILTERS_REGISTRY_BLOCK.forEach(pair -> pair.getFirst().addItems(pair.getSecond().get().asItem().data));
Filters.init();
}
}
14 changes: 14 additions & 0 deletions fabric/src/main/java/ziyue/tjmetro/mapping/FilterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.mtr.mapping.holder.ItemStack;
import org.mtr.mapping.holder.MutableText;
import org.mtr.mapping.holder.PressAction;
import org.mtr.mapping.registry.BlockRegistryObject;
import org.mtr.mapping.registry.CreativeModeTabHolder;
import org.mtr.mapping.registry.ItemRegistryObject;
import ziyue.filters.Filter;

import java.util.function.Supplier;
Expand All @@ -29,4 +31,16 @@ static void filtersVisibility(CreativeModeTabHolder creativeModeTab, boolean vis
static void setReservedButton(CreativeModeTabHolder creativeModeTab, MutableText tooltip, PressAction onPress) {
ziyue.filters.FilterBuilder.setReservedButton(creativeModeTab.creativeModeTab, tooltip.data, onPress);
}

static void addBlocks(Filter filter, BlockRegistryObject... blocks) {
for (BlockRegistryObject block : blocks) {
filter.addItems(block.get().asItem().data);
}
}

static void addItems(Filter filter, ItemRegistryObject... items) {
for (ItemRegistryObject item : items) {
filter.addItems(item.get().data);
}
}
}
28 changes: 28 additions & 0 deletions fabric/src/main/java/ziyue/tjmetro/mapping/RegistryHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ziyue.tjmetro.mapping;

import org.mtr.mapping.holder.Identifier;
import org.mtr.mapping.holder.Item;
import org.mtr.mapping.registry.BlockRegistryObject;
import org.mtr.mapping.registry.ItemRegistryObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
* Converting block to item. This may not be stable since it uses reflection.
*
* @author ZiYueCommentary
* @since 1.0.0-beta-2
*/

// This is a very mad way but I have no choice.
public interface RegistryHelper
{
static ItemRegistryObject RegistryObjectBlock2Item(BlockRegistryObject fabric, Identifier forge)
throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
Class<ItemRegistryObject> clazz = ItemRegistryObject.class;
Constructor<ItemRegistryObject> constructor = clazz.getDeclaredConstructor(Item.class);
constructor.setAccessible(true);
return constructor.newInstance(fabric.get().asItem());
}
}
8 changes: 4 additions & 4 deletions fabric/src/main/java/ziyue/tjmetro/mod/BlockEntityTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public interface BlockEntityTypes
BlockEntityTypeRegistryObject<BlockStationNameEntranceTianjin.BlockEntity> STATION_NAME_ENTRANCE_TIANJIN_PINYIN = Registry.registerBlockEntityType("station_name_entrance_tianjin_pinyin", (pos, state) -> new BlockStationNameEntranceTianjin.BlockEntity(true, false, pos, state), BlockList.STATION_NAME_ENTRANCE_TIANJIN_PINYIN::get);
BlockEntityTypeRegistryObject<BlockStationNameEntranceTianjin.BlockEntity> STATION_NAME_ENTRANCE_TIANJIN_BMT = Registry.registerBlockEntityType("station_name_entrance_tianjin_bmt", (pos, state) -> new BlockStationNameEntranceTianjin.BlockEntity(false, true, pos, state), BlockList.STATION_NAME_ENTRANCE_TIANJIN_BMT::get);
BlockEntityTypeRegistryObject<BlockStationNameEntranceTianjin.BlockEntity> STATION_NAME_ENTRANCE_TIANJIN_BMT_PINYIN = Registry.registerBlockEntityType("station_name_entrance_tianjin_bmt_pinyin", (pos, state) -> new BlockStationNameEntranceTianjin.BlockEntity(true, true, pos, state), BlockList.STATION_NAME_ENTRANCE_TIANJIN_BMT_PINYIN::get);
BlockEntityTypeRegistryObject<BlockPSDDoorTianjin.BlockEntity> PSD_DOOR_TIANJIN = Registry.registerBlockEntityType("psd_door_tianjin", BlockPSDDoorTianjin.BlockEntity::new, BlockList.PSD_DOOR_TIANJIN::get);
BlockEntityTypeRegistryObject<BlockPSDDoorTianjin.BlockEntity> PSD_DOOR_TIANJIN = Registry.registerBlockEntityType("psd_door_tianjin", BlockPSDDoorTianjin.BlockEntity::new, BlockList.PSD_DOOR_TIANJIN_BLOCK::get);
BlockEntityTypeRegistryObject<BlockPSDTopTianjin.BlockEntity> PSD_TOP_TIANJIN = Registry.registerBlockEntityType("psd_top_tianjin", BlockPSDTopTianjin.BlockEntity::new, BlockList.PSD_TOP_TIANJIN::get);
BlockEntityTypeRegistryObject<BlockStationNameWallLegacy.BlockEntity> STATION_NAME_WALL_LEGACY = Registry.registerBlockEntityType("station_name_wall_legacy", BlockStationNameWallLegacy.BlockEntity::new, BlockList.STATION_NAME_WALL_LEGACY::get);
BlockEntityTypeRegistryObject<BlockStationNamePlate.BlockEntity> STATION_NAME_PLATE = Registry.registerBlockEntityType("station_name_plate", BlockStationNamePlate.BlockEntity::new, BlockList.STATION_NAME_PLATE::get);
Expand All @@ -66,9 +66,9 @@ public interface BlockEntityTypes
BlockEntityTypeRegistryObject<BlockRailwaySignTianjinBMT.BlockEntity> RAILWAY_SIGN_TIANJIN_BMT_5_EVEN = Registry.registerBlockEntityType("railway_sign_tianjin_bmt_5_even", (pos, state) -> new BlockRailwaySignTianjinBMT.BlockEntity(5, false, pos, state), BlockList.RAILWAY_SIGN_TIANJIN_BMT_5_EVEN::get);
BlockEntityTypeRegistryObject<BlockRailwaySignTianjinBMT.BlockEntity> RAILWAY_SIGN_TIANJIN_BMT_6_EVEN = Registry.registerBlockEntityType("railway_sign_tianjin_bmt_6_even", (pos, state) -> new BlockRailwaySignTianjinBMT.BlockEntity(6, false, pos, state), BlockList.RAILWAY_SIGN_TIANJIN_BMT_6_EVEN::get);
BlockEntityTypeRegistryObject<BlockRailwaySignTianjinBMT.BlockEntity> RAILWAY_SIGN_TIANJIN_BMT_7_EVEN = Registry.registerBlockEntityType("railway_sign_tianjin_bmt_7_even", (pos, state) -> new BlockRailwaySignTianjinBMT.BlockEntity(7, false, pos, state), BlockList.RAILWAY_SIGN_TIANJIN_BMT_7_EVEN::get);
BlockEntityTypeRegistryObject<BlockAPGDoorTianjin.BlockEntity> APG_DOOR_TIANJIN = Registry.registerBlockEntityType("apg_door_tianjin", BlockAPGDoorTianjin.BlockEntity::new, BlockList.APG_DOOR_TIANJIN::get);
BlockEntityTypeRegistryObject<BlockAPGDoorTianjinBMT.BlockEntity> APG_DOOR_TIANJIN_BMT = Registry.registerBlockEntityType("apg_door_tianjin_bmt", BlockAPGDoorTianjinBMT.BlockEntity::new, BlockList.APG_DOOR_TIANJIN_BMT::get);
BlockEntityTypeRegistryObject<BlockAPGGlassTianjinBMT.BlockEntity> APG_GLASS_TIANJIN_BMT = Registry.registerBlockEntityType("apg_glass_tianjin_bmt", BlockAPGGlassTianjinBMT.BlockEntity::new, BlockList.APG_GLASS_TIANJIN_BMT::get);
BlockEntityTypeRegistryObject<BlockAPGDoorTianjin.BlockEntity> APG_DOOR_TIANJIN = Registry.registerBlockEntityType("apg_door_tianjin", BlockAPGDoorTianjin.BlockEntity::new, BlockList.APG_DOOR_TIANJIN_BLOCK::get);
BlockEntityTypeRegistryObject<BlockAPGDoorTianjinBMT.BlockEntity> APG_DOOR_TIANJIN_BMT = Registry.registerBlockEntityType("apg_door_tianjin_bmt", BlockAPGDoorTianjinBMT.BlockEntity::new, BlockList.APG_DOOR_TIANJIN_BMT_BLOCK::get);
BlockEntityTypeRegistryObject<BlockAPGGlassTianjinBMT.BlockEntity> APG_GLASS_TIANJIN_BMT = Registry.registerBlockEntityType("apg_glass_tianjin_bmt", BlockAPGGlassTianjinBMT.BlockEntity::new, BlockList.APG_GLASS_TIANJIN_BMT_BLOCK::get);
BlockEntityTypeRegistryObject<BlockMetalPoleBMT.BlockEntity> METAL_POLE_BMT = Registry.registerBlockEntityType("metal_pole_bmt", BlockMetalPoleBMT.BlockEntity::new, BlockList.METAL_POLE_BMT::get);

static void registerBlockEntities() {
Expand Down
Loading

0 comments on commit be1714e

Please sign in to comment.