feat: add global cooldown and throwback sounds

This commit is contained in:
Sekwah 2024-02-04 05:31:53 +00:00
parent 1b5ade0e82
commit 687ec8c9c9
14 changed files with 152 additions and 49 deletions

View File

@ -168,7 +168,7 @@ public class CoreListeners {
var pos = entity.getBlockLoc();
if(entity instanceof PlayerContainer player) {
var playerData = playerDataServices.getPlayerData(player);
if(playerData.isPortalCooldown()) {
if(playerData.isNetherPortalCooldown()) {
return false;
}
}

View File

@ -2,6 +2,7 @@ package com.sekwah.advancedportals.core.connector.containers;
import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
import com.sekwah.advancedportals.core.serializeddata.Vector;
public interface EntityContainer {
@ -18,4 +19,6 @@ public interface EntityContainer {
String getName();
String getWorldName();
void setVelocity(Vector vector);
}

View File

@ -35,4 +35,6 @@ public interface PlayerContainer extends EntityContainer {
void giveItem(String material, String itemName, String... itemDescription);
boolean sendPacket(String channel, byte[] bytes);
void playSound(String sound, float volume, float pitch);
}

View File

@ -4,6 +4,7 @@ import com.google.gson.annotations.SerializedName;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.registry.TagTarget;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.registry.TagRegistry;
@ -13,10 +14,7 @@ import com.sekwah.advancedportals.core.tags.activation.TriggerBlockTag;
import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
/**
* @author sekwah41
@ -38,6 +36,9 @@ public class AdvancedPortal implements TagTarget {
@Inject
transient PlayerDataServices playerDataServices;
@Inject
transient ConfigRepository configRepository;
public AdvancedPortal(BlockLocation minLoc, BlockLocation maxLoc) {
this.updateBounds(minLoc, maxLoc);
}
@ -98,7 +99,22 @@ public class AdvancedPortal implements TagTarget {
return false;
}*/
public boolean activate(PlayerContainer player) {
/**
*
* @param player
* @param moveActivated if the portal was activated by a move event (won't trigger knockback)
* @return
*/
public boolean activate(PlayerContainer player, boolean moveActivated) {
var playerData = playerDataServices.getPlayerData(player);
if(playerData.isGlobalCooldown()) {
if(configRepository.playFailSound()) {
player.playSound("block.portal.travel", 0.05f, new Random().nextFloat() * 0.4F + 0.8F);
}
if(moveActivated) throwPlayerBack(player);
return false;
}
ActivationData data = new ActivationData();
DataTag[] portalTags = new DataTag[args.size()];
int i = 0;
@ -129,12 +145,19 @@ public class AdvancedPortal implements TagTarget {
}
}
if(data.hasActivated()) {
playerDataServices.getPlayerData(player).setNetherPortalCooldown(1000);
playerData.setNetherPortalCooldown(1000);
playerData.setGlobalCooldown(configRepository.getPortalCooldown() * 1000);
return true;
}
return false;
}
private void throwPlayerBack(PlayerContainer player) {
var strength = configRepository.getThrowbackStrength();
var playerLoc = player.getLoc().getDirection();
player.setVelocity(playerLoc.setY(0).normalize().multiply(-1).setY(0.5).multiply(strength));
}
public boolean isLocationInPortal(BlockLocation loc) {
return this.isLocationInPortal(loc, 0);

View File

@ -27,4 +27,8 @@ public interface ConfigRepository {
boolean getPortalProtection();
long getPortalCooldown();
double getThrowbackStrength();
boolean playFailSound();
}

View File

@ -82,9 +82,19 @@ public class ConfigRepositoryImpl implements ConfigRepository {
return this.config.portalCooldown;
}
@Override
public double getThrowbackStrength() {
return this.config.throwbackStrength;
}
@Override
public void loadConfig(DataStorage dataStorage) {
this.config = dataStorage.loadJson(Config.class, "config.json");
}
@Override
public boolean playFailSound() {
return this.config.playFailSound;
}
}

View File

@ -107,7 +107,11 @@ public class PlayerData {
this.netherPortalCooldown = System.currentTimeMillis() + netherPortalCooldown;
}
public boolean isPortalCooldown() {
public boolean isGlobalCooldown() {
return System.currentTimeMillis() < globalCooldown;
}
public boolean isNetherPortalCooldown() {
return System.currentTimeMillis() < netherPortalCooldown;
}
}

View File

@ -23,15 +23,15 @@ public class PlayerLocation extends WorldLocation {
}
public double getPosX() {
return posX;
return X;
}
public double getPosY() {
return posY;
return Y;
}
public double getPosZ() {
return posZ;
return Z;
}
public String getWorldName() {
@ -45,4 +45,16 @@ public class PlayerLocation extends WorldLocation {
public float getPitch() {
return pitch;
}
public Vector getDirection() {
double rotX = this.getYaw();
double rotY = this.getPitch();
var y = -Math.sin(Math.toRadians(rotY));
double xz = Math.cos(Math.toRadians(rotY));
var x = (-xz * Math.sin(Math.toRadians(rotX)));
var z = Math.cos(Math.toRadians(rotX));
return new Vector(x, y, z);
}
}

View File

@ -0,0 +1,63 @@
package com.sekwah.advancedportals.core.serializeddata;
import com.google.gson.annotations.SerializedName;
public class Vector {
@SerializedName("x")
public final double X;
@SerializedName("y")
public final double Y;
@SerializedName("z")
public final double Z;
public Vector(double X, double Y, double Z) {
this.X = X;
this.Y = Y;
this.Z = Z;
}
public Vector add(Vector vec) {
return new Vector(this.X + vec.X, this.Y + vec.Y, this.Z + vec.Z);
}
public Vector multiply(double value) {
return new Vector(this.X * value, this.Y * value, this.Z * value);
}
public Vector setY(double y) {
return new Vector(this.X, y, this.Z);
}
public double distanceTo(Vector pos) {
return Math.sqrt(this.distanceToSq(pos));
}
private double distanceToSq(Vector pos) {
double dx = this.X - pos.X;
double dy = this.Y - pos.Y;
double dz = this.Z - pos.Z;
return dx * dx + dy * dy + dz * dz;
}
public double getX() {
return this.X;
}
public double getY() {
return this.Y;
}
public double getZ() {
return this.Z;
}
public Vector normalize() {
return this.multiply(1.0D / this.length());
}
private double length() {
return Math.sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z);
}
}

View File

@ -2,39 +2,17 @@ package com.sekwah.advancedportals.core.serializeddata;
import com.google.gson.annotations.SerializedName;
public class WorldLocation {
@SerializedName("x")
public final double posX;
@SerializedName("y")
public final double posY;
@SerializedName("z")
public final double posZ;
public class WorldLocation extends Vector {
@SerializedName("w")
public final String worldName;
public WorldLocation(String worldName, double posX, double posY, double posZ) {
super(posX, posY, posZ);
this.worldName = worldName;
this.posX = posX;
this.posY = posY;
this.posZ = posZ;
}
public double distanceTo(WorldLocation pos) {
return Math.sqrt(this.distanceToSq(pos));
}
public double distanceToSq(WorldLocation pos) {
double dx = this.posX - pos.posX;
double dy = this.posY - pos.posY;
double dz = this.posZ - pos.posZ;
return dx * dx + dy * dy + dz * dz;
}
public BlockLocation toBlockPos() {
return new BlockLocation(this.worldName, (int) Math.floor(this.posX), (int) Math.floor(this.posY), (int) Math.floor(this.posZ));
return new BlockLocation(this.worldName, (int) Math.floor(this.X), (int) Math.floor(this.Y), (int) Math.floor(this.Z));
}
}

View File

@ -31,4 +31,7 @@ public class Config {
public int maxTriggerVisualisationSize = 1000;
public double throwbackStrength = 1;
public boolean playFailSound = true;
}

View File

@ -73,11 +73,6 @@ public class PortalServices {
}
public void playerMove(PlayerContainer player, PlayerLocation toLoc) {
PlayerData tempData = playerDataServices.getPlayerData(player);
if(tempData.getGlobalCooldown() > System.currentTimeMillis()) {
return;
}
var blockLoc = toLoc.toBlockPos();
var blockEntityTopLoc = blockLoc.addY(player.getHeight());
@ -90,7 +85,9 @@ public class PortalServices {
&& portal.isTriggerBlock(blockMaterial))
|| (portal.isLocationInPortal(blockEntityTopLoc)
&& portal.isTriggerBlock(blockEntityTopMaterial))) {
portal.activate(player);
if(portal.activate(player, true)) {
return;
}
}
}
}

View File

@ -6,16 +6,10 @@ import com.sekwah.advancedportals.core.connector.containers.EntityContainer;
import com.sekwah.advancedportals.core.connector.containers.WorldContainer;
import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
import com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin;
import com.sekwah.advancedportals.spigot.reflection.MinecraftCustomPayload;
import com.sekwah.advancedportals.core.serializeddata.Vector;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
/**
* Just a temporary container for whenever advanced portals needs to get data from a player
@ -67,4 +61,9 @@ public class SpigotEntityContainer implements EntityContainer {
public String getWorldName() {
return this.entity.getWorld().getName();
}
@Override
public void setVelocity(Vector vector) {
this.entity.setVelocity(new org.bukkit.util.Vector(vector.getX(), vector.getY(), vector.getZ()));
}
}

View File

@ -101,4 +101,9 @@ public class SpigotPlayerContainer extends SpigotEntityContainer implements Play
public Player getPlayer() {
return this.player;
}
@Override
public void playSound(String sound, float volume, float pitch) {
this.player.playSound(this.player.getLocation(), sound, volume, pitch);
}
}