2020-04-23 07:12:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* 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:
|
2020-04-23 07:12:50 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-04-23 07:12:50 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* 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.
|
2020-04-23 07:12:50 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-04-23 07:12:50 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
package org.geysermc.connector.network.translators.effect;
|
2020-04-23 04:40:49 +00:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2020-08-03 21:42:43 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.effect.SoundEffect;
|
2020-04-23 04:40:49 +00:00
|
|
|
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;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
2020-10-08 22:33:36 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
2020-04-23 04:40:49 +00:00
|
|
|
import lombok.NonNull;
|
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-05-25 01:07:05 +00:00
|
|
|
import org.geysermc.connector.utils.FileUtils;
|
2020-04-23 04:40:49 +00:00
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
/**
|
|
|
|
* Registry for particles and effects.
|
|
|
|
*/
|
|
|
|
public class EffectRegistry {
|
2020-04-23 04:40:49 +00:00
|
|
|
|
2020-08-03 21:42:43 +00:00
|
|
|
public static final Map<SoundEffect, Effect> SOUND_EFFECTS = new HashMap<>();
|
2020-04-23 04:40:49 +00:00
|
|
|
public static final Int2ObjectMap<SoundEvent> RECORDS = new Int2ObjectOpenHashMap<>();
|
|
|
|
|
2020-10-08 22:33:36 +00:00
|
|
|
/**
|
|
|
|
* Java particle type to Bedrock particle ID
|
|
|
|
* Used for area effect clouds.
|
|
|
|
*/
|
|
|
|
private static final Object2IntMap<ParticleType> PARTICLE_TO_ID = new Object2IntOpenHashMap<>();
|
|
|
|
/**
|
|
|
|
* Java particle type to Bedrock level event
|
|
|
|
*/
|
|
|
|
private static final Map<ParticleType, LevelEventType> PARTICLE_TO_LEVEL_EVENT = new HashMap<>();
|
|
|
|
/**
|
|
|
|
* Java particle type to Bedrock namespaced string ID
|
|
|
|
*/
|
|
|
|
private static final Map<ParticleType, String> PARTICLE_TO_STRING = new HashMap<>();
|
2020-04-23 04:40:49 +00:00
|
|
|
|
|
|
|
public static void init() {
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
static {
|
|
|
|
/* Load particles */
|
2020-05-25 01:07:05 +00:00
|
|
|
InputStream particleStream = FileUtils.getResource("mappings/particles.json");
|
2020-04-23 04:40:49 +00:00
|
|
|
JsonNode particleEntries;
|
|
|
|
try {
|
2020-05-25 01:07:05 +00:00
|
|
|
particleEntries = GeyserConnector.JSON_MAPPER.readTree(particleStream);
|
2020-04-23 04:40:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load particle map", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> particlesIterator = particleEntries.fields();
|
2020-10-08 22:33:36 +00:00
|
|
|
try {
|
|
|
|
while (particlesIterator.hasNext()) {
|
|
|
|
Map.Entry<String, JsonNode> entry = particlesIterator.next();
|
|
|
|
JsonNode bedrockId = entry.getValue().get("bedrockId");
|
|
|
|
JsonNode bedrockIdNumeric = entry.getValue().get("bedrockNumericId");
|
|
|
|
JsonNode eventType = entry.getValue().get("eventType");
|
|
|
|
if (bedrockIdNumeric != null) {
|
|
|
|
PARTICLE_TO_ID.put(ParticleType.valueOf(entry.getKey().toUpperCase()), bedrockIdNumeric.asInt());
|
|
|
|
}
|
|
|
|
if (bedrockId != null) {
|
|
|
|
PARTICLE_TO_STRING.put(ParticleType.valueOf(entry.getKey().toUpperCase()), bedrockId.asText());
|
|
|
|
}
|
|
|
|
if (eventType != null) {
|
|
|
|
PARTICLE_TO_LEVEL_EVENT.put(ParticleType.valueOf(entry.getKey().toUpperCase()), LevelEventType.valueOf(eventType.asText().toUpperCase()));
|
2020-04-23 04:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-08 22:33:36 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2020-04-23 04:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load effects */
|
2020-05-25 01:07:05 +00:00
|
|
|
InputStream effectsStream = FileUtils.getResource("mappings/effects.json");
|
2020-04-23 04:40:49 +00:00
|
|
|
JsonNode effects;
|
|
|
|
try {
|
2020-05-25 01:07:05 +00:00
|
|
|
effects = GeyserConnector.JSON_MAPPER.readTree(effectsStream);
|
2020-04-23 04:40:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new AssertionError("Unable to load effects mappings", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> effectsIterator = effects.fields();
|
|
|
|
while (effectsIterator.hasNext()) {
|
|
|
|
Map.Entry<String, JsonNode> entry = effectsIterator.next();
|
2020-08-03 21:42:43 +00:00
|
|
|
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);
|
2020-04-23 04:40:49 +00:00
|
|
|
}
|
2020-08-03 21:42:43 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
GeyserConnector.getInstance().getLogger().warning("Failed to map sound effect " + entry.getKey() + " : " + e.toString());
|
2020-04-23 04:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 22:33:36 +00:00
|
|
|
/**
|
|
|
|
* @param type the Java particle to search for
|
|
|
|
* @return the Bedrock integer ID of the particle, or -1 if it does not exist
|
|
|
|
*/
|
|
|
|
public static int getParticleId(@NonNull ParticleType type) {
|
|
|
|
return PARTICLE_TO_ID.getOrDefault(type, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param type the Java particle to search for
|
|
|
|
* @return the level event equivalent Bedrock particle
|
|
|
|
*/
|
2020-04-23 04:40:49 +00:00
|
|
|
public static LevelEventType getParticleLevelEventType(@NonNull ParticleType type) {
|
2020-10-08 22:33:36 +00:00
|
|
|
return PARTICLE_TO_LEVEL_EVENT.getOrDefault(type, null);
|
2020-04-23 04:40:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 22:33:36 +00:00
|
|
|
/**
|
|
|
|
* @param type the Java particle to search for
|
|
|
|
* @return the namespaced ID equivalent for Bedrock
|
|
|
|
*/
|
|
|
|
public static String getParticleString(@NonNull ParticleType type) {
|
|
|
|
return PARTICLE_TO_STRING.getOrDefault(type, null);
|
2020-04-23 04:40:49 +00:00
|
|
|
}
|
2020-04-23 07:12:50 +00:00
|
|
|
}
|