Skip to content

Commit

Permalink
wip special poi
Browse files Browse the repository at this point in the history
  • Loading branch information
bazke committed Oct 28, 2023
1 parent f4c8871 commit efadc09
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

class PoiButton extends AbstractButton {
private static final int ICON_SIZE = 16;
private static final int HALF_ICON_SIZE = ICON_SIZE / 2;
private static final int BORDER_SIZE = 3;
private static final int SIZE = ICON_SIZE + BORDER_SIZE * 2;
private static final int TOOLTIP_HEIGHT = 18;

private static final int HOVER_ANIMATION_LENGTH = 8;

private static final ResourceLocation TOOLTIP_LOCATION = new ResourceLocation("textures/gui/advancements/widgets.png");
private static final ResourceLocation SPECIAL_MARKER = new ResourceLocation("textures/item/bell.png");

private final Poi poi;
private final Font font;
Expand Down Expand Up @@ -91,6 +93,10 @@ protected void renderWidget(final GuiGraphics graphics, final int mouseX, final

final ResourceLocation icon = poi.resourceLocation();
graphics.blit(icon, getX() + BORDER_SIZE, getY() + BORDER_SIZE, zOffset, 0.0f, 0.0f, ICON_SIZE, ICON_SIZE, ICON_SIZE, ICON_SIZE);

if (poi.isMarkSpecial()) {
graphics.blit(SPECIAL_MARKER, getX() + BORDER_SIZE + HALF_ICON_SIZE, getY() + BORDER_SIZE + HALF_ICON_SIZE, zOffset, 0.0f, 0.0f, HALF_ICON_SIZE, HALF_ICON_SIZE, HALF_ICON_SIZE, HALF_ICON_SIZE);
}
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/lovetropics/extras/command/PoiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ private static int addWithDefaults(CommandContext<CommandSourceStack> ctx) {
final ResourceLocation icon = getId(ctx, "icon");
final GlobalPos globalPos = GlobalPos.of(ctx.getSource().getLevel().dimension(), ctx.getSource().getPlayer().getOnPos().above());
final boolean enabled = false;
final boolean markSpecial = false;

final Poi newPoi = new Poi(name, description, icon, globalPos, enabled);
final Poi newPoi = new Poi(name, description, icon, globalPos, enabled, markSpecial);
MapPoiManager.get(ctx.getSource().getServer()).add(newPoi);
ctx.getSource().sendSuccess(() -> Component.literal("Added new disabled POI " + newPoi.name() + " at your current position"), false);
return Command.SINGLE_SUCCESS;
Expand Down Expand Up @@ -236,8 +237,9 @@ private static Poi createPoiFromCtx(CommandContext<CommandSourceStack> ctx) {
final WorldCoordinates worldCoordinates = ctx.getArgument("blockpos", WorldCoordinates.class);
final GlobalPos globalPos = GlobalPos.of(ctx.getSource().getLevel().dimension(), worldCoordinates.getBlockPos(ctx.getSource()));
final boolean enabled = getBool(ctx, "enabled");
final boolean markSpecial = false;

return new Poi(name, description, icon, globalPos, enabled);
return new Poi(name, description, icon, globalPos, enabled, markSpecial);
}

private static Stream<String> suggestGlobalPos(CommandContext<CommandSourceStack> ctx) {
Expand Down
66 changes: 62 additions & 4 deletions src/main/java/com/lovetropics/extras/data/poi/MapPoiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
import com.lovetropics.extras.LTExtras;
import com.lovetropics.extras.network.ClientboundPoiPacket;
import com.lovetropics.extras.network.LTExtrasNetwork;
import com.lovetropics.extras.schedule.StreamSchedule;
import com.lovetropics.lib.permission.PermissionsApi;
import com.lovetropics.lib.permission.role.RoleOverrideType;
import com.lovetropics.lib.permission.role.RoleReader;
import com.mojang.serialization.Codec;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.PacketDistributor;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand All @@ -33,8 +37,10 @@ public class MapPoiManager extends SavedData {
public static final BoundingBox MAP_BB = new BoundingBox(2013, 0, 1883, 2910, 0, 2799);
private static final Codec<Map<String, Poi>> CODEC = Codec.unboundedMap(Codec.STRING, Poi.CODEC);
private static final String STORAGE_ID = LTExtras.MODID + "_map_poi";
private static final RoleOverrideType<Boolean> HOST_ROLE = (RoleOverrideType<Boolean>) RoleOverrideType.byId("host");
private final Map<String, Poi> pois;


public MapPoiManager(final Map<String, Poi> pois) {
this.pois = pois;
}
Expand Down Expand Up @@ -77,6 +83,58 @@ public Set<Poi> getEnabledPois() {
.collect(Collectors.toSet());
}

//TODO where would a better place to hook this into be? seems expensive here
@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event) {
if (event.getServer().getTickCount() % 20 != 0) {
return;
}

//TODO: if there is no host, skip all this. can we know if there is a host without looking at all players?

Optional<ServerPlayer> firstHost = event.getServer().getPlayerList().getPlayers()
.stream()
.filter(p -> PermissionsApi.lookup().byPlayer(p).overrides().test(HOST_ROLE))
.findFirst();

if (firstHost.isEmpty()) {
//TODO just for testing, remove before merging
firstHost = Optional.ofNullable(event.getServer().getPlayerList().getPlayerByName("Dev2"));
if (firstHost.isEmpty()) {
return;
}
}

final ServerPlayer host = firstHost.get();
final BlockPos hostPosition = host.blockPosition();

final MapPoiManager manager = MapPoiManager.get(event.getServer());
final Optional<Poi> min = manager.getEnabledPois()
.stream()
.min(Comparator.comparingDouble(p -> p.globalPos().pos().distSqr(hostPosition)));

if (min.isEmpty()) {
return;
}

final Poi nearestPoi = min.get();
manager.markSpecial(nearestPoi.name());
}

public void markSpecial(String name) {
pois.values()
.stream().filter(Poi::isMarkSpecial)
.map(p -> p.setMarkSpecial(false))
.forEach(p -> LTExtrasNetwork.CHANNEL.send(PacketDistributor.ALL.noArg(), new ClientboundPoiPacket(p, false)));

final Poi poi = pois.get(name);
if (poi != null) {
poi.setMarkSpecial(true);
LTExtrasNetwork.CHANNEL.send(PacketDistributor.ALL.noArg(), new ClientboundPoiPacket(poi, false));
}
setDirty();
}

public Set<Poi> getDisabledPois() {
return pois.values().stream()
.filter(Predicate.not(Poi::enabled))
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/lovetropics/extras/data/poi/Poi.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@ public final class Poi {
ExtraCodecs.COMPONENT.fieldOf("description").forGetter(Poi::description),
ResourceLocation.CODEC.fieldOf("resourceLocation").forGetter(Poi::resourceLocation),
GlobalPos.CODEC.fieldOf("blockPos").forGetter(Poi::globalPos),
Codec.BOOL.fieldOf("enabled").forGetter(Poi::enabled)
Codec.BOOL.fieldOf("enabled").forGetter(Poi::enabled),
Codec.BOOL.fieldOf("markSpecial").forGetter(Poi::isMarkSpecial)
).apply(i, Poi::new));

private final String name;
private final Component description;
private final ResourceLocation resourceLocation;
private final GlobalPos globalPos;
private boolean enabled;
private boolean markSpecial;

public Poi(
String name,
Component description,
ResourceLocation resourceLocation,
GlobalPos globalPos,
boolean enabled) {
boolean enabled,
boolean markSpecial) {
this.name = name;
this.description = description;
this.resourceLocation = resourceLocation;
this.globalPos = globalPos;
this.enabled = enabled;
this.markSpecial = markSpecial;
}

//Use only the name for equals&hashCode. Maybe tiny bit risky but dupe handling is free
Expand Down Expand Up @@ -71,6 +75,15 @@ public boolean enabled() {
return enabled;
}

public boolean isMarkSpecial() {
return markSpecial;
}

public Poi setMarkSpecial(boolean markSpecial) {
this.markSpecial = markSpecial;
return this;
}

@Override
public String toString() {
return "Poi[" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.lovetropics.extras.client.ClientMapPoiManager;
import com.lovetropics.extras.data.poi.Poi;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;
Expand All @@ -14,6 +13,7 @@ public ClientboundPoiPacket(final FriendlyByteBuf input) {
input.readComponent(),
input.readResourceLocation(),
input.readGlobalPos(),
input.readBoolean(),
input.readBoolean()),
input.readBoolean());
}
Expand All @@ -24,6 +24,7 @@ public void write(final FriendlyByteBuf output) {
output.writeResourceLocation(poi.resourceLocation());
output.writeGlobalPos(poi.globalPos());
output.writeBoolean(poi.enabled());
output.writeBoolean(poi.isMarkSpecial());
output.writeBoolean(delete);
}

Expand Down

0 comments on commit efadc09

Please sign in to comment.