Source:
<code>private void registerAlwaysInjectedValues() { globalInjectedValues.injectValue(Key.of(Region.class, Selection.class), context -> { LocalSession localSession = context.injectedValue(Key.of(LocalSession.class)) .orElseThrow(() -> new IllegalStateException("No LocalSession")); return context.injectedValue(Key.of(Actor.class)) .map(actor -> { try { <START> World world; <END> if (localSession.hasWorldOverride()) { world = localSession.getWorldOverride(); } else if (actor.isPlayer() && actor instanceof Player) { world = ((Player) actor).getWorld(); } else { throw new MissingWorldException(); } return localSession.getSelection(world); } catch (MissingWorldException | IncompleteRegionException e) { exceptionConverter.convert(e); throw new AssertionError("Should have thrown a new exception."); } }); }); globalInjectedValues.injectValue(Key.of(EditSession.class), context -> { LocalSession localSession = context.injectedValue(Key.of(LocalSession.class)) .orElseThrow(() -> new IllegalStateException("No LocalSession")); return context.injectedValue(Key.of(Actor.class)) .map(player -> { EditSession editSession = localSession.createEditSession(player); editSession.enableStandardMode(); return editSession; }); }); globalInjectedValues.injectValue(Key.of(World.class), context -> { LocalSession localSession = context.injectedValue(Key.of(LocalSession.class)) .orElseThrow(() -> new IllegalStateException("No LocalSession")); return context.injectedValue(Key.of(Actor.class)) .map(actor -> { try { if (localSession.hasWorldOverride()) { return localSession.getWorldOverride(); } else if (actor.isPlayer() && actor instanceof Player) { return ((Player) actor).getWorld(); } else { throw new MissingWorldException(); } } catch (MissingWorldException e) { exceptionConverter.convert(e); throw new AssertionError("Should have thrown a new exception."); } }); }); }</code><technical_language>This re-use world resolved below, context.injectedValue(Key.of(World.class)). duplicate code</technical_language>

Target:
private void registerAlwaysInjectedValues() { globalInjectedValues.injectValue(Key.of(Region.class, Selection.class), context -> { LocalSession localSession = context.injectedValue(Key.of(LocalSession.class)) .orElseThrow(() -> new IllegalStateException("No LocalSession")); return context.injectedValue(Key.of(World.class)) .map(world -> { try { return localSession.getSelection(world); } catch (IncompleteRegionException e) { exceptionConverter.convert(e); throw new AssertionError("Should have thrown a new exception.", e); } }); }); globalInjectedValues.injectValue(Key.of(EditSession.class), context -> { LocalSession localSession = context.injectedValue(Key.of(LocalSession.class)) .orElseThrow(() -> new IllegalStateException("No LocalSession")); return context.injectedValue(Key.of(Actor.class)) .map(actor -> { EditSession editSession = localSession.createEditSession(actor); editSession.enableStandardMode(); return editSession; }); }); globalInjectedValues.injectValue(Key.of(World.class), context -> { LocalSession localSession = context.injectedValue(Key.of(LocalSession.class)) .orElseThrow(() -> new IllegalStateException("No LocalSession")); return context.injectedValue(Key.of(Actor.class)) .map(actor -> { try { if (localSession.hasWorldOverride()) { return localSession.getWorldOverride(); } else if (actor instanceof Locatable && ((Locatable) actor).getExtent() instanceof World) { return (World) ((Locatable) actor).getExtent(); } else { throw new MissingWorldException(); } } catch (MissingWorldException e) { exceptionConverter.convert(e); throw new AssertionError("Should have thrown a new exception.", e); } }); }); }