Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weapon #430

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.event;

import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import one.oktw.galaxy.Main;
import one.oktw.galaxy.event.type.PlayerDropItemEvent;
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;

@Mixin(ServerPlayNetworkHandler.class)
public class MixinPlayerDropItem_NetworkHandler {
@Shadow
public ServerPlayerEntity player;

@Inject(
method = "onPlayerAction",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerEntity;dropSelectedItem(Z)Z",
shift = At.Shift.BEFORE
), cancellable = true)
private void onPlayerAction(PlayerActionC2SPacket packet, CallbackInfo ci) {
Main main = Main.Companion.getMain();
if (main != null && main.getEventManager().emit(new PlayerDropItemEvent(player)).getCancel()) {
ci.cancel();
player.currentScreenHandler.syncState();
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.event;

import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import one.oktw.galaxy.Main;
import one.oktw.galaxy.event.type.PlayerPickupItemEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemEntity.class)
public class MixinPlayerPickupItem_ItemEntity {
@Inject(
method = "onPlayerCollision",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/entity/player/PlayerEntity;triggerItemPickedUpByEntityCriteria(Lnet/minecraft/entity/ItemEntity;)V",
shift = At.Shift.AFTER
))
private void onPlayerCollision(PlayerEntity player, CallbackInfo ci) {
Main main = Main.Companion.getMain();
if (main == null) return;
main.getEventManager().emit(new PlayerPickupItemEvent((ServerPlayerEntity) player));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.event;

import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import one.oktw.galaxy.Main;
import one.oktw.galaxy.event.type.PlayerSwapItemInHandEvent;
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;

@Mixin(ServerPlayNetworkHandler.class)
public class MixinPlayerSwapItemInHand_NetworkHandler {
@Shadow
public ServerPlayerEntity player;

@Inject(method = "onPlayerAction", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;clearActiveItem()V", shift = At.Shift.AFTER))
private void onPlayerAction(PlayerActionC2SPacket packet, CallbackInfo ci) {
Main main = Main.Companion.getMain();
if (main == null) return;
main.getEventManager().emit(new PlayerSwapItemInHandEvent(player));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.event;

import net.minecraft.network.packet.c2s.play.UpdateSelectedSlotC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import one.oktw.galaxy.Main;
import one.oktw.galaxy.event.type.UpdateSelectedSlotEvent;
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;

@Mixin(ServerPlayNetworkHandler.class)
public class MixinUpdateSelectedSlot_NetworkHandler {
@Shadow
public ServerPlayerEntity player;

@Inject(method = "onUpdateSelectedSlot", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;updateLastActionTime()V"))
private void onUpdateSelectedSlot(UpdateSelectedSlotC2SPacket packet, CallbackInfo ci) {
Main main = Main.Companion.getMain();
if (main == null) return;
main.getEventManager().emit(new UpdateSelectedSlotEvent(packet, player));
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import one.oktw.galaxy.command.commands.Spawn
import one.oktw.galaxy.event.EventManager
import one.oktw.galaxy.event.type.ProxyResponseEvent
import one.oktw.galaxy.item.event.CustomItemEventHandler
import one.oktw.galaxy.item.event.Gun
import one.oktw.galaxy.item.event.Wrench
import one.oktw.galaxy.player.Harvest
import one.oktw.galaxy.proxy.api.ProxyAPI
Expand Down Expand Up @@ -99,6 +100,7 @@ class Main : DedicatedServerModInitializer, CoroutineScope {
eventManager.register(Elevator())
eventManager.register(AngelBlock())
eventManager.register(CustomItemEventHandler())
eventManager.register(Gun())
})

ServerLifecycleEvents.SERVER_STOPPING.register {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/one/oktw/galaxy/command/commands/Admin.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand All @@ -23,6 +23,7 @@ import net.minecraft.server.command.CommandManager
import net.minecraft.server.command.ServerCommandSource
import one.oktw.galaxy.command.Command
import one.oktw.galaxy.command.commands.admin.FlySpeed
import one.oktw.galaxy.command.commands.admin.GetGun
import one.oktw.galaxy.command.commands.admin.GetItem
import one.oktw.galaxy.command.commands.admin.RegisterBlock

Expand All @@ -32,6 +33,7 @@ class Admin : Command {
CommandManager.literal("admin")
.requires { source -> source.hasPermissionLevel(2) }
.then(GetItem().command)
.then(GetGun().command)
.then(RegisterBlock.command)
.then(FlySpeed().command)
)
Expand Down
126 changes: 126 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/command/commands/admin/GetGun.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2023
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.command.commands.admin

import com.mojang.brigadier.Command
import com.mojang.brigadier.arguments.DoubleArgumentType
import com.mojang.brigadier.arguments.IntegerArgumentType
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.mojang.brigadier.context.CommandContext
import net.minecraft.command.argument.IdentifierArgumentType
import net.minecraft.item.ItemStack
import net.minecraft.server.command.CommandManager
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.sound.SoundCategory
import net.minecraft.sound.SoundEvents
import net.minecraft.text.Text
import one.oktw.galaxy.item.CustomItem
import one.oktw.galaxy.item.Gun

class GetGun {
// /admin getGun <item> [<heat>] <maxTemp> <cooling> <damage> <range> <through>
private val throughArgument = CommandManager.argument("through", IntegerArgumentType.integer())
.executes {
val identifier = IdentifierArgumentType.getIdentifier(it, "item")
val item = CustomItem.registry.get(identifier) as Gun?

if (item == null) {
it.source.sendError(Text.translatable("argument.item.id.invalid", identifier))
return@executes 0
}

val itemStack = item.apply {
weaponData.applyValue(
IntegerArgumentType.getInteger(it, "heat"),
IntegerArgumentType.getInteger(it, "maxTemp"),
DoubleArgumentType.getDouble(it, "cooling"),
DoubleArgumentType.getDouble(it, "damage"),
DoubleArgumentType.getDouble(it, "range"),
IntegerArgumentType.getInteger(it, "through")
)
}.createItemStack()
return@executes sendItemToSource(it, itemStack)
}

private val rangeArgument = CommandManager.argument("range", DoubleArgumentType.doubleArg())
.then(throughArgument)
private val damageArgument = CommandManager.argument("damage", DoubleArgumentType.doubleArg())
.then(rangeArgument)
private val coolingArgument = CommandManager.argument("cooling", DoubleArgumentType.doubleArg())
.then(damageArgument)
private val maxTempArgument = CommandManager.argument("maxTemp", IntegerArgumentType.integer())
.then(coolingArgument)
private val heatArgument = CommandManager.argument("heat", IntegerArgumentType.integer())
.then(maxTempArgument)

val command: LiteralArgumentBuilder<ServerCommandSource> = CommandManager.literal("getGun")
.then(
CommandManager.argument("item", IdentifierArgumentType.identifier())
.suggests { _, builder ->
CustomItem.registry.getAll().keys.forEach { identifier ->
if (CustomItem.registry.get(identifier) !is Gun) return@forEach
if (identifier.toString().contains(builder.remaining, ignoreCase = true)) {
builder.suggest(identifier.toString())
}
}
return@suggests builder.buildFuture()
}
.executes {
val identifier = IdentifierArgumentType.getIdentifier(it, "item")
val item = CustomItem.registry.get(identifier) as Gun?

if (item == null) {
it.source.sendError(Text.translatable("argument.item.id.invalid", identifier))
return@executes 0
}

return@executes sendItemToSource(it, item.createItemStack())
}
.then(heatArgument)
)

private fun sendItemToSource(it: CommandContext<ServerCommandSource>, itemStack: ItemStack): Int {
val player = it.source.playerOrThrow
if (player.inventory.insertStack(itemStack)) {
itemStack.count = 1
val itemEntity = player.dropItem(itemStack, false)
itemEntity?.setDespawnImmediately()

player.world.playSound(
null,
player.x,
player.y,
player.z,
SoundEvents.ENTITY_ITEM_PICKUP,
SoundCategory.PLAYERS,
0.2F,
((player.random.nextFloat() - player.random.nextFloat()) * 0.7F + 1.0F) * 2.0F
)
player.playerScreenHandler.sendContentUpdates()
} else {
player.dropItem(itemStack, false)?.apply {
resetPickupDelay()
setOwner(player.uuid)
}
}
it.source.sendFeedback({ Text.translatable("commands.give.success.single", 1, itemStack.toHoverableText(), it.source.displayName) }, true)

return Command.SINGLE_SUCCESS
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/event/type/PlayerDropItemEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.event.type

import net.minecraft.server.network.ServerPlayerEntity

class PlayerDropItemEvent(val player: ServerPlayerEntity) : CancelableEvent()
Loading