Skip to content

Commit

Permalink
final breaking change, move the managing into the manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts committed Jul 15, 2024
1 parent 2581238 commit 8c43891
Show file tree
Hide file tree
Showing 23 changed files with 255 additions and 236 deletions.
17 changes: 1 addition & 16 deletions src/main/java/com/gtnewhorizons/navigator/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import com.gtnewhorizons.navigator.api.NavigatorApi;
import com.gtnewhorizons.navigator.config.GeneralConfig;
import com.gtnewhorizons.navigator.impl.DirtyChunkButtonManager;
import com.gtnewhorizons.navigator.impl.DirtyChunkLayerManager;
import com.gtnewhorizons.navigator.impl.journeymap.JMDirtyChunkRenderer;
import com.gtnewhorizons.navigator.impl.journeymap.JMDirtyChunkWaypointManager;
import com.gtnewhorizons.navigator.impl.xaero.XaeroDirtyChunkRenderer;
import com.gtnewhorizons.navigator.impl.xaero.XaeroDirtyChunkWaypointManager;

import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.common.event.FMLInitializationEvent;
Expand All @@ -21,17 +16,7 @@ public void preInit(FMLPreInitializationEvent event) {
super.preInit(event);
ClientRegistry.registerKeyBinding(NavigatorApi.ACTION_KEY);
if (GeneralConfig.enableDebugLayers) {
// Shared
NavigatorApi.registerLayerManager(DirtyChunkLayerManager.instance);
NavigatorApi.registerButtonManager(DirtyChunkButtonManager.instance);

// Journeymap
NavigatorApi.registerLayerRenderer(JMDirtyChunkRenderer.instance);
NavigatorApi.registerWaypointManager(JMDirtyChunkWaypointManager.instance);

// Xaero's maps
NavigatorApi.registerLayerRenderer(XaeroDirtyChunkRenderer.instance);
NavigatorApi.registerWaypointManager(XaeroDirtyChunkWaypointManager.instance);
NavigatorApi.registerLayerManager(DirtyChunkLayerManager.INSTANCE);
}
}

Expand Down
84 changes: 30 additions & 54 deletions src/main/java/com/gtnewhorizons/navigator/api/NavigatorApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
import org.lwjgl.input.Keyboard;

import com.gtnewhorizons.navigator.Navigator;
import com.gtnewhorizons.navigator.api.journeymap.render.JMLayerRenderer;
import com.gtnewhorizons.navigator.api.journeymap.waypoints.JMWaypointManager;
import com.gtnewhorizons.navigator.api.model.SupportedMods;
import com.gtnewhorizons.navigator.api.model.buttons.ButtonManager;
import com.gtnewhorizons.navigator.api.model.layers.InteractableLayerManager;
import com.gtnewhorizons.navigator.api.model.layers.LayerManager;
import com.gtnewhorizons.navigator.api.model.layers.LayerRenderer;
import com.gtnewhorizons.navigator.api.model.waypoints.WaypointManager;
import com.gtnewhorizons.navigator.api.util.Util;
import com.gtnewhorizons.navigator.api.xaero.renderers.XaeroLayerRenderer;
import com.gtnewhorizons.navigator.api.xaero.waypoints.XaeroWaypointManager;
import com.gtnewhorizons.navigator.mixins.late.journeymap.FullscreenAccessor;

import journeymap.client.render.map.GridRenderer;
Expand All @@ -33,54 +29,48 @@ public final class NavigatorApi {
Keyboard.KEY_DELETE,
Navigator.MODNAME);

public static final List<ButtonManager> buttonManagers = new ArrayList<>();
public static final List<LayerManager> layerManagers = new ArrayList<>();
public static final List<LayerRenderer> layerRenderers = new ArrayList<>();
public static final List<WaypointManager> waypointManagers = new ArrayList<>();

/**
* @param buttonManager The {@link ButtonManager} to register
* Only one needs to be registered per layer regardless of how many mods are supported
*/
public static void registerButtonManager(ButtonManager buttonManager) {
buttonManagers.add(buttonManager);
}

/**
* @param layerManager The {@link LayerManager} to register.
* Only one needs to be registered per layer regardless of how many mods are supported
*/
public static void registerLayerManager(LayerManager layerManager) {
layerManagers.add(layerManager);
}

/**
* @param layerRenderer The LayerRenderer to register.
* <p>
* Should be an instance of {@link JMLayerRenderer} or {@link XaeroLayerRenderer}
* Both mods can be registered at the same time and will be handled accordingly
*/
public static void registerLayerRenderer(LayerRenderer layerRenderer) {
layerRenderers.add(layerRenderer);
public static List<LayerRenderer> getActiveRenderersFor(SupportedMods mod) {
return layerManagers.stream()
.filter(LayerManager::isLayerActive)
.map(layerManager -> layerManager.getLayerRenderer(mod))
.collect(Collectors.toList());
}

/**
* @param waypointManager The {@link WaypointManager} to register.
* <p>
* Should be an instance of {@link JMWaypointManager} or {@link XaeroWaypointManager}
* Both mods can be registered at the same time and will be handled accordingly
*/
public static void registerWaypointManager(WaypointManager waypointManager) {
waypointManagers.add(waypointManager);
public static List<LayerManager> getEnabledLayers(SupportedMods mod) {
return layerManagers.stream()
.filter(layerManager -> layerManager.isEnabled(mod))
.collect(Collectors.toList());
}

public static @Nullable LayerRenderer getActiveLayerFor(SupportedMods mod) {
return layerRenderers.stream()
.filter(
layerRenderer -> layerRenderer.isLayerActive() && layerRenderer.getLayerMod()
.equals(mod))
.findFirst()
.orElse(null);
public static List<ButtonManager> getEnabledButtons(SupportedMods mod) {
return layerManagers.stream()
.filter(layerManager -> layerManager.isEnabled(mod))
.map(LayerManager::getButtonManager)
.distinct()
.collect(Collectors.toList());
}

public static List<ButtonManager> getDistinctButtons() {
return layerManagers.stream()
.map(LayerManager::getButtonManager)
.distinct()
.collect(Collectors.toList());
}

public static List<InteractableLayerManager> getInteractableLayers() {
return layerManagers.stream()
.filter(layerManager -> layerManager instanceof InteractableLayerManager)
.map(layerManager -> (InteractableLayerManager) layerManager)
.collect(Collectors.toList());
}

public void openJourneyMapAt(@Nullable LayerManager layer, int blockX, int blockZ, int zoom) {
Expand All @@ -96,18 +86,4 @@ public void openJourneyMapAt(@Nullable LayerManager layer, int blockX, int block
public void openJourneyMapAt(@Nullable LayerManager layer, int blockX, int blockZ) {
this.openJourneyMapAt(layer, blockX, blockZ, -1);
}

public static List<JMLayerRenderer> getJourneyMapLayerRenderers() {
return layerRenderers.stream()
.filter(JMLayerRenderer.class::isInstance)
.map(JMLayerRenderer.class::cast)
.collect(Collectors.toList());
}

public static List<XaeroLayerRenderer> getXaeroLayerRenderers() {
return layerRenderers.stream()
.filter(XaeroLayerRenderer.class::isInstance)
.map(XaeroLayerRenderer.class::cast)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import com.gtnewhorizons.navigator.api.NavigatorApi;
import com.gtnewhorizons.navigator.api.journeymap.drawsteps.JMInteractableStep;
import com.gtnewhorizons.navigator.api.journeymap.drawsteps.JMRenderStep;
import com.gtnewhorizons.navigator.api.model.layers.WaypointProviderManager;
import com.gtnewhorizons.navigator.api.model.layers.InteractableLayerManager;
import com.gtnewhorizons.navigator.api.model.steps.RenderStep;
import com.gtnewhorizons.navigator.api.util.Util;

public abstract class JMInteractableLayerRenderer extends JMLayerRenderer {

protected final WaypointProviderManager manager;
protected InteractableLayerManager manager;
protected JMInteractableStep hoveredDrawStep = null;

public JMInteractableLayerRenderer(WaypointProviderManager manager) {
public JMInteractableLayerRenderer(InteractableLayerManager manager) {
super(manager);
this.manager = manager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.awt.Color;

import com.gtnewhorizons.navigator.api.model.SupportedMods;
import com.gtnewhorizons.navigator.api.model.layers.WaypointProviderManager;
import com.gtnewhorizons.navigator.api.model.layers.InteractableLayerManager;
import com.gtnewhorizons.navigator.api.model.waypoints.Waypoint;
import com.gtnewhorizons.navigator.api.model.waypoints.WaypointManager;

public abstract class JMWaypointManager extends WaypointManager {
public class JMWaypointManager extends WaypointManager {

private journeymap.client.model.Waypoint jmWaypoint;

public JMWaypointManager(WaypointProviderManager layerManager) {
public JMWaypointManager(InteractableLayerManager layerManager) {
super(layerManager, SupportedMods.JourneyMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public boolean isActive() {
}

public void activate() {
NavigatorApi.buttonManagers.forEach(ButtonManager::deactivate);
NavigatorApi.getDistinctButtons()
.forEach(ButtonManager::deactivate);
isActive = true;
if (onToggle != null) {
onToggle.accept(true);
Expand All @@ -51,13 +52,4 @@ public void toggle() {
activate();
}
}

/**
* Whether the button should be added to the GUI.
*
* @return true if the button should be added, false otherwise
*/
public boolean isEnabled(SupportedMods mod) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,51 @@
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import com.gtnewhorizons.navigator.api.model.SupportedMods;
import com.gtnewhorizons.navigator.api.model.buttons.ButtonManager;
import com.gtnewhorizons.navigator.api.model.locations.IWaypointAndLocationProvider;
import com.gtnewhorizons.navigator.api.model.waypoints.Waypoint;
import com.gtnewhorizons.navigator.api.model.waypoints.WaypointManager;

public abstract class WaypointProviderManager extends LayerManager {
public abstract class InteractableLayerManager extends LayerManager {

private List<? extends IWaypointAndLocationProvider> visibleElements = new ArrayList<>();
private final Map<SupportedMods, WaypointManager> waypointManagers = new EnumMap<>(SupportedMods.class);

protected Waypoint activeWaypoint = null;

public WaypointProviderManager(ButtonManager buttonManager) {
public InteractableLayerManager(ButtonManager buttonManager) {
super(buttonManager);
for (SupportedMods mod : SupportedMods.values()) {
WaypointManager waypointManager = addWaypointManager(this, mod);
if (waypointManager != null) {
waypointManagers.put(mod, waypointManager);
}
}
}

/**
* @param manager This layer manager
* @param mod The mod to add the layer renderer for
* @return The layer renderer implementation for the mod or null if none
*/
protected abstract @Nullable LayerRenderer addLayerRenderer(InteractableLayerManager manager, SupportedMods mod);

/**
* @param manager This layer manager
* @param mod The mod to add the waypoint manager for
* @return The waypoint manager implementation for the mod or null if none
*/
protected @Nullable WaypointManager addWaypointManager(InteractableLayerManager manager, SupportedMods mod) {
return null;
}

@Nullable
@Override
protected final LayerRenderer addLayerRenderer(LayerManager manager, SupportedMods mod) {
return addLayerRenderer(this, mod);
}

public void setActiveWaypoint(Waypoint waypoint) {
Expand All @@ -40,12 +70,7 @@ public boolean hasActiveWaypoint() {
return activeWaypoint != null;
}

public void registerWaypointManager(SupportedMods mod, WaypointManager waypointManager) {
if (!mod.isEnabled()) return;
waypointManagers.put(mod, waypointManager);
}

public WaypointManager getWaypointManager(SupportedMods map) {
public @Nullable WaypointManager getWaypointManager(SupportedMods map) {
return waypointManagers.get(map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import com.gtnewhorizons.navigator.api.model.SupportedMods;
import com.gtnewhorizons.navigator.api.model.buttons.ButtonManager;
import com.gtnewhorizons.navigator.api.model.locations.ILocationProvider;
Expand All @@ -15,7 +17,7 @@ public abstract class LayerManager {

protected boolean forceRefresh = false;
private List<? extends ILocationProvider> visibleElements = new ArrayList<>();
protected Map<SupportedMods, LayerRenderer> layerRenderer = new EnumMap<>(SupportedMods.class);
protected final Map<SupportedMods, LayerRenderer> layerRenderer = new EnumMap<>(SupportedMods.class);
private int miniMapWidth = 0;
private int miniMapHeight = 0;
private int fullscreenMapWidth = 0;
Expand All @@ -24,8 +26,25 @@ public abstract class LayerManager {

public LayerManager(ButtonManager buttonManager) {
this.buttonManager = buttonManager;
for (SupportedMods mod : SupportedMods.values()) {
if (!mod.isEnabled() || !isEnabled(mod)) continue;

LayerRenderer renderer = addLayerRenderer(this, mod);
if (renderer == null) continue;
layerRenderer.put(mod, renderer);
}
}

/**
* @param manager This layer manager
* @param mod The mod to add the layer renderer for
* @return The layer renderer implementation for the mod or null if none
*/
protected abstract @Nullable LayerRenderer addLayerRenderer(LayerManager manager, SupportedMods mod);

protected abstract List<? extends ILocationProvider> generateVisibleElements(int minBlockX, int minBlockZ,
int maxBlockX, int maxBlockZ);

public boolean isLayerActive() {
return buttonManager.isActive();
}
Expand Down Expand Up @@ -64,9 +83,6 @@ public final SupportedMods getOpenModGui() {
return openModGui;
}

protected abstract List<? extends ILocationProvider> generateVisibleElements(int minBlockX, int minBlockZ,
int maxBlockX, int maxBlockZ);

protected boolean needsRegenerateVisibleElements(int minBlockX, int minBlockZ, int maxBlockX, int maxBlockZ) {
return true;
}
Expand Down Expand Up @@ -108,12 +124,21 @@ protected void checkAndUpdateElements(int minBlockX, int minBlockZ, int maxBlock
}
}

public void registerLayerRenderer(SupportedMods mod, LayerRenderer renderer) {
if (!mod.isEnabled()) return;
layerRenderer.put(mod, renderer);
public ButtonManager getButtonManager() {
return buttonManager;
}

public LayerRenderer getLayerRenderer(SupportedMods map) {
return layerRenderer.get(map);
}

/**
* Whether the layer is enabled for the corresponding mod.
*
* @param mod the mod checking if it is enabled
* @return true if there is a layer implementation for the mod, false otherwise
*/
public boolean isEnabled(SupportedMods mod) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public abstract class LayerRenderer {
protected List<? extends RenderStep> renderSteps = new ArrayList<>();

public LayerRenderer(LayerManager manager, SupportedMods mod) {
manager.registerLayerRenderer(mod, this);
this.mod = mod;
this.manager = manager;
}
Expand All @@ -25,10 +24,6 @@ public LayerRenderer(LayerManager manager, SupportedMods mod) {
protected abstract List<? extends RenderStep> generateRenderSteps(
List<? extends ILocationProvider> visibleElements);

public final boolean isLayerActive() {
return manager.isLayerActive() && mod.isEnabled();
}

public final SupportedMods getLayerMod() {
return mod;
}
Expand Down
Loading

0 comments on commit 8c43891

Please sign in to comment.