Skip to content

Instantly share code, notes, and snippets.

@ReDestroyDeR
Created June 26, 2022 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReDestroyDeR/82b773a3a8b6d7d388d792eee4ddd14a to your computer and use it in GitHub Desktop.
Save ReDestroyDeR/82b773a3a8b6d7d388d792eee4ddd14a to your computer and use it in GitHub Desktop.
package ru.red.sorting.items
import net.minecraft.client.Minecraft
import net.minecraft.nbt.CompoundTag
import net.minecraft.world.{InteractionHand, InteractionResultHolder}
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.{Item, ItemStack, Rarity}
import net.minecraft.world.level.Level
import net.minecraftforge.event.TickEvent
import net.minecraftforge.eventbus.api.SubscribeEvent
import ru.red.sorting.manager.SortingManager
import java.util.Timer
import scala.util.Random
import scala.util.control.Breaks.break
object Shuffle extends Item(new Item.Properties()
.rarity(Rarity.EPIC)
.stacksTo(1)
.fireResistant()
) {
{
this.setRegistryName("shuffle")
}
override def use(pLevel: Level,
pPlayer: Player,
pUsedHand: InteractionHand): InteractionResultHolder[ItemStack] = {
val itemStack = pPlayer.getItemInHand(pUsedHand)
val plain = SortingManager.getById(
itemStack.getTag
.getInt("sortingPlainId")
)
val random = new Random()
val array = plain.getArray
var i = 0
val level = Minecraft.getInstance().level
def shuffle(): Unit = {
while (true) {
if (level != null && level.getLevelData.getGameTime % 5 == 0) {
val j = random.nextInt(array.length)
val t = array(j)
array(j) = array(i)
array(i) = t
plain.sync(array)
i += 1
if (i >= array.length) {
return
}
}
}
}
shuffle()
InteractionResultHolder.success(itemStack)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment