Skip to content

Commit

Permalink
building PortalType
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed May 30, 2024
1 parent 0a98172 commit 48780da
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
import org.spongepowered.api.util.annotation.eventgen.AbsoluteSortPosition;
import org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod;
import org.spongepowered.api.world.portal.Portal;
import org.spongepowered.api.world.portal.PortalType;
import org.spongepowered.math.vector.Vector3d;

import java.util.Optional;

/**
* Called when an {@link Entity} is using a {@link Portal}.
* Called when an {@link Entity} is using a {@link PortalType portal}.
*/
@NoFactoryMethod
public interface PortalEntityEvent extends Event {
Expand All @@ -48,7 +49,7 @@ public interface PortalEntityEvent extends Event {
@AbsoluteSortPosition(1)
Entity entity();

Portal portal();
PortalType portal();

/**
* Called when entering a portal.
Expand All @@ -72,7 +73,7 @@ interface Enter extends PortalEntityEvent, Cancellable {
interface Pre extends PortalEntityEvent, Cancellable {
// TODO for vanilla we could allow changing the Portal
// TODO expose vanilla portals + sponge variants without block generation?
void setPortal(Portal portal);
void setPortal(PortalType portal);
}

// DimensionTransition was obtained, portal blocks are generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ public interface Portal {
* @return The destination or {@link Optional#empty()} if unknown
*/
Optional<ServerLocation> destination();

}
110 changes: 110 additions & 0 deletions src/main/java/org/spongepowered/api/world/portal/PortalType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@
*/
package org.spongepowered.api.world.portal;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.registry.DefaultedRegistryValue;
import org.spongepowered.api.util.Axis;
import org.spongepowered.api.util.ResettableBuilder;
import org.spongepowered.api.util.annotation.CatalogedBy;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.math.vector.Vector3i;

import java.util.Map;
import java.util.Optional;

/**
Expand All @@ -38,6 +46,10 @@
@CatalogedBy(PortalTypes.class)
public interface PortalType extends DefaultedRegistryValue {

static Builder builder() {
return Sponge.game().builderProvider().provide(Builder.class);
}

/**
* Generates the portal at the {@link ServerLocation location}.
*
Expand Down Expand Up @@ -83,4 +95,102 @@ public interface PortalType extends DefaultedRegistryValue {
* @return True if teleport successful, false if not
*/
boolean teleport(Entity entity, ServerLocation destination, boolean generateDestinationPortal);



interface Factory {

/**
* The default vanilla {@link org.spongepowered.api.block.BlockTypes#END_PORTAL}.
*
* @return the portal type
*/
PortalType endPortal();
/**
* The default vanilla {@link org.spongepowered.api.block.BlockTypes#END_GATEWAY}.
*
* @return the portal type
*/
PortalType endGateway();

/**
* The default vanilla {@link org.spongepowered.api.block.BlockTypes#NETHER_PORTAL}.
*
* @return the portal type
*/
PortalType netherPortal();

/**
* Only finds existing nether portals. But never creates one.
*
* @return the portal type
*/
PortalType netherPortalFinder();

default void example() {
final ResourceKey overworld = ResourceKey.minecraft("overworld");
final ResourceKey end = ResourceKey.minecraft("end");
final ResourceKey nether = ResourceKey.minecraft("nether");
Map<ServerWorld, ServerLocation> lookup = Map.of(); // dont actually save it like this


var portal = builder()
.endPortal(overworld, end, Vector3i.ONE) // places platform at target location
.targetPortal(end, overworld, Vector3d.ONE) // just teleports
.build()
;

var vanillaEndPortalClone = builder()
.endPortal(overworld, end) // vanilla end target + places platform
.spawnPortal(end, overworld) // teleports to spawn
.build()
;

var portalTriangle = builder()
.spawnPortal(overworld, nether)
.spawnPortal(nether, end)
.spawnPortal(end, overworld)
.build()
;

var findNetherPortal = builder()
.findNetherPortal(overworld, nether)
.build();

var pluginLookup = builder()
.portal((from, e, fromPos) -> lookup.get(from))
.build();

var inWorldScaling = builder()
.portal((from, e, fromPos) -> ServerLocation.of(from, fromPos.mul(8)))
.build();

}
}



interface Builder extends ResettableBuilder<PortalType, Builder> {

Builder netherPortal(ResourceKey origin, ResourceKey target); // default scale
Builder netherPortal(ResourceKey origin, ResourceKey target, double scale);
Builder findNetherPortal(ResourceKey origin, ResourceKey target); // default scale
Builder findNetherPortal(ResourceKey origin, ResourceKey target, double scale);
Builder endPortal(ResourceKey origin, ResourceKey target);
Builder endPortal(ResourceKey origin, ResourceKey target, Vector3i targetPos);
Builder targetPortal(ResourceKey origin, ResourceKey target, Vector3d targetPos);
Builder spawnPortal(ResourceKey origin, ResourceKey target);

Builder portal(PortalExitFinder exitFinder);

PortalType build();
}

@FunctionalInterface
interface PortalExitFinder {
@Nullable ServerLocation findPortalExit(ServerWorld from, Entity entity, Vector3i fromPos);
}



}

0 comments on commit 48780da

Please sign in to comment.