33import de .bluecolored .bluemap .api .BlueMapAPI ;
44import de .bluecolored .bluemap .api .BlueMapMap ;
55import de .bluecolored .bluemap .api .BlueMapWorld ;
6- import de .bluecolored .bluemap .api .gson .MarkerGson ;
76import de .bluecolored .bluemap .api .markers .MarkerSet ;
87import de .bluecolored .bluemap .api .markers .POIMarker ;
98import dev .nincodedo .bluemapbanners .BlueMapBanners ;
10- import net .fabricmc .loader .api .FabricLoader ;
9+ import dev .nincodedo .bluemapbanners .storage .JdbcStorage ;
10+ import dev .nincodedo .bluemapbanners .storage .JsonStorage ;
11+ import dev .nincodedo .bluemapbanners .storage .Storage ;
1112import net .minecraft .core .Direction ;
1213import net .minecraft .core .component .DataComponents ;
1314import net .minecraft .network .chat .Component ;
1617import net .minecraft .world .level .block .entity .BannerBlockEntity ;
1718import net .minecraft .world .level .block .state .BlockState ;
1819import net .minecraft .world .phys .Vec3 ;
19- import java .io .FileNotFoundException ;
20- import java .io .FileReader ;
21- import java .io .FileWriter ;
22- import java .io .IOException ;
20+
2321import java .util .Objects ;
2422
2523public class MarkerManager {
2624
2725 private final String bannerMarkerSetId = "bluemap-banners" ;
2826 private static MarkerManager markerManager ;
27+ private Storage storage ;
2928
3029 public MarkerManager () {
3130 markerManager = this ;
@@ -36,47 +35,27 @@ public static MarkerManager getInstance() {
3635 }
3736
3837 public void loadMarkerSets () {
39- BlueMapAPI .getInstance ().ifPresent (api -> {
40- for (BlueMapWorld world : api .getWorlds ()) {
41- // Not the nicest with the names
42- String fileName = FabricLoader .getInstance ().getConfigDir ().resolve (String .format ("bluemap-banners/maps/%s.json" , worldToString (world ))).toString ();
43- try (FileReader reader = new FileReader (fileName )) {
44- MarkerSet markerSet = MarkerGson .INSTANCE .fromJson (reader , MarkerSet .class );
45- world .getMaps ().forEach (map -> map .getMarkerSets ().put (bannerMarkerSetId , markerSet ));
46- } catch (FileNotFoundException ex ) {
47- MarkerSet markerSet = MarkerSet .builder ().label ("BlueMap Banners" ).defaultHidden (false ).toggleable (true ).build ();
48- world .getMaps ().forEach (map -> map .getMarkerSets ().put (bannerMarkerSetId , markerSet ));
49- } catch (IOException ex ) {
50- BlueMapBanners .LOGGER .error (ex .getMessage (), ex );
51- }
38+ switch (ConfigManager .getInstance ().getConfig (Config .STORAGE_TYPE )) {
39+ case "JDBC" -> storage = new JdbcStorage ();
40+ case "JSON" -> storage = new JsonStorage ();
41+ default -> {
42+ storage = new JsonStorage ();
43+ BlueMapBanners .LOGGER .error ("Invalid storage type specified: {}" , ConfigManager .getInstance ().getConfig (Config .STORAGE_TYPE ));
44+ BlueMapBanners .LOGGER .warn ("Using default storage type: JSON" );
5245 }
53- });
54- }
46+ };
5547
56- public void saveMarkerSet (BlueMapWorld world ) {
57- String fileName = FabricLoader .getInstance ().getConfigDir ().resolve (String .format ("bluemap-banners/maps/%s.json" , worldToString (world ))).toString ();
58- BlueMapMap map = world .getMaps ().iterator ().next ();
59- map .getMarkerSets ().forEach ((id , markerSet ) -> {
60- if (id != null && id .equals (bannerMarkerSetId )) {
61- try (FileWriter writer = new FileWriter (fileName )) {
62- MarkerGson .INSTANCE .toJson (markerSet , writer );
63- } catch (IOException ex ) {
64- BlueMapBanners .LOGGER .error (ex .getMessage (), ex );
65- }
48+ BlueMapAPI .getInstance ().ifPresent (api -> {
49+ for (BlueMapWorld world : api .getWorlds ()) {
50+ world .getMaps ().forEach (map -> map .getMarkerSets ().put (bannerMarkerSetId , storage .getGeneratedMarkerSet (world .getId ())));
6651 }
6752 });
6853 }
6954
7055 public boolean doesMarkerExist (BannerBlockEntity bannerBlockEntity ) {
7156 BlueMapAPI api = BlueMapAPI .getInstance ().orElseThrow ();
7257 BlueMapWorld world = api .getWorld (bannerBlockEntity .getLevel ()).orElseThrow ();
73- for (BlueMapMap map : world .getMaps ()) {
74- MarkerSet set = map .getMarkerSets ().get (bannerMarkerSetId );
75- if (set .get (bannerBlockEntity .getBlockPos ().toShortString ()) != null ) {
76- return true ;
77- }
78- }
79- return false ;
58+ return storage .existsMarker (world .getId (), bannerBlockEntity .getBlockPos ().toShortString ());
8059 }
8160
8261 public void removeMarker (BannerBlockEntity bannerBlockEntity ) {
@@ -86,7 +65,7 @@ public void removeMarker(BannerBlockEntity bannerBlockEntity) {
8665 MarkerSet set = map .getMarkerSets ().get (bannerMarkerSetId );
8766 set .remove (bannerBlockEntity .getBlockPos ().toShortString ());
8867 }
89- saveMarkerSet (world );
68+ storage . removeMarker (world . getId (), bannerBlockEntity . getBlockPos (). toShortString () );
9069 }
9170
9271 public String getMarkerName (BlockState blockState , BannerBlockEntity bannerBlockEntity ) {
@@ -108,16 +87,13 @@ public Vec3 getMarkerOffset(BlockState blockState) {
10887 Vec3 offset = Vec3 .ZERO ;
10988 if (blockState .getBlock () instanceof WallBannerBlock ) {
11089 Direction facing = blockState .getValue (WallBannerBlock .FACING );
111- switch (facing ) {
112- case Direction .NORTH :
113- offset .add (0 , 0 , -0.5 );
114- case Direction .EAST :
115- offset .add (0.5 , 0 , 0 );
116- case Direction .SOUTH :
117- offset .add (0 , 0 , 0.5 );
118- case Direction .WEST :
119- offset .add (-0.5 , 0 , 0 );
120- }
90+ offset = switch (facing ) {
91+ case Direction .NORTH -> offset .add (0 , 0 , 0.5 );
92+ case Direction .EAST -> offset .add (-0.5 , 0 , 0 );
93+ case Direction .SOUTH -> offset .add (0 , 0 , -0.5 );
94+ case Direction .WEST -> offset .add (0.5 , 0 , 0 );
95+ default -> offset ;
96+ };
12197 }
12298 return offset ;
12399 }
@@ -127,23 +103,29 @@ public void addMarker(BlockState blockState, BannerBlockEntity bannerBlockEntity
127103 BlueMapWorld world = api .getWorld (bannerBlockEntity .getLevel ()).orElseThrow ();
128104
129105 Vec3 offset = getMarkerOffset (blockState );
106+ String text = getMarkerName (blockState , bannerBlockEntity );
107+ Vec3 pos = bannerBlockEntity .getBlockPos ().getCenter ();
108+ int markerMaxViewDistance = ConfigManager .getInstance ().getIntConfig (Config .MARKER_MAX_VIEW_DISTANCE );
130109
131110 for (BlueMapMap map : world .getMaps ()) {
132111 MarkerSet set = map .getMarkerSets ().get (bannerMarkerSetId );
133- Vec3 pos = bannerBlockEntity .getBlockPos ().getCenter ();
134112 var iconAddress = map .getAssetStorage ().getAssetUrl (bannerBlockEntity .getBaseColor ().name ().toLowerCase () + ".png" );
135- int markerMaxViewDistance = ConfigManager .getInstance ().getIntConfig (Config .MARKER_MAX_VIEW_DISTANCE );
113+
114+ pos = pos .add (
115+ offset .x ,
116+ (Objects .equals (offset , Vec3 .ZERO ) ? -0.5 : 0.5 ),
117+ offset .z
118+ );
119+
136120 POIMarker bannerMarker = POIMarker .builder ()
137- .label (getMarkerName (blockState , bannerBlockEntity ))
121+ .label (text )
122+ .detail (text )
138123 .position (pos .x (), pos .y (), pos .z ())
139- .icon (iconAddress , 12 , 32 )
124+ .icon (iconAddress , 12 , Objects . equals ( offset , Vec3 . ZERO ) ? 32 : 0 )
140125 .maxDistance (markerMaxViewDistance ).build ();
141126 set .put (bannerBlockEntity .getBlockPos ().toShortString (), bannerMarker );
142127 }
143- saveMarkerSet (world );
144- }
145128
146- private String worldToString (BlueMapWorld world ) {
147- return world .getId ().replace ("#minecraft:" , "_" ).replace ("_overworld" , "" );
129+ storage .addMarker (world .getId (), bannerBlockEntity .getBlockPos ().toShortString (), offset , player .getUUID (), "" , bannerBlockEntity .getBaseColor ().name ().toLowerCase () + ".png" , text );
148130 }
149131}
0 commit comments