Skip to content

Commit

Permalink
theoretically implement bukkit platform
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro committed Jun 17, 2022
1 parent 17b90a8 commit 1c16d2c
Show file tree
Hide file tree
Showing 51 changed files with 2,723 additions and 150 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,3 @@ atlassian-ide-plugin.xml

# delombok
*/src/main/lombok

# CloudNet
.launchermeta/
3 changes: 3 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 28 additions & 17 deletions api/src/main/java/com/github/juliarn/npclib/api/Npc.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.github.juliarn.npclib.api.flag.NpcFlaggedObject;
import com.github.juliarn.npclib.api.profile.Profile;
import com.github.juliarn.npclib.api.profile.ProfileResolver;
import com.github.juliarn.npclib.api.protocol.NpcSpecificOutboundPacket;
import com.github.juliarn.npclib.api.protocol.enums.ItemSlot;
import com.github.juliarn.npclib.api.settings.NpcSettings;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
Expand All @@ -37,7 +39,7 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

public interface Npc<W, P, I> extends NpcFlaggedObject {
public interface Npc<W, P, I, E> extends NpcFlaggedObject {

NpcFlag<Boolean> LOOK_AT_PLAYER = NpcFlag.flag("imitate_player_look", false);
NpcFlag<Boolean> HIT_WHEN_PLAYER_HITS = NpcFlag.flag("imitate_player_hit", false);
Expand All @@ -53,46 +55,55 @@ public interface Npc<W, P, I> extends NpcFlaggedObject {

@NotNull NpcSettings<P> settings();

@NotNull Platform<W, P, I> platform();
@NotNull Platform<W, P, I, E> platform();

@NotNull NpcTracker<W, P, I> npcTracker();
@NotNull NpcTracker<W, P, I, E> npcTracker();

boolean shouldIncludePlayer(@NotNull P player);

@UnmodifiableView
@NotNull Collection<P> includedPlayers();

@NotNull Npc<W, P, I> addIncludedPlayer(@NotNull P player);
boolean includesPlayer(@NotNull P player);

@NotNull Npc<W, P, I> removeIncludedPlayer(@NotNull P player);
@NotNull Npc<W, P, I, E> addIncludedPlayer(@NotNull P player);

@NotNull Npc<W, P, I, E> removeIncludedPlayer(@NotNull P player);

@UnmodifiableView
@NotNull Collection<P> trackedPlayers();

@NotNull Npc<W, P, I> trackPlayer(@NotNull P player);
boolean tracksPlayer(@NotNull P player);

@NotNull Npc<W, P, I, E> trackPlayer(@NotNull P player);

@NotNull Npc<W, P, I, E> forceTrackPlayer(@NotNull P player);

@NotNull Npc<W, P, I, E> stopTrackingPlayer(@NotNull P player);

@NotNull Npc<W, P, I> forceTrackPlayer(@NotNull P player);
@NotNull NpcSpecificOutboundPacket<W, P, I, E> lookAt(@NotNull Position position);

@NotNull Npc<W, P, I> stopTrackingPlayer(@NotNull P player);
@NotNull NpcSpecificOutboundPacket<W, P, I, E> changeItem(@NotNull ItemSlot slot, @NotNull I item);

interface Builder<W, P, I> extends NpcFlaggedBuilder<Builder<W, P, I>> {
interface Builder<W, P, I, E> extends NpcFlaggedBuilder<Builder<W, P, I, E>> {

@NotNull Builder<W, P, I> entityId(int id);
@NotNull Builder<W, P, I, E> entityId(int id);

@NotNull Builder<W, P, I> position(@NotNull Position position);
@NotNull Builder<W, P, I, E> position(@NotNull Position position);

@NotNull Builder<W, P, I> profile(@NotNull Profile.Resolved profile);
@NotNull Builder<W, P, I, E> profile(@NotNull Profile.Resolved profile);

default @NotNull CompletableFuture<Builder<W, P, I>> profile(@NotNull Profile profile) {
default @NotNull CompletableFuture<Builder<W, P, I, E>> profile(@NotNull Profile profile) {
return this.profile(null, profile);
}

@NotNull CompletableFuture<Builder<W, P, I>> profile(@Nullable ProfileResolver resolver, @NotNull Profile profile);
@NotNull CompletableFuture<Builder<W, P, I, E>> profile(@Nullable ProfileResolver resolver,
@NotNull Profile profile);

@NotNull Builder<W, P, I> npcSettings(@NotNull Consumer<NpcSettings.Builder<P>> decorator);
@NotNull Builder<W, P, I, E> npcSettings(@NotNull Consumer<NpcSettings.Builder<P>> decorator);

@NotNull Npc<W, P, I> build();
@NotNull Npc<W, P, I, E> build();

@NotNull Npc<W, P, I> buildAndTrack();
@NotNull Npc<W, P, I, E> buildAndTrack();
}
}
12 changes: 6 additions & 6 deletions api/src/main/java/com/github/juliarn/npclib/api/NpcTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

public interface NpcTracker<W, P, I> {
public interface NpcTracker<W, P, I, E> {

@Nullable Npc<W, P, I> npcById(int entityId);
@Nullable Npc<W, P, I, E> npcById(int entityId);

@Nullable Npc<W, P, I> npcByUniqueId(@NotNull UUID uniqueId);
@Nullable Npc<W, P, I, E> npcByUniqueId(@NotNull UUID uniqueId);

void trackNpc(@NotNull Npc<W, P, I> npc);
void trackNpc(@NotNull Npc<W, P, I, E> npc);

void stopTrackingNpc(@NotNull Npc<W, P, I> npc);
void stopTrackingNpc(@NotNull Npc<W, P, I, E> npc);

@UnmodifiableView
@NotNull Collection<Npc<W, P, I>> trackedNpcs();
@NotNull Collection<Npc<W, P, I, E>> trackedNpcs();
}
36 changes: 22 additions & 14 deletions api/src/main/java/com/github/juliarn/npclib/api/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,52 @@
import net.kyori.event.EventBus;
import org.jetbrains.annotations.NotNull;

public interface Platform<W, P, I> {
public interface Platform<W, P, I, E> {

boolean debug();

@NotNull E extension();

@NotNull EventBus<NpcEvent> eventBus();

@NotNull NpcTracker<W, P, I> npcTracker();
@NotNull NpcTracker<W, P, I, E> npcTracker();

@NotNull ProfileResolver profileResolver();

@NotNull PlatformTaskManager taskManager();

@NotNull Npc.Builder<W, P, I> newNpcBuilder();
@NotNull Npc.Builder<W, P, I, E> newNpcBuilder();

@NotNull PlatformVersionAccessor versionAccessor();

@NotNull PlatformWorldAccessor<W> worldAccessor();

@NotNull PlatformPacketAdapter<W, P, I> packetFactory();
@NotNull PlatformPacketAdapter<W, P, I, E> packetFactory();

@NotNull Optional<NpcActionController> actionController();

interface Builder<W, P, I> {
interface Builder<W, P, I, E> {

@NotNull Builder<W, P, I, E> debug(boolean debug);

@NotNull Builder<W, P, I, E> extension(@NotNull E extension);

@NotNull Builder<W, P, I> debug(boolean debug);
@NotNull Builder<W, P, I, E> eventBus(@NotNull EventBus<NpcEvent> eventBus);

@NotNull Builder<W, P, I> eventBus(@NotNull EventBus<NpcEvent> eventBus);
@NotNull Builder<W, P, I, E> npcTracker(@NotNull NpcTracker<W, P, I, E> npcTracker);

@NotNull Builder<W, P, I> npcTracker(@NotNull NpcTracker<W, P, I> npcTracker);
@NotNull Builder<W, P, I, E> taskManager(@NotNull PlatformTaskManager taskManager);

@NotNull Builder<W, P, I> taskManager(@NotNull PlatformTaskManager taskManager);
@NotNull Builder<W, P, I, E> profileResolver(@NotNull ProfileResolver profileResolver);

@NotNull Builder<W, P, I> profileResolver(@NotNull ProfileResolver profileResolver);
@NotNull Builder<W, P, I, E> worldAccessor(@NotNull PlatformWorldAccessor<W> worldAccessor);

@NotNull Builder<W, P, I> worldAccessor(@NotNull PlatformWorldAccessor<W> worldAccessor);
@NotNull Builder<W, P, I, E> versionAccessor(@NotNull PlatformVersionAccessor versionAccessor);

@NotNull Builder<W, P, I> packetFactory(@NotNull PlatformPacketAdapter<W, P, I> packetFactory);
@NotNull Builder<W, P, I, E> packetFactory(@NotNull PlatformPacketAdapter<W, P, I, E> packetFactory);

@NotNull Builder<W, P, I> actionController(@NotNull Consumer<NpcActionController.Builder> decorator);
@NotNull Builder<W, P, I, E> actionController(@NotNull Consumer<NpcActionController.Builder> decorator);

@NotNull Platform<W, P, I> build();
@NotNull Platform<W, P, I, E> build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.juliarn.npclib.api;

public interface PlatformVersionAccessor {

int major();

int minor();

int patch();

boolean atLeast(int major, int minor, int patch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@

public interface NpcEvent {

@NotNull <W, P, I> Npc<W, P, I> npc();
@NotNull <W, P, I, E> Npc<W, P, I, E> npc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class DefaultResolvedProfile implements Profile.Resolved {
private final UUID uniqueId;
private final Set<ProfileProperty> properties;

public DefaultResolvedProfile(
DefaultResolvedProfile(
@NotNull String name,
@NotNull UUID uniqueId,
@NotNull Set<ProfileProperty> properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ final class MojangProfileResolver implements ProfileResolver {
Set<ProfileProperty> properties = GSON.fromJson(responseData.get("properties"), PROFILE_PROPERTIES_TYPE);

// create the profile from the received data
return new DefaultResolvedProfile(name, uniqueId, properties);
return Profile.resolved(name, uniqueId, properties);
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@ public interface Profile {
}

static @NotNull Resolved resolved(@NotNull String name, @NotNull UUID uniqueId) {
return resolved(name, uniqueId, Collections.emptySet());
}

static @NotNull Resolved resolved(
@NotNull String name,
@NotNull UUID uniqueId,
@NotNull Set<ProfileProperty> properties
) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(uniqueId, "unique id");
Objects.requireNonNull(properties, "properties");

return new DefaultResolvedProfile(name, uniqueId, Collections.emptySet());
return new DefaultResolvedProfile(name, uniqueId, properties);
}

boolean resolved();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is part of npc-lib, licensed under the MIT License (MIT).
*
* Copyright (c) 2022 Julian M., Pasqual K. and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.juliarn.npclib.api.protocol;

import com.github.juliarn.npclib.api.Npc;
import java.util.Collection;
import java.util.function.Function;
import org.jetbrains.annotations.NotNull;

final class DefaultNpcSpecificOutboundPacket<W, P, I, E> implements NpcSpecificOutboundPacket<W, P, I, E> {

private final Npc<W, P, I, E> target;
private final OutboundPacket<W, P, I, E> outboundPacket;

public DefaultNpcSpecificOutboundPacket(
@NotNull Npc<W, P, I, E> target,
@NotNull OutboundPacket<W, P, I, E> outboundPacket
) {
this.target = target;
this.outboundPacket = outboundPacket;
}

@Override
public @NotNull Npc<W, P, I, E> npc() {
return this.target;
}

@Override
public void scheduleForTracked() {
this.outboundPacket.scheduleForTracked(this.target);
}

@Override
public void schedule(@NotNull P player) {
this.outboundPacket.schedule(player, this.target);
}

@Override
public void schedule(@NotNull Collection<P> players) {
this.outboundPacket.schedule(players, this.target);
}

@Override
public void schedule(@NotNull Function<Npc<W, P, I, E>, Collection<P>> extractor) {
this.outboundPacket.schedule(extractor, this.target);
}
}
Loading

0 comments on commit 1c16d2c

Please sign in to comment.