mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-04 15:41:38 +00:00
Seat Type
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package net.momirealms.craftengine.core.entity.furniture;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class AbstractSeat implements Seat{
|
||||
protected final Vector3f offset;
|
||||
protected final float yaw;
|
||||
|
||||
public AbstractSeat(Vector3f offset, float yaw) {
|
||||
this.offset = offset;
|
||||
this.yaw = yaw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector3f offset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float yaw() {
|
||||
return yaw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof AbstractSeat seat)) return false;
|
||||
return Float.compare(yaw, seat.yaw()) == 0 && offset.equals(seat.offset());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = Objects.hash(offset);
|
||||
result = 31 * result + Float.hashCode(yaw);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -13,11 +13,7 @@ public interface HitBoxFactory {
|
||||
static Seat[] getSeats(Map<String, Object> arguments) {
|
||||
List<String> seats = (List<String>) arguments.getOrDefault("seats", List.of());
|
||||
return seats.stream()
|
||||
.map(arg -> {
|
||||
String[] split = arg.split(" ");
|
||||
if (split.length == 1) return new Seat(MiscUtils.getAsVector3f(split[0], "seats"), 0, false);
|
||||
return new Seat(MiscUtils.getAsVector3f(split[0], "seats"), Float.parseFloat(split[1]), true);
|
||||
})
|
||||
.map(SeatType::fromString)
|
||||
.toArray(Seat[]::new);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,14 @@
|
||||
package net.momirealms.craftengine.core.entity.furniture;
|
||||
|
||||
import net.momirealms.craftengine.core.entity.Entity;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import java.util.Objects;
|
||||
public interface Seat {
|
||||
|
||||
public record Seat(Vector3f offset, float yaw, boolean limitPlayerRotation) {
|
||||
Entity spawn(Player player, Furniture furniture);
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof Seat seat)) return false;
|
||||
return Float.compare(yaw, seat.yaw) == 0 && Objects.equals(offset, seat.offset) && limitPlayerRotation == seat.limitPlayerRotation;
|
||||
}
|
||||
Vector3f offset();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = Objects.hashCode(offset);
|
||||
result = 31 * result + Float.hashCode(yaw);
|
||||
result = 31 * result + Boolean.hashCode(limitPlayerRotation);
|
||||
return result;
|
||||
}
|
||||
float yaw();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package net.momirealms.craftengine.core.entity.furniture;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SeatFactory {
|
||||
|
||||
Seat create(List<String> args);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.momirealms.craftengine.core.entity.furniture;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.registry.Registries;
|
||||
import net.momirealms.craftengine.core.registry.WritableRegistry;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SeatType {
|
||||
public static final Key SIT = Key.of("craftengine:sit");
|
||||
public static final Key LAY = Key.of("craftengine:lay");
|
||||
public static final Key CRAWL = Key.of("craftengine:crawl");
|
||||
|
||||
public static void register(Key key, SeatFactory factory) {
|
||||
Holder.Reference<SeatFactory> holder =((WritableRegistry<SeatFactory>) BuiltInRegistries.SEAT_FACTORY)
|
||||
.registerForHolder(new ResourceKey<>(Registries.SEAT_FACTORY.location(), key));
|
||||
holder.bindValue(factory);
|
||||
}
|
||||
|
||||
public static Seat fromString(String s) {
|
||||
List<String> split = Lists.newArrayList(s.split(" "));
|
||||
int last = split.size() - 1;
|
||||
Key type = SIT;
|
||||
SeatFactory factory;
|
||||
try {
|
||||
Float.parseFloat(split.get(last));
|
||||
} catch (NullPointerException | NumberFormatException e) {
|
||||
type = Key.withDefaultNamespace(split.get(last), "craftengine");
|
||||
split.remove(last);
|
||||
}
|
||||
factory = BuiltInRegistries.SEAT_FACTORY.getValue(type);
|
||||
if (factory == null) {
|
||||
throw new LocalizedException("warning.config.furniture.hitbox.invalid_type");
|
||||
}
|
||||
return factory.create(split);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package net.momirealms.craftengine.core.entity.player;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.core.entity.AbstractEntity;
|
||||
import net.momirealms.craftengine.core.entity.seat.SeatEntity;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.plugin.context.CooldownData;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
@@ -140,4 +141,8 @@ public abstract class Player extends AbstractEntity implements NetWorkUser {
|
||||
public abstract void clearPotionEffects();
|
||||
|
||||
public abstract CooldownData cooldown();
|
||||
|
||||
public abstract void setSeat(SeatEntity seatEntity);
|
||||
|
||||
public abstract SeatEntity seat();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package net.momirealms.craftengine.core.entity.seat;
|
||||
|
||||
import net.momirealms.craftengine.core.entity.AbstractEntity;
|
||||
import net.momirealms.craftengine.core.entity.Entity;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
|
||||
public abstract class SeatEntity extends AbstractEntity {
|
||||
|
||||
public abstract void sync(Player to);
|
||||
|
||||
public abstract void dismount(Player from);
|
||||
|
||||
public abstract void remove();
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.block.CustomBlock;
|
||||
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.block.properties.PropertyFactory;
|
||||
import net.momirealms.craftengine.core.entity.furniture.HitBoxFactory;
|
||||
import net.momirealms.craftengine.core.entity.furniture.SeatFactory;
|
||||
import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.item.recipe.CustomSmithingTransformRecipe;
|
||||
import net.momirealms.craftengine.core.item.recipe.RecipeFactory;
|
||||
@@ -56,6 +57,9 @@ public class BuiltInRegistries {
|
||||
public static final Registry<FunctionFactory<PlayerOptionalContext>> EVENT_FUNCTION_FACTORY = createRegistry(Registries.EVENT_FUNCTION_FACTORY);
|
||||
public static final Registry<ConditionFactory<PlayerOptionalContext>> EVENT_CONDITION_FACTORY = createRegistry(Registries.EVENT_CONDITION_FACTORY);
|
||||
public static final Registry<PlayerSelectorFactory<?>> PLAYER_SELECTOR_FACTORY = createRegistry(Registries.PLAYER_SELECTOR_FACTORY);
|
||||
public static final Registry<Factory<Function<PlayerBlockActionContext>>> PLAYER_BLOCK_FUNCTION_FACTORY = createRegistry(Registries.PLAYER_BLOCK_FUNCTION_FACTORY);
|
||||
public static final Registry<Factory<Condition<PlayerBlockActionContext>>> PLAYER_BLOCK_CONDITION_FACTORY = createRegistry(Registries.PLAYER_BLOCK_CONDITION_FACTORY);
|
||||
public static final Registry<SeatFactory> SEAT_FACTORY = createRegistry(Registries.SEAT_FACTORY);
|
||||
|
||||
private static <T> Registry<T> createRegistry(ResourceKey<? extends Registry<T>> key) {
|
||||
return new MappedRegistry<>(key);
|
||||
|
||||
@@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.block.CustomBlock;
|
||||
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.block.properties.PropertyFactory;
|
||||
import net.momirealms.craftengine.core.entity.furniture.HitBoxFactory;
|
||||
import net.momirealms.craftengine.core.entity.furniture.SeatFactory;
|
||||
import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.item.recipe.CustomSmithingTransformRecipe;
|
||||
import net.momirealms.craftengine.core.item.recipe.RecipeFactory;
|
||||
@@ -57,4 +58,5 @@ public class Registries {
|
||||
public static final ResourceKey<Registry<FunctionFactory<PlayerOptionalContext>>> EVENT_FUNCTION_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("event_function_factory"));
|
||||
public static final ResourceKey<Registry<ConditionFactory<PlayerOptionalContext>>> EVENT_CONDITION_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("event_condition_factory"));
|
||||
public static final ResourceKey<Registry<PlayerSelectorFactory<?>>> PLAYER_SELECTOR_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("player_selector"));
|
||||
public static final ResourceKey<Registry<SeatFactory>> SEAT_FACTORY = new ResourceKey<>(ROOT_REGISTRY, Key.withDefaultNamespace("seat_factory"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user