Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added actions #198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.bukkit.event.block.Action;

public enum ClickType {
LEFT, RIGHT, SHIFT_LEFT, SHIFT_RIGHT;
LEFT, RIGHT, SHIFT_LEFT, SHIFT_RIGHT,HOVER,HOVER_LEAVE;
Copy link
Contributor

@Andre601 Andre601 Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of a non-click action (just looking at a page/line) being in a enum specifically for click types.

Given this would only be looking and not looking at a hologram can this be just some lists of actions or similar in the Hologram itself, rather than dedicate an entire type to it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your reply,But I think hovering the mouse over the hologram should also be considered a ClickType. If we put them into actions, it would lose its meaning, as it would be triggered by LEFT_CLICK or RIGHT_CLICK instead of when the mouse hovers over the hologram.

While I acknowledge that my programming skills may have room for improvement, I am open to further discussion and finding a solution that best aligns with the project's needs. What are your thoughts on this approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mixed feelings on this to be honest.
On one hand is it nice that you want to add hover support, but on the other hand do I feel like this may not be as good in terms of performance...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it does indeed have performance issues; perhaps it's an immature submission. I hope this PR can remain open for others to see, maybe there are better approaches from others.


public static ClickType fromAction(Action action, boolean sneak) {
switch (action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -198,6 +192,7 @@ public static Hologram fromFile(final @NotNull String filePath) throws LocationP
}
}
hologram.setFacing((float) config.getDouble("facing", 0.0f));
hologram.setHoverCommands(config.getStringList("hover"));
return hologram;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import eu.decentsoftware.holograms.api.utils.file.FileUtils;
import eu.decentsoftware.holograms.api.utils.scheduler.S;
import eu.decentsoftware.holograms.api.utils.tick.Ticked;
import eu.decentsoftware.holograms.event.DecentHologramsRegisterEvent;
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -227,6 +228,9 @@ public boolean containsHologram(@NonNull String name) {
*/
public void registerHologram(@NonNull Hologram hologram) {
hologramMap.put(hologram.getName(), hologram);
Bukkit.getScheduler().runTask(decentHolograms.getPlugin(), ()->{
Bukkit.getServer().getPluginManager().callEvent(new DecentHologramsRegisterEvent());
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class HologramObject extends FlagHolder {
protected Location location;
protected String permission = null;
protected float facing = 0.0f;
protected List<String> hoverCommands = new ArrayList<>();

/*
* Constructors
Expand Down Expand Up @@ -132,6 +133,10 @@ public void setFacing(float facing) {
this.location.setYaw(facing);
}

public void setHoverCommands(List<String> hoverCommands) {
this.hoverCommands = hoverCommands;
}

public void setLocation(@NonNull Location location) {
this.location = location;
this.location.setYaw(facing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

import eu.decentsoftware.holograms.api.DecentHolograms;
import eu.decentsoftware.holograms.api.Lang;
import eu.decentsoftware.holograms.api.actions.ClickType;
import eu.decentsoftware.holograms.api.holograms.Hologram;
import eu.decentsoftware.holograms.api.utils.scheduler.S;
import eu.decentsoftware.holograms.event.DecentHologramsRegisterEvent;
import eu.decentsoftware.holograms.event.HologramClickEvent;
import lombok.Data;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.*;
import org.bukkit.util.Vector;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class PlayerListener implements Listener {
Expand All @@ -19,6 +29,7 @@ public class PlayerListener implements Listener {

public PlayerListener(DecentHolograms decentHolograms) {
this.decentHolograms = decentHolograms;
this.ref();
}

@EventHandler(priority = EventPriority.MONITOR)
Expand Down Expand Up @@ -53,6 +64,89 @@ public void onRespawn(PlayerRespawnEvent e) {
S.async(() -> decentHolograms.getHologramManager().hideAll(player));
}


// 视角对着的全息
meteorOSS marked this conversation as resolved.
Show resolved Hide resolved
private Map<Player,String> viewHolograms = new ConcurrentHashMap<>();


private Map<Player,Long> tickLog = new ConcurrentHashMap<>();

public Map<Chunk,List<Hologram>> chunkListMap = new ConcurrentHashMap<>();

private void ref(){
chunkListMap.clear();
decentHolograms.getHologramManager().getHolograms().stream().forEach(hologram -> {
Chunk chunk = hologram.getLocation().getChunk();
chunkListMap.putIfAbsent(chunk,new ArrayList<>());
chunkListMap.get(chunk).add(hologram);
});
}


@EventHandler
public void onRegisterHd(DecentHologramsRegisterEvent registerEvent){
this.ref();
}

@EventHandler
public void onPlayerMove(PlayerMoveEvent playerMoveEvent){
Player player = playerMoveEvent.getPlayer();
Long orDefault = tickLog.getOrDefault(player, 0L);

if(chunkListMap.isEmpty()) ref();

if((System.currentTimeMillis()-orDefault) >= 200L){
Optional.ofNullable(chunkListMap.get(player.getLocation().getChunk())).ifPresent(list->{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the use of Optionals is needed here. A simple getting and later null check does the same.

List<Hologram> holograms = list.stream().collect(Collectors.toList());
meteorOSS marked this conversation as resolved.
Show resolved Hide resolved
Vector playerDirection = player.getEyeLocation().getDirection();
double threshold = 0.2; // 0.2弧度
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with other comment, use english.


tickLog.put(player,System.currentTimeMillis());

holograms.removeIf(hologram -> {
Location location = hologram.getLocation();
Vector toHologram = location.toVector().subtract(player.getEyeLocation().toVector());
double angle = playerDirection.angle(toHologram);
return angle>=threshold;
});

String currentViewHd = viewHolograms.get(player);


for (Hologram hologram : holograms) {
if(currentViewHd==null) viewHolograms.put(player,hologram.getName());

currentViewHd = viewHolograms.get(player);

// 如果从一个全息移动到了另一个全息上
meteorOSS marked this conversation as resolved.
Show resolved Hide resolved
if(!currentViewHd.equalsIgnoreCase(hologram.getName())){
Hologram fromHologram = decentHolograms.getHologramManager().getHologram(currentViewHd);
List<Integer> clickableEntityIds = fromHologram.getPage(player).getClickableEntityIds();
if(clickableEntityIds.size()>0){
fromHologram.onClick(player,clickableEntityIds.get(0),ClickType.HOVER_LEAVE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to have dedicated onHover methods.
Maybe with a boolean to set whether they look on it or not (i.e. onHover(id, boolean)). The click type feels wrong here.

viewHolograms.put(player,hologram.getName());
}
}

List<Integer> clickableEntityIds = hologram.getPage(player).getClickableEntityIds();
if(!clickableEntityIds.isEmpty()){
hologram.onClick(player,clickableEntityIds.get(0), ClickType.HOVER);
meteorOSS marked this conversation as resolved.
Show resolved Hide resolved
}
return;
}

if(currentViewHd!=null){
Hologram fromHologram = decentHolograms.getHologramManager().getHologram(currentViewHd);
List<Integer> clickableEntityIds = fromHologram.getPage(player).getClickableEntityIds();
if(clickableEntityIds.size()>0){
fromHologram.onClick(player,clickableEntityIds.get(0),ClickType.HOVER_LEAVE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment before.

viewHolograms.remove(player);
}
}
});
}
}

@EventHandler
public void onTeleport(PlayerTeleportEvent e) {
Player player = e.getPlayer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package eu.decentsoftware.holograms.event;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

/**
* This event is called after the DecentHolograms plugin is reloaded.
*
* @author d0by
* @since 2.7.8
*/
meteorOSS marked this conversation as resolved.
Show resolved Hide resolved
public class DecentHologramsRegisterEvent extends Event {

private static final HandlerList HANDLERS = new HandlerList();

public DecentHologramsRegisterEvent() {
meteorOSS marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public HandlerList getHandlers() {
return HANDLERS;
}

public static HandlerList getHandlerList() {
return HANDLERS;
}

public static boolean isRegistered() {
return HANDLERS.getRegisteredListeners().length > 0;
}

}