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

Update effects mappings #949

Merged
merged 8 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -25,19 +25,18 @@

package org.geysermc.connector.network.translators.effect;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerPlayEffectPacket;
import org.geysermc.connector.network.session.GeyserSession;

@Getter
@Setter
@AllArgsConstructor
public class Effect {

private String javaName;
private String bedrockName;
private String type;
private int data;
private String identifier;

}
/**
* Represents an effect capable of translating itself into bedrock
*/
public interface Effect {
/**
* Translates the given {@link ServerPlayEffectPacket} into bedrock and sends it upstream.
*
* @param session GeyserSession
* @param packet the effect packet to handle
*/
void handleEffectPacket(GeyserSession session, ServerPlayEffectPacket packet);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.geysermc.connector.network.translators.effect;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.steveice10.mc.protocol.data.game.world.effect.SoundEffect;
import com.github.steveice10.mc.protocol.data.game.world.particle.ParticleType;
import com.nukkitx.protocol.bedrock.data.LevelEventType;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
Expand All @@ -46,7 +47,7 @@
*/
public class EffectRegistry {

public static final Map<String, Effect> EFFECTS = new HashMap<>();
public static final Map<SoundEffect, Effect> SOUND_EFFECTS = new HashMap<>();
public static final Int2ObjectMap<SoundEvent> RECORDS = new Int2ObjectOpenHashMap<>();

private static Map<ParticleType, LevelEventType> particleTypeMap = new HashMap<>();
Expand Down Expand Up @@ -97,19 +98,54 @@ public static void init() {
Iterator<Map.Entry<String, JsonNode>> effectsIterator = effects.fields();
while (effectsIterator.hasNext()) {
Map.Entry<String, JsonNode> entry = effectsIterator.next();
// Separate records database since they're handled differently between the two versions
if (entry.getValue().has("records")) {
JsonNode records = entry.getValue().get("records");
Iterator<Map.Entry<String, JsonNode>> recordsIterator = records.fields();
while (recordsIterator.hasNext()) {
Map.Entry<String, JsonNode> recordEntry = recordsIterator.next();
RECORDS.put(Integer.parseInt(recordEntry.getKey()), SoundEvent.valueOf(recordEntry.getValue().asText()));
JsonNode node = entry.getValue();
try {
String type = node.get("type").asText();
SoundEffect javaEffect = null;
Effect effect = null;
switch (type) {
case "soundLevel": {
javaEffect = SoundEffect.valueOf(entry.getKey());
LevelEventType levelEventType = LevelEventType.valueOf(node.get("name").asText());
int data = node.has("data") ? node.get("data").intValue() : 0;
effect = new SoundLevelEffect(levelEventType, data);
break;
}
case "soundEvent": {
javaEffect = SoundEffect.valueOf(entry.getKey());
SoundEvent soundEvent = SoundEvent.valueOf(node.get("name").asText());
String identifier = node.has("identifier") ? node.get("identifier").asText() : "";
int extraData = node.has("extraData") ? node.get("extraData").intValue() : -1;
effect = new SoundEventEffect(soundEvent, identifier, extraData);
break;
}
case "playSound": {
javaEffect = SoundEffect.valueOf(entry.getKey());
String name = node.get("name").asText();
float volume = node.has("volume") ? node.get("volume").floatValue() : 1.0f;
boolean pitchSub = node.has("pitch_sub") ? node.get("pitch_sub").booleanValue() : false;
float pitchMul = node.has("pitch_mul") ? node.get("pitch_mul").floatValue() : 1.0f;
float pitchAdd = node.has("pitch_add") ? node.get("pitch_add").floatValue() : 0.0f;
boolean relative = node.has("relative") ? node.get("relative").booleanValue() : true;
effect = new PlaySoundEffect(name, volume, pitchSub, pitchMul, pitchAdd, relative);
break;
}
case "record": {
JsonNode records = entry.getValue().get("records");
Iterator<Map.Entry<String, JsonNode>> recordsIterator = records.fields();
while (recordsIterator.hasNext()) {
Map.Entry<String, JsonNode> recordEntry = recordsIterator.next();
RECORDS.put(Integer.parseInt(recordEntry.getKey()), SoundEvent.valueOf(recordEntry.getValue().asText()));
}
break;
}
}
if (javaEffect != null) {
SOUND_EFFECTS.put(javaEffect, effect);
}
} catch (Exception e) {
GeyserConnector.getInstance().getLogger().warning("Failed to map sound effect " + entry.getKey() + " : " + e.toString());
}
String identifier = (entry.getValue().has("identifier")) ? entry.getValue().get("identifier").asText() : "";
int data = (entry.getValue().has("data")) ? entry.getValue().get("data").asInt() : -1;
Effect effect = new Effect(entry.getKey(), entry.getValue().get("name").asText(), entry.getValue().get("type").asText(), data, identifier);
EFFECTS.put(entry.getKey(), effect);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*
*/

package org.geysermc.connector.network.translators.effect;

import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerPlayEffectPacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.packet.PlaySoundPacket;
import lombok.Value;
import org.geysermc.connector.network.session.GeyserSession;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

@Value
public class PlaySoundEffect implements Effect {
/**
* Bedrock playsound identifier
*/
String name;

/**
* Volume of the sound
*/
float volume;

/**
* If true, the initial value used for random pitch is the difference between two random floats.
* If false, it is a single random float
*/
boolean pitchSub;

/**
* Multiplier for random pitch value
*/
float pitchMul;

/**
* Constant addition to random pitch value after multiplier
*/
float pitchAdd;

/**
* True if the sound is meant to be played in 3d space
*/
boolean relative;

@Override
public void handleEffectPacket(GeyserSession session, ServerPlayEffectPacket packet) {
Random rand = ThreadLocalRandom.current();
PlaySoundPacket playSoundPacket = new PlaySoundPacket();
playSoundPacket.setSound(name);
playSoundPacket.setPosition(!relative ? session.getPlayerEntity().getPosition() : Vector3f.from(packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ()).add(0.5f, 0.5f, 0.5f));
playSoundPacket.setVolume(volume);
playSoundPacket.setPitch((pitchSub ? (rand.nextFloat() - rand.nextFloat()) : rand.nextFloat()) * pitchMul + pitchAdd); //replicates java client randomness
session.sendUpstreamPacket(playSoundPacket);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*
*/

package org.geysermc.connector.network.translators.effect;

import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerPlayEffectPacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.SoundEvent;
import com.nukkitx.protocol.bedrock.packet.LevelSoundEventPacket;
import lombok.Value;
import org.geysermc.connector.network.session.GeyserSession;

@Value
public class SoundEventEffect implements Effect {
/**
* Bedrock sound event
*/
SoundEvent soundEvent;

/**
* Entity identifier. Usually an empty string
*/
String identifier;

/**
* Extra data. Usually -1
*/
int extraData;

@Override
public void handleEffectPacket(GeyserSession session, ServerPlayEffectPacket packet) {
LevelSoundEventPacket levelSoundEvent = new LevelSoundEventPacket();
levelSoundEvent.setSound(soundEvent);
levelSoundEvent.setIdentifier(identifier);
levelSoundEvent.setExtraData(extraData);
levelSoundEvent.setRelativeVolumeDisabled(packet.isBroadcast());
levelSoundEvent.setPosition(Vector3f.from(packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ()).add(0.5f, 0.5f, 0.5f));
levelSoundEvent.setBabySound(false);
session.sendUpstreamPacket(levelSoundEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*
*/

package org.geysermc.connector.network.translators.effect;

import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerPlayEffectPacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.LevelEventType;
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
import lombok.Value;
import org.geysermc.connector.network.session.GeyserSession;

@Value
public class SoundLevelEffect implements Effect {
/**
* Bedrock level event type
*/
LevelEventType levelEventType;

/**
* Event data. Usually 0
*/
int data;

@Override
public void handleEffectPacket(GeyserSession session, ServerPlayEffectPacket packet) {
LevelEventPacket eventPacket = new LevelEventPacket();
eventPacket.setType(levelEventType);
eventPacket.setData(data);
eventPacket.setPosition(Vector3f.from(packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ()).add(0.5f, 0.5f, 0.5f));
session.sendUpstreamPacket(eventPacket);
}
}
Loading