Skip to content

Commit

Permalink
20.6: Change TravelTargetApi over to ServiceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Rover656 committed May 10, 2024
1 parent fa21fa5 commit 63631e7
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 107 deletions.
1 change: 1 addition & 0 deletions src/api/java/com/enderio/api/conduit/ConduitApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public interface ConduitApi {

ConduitApi INSTANCE = ServiceLoader.load(ConduitApi.class).findFirst().orElseThrow();

/**
* This will create a Conduit Item. If, during porting, EIOConduits isn't available, a dummy item will be returned.
*/
Expand Down
72 changes: 0 additions & 72 deletions src/api/java/com/enderio/api/travel/TravelTargetAPI.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/api/java/com/enderio/api/travel/TravelTargetApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.enderio.api.travel;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;

import java.util.Collection;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.stream.Stream;

public interface TravelTargetApi {

TravelTargetApi INSTANCE = ServiceLoader.load(TravelTargetApi.class).findFirst().orElseThrow();

Optional<TravelTarget> get(Level level, BlockPos pos);
<T extends TravelTarget> void set(Level level, T travelTarget);
void removeAt(Level level, BlockPos pos);
Collection<TravelTarget> getAll(Level level);
Stream<TravelTarget> getInItemRange(Level level, BlockPos center);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.enderio.machines.common.block;

import com.enderio.api.travel.TravelTargetAPI;
import com.enderio.api.travel.TravelTargetApi;
import com.enderio.machines.common.blockentity.TravelAnchorBlockEntity;
import com.enderio.machines.common.blockentity.base.MachineBlockEntity;
import com.enderio.machines.common.init.MachineBlockEntities;
Expand Down Expand Up @@ -30,7 +30,7 @@ public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
@Override
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean movedByPiston) {
if (level.getBlockEntity(pos) instanceof TravelAnchorBlockEntity anchorBlock) {
TravelTargetAPI.removeAt(level, pos);
TravelTargetApi.INSTANCE.removeAt(level, pos);
}

super.onRemove(state, level, pos, newState, movedByPiston);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.enderio.machines.common.blockentity;

import com.enderio.api.travel.TravelTarget;
import com.enderio.api.travel.TravelTargetAPI;
import com.enderio.api.travel.TravelTargetApi;
import com.enderio.core.common.network.NetworkDataSlot;
import com.enderio.machines.common.blockentity.base.MachineBlockEntity;
import com.enderio.machines.common.init.MachineBlockEntities;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void setIsVisible(boolean isVisible) {
}

private AnchorTravelTarget getOrCreateTravelTarget() {
Optional<TravelTarget> travelTarget = TravelTargetAPI.get(level, worldPosition);
Optional<TravelTarget> travelTarget = TravelTargetApi.INSTANCE.get(level, worldPosition);
if (travelTarget.isPresent() && travelTarget.get() instanceof AnchorTravelTarget anchorTravelTarget) {
return anchorTravelTarget;
}
Expand All @@ -108,7 +108,7 @@ private AnchorTravelTarget getOrCreateTravelTarget() {
}

private void setTravelTarget(AnchorTravelTarget target) {
TravelTargetAPI.set(level, target);
TravelTargetApi.INSTANCE.set(level, target);
}

}
3 changes: 0 additions & 3 deletions src/main/java/com/enderio/EnderIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ public EnderIO(IEventBus modEventBus, ModContainer modContainer) {
EIOCriterions.register(modEventBus);
regilite.register(modEventBus);

// Initialize API layers.
TravelTargetSavedData.initAPI();

// Run datagen after registrate is finished.
modEventBus.addListener(EventPriority.LOWEST, this::onGatherData);
modEventBus.addListener(SoulVialItem::onCommonSetup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.enderio.api.travel.RegisterTravelRenderersEvent;
import com.enderio.api.travel.TravelRenderer;
import com.enderio.api.travel.TravelTarget;
import com.enderio.api.travel.TravelTargetAPI;
import com.enderio.api.travel.TravelTargetApi;
import com.enderio.api.travel.TravelTargetType;
import com.enderio.base.common.handler.TravelHandler;
import com.mojang.blaze3d.vertex.PoseStack;
Expand Down Expand Up @@ -63,7 +63,7 @@ public static void renderLevel(RenderLevelStageEvent event) {

@Nullable
TravelTarget activeTarget = TravelHandler.getAnchorTarget(player).orElse(null);
for (TravelTarget target : TravelTargetAPI.getAll(level)) {
for (TravelTarget target : TravelTargetApi.INSTANCE.getAll(level)) {
double range = itemTeleport ? target.item2BlockRange() : target.block2BlockRange();
double distanceSquared = target.pos().distToCenterSqr(player.position());
if (range * range < distanceSquared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.enderio.api.integration.IntegrationManager;
import com.enderio.api.travel.TravelTarget;
import com.enderio.api.travel.TravelTargetAPI;
import com.enderio.api.travel.TravelTargetApi;
import com.enderio.base.common.config.BaseConfig;
import com.enderio.base.common.init.EIOItems;
import com.enderio.base.common.network.RequestTravelPacket;
Expand Down Expand Up @@ -211,7 +211,7 @@ private static BlockPos traversalCheck(Level level, BlockPos traversePos) {
public static Optional<TravelTarget> getAnchorTarget(Player player) {
Vec3 positionVec = player.position().add(0, player.getEyeHeight(), 0);

return TravelTargetAPI
return TravelTargetApi.INSTANCE
.getInItemRange(player.level(), player.blockPosition())
.filter(target -> target.canTravelTo())
.filter(target -> target.pos().distToCenterSqr(player.position()) > MIN_TELEPORTATION_DISTANCE_SQUARED)
Expand Down Expand Up @@ -239,7 +239,7 @@ public static Optional<TravelTarget> getElevatorAnchorTarget(Player player, Dire
lowerY = anchorY - anchorRange - 1;
}

return TravelTargetAPI
return TravelTargetApi.INSTANCE
.getAll(player.level())
.stream()
.filter(target -> target.pos().getX() == anchorX && target.pos().getZ() == anchorZ)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class EIOAttachments {
private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, EnderIO.MODID);

public static void register(IEventBus bus) {
ATTACHMENT_TYPES.register(bus);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.enderio.base.common.network;

import com.enderio.api.travel.TravelTargetAPI;
import com.enderio.api.travel.TravelTargetApi;
import com.enderio.base.common.travel.TravelTargetSavedData;
import net.minecraft.client.Minecraft;
import net.neoforged.neoforge.network.handling.IPayloadContext;
Expand Down Expand Up @@ -28,14 +28,14 @@ public void handleLightUpdate(ServerToClientLightUpdate message, IPayloadContext
public void handleAddTravelTarget(TravelTargetUpdatedPacket packet, IPayloadContext context) {
context.enqueueWork(() -> {
var level = context.player().level();
TravelTargetAPI.set(level, packet.target());
TravelTargetApi.INSTANCE.set(level, packet.target());
});
}

public void handleRemoveTravelTarget(TravelTargetRemovedPacket packet, IPayloadContext context) {
context.enqueueWork(() -> {
var level = context.player().level();
TravelTargetAPI.removeAt(level, packet.pos());
TravelTargetApi.INSTANCE.removeAt(level, packet.pos());
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.enderio.base.common.network;

import com.enderio.api.travel.TravelTarget;
import com.enderio.api.travel.TravelTargetAPI;
import com.enderio.api.travel.TravelTargetApi;
import com.enderio.base.common.handler.TravelHandler;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -28,7 +28,7 @@ public void handleTravelRequest(RequestTravelPacket packet, IPayloadContext cont
context.enqueueWork(() -> {
var player = context.player();

Optional<TravelTarget> target = TravelTargetAPI.get(player.level(), packet.pos());
Optional<TravelTarget> target = TravelTargetApi.INSTANCE.get(player.level(), packet.pos());

// These errors should only ever be triggered if there's some form of desync
if (!TravelHandler.canBlockTeleport(player)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.enderio.base.common.travel;

import com.enderio.api.travel.TravelTarget;
import com.enderio.api.travel.TravelTargetApi;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

public class TravelTargetApiImpl implements TravelTargetApi {

@Override
public Optional<TravelTarget> get(Level level, BlockPos pos) {
return TravelTargetSavedData.getTravelData(level).getTravelTarget(pos);
}

@Override
public <T extends TravelTarget> void set(Level level, T travelTarget) {
TravelTargetSavedData.getTravelData(level).setTravelTarget(level, travelTarget);
}

@Override
public void removeAt(Level level, BlockPos pos) {
TravelTargetSavedData.getTravelData(level).removeTravelTargetAt(level, pos);
}

@Override
public Collection<TravelTarget> getAll(Level level) {
return TravelTargetSavedData.getTravelData(level).getTravelTargets();
}

@Override
public Stream<TravelTarget> getInItemRange(Level level, BlockPos center) {
return TravelTargetSavedData.getTravelData(level).getTravelTargetsInItemRange(center);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.enderio.base.common.travel;

import com.enderio.api.travel.TravelTarget;
import com.enderio.api.travel.TravelTargetAPI;
import com.enderio.api.travel.TravelTargetApi;
import com.enderio.base.common.network.SyncTravelDataPacket;
import com.enderio.base.common.network.TravelTargetRemovedPacket;
import com.enderio.base.common.network.TravelTargetUpdatedPacket;
Expand Down Expand Up @@ -32,17 +32,6 @@
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME)
public class TravelTargetSavedData extends SavedData {

// Allow the API to interact with the saved data.
public static void initAPI() {
TravelTargetAPI.init(
(l, p) -> getTravelData(l).getTravelTarget(p),
(l, t) -> getTravelData(l).setTravelTarget(l, t),
(l, t) -> getTravelData(l).removeTravelTargetAt(l, t),
(l) -> getTravelData(l).getTravelTargets(),
(l, p) -> getTravelData(l).getTravelTargetsInItemRange(p)
);
}

// Even though the client doesn't need to know the data in the old dimensions,
// I am more comfortable with each dimension having its own data on the client.
private static final Map<ResourceKey<Level>, TravelTargetSavedData> CLIENT_DATA = new ConcurrentHashMap<>();
Expand All @@ -65,11 +54,11 @@ public static TravelTargetSavedData getTravelData(Level level) {
}
}

private Optional<TravelTarget> getTravelTarget(BlockPos pos) {
public Optional<TravelTarget> getTravelTarget(BlockPos pos) {
return Optional.ofNullable(travelTargets.get(pos));
}

private Collection<TravelTarget> getTravelTargets() {
public Collection<TravelTarget> getTravelTargets() {
return travelTargets.values();
}

Expand All @@ -80,15 +69,15 @@ public Stream<TravelTarget> getTravelTargetsInItemRange(BlockPos center) {
}

// Adds or updates.
private void setTravelTarget(Level level, TravelTarget target) {
public void setTravelTarget(Level level, TravelTarget target) {
if (level instanceof ServerLevel serverLevel) {
PacketDistributor.sendToPlayersInDimension(serverLevel, new TravelTargetUpdatedPacket(target));
}

travelTargets.put(target.pos(), target);
}

private void removeTravelTargetAt(Level level, BlockPos pos) {
public void removeTravelTargetAt(Level level, BlockPos pos) {
if (level instanceof ServerLevel serverLevel) {
PacketDistributor.sendToPlayersInDimension(serverLevel, new TravelTargetRemovedPacket(pos));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.enderio.base.common.travel.TravelTargetApiImpl

0 comments on commit 63631e7

Please sign in to comment.