Skip to content
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
153 changes: 153 additions & 0 deletions src/main/java/kroppeb/server/command/commands/EffectCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2020 MadHau5
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package kroppeb.server.command.commands;

import kroppeb.server.command.Command;
import kroppeb.server.command.arguments.Selector;
import kroppeb.server.command.reader.Reader;
import kroppeb.server.command.reader.ReaderException;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.registry.Registry;

import java.util.Collection;

abstract public class EffectCommand implements Command {
final Selector targets;
final StatusEffect effect;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EffectCommand needs a constructor to initialize these.


public static EffectCommand read(Reader reader) throws ReaderException {
String sub = reader.readLiteral();
switch (sub) {
case "give":
return GiveCommand.read(reader);
case "clear":
return ClearCommand.read(reader);
default:
throw new ReaderException("Unexpected effect literal: " + sub);
}

}

public static class GiveCommand extends EffectCommand {
final int duration;
final int amplifier;
final boolean hideParticles;

public GiveCommand(Selector targets, StatusEffect effect, int duration, int amplifier, boolean hideParticles) {
this.targets = targets;
this.effect = effect;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be passed to the super constructor.

this.duration = duration;
this.amplifier = amplifier;
this.hideParticles = hideParticles;
}

public static GiveCommand read(Reader reader) throws ReaderException {
Selector targets = Selector.read(reader);
StatusEffect effect = Registry.STATUS_EFFECT.get(reader.readIdentifier());
int duration;
int amplifier = 0;
boolean hideParticles = false;
if (reader.hasNext()) {
reader.moveNext();
duration = reader.readInt();
} else {
duration = -1;
}
if (reader.hasNext()) {
reader.moveNext();
amplifier = reader.readInt();
}
if (reader.hasNext()) {
String bool = reader.readLiteral();
switch (bool) {
case "true":
hideParticles = true;
break;
case "false":
hideParticles = false;
break;
default:
throw new ReaderException("Unexpected boolean literal: " + bool);
}
}
return new GiveCommand(targets, effect, duration, amplifier, hideParticles);
}

public int execute(ServerCommandSource source) {
int e = 0;
Collection<Entity> entities = targets.getEntities(source);
for (Entity entity : entities) {
if (entity instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) entity;
if (effect.isInstant()) {
if (duration == -1) {
duration = 1;
}
} else {
if (duration == -1) {
duration = 30;
}
duration *= 20;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this multiply the duration with 20 each time?
Maybe do the multiply and the effect.isInstant in the constructor?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also gives an error as duration is final

}
livingEntity.addStatusEffect(new StatusEffectInstance(effect,
duration, amplifier, false, hideParticles));
e++;
}
}
if (e == 0) {
// TODO: throw error on failed command
}
return e;
}
}

public static class ClearCommand extends EffectCommand {

public ClearCommand(Selector targets, StatusEffect effect) {
this.targets = targets;
this.effect = null;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this should be this.effect = effect?

}

public static ClearCommand read(Reader reader) throws ReaderException {
Selector targets = Selector.read(reader);
StatusEffect effect;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be initialized with null in case the if does not execute.

if (reader.hasNext()) {
effect = Registry.STATUS_EFFECT.get(reader.readIdentifier());
}
return new ClearCommand(targets, effect);
}

public int execute(ServerCommandSource source) {
int e = 0;
Collection<Entity> entities = targets.getEntities(source);
if (effect == null) {
for (Entity entity : entities) {
if (entity instanceof LivingEntity) {
((LivingEntity) entity).clearStatusEffects();
e++;
}
}
} else {
for (Entity entity : entities) {
if (entity instanceof LivingEntity) {
((LivingEntity) entity).removeStatusEffect(effect);
e++;
}
}
}
if (e == 0) {
// TODO: throw error on failed command
}
return e;
}
}
}
60 changes: 60 additions & 0 deletions src/main/java/kroppeb/server/command/commands/GameModeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2020 MadHau5
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package kroppeb.server.command.commands;

import kroppeb.server.command.Command;
import kroppeb.server.command.arguments.Selector;
import kroppeb.server.command.reader.Reader;
import kroppeb.server.command.reader.ReaderException;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.GameMode;

import java.util.Collection;

abstract public class GameModeCommand implements Command {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't be abstract

final Selector targets;
final GameMode gm;

public GameModeCommand(Selector targets, GameMode gm) {
this.targets = targets;
this.gm = gm;
}

public static GameModeCommand read(Reader reader) throws ReaderException {
Selector targets = null;
GameMode gm = GameMode.byName(reader.readLiteral()); // TODO: handle custom gamemodes
if (reader.hasNext()) {
targets = Selector.read(reader);
}
return new GameModeCommand(targets, gm);
}

public int execute(ServerCommandSource source) {
int e = 0;
Collection<Entity> entities;
if (targets == null) {
entities = Collections.<Entity>singleton(source.getPlayer());
} else {
entities = targets.getEntities(source);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I'm gonna make a PlayerSelector and getPlayers.

}
for (Entity entity : entities) {
if (entity instanceof PlayerEntity) {
((PlayerEntity) entity).setGameMode(gm);
e++;
}
}
if (e == 0) {
// TODO: throw error on failed command
}
return e;
}
}
85 changes: 85 additions & 0 deletions src/main/java/kroppeb/server/command/commands/GiveCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2020 MadHau5
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package kroppeb.server.command.commands;

import kroppeb.server.command.Command;
import kroppeb.server.command.arguments.Selector;
import kroppeb.server.command.reader.Reader;
import kroppeb.server.command.reader.ReaderException;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

import java.util.Collection;

abstract public class GiveCommand implements Command {
final Selector targets;
final Identifier item;
final CompoundTag nbt;
final int count;

public GiveCommand(Selector targets, Identifier item, CompoundTag nbt, int count) {
this.targets = targets;
this.item = item;
this.nbt = nbt;
this.count = count;
}

public static GiveCommand read(Reader reader) throws ReaderException {
Selector targets = Selector.read(reader);
Identifier item = Registry.ITEM.get(reader.readIdentifier());
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be stored in an Item

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a readItem that handles the nbt.

CompoundTag nbt;
int count = 1;
if (reader.hasNext()) {
// nbt = ???;
// TODO: Process NBT tags
}
if (reader.hasNext()) {
reader.moveNext();
count = reader.readInt();
}
return new GiveCommand(targets, item, nbt, count);
}

public int execute(ServerCommandSource source) {
int e = 0;

int stackMax = item.getMaxCount();
int stackCount = count / stackMax;
ItemStack stack = ItemStack(item, stackMax);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new.

// TODO: throw error for 'illegal' item (not found)

Collection<Entity> entities = targets.getEntities(source);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna make a getPlayers

for (Entity entity : entities) {
if (entity instanceof PlayerEntity) {
player = (PlayerEntity) entity;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing type.

for (int i = 0; i < stackCount + 1; i++) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i <= stackCount is a bit neater.

if (i == stackCount) {
stack.setCount(count % stackMax);
}
ItemEntity itemEntity = player.dropItem(stack, false);
if (itemEntity != null) {
itemEntity.setOwner(player);
itemEntity.resetPickupDelay();
} else {
i--;
}
}
e++;
}
}
if (e == 0) {
// TODO: throw error on failed command
}
return e;
}
}