Skip to content

Commit

Permalink
feat: add global cooldown and throwback sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Nov 18, 2024
1 parent 8018d68 commit 9448fd7
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public boolean entityPortalEvent(EntityContainer entity) {
var pos = entity.getBlockLoc();
if(entity instanceof PlayerContainer player) {
var playerData = playerDataServices.getPlayerData(player);
if(playerData.isPortalCooldown()) {
if(playerData.isNetherPortalCooldown()) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
import com.sekwah.advancedportals.core.serializeddata.Vector;

public interface EntityContainer {

Expand All @@ -18,4 +19,6 @@ public interface EntityContainer {
String getName();

String getWorldName();

void setVelocity(Vector vector);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -13,10 +14,7 @@
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
Expand All @@ -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);
}
Expand Down Expand Up @@ -98,7 +99,22 @@ public void updateBounds(BlockLocation loc1, BlockLocation loc2) {
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;
Expand Down Expand Up @@ -129,12 +145,19 @@ public boolean activate(PlayerContainer player) {
}
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public interface ConfigRepository {
boolean getPortalProtection();

long getPortalCooldown();

double getThrowbackStrength();

boolean playFailSound();
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,19 @@ public long getPortalCooldown() {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public void setNetherPortalCooldown(long netherPortalCooldown) {
this.netherPortalCooldown = System.currentTimeMillis() + netherPortalCooldown;
}

public boolean isPortalCooldown() {
public boolean isGlobalCooldown() {
return System.currentTimeMillis() < globalCooldown;
}

public boolean isNetherPortalCooldown() {
return System.currentTimeMillis() < netherPortalCooldown;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public PlayerLocation(String worldName, double posX, double posY, double posZ, f
}

public double getPosX() {
return posX;
return X;
}

public double getPosY() {
return posY;
return Y;
}

public double getPosZ() {
return posZ;
return Z;
}

public String getWorldName() {
Expand All @@ -45,4 +45,16 @@ public float getYaw() {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,17 @@

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ public class Config {

public int maxTriggerVisualisationSize = 1000;

public double throwbackStrength = 1;

public boolean playFailSound = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ public boolean inPortalRegion(BlockLocation loc, int extraBlocks) {
}

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());
Expand All @@ -90,7 +85,9 @@ public void playerMove(PlayerContainer player, PlayerLocation toLoc) {
&& portal.isTriggerBlock(blockMaterial))
|| (portal.isLocationInPortal(blockEntityTopLoc)
&& portal.isTriggerBlock(blockEntityTopMaterial))) {
portal.activate(player);
if(portal.activate(player, true)) {
return;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
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
Expand Down Expand Up @@ -67,4 +61,9 @@ public String getName() {
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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ public boolean sendPacket(String channel, byte[] bytes) {
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);
}
}

0 comments on commit 9448fd7

Please sign in to comment.