Files
src
592481__baconation__coin-flip-9.mp3
592481__baconation__coin-flip-9.ogg
build.gradle
gradle.properties
settings.gradle
Code
21 11,678
src/main/java/br/kodari/moedarng/MoedaRng.java
package br.kodari.moedarng;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
public class MoedaRng implements ModInitializer {
public static final Identifier FLIP = new Identifier("moeda_rng", "flip");
public static final Item COIN = Registry.register(Registries.ITEM, new Identifier("moeda_rng", "moeda_dourada"), new Item(new Item.Settings().maxDamage(50)));
public static final SoundEvent FLIP_SOUND = Registry.register(Registries.SOUND_EVENT, new Identifier("moeda_rng", "coin_flip"), SoundEvent.of(new Identifier("moeda_rng", "coin_flip")));
private final List<PendingFlip> pending = new ArrayList<>();
@Override
public void onInitialize() {
RegistryKey<ItemGroup> tools = RegistryKey.of(RegistryKeys.ITEM_GROUP, new Identifier("minecraft", "tools_and_utilities"));
ItemGroupEvents.modifyEntriesEvent(tools).register(entries -> entries.add(COIN));
ServerPlayNetworking.registerGlobalReceiver(FLIP, (server, player, handler, buf, sender) -> server.execute(() -> flip(player)));
ServerTickEvents.END_SERVER_TICK.register(server -> {
Iterator<PendingFlip> iterator = pending.iterator();
while (iterator.hasNext()) {
PendingFlip flip = iterator.next();
if (--flip.ticks == 0) {
iterator.remove();
ServerPlayerEntity player = server.getPlayerManager().getPlayer(flip.player.getUuid());
if (player == flip.player && player.isAlive()) outcome(player);
}
}
});
}
private void flip(ServerPlayerEntity player) {
ItemStack stack = player.getStackInHand(Hand.MAIN_HAND);
if (!stack.isOf(COIN) || player.getItemCooldownManager().isCoolingDown(COIN)) return;
player.getItemCooldownManager().set(COIN, 40);
ServerWorld world = player.getServerWorld();
world.playSound(null, player.getX(), player.getY(), player.getZ(), FLIP_SOUND, SoundCategory.PLAYERS, 1.0f, 1.0f);
world.spawnParticles(ParticleTypes.CRIT, player.getX(), player.getY() + 1.5, player.getZ(), 12, 0.25, 0.3, 0.25, 0.1);
stack.damage(1, player, p -> p.sendToolBreakStatus(Hand.MAIN_HAND));
pending.add(new PendingFlip(player));
}
private void outcome(ServerPlayerEntity player) {
ServerWorld world = player.getServerWorld();
if (world.random.nextInt(100) == 0) {
player.sendMessage(Text.literal("A moeda decidiu seu destino..."), true);
player.kill();
return;
}
int roll = world.random.nextInt(20);
switch (roll) {
case 0 -> {
player.sendMessage(Text.literal("Chuva de diamantes!"), true);
for (int i = 0; i < 12; i++) {
double x = player.getX() + world.random.nextDouble() * 8 - 4;
double z = player.getZ() + world.random.nextDouble() * 8 - 4;
world.spawnEntity(new ItemEntity(world, x, player.getY() + 8 + world.random.nextInt(4), z, new ItemStack(Items.DIAMOND)));
}
}
case 1 -> {
player.sendMessage(Text.literal("Esmeraldas da sorte!"), true);
player.dropItem(new ItemStack(Items.EMERALD, 5), false);
}
case 2 -> {
player.sendMessage(Text.literal("Uma tempestade começou!"), true);
world.setWeather(0, 6000, true, true);
spawn(world, EntityType.LIGHTNING_BOLT, player, 3, 0);
}
case 3 -> {
player.sendMessage(Text.literal("Zumbis apareceram!"), true);
for (int i = 0; i < 3; i++) spawn(world, EntityType.ZOMBIE, player, i - 1, 3);
}
case 4 -> {
player.sendMessage(Text.literal("Cuidado com o creeper!"), true);
spawn(world, EntityType.CREEPER, player, 3, 3);
}
case 5 -> {
player.sendMessage(Text.literal("A sorte trouxe ouro!"), true);
player.dropItem(new ItemStack(Items.GOLD_INGOT, 6), false);
}
case 6 -> {
player.sendMessage(Text.literal("Um raio caiu perto de você!"), true);
spawn(world, EntityType.LIGHTNING_BOLT, player, 1, 1);
}
case 7 -> {
player.sendMessage(Text.literal("Uma horda de esqueletos apareceu!"), true);
for (int i = 0; i < 3; i++) spawn(world, EntityType.SKELETON, player, i * 2 - 2, 4);
}
case 8 -> {
player.sendMessage(Text.literal("Aranhas!"), true);
for (int i = 0; i < 3; i++) spawn(world, EntityType.SPIDER, player, i * 2 - 2, 3);
}
case 9 -> {
player.sendMessage(Text.literal("Uma bruxa apareceu!"), true);
spawn(world, EntityType.WITCH, player, 4, 2);
}
case 10 -> {
player.sendMessage(Text.literal("Animais apareceram!"), true);
spawn(world, EntityType.COW, player, 2, 2);
spawn(world, EntityType.SHEEP, player, -2, 2);
}
case 11 -> {
player.sendMessage(Text.literal("A sorte trouxe ferro!"), true);
player.dropItem(new ItemStack(Items.IRON_INGOT, 12), false);
}
case 12 -> {
player.sendMessage(Text.literal("Um lanche dourado!"), true);
player.dropItem(new ItemStack(Items.GOLDEN_APPLE), false);
}
case 13 -> {
player.sendMessage(Text.literal("Comida para a viagem!"), true);
player.dropItem(new ItemStack(Items.BREAD, 8), false);
}
case 14 -> {
player.sendMessage(Text.literal("Velocidade da sorte!"), true);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.SPEED, 1200, 1));
}
case 15 -> {
player.sendMessage(Text.literal("A moeda restaurou suas forças!"), true);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 200, 1));
}
case 16 -> {
player.sendMessage(Text.literal("Força da sorte!"), true);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.STRENGTH, 600, 1));
}
case 17 -> {
player.sendMessage(Text.literal("Você ficou invisível!"), true);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.INVISIBILITY, 600));
}
case 18 -> {
player.sendMessage(Text.literal("A moeda estava envenenada!"), true);
player.addStatusEffect(new StatusEffectInstance(StatusEffects.POISON, 160));
}
default -> {
player.sendMessage(Text.literal("O céu se abriu!"), true);
world.setWeather(6000, 0, false, false);
}
}
world.spawnParticles(ParticleTypes.CRIT, player.getX(), player.getY() + 1, player.getZ(), 20, 0.5, 0.5, 0.5, 0.1);
}
private static void spawn(ServerWorld world, EntityType<?> type, ServerPlayerEntity player, double x, double z) {
Entity entity = type.create(world);
if (entity != null) {
entity.refreshPositionAndAngles(player.getX() + x, player.getY(), player.getZ() + z, 0, 0);
world.spawnEntity(entity);
}
}
private static class PendingFlip {
final ServerPlayerEntity player;
int ticks = 20;
PendingFlip(ServerPlayerEntity player) { this.player = player; }
}
}