Skip to content

Commit

Permalink
mixin refactor, keybindings refactor and added CameraPullEntity netwo…
Browse files Browse the repository at this point in the history
…rking
  • Loading branch information
JR1811 committed Oct 7, 2024
1 parent d7e701c commit b8ddfd3
Show file tree
Hide file tree
Showing 23 changed files with 460 additions and 280 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/fabricatedatelier/mayor/MayorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.fabricatedatelier.mayor.entity.custom.client.CameraPullEntityRenderer;
import io.fabricatedatelier.mayor.init.MayorClientEvents;
import io.fabricatedatelier.mayor.init.MayorEntities;
import io.fabricatedatelier.mayor.init.MayorKeyBindings;
import io.fabricatedatelier.mayor.init.MayorKeyBind;
import io.fabricatedatelier.mayor.init.MayorRenderers;
import io.fabricatedatelier.mayor.network.CustomS2CNetworking;
import net.fabricmc.api.ClientModInitializer;
Expand All @@ -17,7 +17,7 @@ public class MayorClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
CustomS2CNetworking.initialize();
MayorKeyBindings.initialize();
MayorKeyBind.initialize();
MayorClientEvents.initialize();
MayorRenderers.initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import io.fabricatedatelier.mayor.camera.CameraHandler;
import io.fabricatedatelier.mayor.camera.mode.FreeFlyMode;
import io.fabricatedatelier.mayor.camera.util.CameraMode;
import io.fabricatedatelier.mayor.camera.util.CameraTarget;
import io.fabricatedatelier.mayor.init.MayorEntities;
import io.fabricatedatelier.mayor.util.NbtKeys;
import net.fabricmc.fabric.api.lookup.v1.entity.EntityApiLookup;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.data.DataTracker;
Expand All @@ -20,13 +16,15 @@
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.entity.EntityLookup;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.UUID;

public class CameraPullEntity extends Entity implements CameraTarget {
@Nullable
private DirectionInput movementInput = null;
private int tick = 0;
private static final TrackedData<Optional<UUID>> USER = DataTracker.registerData(CameraPullEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID);

public CameraPullEntity(EntityType<?> type, World world) {
Expand All @@ -41,6 +39,14 @@ public void setUser(@Nullable UUID user) {
this.dataTracker.set(USER, Optional.ofNullable(user));
}

public Optional<DirectionInput> getMovementInput() {
return Optional.ofNullable(movementInput);
}

public void setMovementInput(@Nullable DirectionInput movementInput) {
this.movementInput = movementInput;
}

@Override
protected void initDataTracker(DataTracker.Builder builder) {
builder.add(USER, Optional.empty());
Expand All @@ -60,28 +66,59 @@ public ActionResult interact(PlayerEntity player, Hand hand) {
return ActionResult.SUCCESS;
}

@Override
public void tick() {
super.tick();
tick++;
if (tick % 20 != 0) return;
double speed = 1.5;
Vec3d input = getMovementInput().map(DirectionInput::getDirection).orElse(new Vec3d(0, 0, 0));
this.addVelocity(input.multiply(speed));
}

@Override
protected void readCustomDataFromNbt(NbtCompound nbt) {
if (nbt.contains(NbtKeys.USER_UUID)) {
setUser(nbt.getUuid(NbtKeys.USER_UUID));
} else {
setUser(null);
}
if (nbt.contains(NbtKeys.USER_UUID)) setUser(nbt.getUuid(NbtKeys.USER_UUID));
else setUser(null);

if (nbt.contains(NbtKeys.DIRECTION_INPUT))
setMovementInput(DirectionInput.valueOf(nbt.getString(NbtKeys.DIRECTION_INPUT)));
else setMovementInput(null);
}

@Override
protected void writeCustomDataToNbt(NbtCompound nbt) {
getUser().ifPresentOrElse(userUUID -> {
nbt.putUuid(NbtKeys.USER_UUID, userUUID);
}, () -> nbt.remove(NbtKeys.USER_UUID));
getUser().ifPresentOrElse(userUUID -> nbt.putUuid(NbtKeys.USER_UUID, userUUID), () -> nbt.remove(NbtKeys.USER_UUID));
getMovementInput().ifPresentOrElse(directionInput -> nbt.putString(NbtKeys.DIRECTION_INPUT, directionInput.name()), () -> nbt.remove(NbtKeys.DIRECTION_INPUT));
}

public Optional<PlayerEntity> getUserPlayer() {
return getUser().flatMap(uuid -> Optional.ofNullable(this.getWorld().getPlayerByUuid(uuid)));
public static boolean hasCorrectUUID(Entity entity, ServerPlayerEntity player) {
if (!(entity instanceof CameraPullEntity cameraPullEntity)) return false;
if (cameraPullEntity.getUser().isEmpty()) return false;
return cameraPullEntity.getUser().get().equals(player.getUuid());
}

@Override
public Vec3d mayor$getTargetPosition() {
return this.getPos();
}

public enum DirectionInput {
FORWARD(new Vec3d(0, 0, 1)),
BACKWARD(new Vec3d(0, 0, -1)),
LEFT(new Vec3d(1, 0, 0)),
RIGHT(new Vec3d(-1, 0, 0)),
UP(new Vec3d(0, 1, 0)),
DOWN(new Vec3d(0, -1, 0));

private final Vec3d direction;

DirectionInput(Vec3d direction) {
this.direction = direction;
}

public Vec3d getDirection() {
return direction;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,35 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.render.RenderTickCounter;

@Environment(EnvType.CLIENT)
public class MayorClientEvents {

public static void initialize() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
if (!client.isInSingleplayer()) {
MayorManager.mayorStructureMap.clear();
}
});
ClientPlayConnectionEvents.JOIN.register(MayorClientEvents::handleClientConnection);
WorldRenderEvents.AFTER_ENTITIES.register(RenderUtil::renderVillageStructure);
HudRenderCallback.EVENT.register((drawContext, tickCounter) -> {
RenderUtil.renderMayorHud(drawContext);
CameraHandler camera = CameraHandler.getInstance();
if (camera.getTarget().isEmpty()) return;
if (camera.getStartTransition().isRunning()) {
camera.getStartTransition().renderOverlay(drawContext);
}
if (camera.getEndTransition().isRunning()) {
camera.getEndTransition().renderOverlay(drawContext);
}
});
HudRenderCallback.EVENT.register(MayorClientEvents::handleHudRendering);
}

private static void handleClientConnection(ClientPlayNetworkHandler handler, PacketSender sender, MinecraftClient client) {
if (!client.isInSingleplayer()) {
MayorManager.mayorStructureMap.clear();
}
}

private static void handleHudRendering(DrawContext drawContext, RenderTickCounter tickCounter) {
RenderUtil.renderMayorHud(drawContext);
CameraHandler camera = CameraHandler.getInstance();
if (camera.getTarget().isEmpty()) return;
if (camera.getStartTransition().isRunning()) {
camera.getStartTransition().renderOverlay(drawContext);
}
if (camera.getEndTransition().isRunning()) {
camera.getEndTransition().renderOverlay(drawContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.passive.IronGolemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Identifier;
Expand All @@ -24,57 +27,58 @@
import java.util.Map;

public class MayorCommonEvents {

public static void initialize() {
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
if (entity instanceof IronGolemEntity) {
BlockPos villageCenterPos = MayorStateHelper.getVillageCenterPos((ServerWorld) entity.getWorld(), entity.getBlockPos());
if (villageCenterPos != null) {
MayorStateHelper.updateVillageUuids((ServerWorld) entity.getWorld(), villageCenterPos, entity);
}
}
});
ServerLivingEntityEvents.AFTER_DEATH.register(MayorCommonEvents::handleAfterDeath);
ServerLifecycleEvents.SERVER_STARTED.register(MayorCommonEvents::handleServerStarted);
}

ServerLifecycleEvents.SERVER_STARTED.register((server) -> {
MayorManager.mayorStructureMap.clear();
private static void handleAfterDeath(LivingEntity entity, DamageSource damageSource) {
if (entity instanceof IronGolemEntity) {
BlockPos villageCenterPos = MayorStateHelper.getVillageCenterPos((ServerWorld) entity.getWorld(), entity.getBlockPos());
if (villageCenterPos != null) {
MayorStateHelper.updateVillageUuids((ServerWorld) entity.getWorld(), villageCenterPos, entity);
}
}
}

Iterator<Identifier> iterator = server.getStructureTemplateManager().streamTemplates().iterator();
while (iterator.hasNext()) {
Identifier identifier = iterator.next();
private static void handleServerStarted(MinecraftServer server) {
MayorManager.mayorStructureMap.clear();

if (StringUtil.shouldStoreStructureIdentifier(identifier)) {
Iterator<Identifier> iterator = server.getStructureTemplateManager().streamTemplates().iterator();
while (iterator.hasNext()) {
Identifier identifier = iterator.next();

Identifier mayorStructureIdentifier = StringUtil.getMayorStructureIdentifier(identifier);
int level = StringUtil.getStructureLevelByIdentifier(identifier);
List<ItemStack> requiredItemStacks = StructureHelper.getStructureItemRequirements(server.getOverworld(), identifier);
if (StringUtil.shouldStoreStructureIdentifier(identifier)) {

int experience = 0;
int price = 0;
if (StructureDataLoader.structureDataMap.containsKey(StringUtil.getMayorStructureString(identifier))) {
experience = StructureDataLoader.structureDataMap.get(StringUtil.getMayorStructureString(identifier)).get(0);
price = StructureDataLoader.structureDataMap.get(StringUtil.getMayorStructureString(identifier)).get(1);
} else {
experience = StructureHelper.getStructureExperience(requiredItemStacks);
price = 8;
StructureDataLoader.structureDataMap.put(StringUtil.getMayorStructureString(identifier), List.of(experience, price));
}
Map<BlockPos, BlockState> blockMap = StructureHelper.getBlockPosBlockStateMap(server.getOverworld(), identifier, BlockRotation.NONE, false);
MayorCategory.BiomeCategory biomeCategory = StructureHelper.getBiomeCategory(identifier);
MayorCategory.BuildingCategory buildingCategory = StructureHelper.getBuildingCategory(identifier);
Vec3i size = StructureHelper.getStructureSize(server.getOverworld(), identifier);
Identifier mayorStructureIdentifier = StringUtil.getMayorStructureIdentifier(identifier);
int level = StringUtil.getStructureLevelByIdentifier(identifier);
List<ItemStack> requiredItemStacks = StructureHelper.getStructureItemRequirements(server.getOverworld(), identifier);

MayorStructure mayorStructure = new MayorStructure(mayorStructureIdentifier, level, experience, price, biomeCategory, buildingCategory, requiredItemStacks, blockMap, size);
if (MayorManager.mayorStructureMap.containsKey(biomeCategory)) {
MayorManager.mayorStructureMap.get(biomeCategory).add(mayorStructure);
} else {
List<MayorStructure> list = new ArrayList<>();
list.add(mayorStructure);
MayorManager.mayorStructureMap.put(biomeCategory, list);
}
int experience;
int price;
if (StructureDataLoader.structureDataMap.containsKey(StringUtil.getMayorStructureString(identifier))) {
experience = StructureDataLoader.structureDataMap.get(StringUtil.getMayorStructureString(identifier)).get(0);
price = StructureDataLoader.structureDataMap.get(StringUtil.getMayorStructureString(identifier)).get(1);
} else {
experience = StructureHelper.getStructureExperience(requiredItemStacks);
price = 8;
StructureDataLoader.structureDataMap.put(StringUtil.getMayorStructureString(identifier), List.of(experience, price));
}
Map<BlockPos, BlockState> blockMap = StructureHelper.getBlockPosBlockStateMap(server.getOverworld(), identifier, BlockRotation.NONE, false);
MayorCategory.BiomeCategory biomeCategory = StructureHelper.getBiomeCategory(identifier);
MayorCategory.BuildingCategory buildingCategory = StructureHelper.getBuildingCategory(identifier);
Vec3i size = StructureHelper.getStructureSize(server.getOverworld(), identifier);

MayorStructure mayorStructure = new MayorStructure(mayorStructureIdentifier, level, experience, price, biomeCategory, buildingCategory, requiredItemStacks, blockMap, size);
if (MayorManager.mayorStructureMap.containsKey(biomeCategory)) {
MayorManager.mayorStructureMap.get(biomeCategory).add(mayorStructure);
} else {
List<MayorStructure> list = new ArrayList<>();
list.add(mayorStructure);
MayorManager.mayorStructureMap.put(biomeCategory, list);
}

}
});
}
}

}
65 changes: 65 additions & 0 deletions src/main/java/io/fabricatedatelier/mayor/init/MayorKeyBind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.fabricatedatelier.mayor.init;

import io.fabricatedatelier.mayor.util.KeyHelper;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import org.lwjgl.glfw.GLFW;

@Environment(EnvType.CLIENT)
public enum MayorKeyBind {
MAYOR_VIEW("mayor_view", GLFW.GLFW_KEY_Y, MayorKeyBindCategory.MAIN),
MAYOR_VIEW_SELECTION("mayor_view_selection", GLFW.GLFW_KEY_U, MayorKeyBindCategory.MAIN),
ROTATE_LEFT("structure_rotate_left", GLFW.GLFW_KEY_Q, MayorKeyBindCategory.MAIN),
ROTATE_RIGHT("structure_rotate_right", GLFW.GLFW_KEY_E, MayorKeyBindCategory.MAIN),
TARGET_TO_CENTER("structure_center", GLFW.GLFW_KEY_R, MayorKeyBindCategory.MAIN),
UPWARD("structure_upward", GLFW.GLFW_KEY_PAGE_UP, MayorKeyBindCategory.MAIN),
DOWNWARD("structure_downward", GLFW.GLFW_KEY_PAGE_DOWN, MayorKeyBindCategory.MAIN),
FREE_FLY_CAMERA_MODE("free_fly_camera_mode", GLFW.GLFW_KEY_SPACE, MayorKeyBindCategory.MAIN);

private final String name;

private final KeyBinding keyBinding;

MayorKeyBind(String name, int key, MayorKeyBindCategory category) {
this.name = name;
this.keyBinding = register(name, key, category.getTranslation());
}

public String getTranslation() {
return "key.mayor." + this.name;
}

public KeyBinding register(String name, int key, String category) {
return KeyBindingHelper.registerKeyBinding(new KeyBinding("key.mayor." + name, key, category));
}

public static void initialize() {
ClientTickEvents.END_CLIENT_TICK.register((client) -> {
KeyHelper.viewKey(client);
KeyHelper.centerKey(client);
KeyHelper.heightKey(client);
});
ClientTickEvents.START_CLIENT_TICK.register(KeyHelper::useKey);
}

public KeyBinding get() {
return keyBinding;
}

private enum MayorKeyBindCategory {
MAIN("key.categories.mayor");

private final String translation;

MayorKeyBindCategory(String translation) {
this.translation = translation;
}

public String getTranslation() {
return translation;
}
}
}
Loading

0 comments on commit b8ddfd3

Please sign in to comment.