Skip to content

Commit

Permalink
fix(swap)
Browse files Browse the repository at this point in the history
If an item was swapped with a vibe in a container, it would not change the source.
  • Loading branch information
auxves committed Feb 27, 2021
1 parent 034c03c commit ec3f54f
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 142 deletions.
48 changes: 12 additions & 36 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.modrinth.minotaur.TaskModrinthUpload

java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8

fun getArtifactPath(): String {
return tasks.named<Jar>("jar").get().archiveFile.get().toString()
}

fun p(name: String): String {
return project.property(name).toString()
}
fun p(name: String) = project.property(name).toString()

plugins {
id("fabric-loom")
id("com.modrinth.minotaur") version "1.1.0"
kotlin("jvm") version "1.4.21"
}

repositories {
maven("https://maven.fabricmc.net/")
kotlin("jvm")
}

dependencies {
minecraft("com.mojang:minecraft:${p("minecraft.target")}")
minecraft("com.mojang:minecraft:${p("minecraft")}")
mappings("net.fabricmc:yarn:${p("yarn")}:v2")

modImplementation("net.fabricmc:fabric-loader:${p("loader")}")
modImplementation("net.fabricmc.fabric-api:fabric-api:${p("fabric_api")}")

listOf("fabric-resource-loader-v0", "fabric-networking-api-v1").forEach {
modImplementation(fabricApi.module(it, p("fabric_api")))
}

modImplementation("net.fabricmc:fabric-language-kotlin:${p("fabric_kotlin")}")
}

Expand All @@ -35,7 +28,7 @@ tasks.named<Copy>("processResources") {

from(sourceSets["main"].resources.srcDirs) {
include("fabric.mod.json")
expand("version" to project.version)
expand(project.properties)
}

from(sourceSets["main"].resources.srcDirs) {
Expand All @@ -47,23 +40,6 @@ tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

task<TaskModrinthUpload>("modrinth") {
token = System.getenv("MODRINTH_TOKEN")
projectId = p("id")

versionNumber = p("version")
versionName = "${project.property("name")} v$versionNumber"

uploadFile = getArtifactPath()

addGameVersion(p("minecraft.target"))

p("minecraft.compatible").split(", ").forEach { addGameVersion(it) }

addLoader("fabric")
}

task("publish") {
dependsOn("build")
dependsOn("modrinth")
tasks.withType<Jar> {
from("LICENSE")
}
19 changes: 7 additions & 12 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ org.gradle.jvmargs=-Xmx1G

# Project
id=vibes
name=Vibes
version=1.0.0

# Curse

curse.id=??????
title=Vibes
version=1.0.1

# Versions (https://modmuss50.me/fabric.html)
minecraft.target=1.16.4
minecraft.compatible=1.16.3, 1.16.2
yarn=1.16.4+build.7
loader=0.10.8
minecraft=1.16.5
yarn=1.16.5+build.5
loader=0.11.2

fabric_api=0.29.2+1.16
fabric_kotlin=1.4.21+build.1
fabric_api=0.31.0+1.16
fabric_kotlin=1.4.30+build.2
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pluginManagement {

plugins {
id("fabric-loom") version "0.5-SNAPSHOT"
kotlin("jvm") version embeddedKotlinVersion
kotlin("jvm") version "1.4.21"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(AbstractFurnaceBlockEntity.class)
abstract
class AbstractFurnaceBlockEntityMixin extends BlockEntity {
@Shadow public abstract ItemStack getStack(int slot);

public AbstractFurnaceBlockEntityMixin(BlockEntityType<?> type) {
super(type);
}

@Inject(method = "setStack", at = @At("HEAD"))
private void onSetStack(int slot, ItemStack stack, CallbackInfo ci) {
ServerNetworking.INSTANCE.changePositionProvider(stack, this);
ServerNetworking.INSTANCE.changePositionProvider(stack, this, getStack(slot));
}

@Inject(method = "removeStack(II)Lnet/minecraft/item/ItemStack;", at = @At("RETURN"))
private void onRemoveStack(int slot, int amount, CallbackInfoReturnable<ItemStack> cir) {
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), this.world);
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), world);
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/glossnyx/vibes/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EntityMixin {

@Inject(method = "remove", at = @At("HEAD"))
private void onRemove(CallbackInfo ci) {
if (this.world.isClient) return;
if (world.isClient) return;

//noinspection ConstantConditions
if (!ItemEntity.class.isInstance(this)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -16,26 +18,28 @@

@Mixin(HopperBlockEntity.class)
class HopperBlockEntityMixin extends BlockEntity {
@Shadow private DefaultedList<ItemStack> inventory;

public HopperBlockEntityMixin(BlockEntityType<?> type) {
super(type);
}

@Inject(method = "setStack", at = @At("HEAD"))
private void onSetStack(int slot, ItemStack stack, CallbackInfo ci) {
ServerNetworking.INSTANCE.changePositionProvider(stack, this);
ServerNetworking.INSTANCE.changePositionProvider(stack, this, inventory.get(slot));
}

@Inject(method = "removeStack(II)Lnet/minecraft/item/ItemStack;", at = @At("RETURN"))
private void onRemoveStack(int slot, int amount, CallbackInfoReturnable<ItemStack> cir) {
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), this.world);
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), world);
}

@Redirect(
method = "extract(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/entity/ItemEntity;)Z",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/ItemEntity;remove()V")
)
private static void onRemoveItem(ItemEntity entity) {
if (!entity.world.isClient && ItemsKt.vibeTypeOf(entity.getStack()) == VibeType.VIBE) entity.removed = true;
if (!entity.world.isClient && ItemsKt.isPlaying(entity.getStack())) entity.removed = true;
else entity.remove();
}
}
1 change: 0 additions & 1 deletion src/main/java/io/glossnyx/vibes/mixin/ItemEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
abstract
class ItemEntityMixin {
@Shadow private int age;
@Shadow public abstract ItemStack getStack();

@Redirect(
method = "onPlayerCollision",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(LootableContainerBlockEntity.class)
abstract
class LootableContainerBlockEntityMixin extends BlockEntity {
@Shadow public abstract ItemStack getStack(int slot);

public LootableContainerBlockEntityMixin(BlockEntityType<?> type) {
super(type);
}

@Inject(method = "setStack", at = @At("HEAD"))
private void onSetStack(int slot, ItemStack stack, CallbackInfo ci) {
ServerNetworking.INSTANCE.changePositionProvider(stack, this);
ServerNetworking.INSTANCE.changePositionProvider(stack, this, getStack(slot));
}

@Inject(method = "removeStack(II)Lnet/minecraft/item/ItemStack;", at = @At("RETURN"))
private void onRemoveStack(int slot, int amount, CallbackInfoReturnable<ItemStack> cir) {
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), this.world);
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), world);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
class ShulkerBoxBlockMixin {
@Inject(method = "onPlaced", at = @At("HEAD"))
private void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack, CallbackInfo ci) {
ServerNetworking.INSTANCE.changePositionProvider(itemStack, world.getBlockEntity(pos));
ServerNetworking.INSTANCE.changePositionProvider(itemStack, world.getBlockEntity(pos), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private void onSetStack(int slot, ItemStack stack, CallbackInfo ci) {

@Inject(method = "removeStack(II)Lnet/minecraft/item/ItemStack;", at = @At("RETURN"))
private void onRemoveStack(int slot, int amount, CallbackInfoReturnable<ItemStack> cir) {
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), this.world);
ServerNetworking.INSTANCE.changePositionProvider(cir.getReturnValue(), world);
}

@Override
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/io/glossnyx/vibes/item/Vibe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ object Vibe : Item(settings) {

override fun appendTooltip(stack: ItemStack?, world: World?, tooltip: MutableList<Text>?, context: TooltipContext?) {
if (tooltip == null || stack == null) return

val tag = stack.getSubTag(Tags.DISC) ?: return

ItemStack.fromTag(tag).item.appendTooltip(stack, world, tooltip, context)
}
}
42 changes: 14 additions & 28 deletions src/main/kotlin/io/glossnyx/vibes/network/ClientNetworking.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.minecraft.client.MinecraftClient
import net.minecraft.client.world.ClientWorld
import net.minecraft.entity.Entity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
Expand All @@ -20,16 +18,18 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
import java.util.UUID

object ClientNetworking {
private val client = MinecraftClient.getInstance()
private val vibeInstances = mutableListOf<VibeInstance>()

private fun getInstance(uuid: UUID): VibeInstance? {
return vibeInstances.find { it.uuid == uuid }
}
private fun getInstance(uuid: UUID) = vibeInstances.find { it.uuid == uuid }

private fun getEntity(uuid: UUID) = client.world?.let { world ->
world.players.plus(world.entities).find { it.uuid == uuid }
} ?: throw Exception("entity with uuid $uuid not found")

private fun getEntity(world: ClientWorld, uuid: UUID): Entity {
return world.players.find { it.uuid == uuid }
?: world.entities.find { it.uuid == uuid }
?: throw Exception("entity with uuid $uuid not found")
private fun stop(uuid: UUID) = getInstance(uuid)?.let {
client.soundManager.stop(it)
vibeInstances.remove(it)
}

fun onRightClick(slot: Slot, clickType: Int, actionType: SlotActionType, player: PlayerEntity, cir: CallbackInfoReturnable<ItemStack>) {
Expand Down Expand Up @@ -68,37 +68,23 @@ object ClientNetworking {

fun init() {
register<Play> { data ->
val client = MinecraftClient.getInstance()

getInstance(data.uuid)?.let {
client.soundManager.stop(it)
vibeInstances.remove(it)
}

val world = client.world ?: throw Exception("no world")
val entity = getEntity(world, data.entityUUID)
stop(data.uuid)

val entity = getEntity(data.entityUUID)
val instance = VibeInstance(data.uuid, EntityPositionProvider(entity), SoundEvent(data.identifier))
vibeInstances.add(instance)

client.soundManager.play(instance)
}

register<Stop> { data ->
getInstance(data.uuid)?.let {
MinecraftClient.getInstance().soundManager.stop(it)
vibeInstances.remove(it)
}
}
register<Stop> { data -> stop(data.uuid) }

register<ChangePositionEntity> { data ->
getInstance(data.uuid)?.let {
GlobalScope.launch {
val world = MinecraftClient.getInstance().world ?: throw Exception("no world")

val entity = runCatching { getEntity(world, data.entityUUID) }.getOrElse {
val entity = runCatching { getEntity(data.entityUUID) }.getOrElse {
delay(100)
getEntity(world, data.entityUUID)
getEntity(data.entityUUID)
}

it.position = EntityPositionProvider(entity)
Expand Down
17 changes: 10 additions & 7 deletions src/main/kotlin/io/glossnyx/vibes/network/ServerNetworking.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object ServerNetworking {
/** Stops playing sounds */
fun stopPlaying(stack: ItemStack, world: World) {
forEachVibe(stack) {
world.sendAll(Stop(uuidOf(it)!!))
world.sendAll(Stop(uuidOf(it) ?: return@forEachVibe))
}
}

Expand All @@ -52,7 +52,6 @@ object ServerNetworking {
fun onPickup(player: PlayerEntity, entity: Entity?) {
if (entity == null) return
if (entity !is ItemEntity) return
if (player.world.isClient) return

changePositionProvider(entity.stack, player)
}
Expand Down Expand Up @@ -81,12 +80,15 @@ object ServerNetworking {
}

/** When player puts vibe into block */
fun changePositionProvider(stack: ItemStack, block: BlockEntity?) {
if (block == null) return
if (block.world!!.isClient) return
fun changePositionProvider(stack: ItemStack, block: BlockEntity?, oldStack: ItemStack?) {
val world = block?.world ?: return
if (world.isClient) return

if (oldStack != null) changePositionProvider(oldStack, world)

forEachVibe(stack) {
block.world!!.sendAll(ChangePositionBlock(uuidOf(it)!!, block.pos))
val uuid = uuidOf(it) ?: return@forEachVibe
world.sendAll(ChangePositionBlock(uuid, block.pos))
}
}

Expand All @@ -96,7 +98,8 @@ object ServerNetworking {
if (entity.world.isClient) return

forEachVibe(stack) {
entity.world.sendAll(ChangePositionEntity(uuidOf(it)!!, entity.uuid))
val uuid = uuidOf(it) ?: return@forEachVibe
entity.world.sendAll(ChangePositionEntity(uuid, entity.uuid))
}
}

Expand Down
Loading

0 comments on commit ec3f54f

Please sign in to comment.